Title: Parameterized Context loses request state in resource and prompt handlers · Issue #3235 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Parameterized Context loses request state in resource and prompt handlers · Issue #3235 · modelcontextprotocol/python-sdk
X Title: Parameterized Context loses request state in resource and prompt handlers · Issue #3235 · modelcontextprotocol/python-sdk
Description: Description A resource-template or prompt handler annotated with a parameterized Context[LifespanContextT] receives a different Context object from the one the server injected. The reconstructed object has none of the private request sta...
Open Graph Description: Description A resource-template or prompt handler annotated with a parameterized Context[LifespanContextT] receives a different Context object from the one the server injected. The reconstructed ob...
X Description: Description A resource-template or prompt handler annotated with a parameterized Context[LifespanContextT] receives a different Context object from the one the server injected. The reconstructed ob...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3235
X: @github
Domain: redirect.github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Parameterized Context loses request state in resource and prompt handlers","articleBody":"## Description\n\nA resource-template or prompt handler annotated with a parameterized `Context[LifespanContextT]` receives a different `Context` object from the one the server injected. The reconstructed object has none of the private request state, so accessing `ctx.request_context`, `ctx.session`, `ctx.request_id`, or lifespan state fails with:\n\n```text\nValueError: Context is not available outside of a request\n```\n\nThe equivalent tool handler works, and handlers annotated with unparameterized `Context` happen to work. This makes the documented typed lifespan-context pattern unusable specifically in dynamic resources and prompts.\n\n## Minimal reproduction\n\n```python\nfrom collections.abc import AsyncIterator\nfrom contextlib import asynccontextmanager\n\nimport anyio\nfrom mcp.client import Client\nfrom mcp.server.mcpserver import Context, MCPServer\n\n\n@asynccontextmanager\nasync def lifespan(_: MCPServer[str]) -> AsyncIterator[str]:\n yield \"live-state\"\n\n\nasync def main() -> None:\n server = MCPServer(\"probe\", lifespan=lifespan)\n\n @server.resource(\"probe://{name}\")\n async def resource(name: str, ctx: Context[str]) -> str:\n return f\"{name}:{ctx.request_context.lifespan_context}\"\n\n @server.prompt(\"probe\")\n async def prompt(name: str, ctx: Context[str]) -> str:\n return f\"{name}:{ctx.request_context.lifespan_context}\"\n\n async with Client(server) as client:\n await client.read_resource(\"probe://value\")\n await client.get_prompt(\"probe\", {\"name\": \"value\"})\n\n\nanyio.run(main)\n```\n\nBoth calls fail. Each should return content containing `value:live-state`.\n\n## Root cause\n\n`ResourceTemplate.from_function` and `Prompt.from_function` correctly omit the detected context parameter from their public argument schemas, but then wrap the original handler with `pydantic.validate_call`. At invocation time they inject the live `Context` into the wrapped handler, so Pydantic validates it again.\n\nFor a generic annotation, Pydantic converts the unparameterized runtime instance into `Context[str, Any]`. That creates a new model instance and does not carry over `Context`'s private `_request_context`, `_mcp_server`, and related attributes:\n\n```text\nserver-created Context\n |\n v\nvalidate_call parameter: Context[str]\n |\n v\nnew Context[str, Any] instance\n |\n v\nprivate request state absent\n```\n\nA direct identity probe on current `main` shows:\n\n```text\nContext -> same object, request state present\nContext[str] -> new object, request state absent\n```\n\nTools avoid this because `FuncMetadata.call_fn_with_arg_validation` validates only client-supplied arguments and passes injected values directly to the raw handler.\n\n## Impact\n\nThis affects resource templates and prompts that use typed lifespan context, including otherwise valid code following the SDK's generic `Context[LifespanContextT]` typing. It can also hide in tests that only assert that `ctx` is non-null rather than reading request-scoped state.\n\nA compatible fix should preserve the existing validation/coercion of client-supplied resource or prompt arguments while passing the server-created context object directly, with its identity and private state intact.\n\n## Environment\n\n- MCP Python SDK: 2.0.0 and current `main` at `a4f4ccd091138771535e17191123f20b30fda68e`\n- Python: 3.12.13\n- Pydantic: 2.12.5\n\nAI assistance was used to investigate and draft this report.\n","author":{"url":"https://github.com/morluto","@type":"Person","name":"morluto"},"datePublished":"2026-08-01T19:31:32Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3235"}
| 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 | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:138ccfb1-e1bc-93b8-54ff-f5c0642cf6a9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C008:64B1C:4A5B1D:64E4C7:6B055D27 |
| html-safe-nonce | 7e0f5cd78c83f0c4b421883188786a500c597ea7fc0688f1104147d2384c30eb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDA4OjY0QjFDOjRBNUIxRDo2NEU0Qzc6NkIwNTVEMjciLCJ2aXNpdG9yX2lkIjoiMjA2NTYzNjQ2ODA4MDMzNjE2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5a894e63110cd4cf667a72cd2a6ded6801b965241ef09342168daf1e15fcc208 |
| hovercard-subject-tag | issue:5038677347 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3235/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3f0c2027dee203da7833e6cb57c95cbfff8a98aca58cc6082ec40bdaf8f6c45e/modelcontextprotocol/python-sdk/issues/3235 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3f0c2027dee203da7833e6cb57c95cbfff8a98aca58cc6082ec40bdaf8f6c45e/modelcontextprotocol/python-sdk/issues/3235 |
| og:image:alt | Description A resource-template or prompt handler annotated with a parameterized Context[LifespanContextT] receives a different Context object from the one the server injected. The reconstructed ob... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | morluto |
| hostname | github.com |
| expected-hostname | github.com |
| None | 8a6760da3cce5e2d079d07e3b62df71194316c4478db1a5e38050f44fce72c72 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8327739ac0648b9c3c722b5f401f7338553936b8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width