Title: how to manage multi streamable http server lifespan · Issue #713 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: how to manage multi streamable http server lifespan · Issue #713 · modelcontextprotocol/python-sdk
X Title: how to manage multi streamable http server lifespan · Issue #713 · modelcontextprotocol/python-sdk
Description: Describe the bug I write a multi streamable http server example according to your readme doc, but only the mcp server which pass the session_manager.run() to FastAPI works right To Reproduce mcp python sdk ver 1.8.1 mcp server code from ...
Open Graph Description: Describe the bug I write a multi streamable http server example according to your readme doc, but only the mcp server which pass the session_manager.run() to FastAPI works right To Reproduce mcp py...
X Description: Describe the bug I write a multi streamable http server example according to your readme doc, but only the mcp server which pass the session_manager.run() to FastAPI works right To Reproduce mcp py...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/713
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"how to manage multi streamable http server lifespan","articleBody":"**Describe the bug**\nI write a multi streamable http server example according to your readme doc, but only the mcp server which pass the session_manager.run() to FastAPI works right\n\n**To Reproduce**\nmcp python sdk ver 1.8.1\n\nmcp server code\n```python\nfrom fastapi import FastAPI\nimport uvicorn\nfrom mcp.server.fastmcp import FastMCP\n\n\necho_mcp = FastMCP(name=\"EchoServer\", stateless_http=True)\n\n\n@echo_mcp.tool(description=\"A simple echo tool\")\ndef echo(message: str) -\u003e str:\n return f\"Echo: {message}\"\n\nmath_mcp = FastMCP(name=\"MathServer\", stateless_http=True)\n\n\n@math_mcp.tool(description=\"A simple add tool\")\ndef add_two(m: int, n: int) -\u003e int:\n return m + n\n\n\napp = FastAPI(lifespan=lambda app: echo_mcp.session_manager.run())\napp.mount(\"/echo\", echo_mcp.streamable_http_app())\napp.mount(\"/math\", math_mcp.streamable_http_app())\n\n\nif __name__ == \"__main__\":\n uvicorn.run(app, host=\"127.0.0.1\", port=8000)\n```\n\nmcp client code\n```python\nimport asyncio\nfrom mcp import ClientSession\nfrom mcp.client.streamable_http import streamablehttp_client\n\n\nasync def main():\n server_url = \"http://127.0.0.1:8000/math/mcp\"\n\n async with streamablehttp_client(server_url) as (read, write, _):\n async with ClientSession(read, write) as session:\n await session.initialize()\n tools = await session.list_tools()\n tools = tools.tools\n\n print(f\"found {len(tools)} tools:\")\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n\n\n**Expected behavior**\nEvery mcp server works right.\n\n**Error Stack**\n```SHELL\n/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/bin/python /Users/karsalina/PycharmProjects/playGround/script/http_server.py \nINFO: Started server process [44368]\nINFO: Waiting for application startup.\n[05/14/25 16:21:02] INFO StreamableHTTP streamable_http_manager.py:109\n session manager \n started \nINFO: Application startup complete.\nINFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\nINFO: 127.0.0.1:49685 - \"POST /math/mcp HTTP/1.1\" 307 Temporary Redirect\nINFO: 127.0.0.1:49687 - \"POST /math/mcp/ HTTP/1.1\" 500 Internal Server Error\nERROR: Exception in ASGI application\nTraceback (most recent call last):\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py\", line 411, in run_asgi\n result = await app( # type: ignore[func-returns-value]\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py\", line 69, in __call__\n return await self.app(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/fastapi/applications.py\", line 1054, in __call__\n await super().__call__(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/applications.py\", line 112, in __call__\n await self.middleware_stack(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py\", line 187, in __call__\n raise exc\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py\", line 165, in __call__\n await self.app(scope, receive, _send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py\", line 62, in __call__\n await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n raise exc\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/_exception_handler.py\", line 42, in wrapped_app\n await app(scope, receive, sender)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 714, in __call__\n await self.middleware_stack(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 734, in app\n await route.handle(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 460, in handle\n await self.app(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/applications.py\", line 112, in __call__\n await self.middleware_stack(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py\", line 187, in __call__\n raise exc\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/errors.py\", line 165, in __call__\n await self.app(scope, receive, _send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/middleware/exceptions.py\", line 62, in __call__\n await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/_exception_handler.py\", line 53, in wrapped_app\n raise exc\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/_exception_handler.py\", line 42, in wrapped_app\n await app(scope, receive, sender)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 714, in __call__\n await self.middleware_stack(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 734, in app\n await route.handle(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py\", line 460, in handle\n await self.app(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/mcp/server/fastmcp/server.py\", line 782, in handle_streamable_http\n await self.session_manager.handle_request(scope, receive, send)\n File \"/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/mcp/server/streamable_http_manager.py\", line 137, in handle_request\n raise RuntimeError(\"Task group is not initialized. Make sure to use run().\")\nRuntimeError: Task group is not initialized. Make sure to use run().\n```","author":{"url":"https://github.com/AniviaTn","@type":"Person","name":"AniviaTn"},"datePublished":"2025-05-14T08:24:51.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":18},"url":"https://github.com/713/python-sdk/issues/713"}
| 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:56c5ce8e-c103-cdb6-58a0-09e409733ab9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E05C:13E6B4:18C27CF:2391CCE:6A60902F |
| html-safe-nonce | 008e7e2394668e34de50b217d5206a88c8ee406decc60b76b444b9ea8e05e6c9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMDVDOjEzRTZCNDoxOEMyN0NGOjIzOTFDQ0U6NkE2MDkwMkYiLCJ2aXNpdG9yX2lkIjoiODQwNjcyMDUyNzQxMjAwNjk1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f19b70da997ff67a1a43b6f77e9bd747808c2b03b621ef0ac16f8d7f3f2c4289 |
| hovercard-subject-tag | issue:3062267065 |
| 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/713/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d40b11bf40ccf9d08d9d6b5462f03c01f9d2ddb315aec22999f989f0d6d1a45/modelcontextprotocol/python-sdk/issues/713 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d40b11bf40ccf9d08d9d6b5462f03c01f9d2ddb315aec22999f989f0d6d1a45/modelcontextprotocol/python-sdk/issues/713 |
| og:image:alt | Describe the bug I write a multi streamable http server example according to your readme doc, but only the mcp server which pass the session_manager.run() to FastAPI works right To Reproduce mcp py... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AniviaTn |
| hostname | github.com |
| expected-hostname | github.com |
| None | c0632c7929f76e8db538ffed06ab2fc4c524a012b0888e95d96928690fcc604d |
| 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 | 4f9009410bd92637ba907f71dc7f7af3f0df45aa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width