Title: fix(mcpserver): ignore the return annotation when finding the context parameter by hishammoizuddin · Pull Request #3301 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: fix(mcpserver): ignore the return annotation when finding the context parameter by hishammoizuddin · Pull Request #3301 · modelcontextprotocol/python-sdk
X Title: fix(mcpserver): ignore the return annotation when finding the context parameter by hishammoizuddin · Pull Request #3301 · modelcontextprotocol/python-sdk
Description: Fixes #3298 What's wrong find_context_parameter() loops over everything typing.get_type_hints() returns. That mapping includes the function's return annotation under the special "return" key, which isn't a parameter. So a handler annotated -> Context (or a union containing it) gets recorded as having a context parameter named "return", and the context is injected under that name when the handler is called. return is a keyword and can't be declared as a parameter, so the call always fails — registration succeeds and the breakage only shows up at invocation time. It isn't only tools: prompts and resource templates resolve their context through the same function. Reverting just context_injection.py and exercising one of each: TOOL is_error: True | Error executing tool tool_fn: tool_fn() got an unexpected keyword argument 'return' PROMPT RAISED: MCPError Internal server error RESOURCE RAISED: MCPError Error creating resource from template res://x The fix Drop the "return" key before scanning the hints, so only real parameters are considered. Why this should be safe Every path that previously resolved to "return" failed at invocation, so nothing could have been depending on it — this only turns a guaranteed failure into working behaviour. Handlers that have both a real Context parameter and a Context return annotation were already fine, because get_type_hints() yields parameters before return (you get ['a', 'ctx', 'b', 'return'] for def f(a, ctx, b) -> Context). I've pinned that ordering in a test so a later refactor can't quietly break it. Tests New tests/server/mcpserver/test_context_injection.py: the positive path (a parameter annotated Context is found) -> Context isn't reported as a parameter -> Context | None isn't reported as a parameter a real Context parameter still wins when the return annotation is also Context end-to-end through Client(server) for a tool, a prompt and a resource template Five of the seven fail without the src/ change. ./scripts/test passes (100% branch coverage, strict-no-cover clean), ruff and pyright are clean on the changed files, and the new tests pass on 3.10 and 3.14. Related PRs No overlap with #3236 or #2623, which touch the same module for unrelated reasons. I test-merged both against this branch and neither conflicts. Disclosure: I used AI assistance (Claude Code) for this change. I've reviewed it myself and can explain it.
Open Graph Description: Fixes #3298 What's wrong find_context_parameter() loops over everything typing.get_type_hints() returns. That mapping includes the function's return annotation under the special "retur...
X Description: Fixes #3298 What's wrong find_context_parameter() loops over everything typing.get_type_hints() returns. That mapping includes the function's return annotation under the special &am...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3301
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d52fe622-56c2-33f2-f0de-5672ff0a3010 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8D56:1EC64C:2C559:3C224:6B0568DD |
| html-safe-nonce | 37208a40ea74e351093396db0a9d9ba05b2bc592e68ae84a9f1d6d0f85c09312 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDU2OjFFQzY0QzoyQzU1OTozQzIyNDo2QjA1NjhERCIsInZpc2l0b3JfaWQiOiI0MDEyMjUwMDc5MTAzOTAzOTY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | bfc8fbadc62db8a745eafa3256b66002444fc61cd78c4fd34279aa6200338f83 |
| hovercard-subject-tag | pull_request:4268843667 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/pull/3301/files |
| twitter:image | https://avatars.githubusercontent.com/u/78191578?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/78191578?s=400&v=4 |
| og:image:alt | Fixes #3298 What's wrong find_context_parameter() loops over everything typing.get_type_hints() returns. That mapping includes the function's return annotation under the special "retur... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1be5cc09efdd4833c24a4ffa71d698e3999fa09071e159fe14c2c08e41c98331 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f6d69234e682c947a48074340e69d45d5901de31 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width