Title: Make client-side cancellation work over the 2026 transports by maxisbey · Pull Request #3046 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Make client-side cancellation work over the 2026 transports by maxisbey · Pull Request #3046 · modelcontextprotocol/python-sdk
X Title: Make client-side cancellation work over the 2026 transports by maxisbey · Pull Request #3046 · modelcontextprotocol/python-sdk
Description: Client-side request cancellation was a no-op on the 2026 wire: the modern session stamp forced cancel_on_abandon=False for every request, so abandoning a request (caller cancellation or timeout) over streamable HTTP left the POST/SSE stream open and the server running, and over 2026 stream-pair transports never sent the notifications/cancelled the spec requires. This PR makes cancellation real on both modern transports, and adds the caller-supplied request-id seam the upcoming client subscriptions/listen driver needs. Motivation and Context Per the 2026-07-28 spec, the two transports spell client cancellation differently: Streamable HTTP: "Closing the SSE response stream is the cancellation signal. The server MUST treat a client disconnect as cancellation of that request. No notifications/cancelled message is required or expected" — and the wire defines no client-to-server notifications at all. stdio / stream-pair: "The client MUST send a notifications/cancelled notification referencing the request ID." The courtesy frame the dispatcher already emits on abandon is now the uniform internal signal: stream transports write it (spec-correct at 2026 stdio, unchanged at 2025), while the streamable-HTTP transport translates it into aborting the named request's own in-flight POST instead of writing it. Every request POST is registered with an abort scope and the era it was sent under, so a late cancel is interpreted per the named request's era rather than whatever was negotiated since; on pre-2026 wires the frame still POSTs (a 2025 disconnect is explicitly not a cancel). Registration happens synchronously in the write loop, before the POST task is spawned, so a cancel dequeued immediately after its request can never miss it, and teardown is identity-guarded so a finished task unwinding late cannot evict a reused id's successor registration. The protocol-version cache moves into the same serialized loop for the same reason: it now reflects wire order instead of POST-task scheduling order. Separately, CallOptions["request_id"] lets a caller supply the outbound request id on both dispatchers. A subscriptions/listen client driver must know its subscription id (= the request's JSON-RPC id) before the result arrives, which the mint-internally-only design made impossible. Supplied ids reach the peer verbatim ("7" stays a string), collide loudly only for the caller who chose them (minting skips occupied keys), and both dispatchers share one coerced collision domain — so the in-memory DirectDispatcher raises exactly where JSONRPCDispatcher would in production. The negotiation methods (initialize, server/discover) keep their cancellation opt-out on every path. How Has This Been Tested? Full suite: 100.00% line+branch coverage, pyright clean. New transport tests pin the translation matrix: modern abort of the matching POST; legacy frame pass-through with the stream left open; unmatched/typed-id/bool frames swallowed at modern without touching other streams; per-request era honored across a mid-session version flip; reused-id successor registration surviving a stale task's teardown. An end-to-end test over a real ASGI bridge: cancelling the caller of a parked subscriptions/listen closes the POST, the server releases the subscription (observed through the bus seam), and no notifications/cancelled crosses the wire. Manually against a real uvicorn server with a wire tap, via the public Client: at 2026, a parked tools/call cancelled from the caller's scope is observed as a genuine cancellation server-side with zero cancelled frames on the wire; at 2025 (mode="legacy"), the same flow POSTs exactly one notifications/cancelled frame and the stream stays open. A raw subscriptions/listen sent with request_id="verify-listen-1" came back ack-stamped with that id. Breaking Changes None against released surfaces. Within the v2 line: abandoning a modern-era request now produces the spec's cancellation behavior (POST abort at 2026 HTTP, a cancelled frame at 2026 stdio) instead of silently leaking the request. 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 One deliberate boundary: the transport translates exactly notifications/cancelled because it is the only client-to-server notification the 2026 core protocol defines. A table-driven guard at the POST boundary (rejecting any future client notification a modern wire doesn't define) would make illegal frames unconstructible for extensions too — left as a follow-up seam rather than smuggled into this change. The client subscriptions/listen driver (context-managed client.listen(...) yielding typed events) builds directly on these seams and follows as its own PR.
Open Graph Description: Client-side request cancellation was a no-op on the 2026 wire: the modern session stamp forced cancel_on_abandon=False for every request, so abandoning a request (caller cancellation or timeout) ov...
X Description: Client-side request cancellation was a no-op on the 2026 wire: the modern session stamp forced cancel_on_abandon=False for every request, so abandoning a request (caller cancellation or timeout) ov...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/pull/3046
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:24981b53-5cd6-47dc-6ad0-1d5378d2dcd7 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C310:1D4E56:209BDAB:2B341CB:6A5CB2C5 |
| html-safe-nonce | 4450798bc077a27f65571fa15d19a71d58719d3846664887b2ec866ca6b208fe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzEwOjFENEU1NjoyMDlCREFCOjJCMzQxQ0I6NkE1Q0IyQzUiLCJ2aXNpdG9yX2lkIjoiNDk5ODAzMjk5NTgwMDM2MzcxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | cddbf2ca73095e680b0952b4d4db5cb016b0d2341aee591f7e8f0e3104130c89 |
| hovercard-subject-tag | pull_request:3972456633 |
| 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/3046/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 | Client-side request cancellation was a no-op on the 2026 wire: the modern session stamp forced cancel_on_abandon=False for every request, so abandoning a request (caller cancellation or timeout) ov... |
| 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