Title: Streamable HTTP notification triggers duplicate response after HTTP 202 · Issue #3631 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: Streamable HTTP notification triggers duplicate response after HTTP 202 · Issue #3631 · modelcontextprotocol/python-sdk
X Title: Streamable HTTP notification triggers duplicate response after HTTP 202 · Issue #3631 · modelcontextprotocol/python-sdk
Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before openi...
Open Graph Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex...
X Description: Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/mod...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/3631
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Streamable HTTP notification triggers duplicate response after HTTP 202","articleBody":"### Initial Checks\n\n- [x] I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)\n- [x] I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue\n\n### Release line\n\n2.x (current stable)\n\n### Description\n\n## Summary\n\nMCP 2.1.1 can send HTTP 202 for a JSON-RPC notification and then raise `ClosedResourceError` when writing the notification to a closed internal session writer.\n\nThe outer error handler then attempts to send a second HTTP error response after the 202 response has already started. This can result in an unhandled `ExceptionGroup` or duplicate-response error.\n\n## Environment\n\n* MCP: 2.1.1\n* FastMCP: 4.0.3\n* Python: 3.11\n* Transport: Streamable HTTP\n* Notification: `notifications/initialized`\n\n## Expected behavior\n\nOnce MCP has sent HTTP 202 for a notification, a closed internal session writer should not trigger:\n\n* A second HTTP response.\n* An unhandled exception.\n* A second write attempt to the closed writer.\n\n## Actual behavior\n\nThe notification path currently:\n\n1. Sends HTTP 202 Accepted.\n2. Calls `writer.send(session_message)`.\n3. Raises `ClosedResourceError` because the writer is closed.\n4. Enters the broad POST error handler.\n5. Attempts to send an HTTP 500 response after the 202 response has already started.\n6. Attempts another write through the closed writer.\n\nThis can result in an unhandled `ExceptionGroup` or duplicate-response failure.\n\n## Minimal reproduction\n\n1. Create a `StreamableHTTPServerTransport` with JSON response mode enabled.\n2. Call `transport.connect()`.\n3. Close `transport._read_stream_writer`.\n4. Send a JSON-RPC `notifications/initialized` POST request through `transport.handle_request` with ASGI exceptions enabled.\n5. Observe the unhandled exception after the initial HTTP 202 response has started.\n\n## Evidence\n\nThe closed writer failure originates in:\n\n`mcp/shared/_context_streams.py:37`\n\nThe notification handler sends HTTP 202 before attempting to write the notification to the internal session stream.\n\n## Related issues\n\nThis is related to the closed-writer handling discussed in #2064, but the failure occurs on the MCP v2 notification path.\n\n## Temporary workaround\n\nI added a version-specific safeguard that:\n\n* Preserves the original HTTP 202 response.\n* Prevents the invalid secondary HTTP response.\n* Prevents the final `ClosedResourceError` from escaping.\n\nThe SDK still emits the initial `Error handling POST request` log because that logging occurs before the workaround can intercept the secondary failure.\n\n\n### Example Code\n\n```Python\nInstall the reproduction dependencies:\n\n\n\npython -m pip install \"mcp==2.1.1\" \"httpx==0.28.1\"\n\n\nRun this script:\n\n\n\nimport asyncio\n\nimport httpx\nfrom mcp.server.streamable_http import StreamableHTTPServerTransport\n\nasync def main() -> None:\n transport = StreamableHTTPServerTransport(\n mcp_session_id=None,\n is_json_response_enabled=True,\n )\n\n async with transport.connect():\n assert transport._read_stream_writer is not None\n\n # Simulate a terminated MCP session after the transport connects.\n await transport._read_stream_writer.aclose()\n\n async with httpx.AsyncClient(\n transport=httpx.ASGITransport(\n app=transport.handle_request,\n raise_app_exceptions=True,\n ),\n base_url=\"http://testserver\",\n ) as client:\n response = await client.post(\n \"/mcp\",\n headers={\n \"accept\": \"application/json\",\n \"content-type\": \"application/json\",\n },\n json={\n \"jsonrpc\": \"2.0\",\n \"method\": \"notifications/initialized\",\n \"params\": {},\n },\n )\n\n print(response.status_code)\n\nasyncio.run(main())\n\n\nActual Result\nThe notification handler first sends `HTTP 202 Accepted`.\n\nIt then calls:\n\n\nawait writer.send(session_message)\n\n\nThe closed writer raises:\n\n\nanyio.ClosedResourceError\n\n\nThe outer error handler then attempts a second HTTP error response after the `202` response started. With `raise_app_exceptions=True`, the reproduction exits nonzero with an `ExceptionGroup` that contains the closed-writer failure and duplicate-response error.\n\nRelevant trace locations:\n\nmcp/server/streamable_http.py\nmcp/shared/_context_streams.py:37\nanyio/streams/memory.py\n```\n\n### Python & MCP Python SDK\n\n```Text\nPython: 3.11.14\nMCP Python SDK: 2.1.1\nFastMCP: 4.0.3 in the production incident container\nTransport: Streamable HTTP\n```","author":{"url":"https://github.com/lecton-apptio","@type":"Person","name":"lecton-apptio"},"datePublished":"2026-10-02T17:12:26Z","url":"https://github.com/modelcontextprotocol/python-sdk/issues/3631"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:4cc34630-9263-8572-50f7-7c1527ed993b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A3EC:2F9718:1634C:1E6C9:6B00B733 |
| html-safe-nonce | f837baf0377285becab9cfb939650f80a91fada82a7d603c62e0152da6c43602 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBM0VDOjJGOTcxODoxNjM0QzoxRTZDOTo2QjAwQjczMyIsInZpc2l0b3JfaWQiOiI0OTU2MTQxODgwOTQyNzMzMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2a10e6ddb9b3d868cc82a69e759980dae5034d4713259d309f4d64024d791490 |
| hovercard-subject-tag | issue:5680800407 |
| 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/3631/issue_layout |
| twitter:image | https://opengraph.githubassets.com/294542cf92365e7859618bd305fe9f3b929d20573529a212fe5beb444b3d84b1/modelcontextprotocol/python-sdk/issues/3631 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/294542cf92365e7859618bd305fe9f3b929d20573529a212fe5beb444b3d84b1/modelcontextprotocol/python-sdk/issues/3631 |
| og:image:alt | Initial Checks I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1) I confirm that I searched for my issue in https://github.com/modelcontex... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lecton-apptio |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1d07c25977adfc5ff6acb0a962a9bc168c07afbda6fef765f9761c350c2ae4a2 |
| 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 | 5429cbfc6aca8c7b9a086a3c8f47400346f38cc9 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width