Skip to content

how to manage multi streamable http server lifespan #713

Description

@AniviaTn

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

fromfastapiimportFastAPIimportuvicornfrommcp.server.fastmcpimportFastMCPecho_mcp=FastMCP(name="EchoServer", stateless_http=True)
@echo_mcp.tool(description="A simple echo tool")defecho(message: str) ->str:
returnf"Echo: {message}"math_mcp=FastMCP(name="MathServer", stateless_http=True)
@math_mcp.tool(description="A simple add tool")defadd_two(m: int, n: int) ->int:
returnm+napp=FastAPI(lifespan=lambdaapp: echo_mcp.session_manager.run())
app.mount("/echo", echo_mcp.streamable_http_app())
app.mount("/math", math_mcp.streamable_http_app())
if__name__=="__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)

mcp client code

importasynciofrommcpimportClientSessionfrommcp.client.streamable_httpimportstreamablehttp_clientasyncdefmain():
server_url="http://127.0.0.1:8000/math/mcp"asyncwithstreamablehttp_client(server_url) as (read, write, _):
asyncwithClientSession(read, write) assession:
awaitsession.initialize()
tools=awaitsession.list_tools()
tools=tools.toolsprint(f"found {len(tools)} tools:")
if__name__=="__main__":
asyncio.run(main())

Expected behavior
Every mcp server works right.

Error Stack

/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/bin/python /Users/karsalina/PycharmProjects/playGround/script/http_server.py INFO: Started server process [44368]
INFO: Waiting for application startup.
[05/14/25 16:21:02] INFO StreamableHTTP streamable_http_manager.py:109
session manager started INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:49685 - "POST /math/mcp HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:49687 - "POST /math/mcp/ HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
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
result = await app( # type: ignore[func-returns-value]
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__
return await self.app(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
await super().__call__(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
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__
raise exc
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__
await self.app(scope, receive, _send)
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__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
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
raise exc
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
await app(scope, receive, sender)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 460, in handle
await self.app(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
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__
raise exc
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__
await self.app(scope, receive, _send)
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__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
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
raise exc
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
await app(scope, receive, sender)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 714, in __call__
await self.middleware_stack(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 734, in app
await route.handle(scope, receive, send)
File "/Users/karsalina/Library/Caches/pypoetry/virtualenvs/playground-p-gXMEVB-py3.10/lib/python3.10/site-packages/starlette/routing.py", line 460, in handle
await self.app(scope, receive, send)
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
await self.session_manager.handle_request(scope, receive, send)
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
raise RuntimeError("Task group is not initialized. Make sure to use run().")
RuntimeError: Task group is not initialized. Make sure to use run().

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions