fix(mcp): support MCP SDK v2 handler signature and removed request_ctx#6583
fix(mcp): support MCP SDK v2 handler signature and removed request_ctx#6583ericapisani wants to merge 2 commits into
Conversation
MCP SDK v2 changed handler signatures from (name, arguments) to (ctx, params) and removed the request_ctx ContextVar. Add version detection to patch the correct entry-points (Server.__init__ and add_request_handler for v2, decorator methods for v1), extract request context from the ServerRequestContext argument directly, and update tests to work with both SDK versions. Migration notes for MCP SDK v2 can be found here: - https://github.com/modelcontextprotocol/python-sdk/releases/tag/v2.0.0a1 - https://github.com/modelcontextprotocol/python-sdk/blob/main/docs/migration.md Fixes PY-2532 Fixes #6574
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 06bb878. Configure here.
| if MCP_PACKAGE_VERSION and MCP_PACKAGE_VERSION >= (2, 0, 0): | ||
| _patch_lowlevel_server_v2() | ||
| else: | ||
| _patch_lowlevel_server_v1() |
There was a problem hiding this comment.
Unknown MCP version misroutes
Medium Severity
When package_version("mcp") is falsy, the integration always applies v1 patches and never imports request_ctx, yet v1 HTTP context lookup is gated on an explicit version less than 2.0.0. MCP v2 servers can miss constructor/add_request_handler wrapping, and v1 HTTP spans may lose request, session, and scope data that previously came from the context variable.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 06bb878. Configure here.
There was a problem hiding this comment.
If it's falsy in the way it's being described (where MCP_VERSION_PACKAGE is None resulting in the request_ctx not being imported), that would mean the package isn't installed and we shouldn't be patching. 🤔
Codecov Results 📊✅ 90940 passed | ❌ 108 failed | ⏭️ 6073 skipped | Total: 97121 | Pass Rate: 93.64% | Execution Time: 321m 2s 📊 Comparison with Base Branch
❌ Patch coverage is 66.95%. Project has 2451 uncovered lines. Files with missing lines (1)
Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 89.79% 89.67% -0.12%
==========================================
Files 192 192 —
Lines 23652 23732 +80
Branches 8154 8194 +40
==========================================
+ Hits 21238 21281 +43
- Misses 2414 2451 +37
- Partials 1344 1352 +8Generated by Codecov Action |


MCP SDK v2 changed handler signatures from (name, arguments) to
(ctx, params) and removed the request_ctx ContextVar. Add version
detection to patch the correct entry-points (Server.init and
add_request_handler for v2, decorator methods for v1), extract
request context from the ServerRequestContext argument directly,
and update tests to work with both SDK versions.
Migration notes for MCP SDK v2 can be found here:
What this PR does not do:
This was tested on the branch in #6567 to confirm it doesn't introduce regressions while addressing the v2 breaking change.
Fixes PY-2532
Fixes #6574