feat: support mcp 2.x alongside mcp 1.x - #342
Open
K4bain wants to merge 1 commit into
Open
Conversation
mcp 2.x redesigned the lowlevel Server API: the list_tools/call_tool decorators are gone (handlers are now registered as constructor callbacks), handler signatures gained an explicit request-context and params argument, and handlers must return typed ListToolsResult / CallToolResult instead of a bare content list. It also dropped the in-memory test helper and renamed several fields (isError -> is_error, inputSchema -> input_schema, and JSONRPCMessage became a raw Union type). Change the server to version-branch on whether the decorator API exists: - mcp 1.x: unchanged decorator path. - mcp 2.x: constructor-callback handlers with jsonschema argument validation and filtered exception results, mirroring the 1.x call_tool contract; HTTP request info is still extracted from the per-request context (now passed explicitly). - Relax the dependency cap to mcp>=1.12,<3 and add a tests/_mcp_compat shim so the full suite runs under both major versions. The SSE transport now parses version-agnostically via a TypeAdapter. The full unit suite passes under both mcp 1.12.1 and mcp 2.1.1.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: support mcp 2.x alongside mcp 1.x
Why this matters
mcp 2.x is now out, and this package could not even import against it. Every fresh
pip install fastapi-mcptoday resolves to mcp 2.x and breaks at setup_server() — the lowlevelServerAPI was redesigned and almost everything the library used is gone. (PR #331 was the stopgap, capping<2; this PR is the real fix.)The changes needed to run on 2.x:
setup_server()now version-branches on whether the decorator API exists:on_list_tools/on_call_tool), each receiving an explicit request context + typed params, returning typedListToolsResult/CallToolResult.request_ctxContextVar, 2.x passes the context into the handler. Shared_extract_http_request_infohelper.call_toolcontract: arguments are validated against the tool's JSON schema and handler exceptions become error results (is_error=True) instead of protocol errors.mcp>=1.12.0,<3.0.0.jsonschemadeclared explicitly (now a direct import).tests/_mcp_compatshim so the test suite runs unchanged under both majors, and made the SSE transport parse JSON-RPC version-agnostically via aTypeAdapter.Verification
isError->is_error,inputSchema->input_schema, and the removed in-memory test helper are all handled compatibly.Note
I could not regenerate
uv.locklocally (nouvon this machine); it should be refreshed withuv lockbefore merging. I also authored this against my fork (K4bain), mirroring the existing adoption PRs (#331-341) from the maintained community fork.