Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

how to manage multi streamable http server lifespan #713

Copy link
Copy link
Open
@AniviaTn

Description

@AniviaTn
Issue body actions

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 fastapi import FastAPI
import uvicorn
from mcp.server.fastmcp import FastMCP


echo_mcp = FastMCP(name="EchoServer", stateless_http=True)


@echo_mcp.tool(description="A simple echo tool")
def echo(message: str) -> str:
    return f"Echo: {message}"

math_mcp = FastMCP(name="MathServer", stateless_http=True)


@math_mcp.tool(description="A simple add tool")
def add_two(m: int, n: int) -> int:
    return m + n


app = FastAPI(lifespan=lambda app: 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

import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client


async def main():
    server_url = "http://127.0.0.1:8000/math/mcp"

    async with streamablehttp_client(server_url) as (read, write, _):
        async with ClientSession(read, write) as session:
            await session.initialize()
            tools = await session.list_tools()
            tools = tools.tools

            print(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

No labels
No labels

Type

No type

Projects

Status

To triage
Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.