[Python] Building an MCP Proxy: RuntimeError: FastMCP's StreamableHTTPSessionManager task...

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Some context: I am building an MCP proxy server using the FastMCP library. The proxy server connects to different MCP servers (mcp-server-chart, sequential thinking, some custom servers) through streamable-http. The proxy stores the transport object of these servers and forwards requests to the appropriate servers

    I run my proxy like this:

    proxy = FastMCP("name")
    proxy.run(transport, host, port)


    When I send requests to server-chart or a custom server I developed, it works perfectly fine. However, when I forward requests to sequential-thinking, I get the following error:

    raise RuntimeError(f"{new_error_message}\\nOriginal error: {e}") from e
    2025-07-09 09:37:21 RuntimeError: FastMCP's StreamableHTTPSessionManager task group was not initialized. This commonly occurs when the FastMCP application's lifespan is not passed to the parent ASGI application (e.g., FastAPI or Starlette). Please ensure you are setting `lifespan=mcp_app.lifespan` in your parent app's constructor, where `mcp_app` is the application instance returned by `fastmcp_instance.http_app()`. \nFor more details, see the FastMCP ASGI integration documentation: https://gofastmcp.com/deployment/asgi\nOriginal error: Task group is not initialized. Make sure to use run().


    Since sequential thinking did not support http, I had to clone the repo and implement it myself. Once again I am simply running this with mcp.run(args)

    Why do I get this error and how do I solve it? Is it because I am expected to use run_async? Or is there another issue?

    I thought of running my proxy and sequential thinking both with run_async (because my custom server uses run_async and it seems to work fine). I also thought about wrapping the mcp app in a FastAPI app and setting the lifetime argument:

    mcp = FastMCP("demo")
    mcp_app = mcp.http_app(path="/mcp")

    app = FastAPI(lifespan=mcp_app.lifespan)
    app.mount("/demo", mcp_app)

    Continue reading...

Compartilhe esta Página