Title: MCP Python SDK Implementation Gap-17: Missing Cancellation Notifications on Request Timeout · Issue #1458 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: MCP Python SDK Implementation Gap-17: Missing Cancellation Notifications on Request Timeout · Issue #1458 · modelcontextprotocol/python-sdk
X Title: MCP Python SDK Implementation Gap-17: Missing Cancellation Notifications on Request Timeout · Issue #1458 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue Description Summary The MCP Python...
Open Graph Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ...
X Description: Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening t...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1458
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"MCP Python SDK Implementation Gap-17: Missing Cancellation Notifications on Request Timeout","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the latest version of MCP Python SDK\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Description\n\n## Summary\n\nThe MCP Python SDK implementation does not fully comply with the MCP specification regarding timeout handling. While the SDK implements request timeouts, it does not send cancellation notifications when requests timeout.\n\n## Specification Reference\n\nAccording to the MCP specification:\n\n\u003e Implementations **SHOULD** establish timeouts for all sent requests, to prevent hung connections and resource exhaustion. When the request has not received a success or error response within the timeout period, the sender **SHOULD** issue a cancellation notification for that request and stop waiting for a response.\n\n## Current Implementation Analysis\n\n### What's Working\n\n1. **Timeout Mechanism**: The SDK correctly implements timeout detection in `BaseSession.send_request()` method\n2. **Timeout Configuration**: Supports both session-level and request-level timeout configuration \n3. **Exception Handling**: Properly raises `McpError` with `REQUEST_TIMEOUT` code when timeout occurs\n4. **Manual Cancellation**: Supports manual cancellation notifications via `CancelledNotification`\n\n### What's Missing\n\nWhen a request times out, the SDK does not automatically send a `CancelledNotification` to inform the server that the request should be cancelled.\n\n## Code Evidence\n\n### Current Timeout Implementation\n\n**File**: `src/mcp/shared/session.py` \n**Lines**: 270-283\n\n```python\ntry:\n with anyio.fail_after(timeout):\n response_or_error = await response_stream_reader.receive()\nexcept TimeoutError:\n raise McpError(\n ErrorData(\n code=httpx.codes.REQUEST_TIMEOUT,\n message=(\n f\"Timed out while waiting for response to \"\n f\"{request.__class__.__name__}. Waited \"\n f\"{timeout} seconds.\"\n ),\n )\n )\n```\n\n**Problem**: The code only raises an exception but does not send a cancellation notification to the server.\n\n## Impact\n\n**Specification Non-compliance**: The implementation does not follow the MCP specification requirements\n\n## Proposed Solution\n\nModify the timeout handling in `BaseSession.send_request()` to automatically send a cancellation notification:\n\n```python\nexcept TimeoutError:\n # Send cancellation notification as per MCP specification\n try:\n await self.send_notification(\n CancelledNotification(\n params=CancelledNotificationParams(requestId=request_id)\n )\n )\n except Exception:\n # Log but don't fail if notification sending fails\n logger.warning(f\"Failed to send cancellation notification for request {request_id}\")\n \n raise McpError(\n ErrorData(\n code=httpx.codes.REQUEST_TIMEOUT,\n message=(\n f\"Timed out while waiting for response to \"\n f\"{request.__class__.__name__}. Waited \"\n f\"{timeout} seconds.\"\n ),\n )\n )\n```\n\n## Required Changes\n\n1. **Core Implementation**: Update `BaseSession.send_request()` in `src/mcp/shared/session.py`\n2. **Import Addition**: Add necessary imports for `CancelledNotification` and `CancelledNotificationParams`\n\n## Files to Modify\n\n- `src/mcp/shared/session.py` (primary fix)\n\n### Example Code\n\n```Python\n\n```\n\n### Python \u0026 MCP Python SDK\n\n```Text\nlatest\n```","author":{"url":"https://github.com/younaman","@type":"Person","name":"younaman"},"datePublished":"2025-10-09T21:50:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/1458/python-sdk/issues/1458"}
| 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:9d8d184a-204e-dccf-c598-023a073004c6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D3B4:214886:289943:37713B:6A5A5E64 |
| html-safe-nonce | 95ceea410d6b3b7fbd398200a4d3b409c2e01298a6596828d3bb7f4b2f97b543 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEM0I0OjIxNDg4NjoyODk5NDM6Mzc3MTNCOjZBNUE1RTY0IiwidmlzaXRvcl9pZCI6IjkxNzU3NDczMDY2NjIyODg5OTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0f333292baf9983ce2806823db63f68e03ef472ac2686242b0399402c1fb25e0 |
| hovercard-subject-tag | issue:3500724105 |
| 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/1458/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0a20a787d54e262c2cafdfb00a968813b9cbb2d5397e60c302997840eb13d00e/modelcontextprotocol/python-sdk/issues/1458 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0a20a787d54e262c2cafdfb00a968813b9cbb2d5397e60c302997840eb13d00e/modelcontextprotocol/python-sdk/issues/1458 |
| og:image:alt | Initial Checks I confirm that I'm using the latest version of MCP Python SDK I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | younaman |
| hostname | github.com |
| expected-hostname | github.com |
| None | 05b9ddf6a47d2dbe13944873a99f5fb4b83ba4871f9cb8a8e256793a63ca9687 |
| 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 | f8d29d1bd03dda2dd14b3f80b8bc27e1111f43bd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width