Title: refactor: remove request_ctx ContextVar, thread Context explicitly by maxisbey · Pull Request #2203 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: refactor: remove request_ctx ContextVar, thread Context explicitly by maxisbey · Pull Request #2203 · modelcontextprotocol/python-sdk
X Title: refactor: remove request_ctx ContextVar, thread Context explicitly by maxisbey · Pull Request #2203 · modelcontextprotocol/python-sdk
Description: Removes the request_ctx ContextVar and threads Context explicitly through the MCPServer request-handling chain. Motivation and Context The request_ctx ContextVar in mcp.server.lowlevel.server was redundant: the lowlevel server already passes ServerRequestContext as the first argument to every _handle_* method. The ContextVar was a second mechanism carrying the same value, used only by MCPServer.get_context(). This PR removes the ContextVar entirely, making the data flow explicit. _handle_* methods construct the high-level Context at the lowlevel→MCPServer boundary and pass it through. Part of #2112 (context refactor) / #1684 (contextvars cleanup). How Has This Been Tested? All existing tests pass (1123 passed, 98 skipped, 1 xfailed) New tests for the context-required guards in Tool.run(), Prompt.render(), ResourceTemplate.create_resource() uv run --frozen pyright src/ tests/ — 0 errors uv run --frozen ruff check — clean Breaking Changes Yes — documented in docs/migration.md: MCPServer.get_context() removed — use ctx: Context parameter injection in tool/resource/prompt functions instead (the existing recommended pattern) request_ctx ContextVar removed from mcp.server.lowlevel.server — code importing it directly will need to use parameter injection MCPServer.call_tool(), read_resource(), get_prompt() now accept an optional context: Context | None = None parameter — backward-compatible for callers that don't pass it New behavior: if a tool/resource/prompt declares a ctx: Context parameter but is called with context=None, a clear error is raised (ToolError for tools, ValueError for prompts/resource templates). Previously None was silently injected. Types of changes Breaking change (fix or feature that would cause existing functionality to change) Documentation update Checklist I have read the MCP Documentation My code follows the repository's style guidelines New and existing tests pass locally I have added appropriate error handling I have added or updated documentation as needed Additional context Context class moved from mcp.server.mcpserver.server to mcp.server.mcpserver.context. The public import path (from mcp.server.mcpserver import Context) is unchanged via __init__.py re-export. The old direct-module path (from mcp.server.mcpserver.server import Context) also still works because server.py imports Context at module level. Exception hierarchy cleanup (making prompts/resources raise MCPServerError subclasses instead of ValueError) was considered but deferred to #1742 (error taxonomy) to avoid scope creep — see that issue for the full analysis of the existing inconsistencies. AI Disclaimer
Open Graph Description: Removes the request_ctx ContextVar and threads Context explicitly through the MCPServer request-handling chain. Motivation and Context The request_ctx ContextVar in mcp.server.lowlevel.server was r...
X Description: Removes the request_ctx ContextVar and threads Context explicitly through the MCPServer request-handling chain. Motivation and Context The request_ctx ContextVar in mcp.server.lowlevel.server was r...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2203
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:9b01d5b7-04ac-dd03-1098-55a221e8e900 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BDEA:2AA07D:BE08E3:100C101:6A5CF0B9 |
| html-safe-nonce | 4400961f1e517939c2013a6bc804ac842c98ceb256c68cac52dddb0f6a503c91 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCREVBOjJBQTA3RDpCRTA4RTM6MTAwQzEwMTo2QTVDRjBCOSIsInZpc2l0b3JfaWQiOiIzMTA4Nzc0NTg4NTAwOTk2MjgxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2f11d146ec6a9ddd900dd8730454861389b52c6f941eac08fc68e3a185658508 |
| hovercard-subject-tag | pull_request:3344371728 |
| 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/2203/files |
| twitter:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/224885523?s=400&v=4 |
| og:image:alt | Removes the request_ctx ContextVar and threads Context explicitly through the MCPServer request-handling chain. Motivation and Context The request_ctx ContextVar in mcp.server.lowlevel.server was r... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width