| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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
Sorry, something went wrong.
Codecov Results 📊✅ 90992 passed | ⏭️ 6121 skipped | Total: 97113 | Pass Rate: 93.7% | Execution Time: 326m 5s 📊 Comparison with Base Branch
All tests are passing successfully. ❌ Patch coverage is 54.24%. Project has 2467 uncovered lines.
@@ Coverage Diff @@
## main #PR +/-##
==========================================
- Coverage 89.79% 89.60% -0.19%
==========================================
Files 192 192 —
Lines 23652 23732 +80
Branches 8154 8194 +40
==========================================
+ Hits 21238 21265 +27
- Misses 2414 2467 +53
- Partials 1344 1357 +13Generated by Codecov Action |
Sorry, something went wrong.
There was a problem hiding this comment.
handler_name unbound in _extract_handler_data_from_args for tool/prompt types
In sentry_sdk/integrations/mcp.py, _extract_handler_data_from_args for handler_type == 'tool' or 'prompt' assigns handler_name only inside if original_args: / elif original_kwargs.get('name'):. If both conditions are false, handler_name is never set; the prompt branch crashes immediately at arguments = {"name": handler_name, ...} with UnboundLocalError, and the tool branch crashes at the return statement.
EvidenceIdentified by Warden find-bugs
Sorry, something went wrong.
| def test_wrap_v2_handler_is_idempotent(): | ||
| """_wrap_v2_handler must not double-wrap a handler registered through more | ||
| than one path (e.g. MCPServer building a Server, then re-registration).""" | ||
| import sentry_sdk.integrations.mcp as mcp_module | ||
|
|
||
| async def handler(ctx, params): | ||
| return None | ||
|
|
||
| wrapped = mcp_module._wrap_v2_handler("tool", handler) | ||
| assert wrapped is not handler | ||
| assert getattr(wrapped, "__sentry_mcp_wrapped__", False) is True | ||
|
|
||
| rewrapped = mcp_module._wrap_v2_handler("tool", wrapped) | ||
| assert rewrapped is wrapped |
There was a problem hiding this comment.
This test is relying on private implementation details, making it brittle to internal changes such as renaming _wrap_v2_handler or __sentry_mcp_wrapped__.
I'd say we can just remove the test to be honest. If we want to keep it let's ensure we don't double wrap by checking behavior observable by the user. For example, ensuring that we don't emit the same span twice (I assume that's what happens when you double wrap).
Sorry, something went wrong.
…ods rather than invoking the wrap handler method directly
| original_args: "tuple[Any, ...]", | ||
| original_kwargs: "Optional[dict[str, Any]]" = None, | ||
| ) -> "tuple[str, dict[str, Any], str, str, str, Optional[str]]": | ||
| ) -> "tuple[str, dict[str, Any]]": | ||
| """ | ||
| Prepare common handler data for both async and sync wrappers. | ||
| Extract handler name and arguments from v1 positional args. |
There was a problem hiding this comment.
Bug: The handler_name variable is not initialized in _extract_handler_data_from_args for "tool" and "prompt" types if arguments are missing, leading to an UnboundLocalError.
Severity: LOW
Initialize handler_name to a default value, such as "unknown", at the beginning of the logic for both the "tool" and "prompt" handler types. This mirrors the existing safe-handling pattern used for the "resource" type.
Prompt for AI AgentReview the code at the location below. A potential bug has been identified by an AI agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not valid. Location: sentry_sdk/integrations/mcp.py#L403-L407 Potential issue: In the `_extract_handler_data_from_args` function, when the `handler_type` is either "tool" or "prompt", the `handler_name` variable is only assigned a value if `original_args` is provided or if `original_kwargs` contains a "name" key. If neither of these conditions is met, the variable is never initialized. This will cause an `UnboundLocalError` when the code later attempts to use `handler_name` to construct the `arguments` dictionary. This scenario can occur if a developer invokes a v1 handler directly without passing the expected arguments, bypassing the standard MCP SDK dispatcher.
Sorry, something went wrong.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 787e77a. Configure here.
Sorry, something went wrong.
|
|
||
| @server.call_tool() | ||
| async def test_tool(tool_name, arguments): | ||
| return {"result": "success"} |
There was a problem hiding this comment.
Medium Severity
test_sse_transport_detection is skipped on MCP v2 and only runs on v1, but it asserts result["result"]["structuredContent"], which is a v2 JSON-RPC shape. The v1 handler returns a plain dict and the json_rpc_sse fixture waits for a result with content, so this assertion fails on v1 runs.
Reviewed by Cursor Bugbot for commit 787e77a. Configure here.
Sorry, something went wrong.
getsentry#6583) 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 What this PR does not do: - address all breaking changes outlined in the migration guide. This is meant to fix the test failures on getsentry#6567 while still being comprehensive enough to make sense for the next developer who continues the migration. This was tested on the branch in getsentry#6567 to confirm it doesn't introduce regressions while addressing the v2 breaking change. Fixes PY-2532 Fixes getsentry#6574
| Back | FazBrowse Home | New Git URL |
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