Title: Resolve protocol version per request and expose it as ctx.protocol_version by maxisbey · Pull Request #2886 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Resolve protocol version per request and expose it as ctx.protocol_version by maxisbey · Pull Request #2886 · modelcontextprotocol/python-sdk
X Title: Resolve protocol version per request and expose it as ctx.protocol_version by maxisbey · Pull Request #2886 · modelcontextprotocol/python-sdk
Description: Adds ctx.protocol_version: str — the protocol version a request is being served at — resolved per request and always set, including on stateless connections. Motivation and Context Since #2849, the runner's inbound surface validation (validate_client_request/validate_client_notification) and outbound result serialization are version-keyed, and they need a version on every request. Today the only source is Connection.protocol_version, which is set by the initialize handshake — so on stateless streamable-HTTP connections it stays None for the connection's whole life and the runner falls back to a hard-coded "2025-11-25". The streamable HTTP transport already reads and validates the MCP-Protocol-Version header per request, but only uses it for transport-local SSE-priming/close-callback gating and then drops it. This threads that value through to the runner and exposes it to handlers as ctx.protocol_version. Resolution rule. A handshake-committed value governs the whole connection when present; otherwise per-request signals apply (_meta["io.modelcontextprotocol/protocolVersion"], then the transport's per-message hint), then the literal 2025-11-25 terminal default. This mirrors the spec's own framing in draft Versioning § Terminology: handshake-based (≤2025-11-25) versions are a connection fact; per-request-metadata (2026-07-28+) versions are a request fact. The resolver doesn't branch on a mode flag — connection.protocol_version is not None is "this connection ran a handshake." Connection.protocol_version and ServerSession.protocol_version keep their existing meaning ("the version negotiated by initialize, None if no handshake ran"); their docstrings now point at ctx.protocol_version for the per-request value. How Has This Been Tested? Unit tests for _resolve_protocol_version covering negotiated-wins, _meta, transport-hint, unsupported-value fall-through, and the terminal default. connected_runner tests for ctx.protocol_version on stateful (negotiated) and stateless in-memory (terminal default, with ctx.session.protocol_version still None). End-to-end streamable-HTTP tests: a stateless server's handler observes ctx.protocol_version matching the request's MCP-Protocol-Version header (parametrized over two values), and the spec's 2025-03-26 default when the header is absent. Full suite at 100% coverage; pyright/ruff clean. Breaking Changes ServerRequestContext gains a required protocol_version: str field. The runner always sets it; the only direct constructors were two tests, updated here. ServerMessageMetadata gains an optional protocol_version field (defaults to None). Types of changes Bug fix (non-breaking change which fixes an issue) New feature (non-breaking change which adds functionality) 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 Values not in SUPPORTED_PROTOCOL_VERSIONS are skipped by the resolver so an unrecognized declaration falls through rather than poisoning surface validation. Explicit UnsupportedProtocolVersionError rejection for _meta (and the header/_meta MUST-match check) belong in the transport and arrive with the 2026-07-28 negotiation work. The or "2025-11-25" fallbacks on the outbound server-to-client paths (ServerSession.send_request, Connection.send_request) are unchanged; those are connection-scoped sends without access to per-request context, and on stateless connections they fail fast with NoBackChannelError anyway.
Open Graph Description: Adds ctx.protocol_version: str — the protocol version a request is being served at — resolved per request and always set, including on stateless connections. Motivation and Context Since #2849, the...
X Description: Adds ctx.protocol_version: str — the protocol version a request is being served at — resolved per request and always set, including on stateless connections. Motivation and Context Since #2849, the...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/2886
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:76777b17-59c5-3876-89de-26fa6408ad45 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 95D8:116EB3:580882:759FDF:6A5A756E |
| html-safe-nonce | 7eaab6a634b46a266db9fc9264ee9c4a7d54b386c555d70e9a9f0c07839b922a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NUQ4OjExNkVCMzo1ODA4ODI6NzU5RkRGOjZBNUE3NTZFIiwidmlzaXRvcl9pZCI6Ijg2MjUzOTgzMTkxMzYwMTE2MzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 20fb769c8bcec38a59dee88f3f1c0132418b06999eccd3b007beea210e43ba9a |
| hovercard-subject-tag | pull_request:3878450892 |
| 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/2886/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 | Adds ctx.protocol_version: str — the protocol version a request is being served at — resolved per request and always set, including on stateless connections. Motivation and Context Since #2849, the... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 922bdba685ea48400234d557977f0e32d9adbae6f5125725b4f48cacc6801106 |
| 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 | 0437efb828a4041956ce45d1f7fe69fff38926c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width