Skip to content

fix(py/mcp): allow mcp 2.x - #164

Closed
huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-mcp-cap
Closed

huangjeff5 wants to merge 1 commit into
mainfrom
fix/py-mcp-cap

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

genkit-mcp capped mcp<2, but mcp 2.0 shipped 2026-07-28 (latest 2.3.0). Anyone already on mcp 2.x couldn't install genkit-mcp. This widens the range to mcp>=1.26.0,<3; the plugin works on both majors.

# --- BEFORE ---
# uv add genkit-mcp 'mcp>=2'
# => resolver error: genkit-mcp requires mcp>=1.26.0,<2

# --- AFTER: the same code runs on mcp 1.26 through 2.3 ---
client = define_mcp_client(ai, 'bookshop', command=sys.executable, args=['bookshop_server.py'])
tools = await client.get_active_tools()
# => list[Tool] from the bookshop server

Main Changes

  • Every 1.x/2.x difference lives in _compat.py, detected from the SDK, not its version: mcp 2 renamed McpError to MCPError, moved the timeout error code from 408 to -32001, takes float seconds instead of timedelta, replaced get_server_capabilities() with a property, and reads protocol fields in snake_case. Each one is looked up with getattr/hasattr, so the rest of the plugin doesn't branch on versions and the module type-checks against either.
  • Tool error results are built with model_validate({'isError': True}): On mcp 1.x, passing is_error=True as a keyword is silently kept as an unknown extra, so the result would not read as an error. Validating the wire-shaped dict sets the right field on both majors.
  • Log "MCP connection ended" when the owner task exits cleanly: On mcp 2, a broken stdin fails the request with CONNECTION_CLOSED instead of raising BrokenResourceError out of stdio_client, so the owner task exits without an exception and the end-of-connection log never fired. It now logs on the else: path too; the except and else paths are exclusive, so 1.x doesn't log twice.
  • samples/mcp-hello moves to mcp 2: The workspace locks a single mcp version, and mcp 2 removed mcp.server.fastmcp, so the sample uses mcp.server.mcpserver.MCPServer and requires mcp>=2,<3.

Verification

  • pytest packages/genkit-mcp: 256 passed at mcp 1.26.0 (floor) and 1.29.1 (latest 1.x); 254 passed, 2 skipped at 2.3.0 (lock). samples/mcp-hello: 7 passed against a real MCPServer child.
  • Full suite at the lock: 7898 passed, 46 skipped. pyright, pyrefly, ty, ruff, check_consistency.py and uv lock --check clean.

Open questions

  • Trace parenting under mcp 2 with a global OTel provider: mcp 2 adds a traceparent to each request from the current context. Our requests run in the connection's owner task, which copied the first caller's context, so with genkit-google-cloud telemetry enabled, later MCP requests could parent to the wrong trace. Dev UI isn't affected. Fix would be running each request under the caller's contextvars.copy_context(). Not done here; flagging it as new 2.x behavior, not a regression.
  • Two broken-stdin tests skip on 2.x: they rely on 1.x cancelling the in-flight request, which 2.x no longer does. The 2.x path (fail, record cause, log) is covered by the other broken-stdin tests.
  • Shutdown grace is looser on 2.x: the SDK's own 2s stdio shutdown wait is shielded, so _CLOSE_GRACE_SECONDS can't cut it short. Test bound is <3.5s on 2.x vs <1.5s on 1.x.

Widen genkit-mcp to mcp>=1.26.0,<3. mcp 2 renames McpError to MCPError,
moves protocol fields to snake_case, replaces get_server_capabilities()
with a property, takes float-second timeouts, and reports request
timeouts as -32001 instead of 408. genkit_mcp._compat detects each from
the installed SDK, so the plugin runs unchanged on 1.x and 2.x.

mcp 2 also reports a dead transport to the request instead of raising it
out of stdio_client, so the connection now logs the failure it recorded
when the owner unwinds without an exception of its own.

The test server registers handlers through either SDK's API, and the
malformed-reply case writes raw JSON-RPC since mcp 2 validates handler
results. mcp-hello moves to MCPServer and mcp>=2,<3.
@huangjeff5 huangjeff5 closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant