René's URL Explorer Experiment


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

direct link

Domain: redirect.github.com

IPv4 address: 140.82.114.4


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:138ccfb1-e1bc-93b8-54ff-f5c0642cf6a9
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC008:64B1C:4A5B1D:64E4C7:6B055D27
html-safe-nonce7e0f5cd78c83f0c4b421883188786a500c597ea7fc0688f1104147d2384c30eb
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMDA4OjY0QjFDOjRBNUIxRDo2NEU0Qzc6NkIwNTVEMjciLCJ2aXNpdG9yX2lkIjoiMjA2NTYzNjQ2ODA4MDMzNjE2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac5a894e63110cd4cf667a72cd2a6ded6801b965241ef09342168daf1e15fcc208
hovercard-subject-tagissue:5038677347
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/3235/issue_layout
twitter:imagehttps://opengraph.githubassets.com/3f0c2027dee203da7833e6cb57c95cbfff8a98aca58cc6082ec40bdaf8f6c45e/modelcontextprotocol/python-sdk/issues/3235
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/3f0c2027dee203da7833e6cb57c95cbfff8a98aca58cc6082ec40bdaf8f6c45e/modelcontextprotocol/python-sdk/issues/3235
og:image:altDescription 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemorluto
hostnamegithub.com
expected-hostnamegithub.com
None8a6760da3cce5e2d079d07e3b62df71194316c4478db1a5e38050f44fce72c72
turbo-cache-controlno-preview
go-importgithub.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git
octolytics-dimension-user_id182288589
octolytics-dimension-user_loginmodelcontextprotocol
octolytics-dimension-repository_id862584018
octolytics-dimension-repository_nwomodelcontextprotocol/python-sdk
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id862584018
octolytics-dimension-repository_network_root_nwomodelcontextprotocol/python-sdk
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8327739ac0648b9c3c722b5f401f7338553936b8
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://redirect.github.com/modelcontextprotocol/python-sdk/issues/3235#start-of-content
https://redirect.github.com/
Sign inhttps://redirect.github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3235
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://redirect.github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmodelcontextprotocol%2Fpython-sdk%2Fissues%2F3235
Sign uphttps://redirect.github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=modelcontextprotocol%2Fpython-sdk
Reloadhttps://redirect.github.com/modelcontextprotocol/python-sdk/issues/3235
Reloadhttps://redirect.github.com/modelcontextprotocol/python-sdk/issues/3235
Reloadhttps://redirect.github.com/modelcontextprotocol/python-sdk/issues/3235
modelcontextprotocolhttps://redirect.github.com/modelcontextprotocol
python-sdkhttps://redirect.github.com/modelcontextprotocol/python-sdk
Codehttps://redirect.github.com/modelcontextprotocol/python-sdk
Issues320 (320)https://redirect.github.com/modelcontextprotocol/python-sdk/issues
Pull requests112 (112)https://redirect.github.com/modelcontextprotocol/python-sdk/pulls
Actionshttps://redirect.github.com/modelcontextprotocol/python-sdk/actions
Projectshttps://redirect.github.com/modelcontextprotocol/python-sdk/projects
Security and quality12 (12)https://redirect.github.com/modelcontextprotocol/python-sdk/security
Insightshttps://redirect.github.com/modelcontextprotocol/python-sdk/insights
https://redirect.github.com/modelcontextprotocol/python-sdk
New issuehttps://redirect.github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3235
New issuehttps://redirect.github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3235
#3624https://github.com/modelcontextprotocol/python-sdk/pull/3624
Parameterized Context loses request state in resource and prompt handlershttps://redirect.github.com/modelcontextprotocol/python-sdk/issues/3235#top
#3624https://github.com/modelcontextprotocol/python-sdk/pull/3624
P1https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22P1%22
v2https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
https://github.com/morluto
morlutohttps://github.com/morluto
on Aug 1, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#issue-5038677347
https://redirect.github.com/morluto
morlutohttps://redirect.github.com/morluto
on Aug 1, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#issuecomment-5153187846
https://redirect.github.com/Ecocitizenz
Ecocitizenzhttps://redirect.github.com/Ecocitizenz
on Aug 4, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#issuecomment-5182531009
maxisbeyhttps://redirect.github.com/maxisbey
P1https://redirect.github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22P1%22
v1https://redirect.github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v1%22
v2https://redirect.github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
v1https://redirect.github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v1%22
on Aug 12, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#event-29354921135
maxisbeyhttps://redirect.github.com/maxisbey
completedhttps://redirect.github.com/modelcontextprotocol/python-sdk/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#3624https://github.com/modelcontextprotocol/python-sdk/pull/3624
on Oct 2, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#event-32334665663
https://redirect.github.com/maxisbey
maxisbeyhttps://redirect.github.com/maxisbey
on Oct 2, 2026https://github.com/modelcontextprotocol/python-sdk/issues/3235#issuecomment-5957089320
#3624https://github.com/modelcontextprotocol/python-sdk/pull/3624
#3236https://github.com/modelcontextprotocol/python-sdk/pull/3236
AI Disclaimerhttps://gist.github.com/maxisbey/6123d132484e4c533eab519a2800693d
Sign up for freehttps://redirect.github.com/signup?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3235
Sign in to commenthttps://redirect.github.com/login?return_to=https://github.com/modelcontextprotocol/python-sdk/issues/3235
P1https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22P1%22
v2https://github.com/modelcontextprotocol/python-sdk/issues?q=state%3Aopen%20label%3A%22v2%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Response headers:
Servergithub.com
Content-Typetext/html; charset=utf-8
Cache-Controlmax-age=0, private, must-revalidate

Viewport: width=device-width


URLs of crawlers that visited me.