Title: RequestResponder.cancel sends JSON-RPC response, violating cancellation spec · Issue #2480 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: RequestResponder.cancel sends JSON-RPC response, violating cancellation spec · Issue #2480 · modelcontextprotocol/python-sdk
X Title: RequestResponder.cancel sends JSON-RPC response, violating cancellation spec · Issue #2480 · modelcontextprotocol/python-sdk
Description: Description RequestResponder.cancel in mcp/shared/session.py sends an ErrorData(code=0, message="Request cancelled") JSON-RPC response back to the sender after a notifications/cancelled is received. The cancellation spec explicitly says ...
Open Graph Description: Description RequestResponder.cancel in mcp/shared/session.py sends an ErrorData(code=0, message="Request cancelled") JSON-RPC response back to the sender after a notifications/cancelled is received...
X Description: Description RequestResponder.cancel in mcp/shared/session.py sends an ErrorData(code=0, message="Request cancelled") JSON-RPC response back to the sender after a notifications/cancelled i...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/2480
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"RequestResponder.cancel sends JSON-RPC response, violating cancellation spec","articleBody":"## Description\n\n`RequestResponder.cancel` in `mcp/shared/session.py` sends an `ErrorData(code=0, message=\"Request cancelled\")` JSON-RPC response back to the sender after a `notifications/cancelled` is received. The cancellation spec explicitly says receivers SHOULD NOT do this:\n\n\u003e Receivers of cancellation notifications **SHOULD**:\n\u003e - Stop processing the cancelled request\n\u003e - Free associated resources\n\u003e - **Not send a response for the cancelled request**\n\n— https://github.com/modelcontextprotocol/modelcontextprotocol/blob/main/docs/specification/draft/basic/utilities/cancellation.mdx#L33-L36\n\n## Reproduction\n\n`mcp` 1.27.0, Python 3.11. Driving the server over stdio:\n\n1. Send `initialize` + `notifications/initialized`.\n2. Send any `tools/call` whose handler enters an `await` (e.g. `await asyncio.sleep`).\n3. Send `notifications/cancelled` with the request id.\n4. Observe stdout: an unsolicited response is sent for the cancelled request.\n\n```json\n{\"jsonrpc\": \"2.0\", \"id\": 99, \"error\": {\"code\": 0, \"message\": \"Request cancelled\"}}\n```\n\nSource:\nhttps://github.com/modelcontextprotocol/python-sdk/blob/v1.27.0/src/mcp/shared/session.py#L137-L150\n\n```python\nasync def cancel(self) -\u003e None:\n ...\n self._cancel_scope.cancel()\n self._completed = True\n # Send an error response to indicate cancellation\n await self._session._send_response(\n request_id=self.request_id,\n response=ErrorData(code=0, message=\"Request cancelled\", data=None),\n )\n```\n\n## Impact\n\nStrict clients treat the unexpected response as an unknown message id and drop the transport. Claude Code, for example, logs `Received a response for an unknown message ID`, closes the stdio transport, and reconnects. With long-running tools (custom polling tools, long DB queries, etc.) every cancellation costs a reconnect cycle.\n\n## Suggested fix\n\nDrop the `_send_response` call from `RequestResponder.cancel`. The cancel scope is already cancelled and `_completed` is already set, so the request is correctly cleaned up server-side without violating the spec.\n\n## Workaround\n\nUntil this lands, downstream servers can monkey-patch `RequestResponder.cancel`:\n\n```python\nfrom mcp.shared.session import RequestResponder\n\nasync def _cancel_without_response(self) -\u003e None:\n if not self._entered:\n raise RuntimeError(\"RequestResponder must be used as a context manager\")\n if not self._cancel_scope:\n raise RuntimeError(\"No active cancel scope\")\n self._cancel_scope.cancel()\n self._completed = True\n\nRequestResponder.cancel = _cancel_without_response\n```\n\n## Environment\n- mcp 1.27.0\n- Python 3.11\n- stdio transport\n","author":{"url":"https://github.com/q-thomasdickson","@type":"Person","name":"q-thomasdickson"},"datePublished":"2026-04-20T09:26:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/2480/python-sdk/issues/2480"}
| 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:0b893a0a-a253-1db9-378b-cba9a75f45e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8F8:6650E:566A8F:7AF1B5:6A59E4AB |
| html-safe-nonce | 4a8ee87bf24f319224bcd22ed7f0f03043540d95f6a116a7023a0d64af7ece19 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEY4OjY2NTBFOjU2NkE4Rjo3QUYxQjU6NkE1OUU0QUIiLCJ2aXNpdG9yX2lkIjoiNTYzMzEzMzM3ODk3NzUyMjg2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 315177256cde5468fc9e0024811420d7f1e5daa1c6ae69a456b325e754d2c399 |
| hovercard-subject-tag | issue:4294592004 |
| 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/2480/issue_layout |
| twitter:image | https://opengraph.githubassets.com/af5a706956e4648c7c6e791de6c581552a54939548ee02c9cf6c24a0c6aacad3/modelcontextprotocol/python-sdk/issues/2480 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/af5a706956e4648c7c6e791de6c581552a54939548ee02c9cf6c24a0c6aacad3/modelcontextprotocol/python-sdk/issues/2480 |
| og:image:alt | Description RequestResponder.cancel in mcp/shared/session.py sends an ErrorData(code=0, message="Request cancelled") JSON-RPC response back to the sender after a notifications/cancelled is received... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | q-thomasdickson |
| hostname | github.com |
| expected-hostname | github.com |
| None | ba3976babb66479b1c943a8edc0777d96157da48fadc0161f9ddb219deee8353 |
| 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 | 26f17126414f953984d8ae42f57c0db48e7dbde3 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width