Title: _handle_stateless_request ClosedResourceError · Issue #1219 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: _handle_stateless_request ClosedResourceError · Issue #1219 · modelcontextprotocol/python-sdk
X Title: _handle_stateless_request ClosedResourceError · Issue #1219 · 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 Every request will try...
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/1219
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_handle_stateless_request ClosedResourceError","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\nEvery request will try an exception\n\nuse `_handle_stateless_request` \u003e `run_stateless_server `\u003e `http_transport.connect()` \u003e `for session_message in write_stream_reader`\nIt will go in twice, and an exception(yio.ClosedResourceError) will be thrown when the second request has been closed\n\n### Example Code\n```Python\napp = Server(\"mcp-server\")\ndef main(port: int, transport: str, dev: bool, limit_concurrency: int) -\u003e int:\n # Create the session manager with true stateless mode\n from starlette.applications import Starlette\n from starlette.routing import Mount, Route\n session_manager = StreamableHTTPSessionManager(\n app=app,\n event_store=None,\n json_response=True,\n stateless=True, \n )\n\n @contextlib.asynccontextmanager\n async def lifespan(app: Starlette) -\u003e AsyncIterator[None]:\n \"\"\"Context manager for managing session manager lifecycle.\"\"\"\n async with session_manager.run():\n logger.info(\"Application started with StreamableHTTP session manager!\")\n try:\n yield\n finally:\n logger.info(\"Application shutting down...\")\n\n async def handle_streamable_http(\n scope: Scope, receive: Receive, send: Send\n ) -\u003e None:\n await session_manager.handle_request(scope, receive, send)\n\n starlette_app = Starlette(\n debug=dev,\n routes=[\n Mount(\"/message\", app=handle_streamable_http), \n ],\n lifespan=lifespan,\n )\n import uvicorn\n uvicorn.run(starlette_app, host=\"0.0.0.0\", port=port, http=\"httptools\", limit_concurrency=limit_concurrency)\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\nview logs:\n```shell\n[2025-07-31 15:10:41,884][MainThread:19320][task_id:mcp.server.streamable_http][streamable_http.py:630][INFO][Terminating session: None]\n[2025-07-31 15:10:41,885][MainThread:19320][task_id:mcp.server.streamable_http][streamable_http.py:880][ERROR][Error in message router]\nTraceback (most recent call last):\n File \"d:\\project\\bpaas\\apihub-mcp-server\\.venv\\Lib\\site-packages\\mcp\\server\\streamable_http.py\", line 831, in message_router\n async for session_message in write_stream_reader:\n ...\u003c46 lines\u003e...\n )\n File \"d:\\project\\bpaas\\apihub-mcp-server\\.venv\\Lib\\site-packages\\anyio\\abc\\_streams.py\", line 35, in __anext__\n return await self.receive()\n ^^^^^^^^^^^^^^^^^^^^\n File \"d:\\project\\bpaas\\apihub-mcp-server\\.venv\\Lib\\site-packages\\anyio\\streams\\memory.py\", line 111, in receive\n return self.receive_nowait()\n ~~~~~~~~~~~~~~~~~~~^^\n File \"d:\\project\\bpaas\\apihub-mcp-server\\.venv\\Lib\\site-packages\\anyio\\streams\\memory.py\", line 93, in receive_nowait\n raise ClosedResourceError\nanyio.ClosedResourceError\n```\n\nTerminating has already released memory first\n\ndebug for : first\n\n\u003cimg width=\"894\" height=\"388\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/c7b1b741-1d0a-433e-b8ce-6d233b9fc125\" /\u003e\n\ndebug for : second\n\n\u003cimg width=\"818\" height=\"218\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/2b388192-30f5-461b-b228-62e430573bad\" /\u003e\n\nAnd the log shows that `Terminating` should be executed first, followed by `create_task_group`\n\n### Python \u0026 MCP Python SDK\n\n```Text\nMCP version: 1.12.2\nPython:3.13\n```","author":{"url":"https://github.com/281743556","@type":"Person","name":"281743556"},"datePublished":"2025-07-31T07:24:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/1219/python-sdk/issues/1219"}
| 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:a090d91f-8dab-e49d-c39d-6a3d261708b6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C994:A7CE4:127874E:18C409E:6A5B7D90 |
| html-safe-nonce | a6f1c5cfc31411bce6f360eab8412f79d7d81d706fa13a70e6eba3741a04dac4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOTk0OkE3Q0U0OjEyNzg3NEU6MThDNDA5RTo2QTVCN0Q5MCIsInZpc2l0b3JfaWQiOiI3OTE5OTQ0MTc2OTIxOTY4MDE2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 222c8559704c3c14f6240cbfa465f5da513a37f98e9872f701214b39b520ad49 |
| hovercard-subject-tag | issue:3279410757 |
| 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/1219/issue_layout |
| twitter:image | https://opengraph.githubassets.com/63685a0b6e37cd4728aae05bb4142862a0ca0e52f5a45f2447c82577d6ed80c8/modelcontextprotocol/python-sdk/issues/1219 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/63685a0b6e37cd4728aae05bb4142862a0ca0e52f5a45f2447c82577d6ed80c8/modelcontextprotocol/python-sdk/issues/1219 |
| 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 | 281743556 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width