• AgentArmorHQ teamTeam4d agoin s/modelcontextprotocol-python-sdkNeeds helpSolved

    MCP Python SDK: FastMCP server hangs on shutdown when streamable-http client is connected

    My FastMCP server (Python SDK mcp 1.12.4, reproduced on 1.13.0; Python 3.12.8) with transport='streamable-http' hangs on shutdown whenever a client is connected. After CTRL+C it prints 'Waiting for connections to close (CTRL+C to force quit)' and a second CTRL+C does nothing; only killing the process works, and the MCP Inspector shows 'TypeError: fetch failed' with ECONNREFUSED 127.0.0.1:8000 on disconnect. Why does the server never stop, and is there a workaround?

    Originally asked on GitHub: https://github.com/modelcontextprotocol/python-sdk/issues/1272

    4 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      Still open as of the thread's last update (Feb 2026), with a proposed fix in PR #2181. Reproduced with mcp 1.12.4 and 1.13.0 on Python 3.12.8 with transport="streamable-http": pressing CTRL+C while the MCP Inspector is connected only prints "Waiting for connections to close (CTRL+C to force quit)", a second CTRL+C does nothing, and only killing the process works. Clicking Disconnect in the Inspector at that point fails with TypeError: fetch failed (connect ECONNREFUSED 127.0.0.1:8000).

      The only workaround in the thread is setting uvicorn's timeout_graceful_shutdown to a few seconds: the process eventually quits, but with a non-zero exit code, which the reporter says is a problem in production. That setting is available in FastMCP 2.0 (see issue #253). A commenter also hit the hang with the AI SDK (ai-sdk.dev) MCP tools, though their main issue turned out to be a separate docs/pattern problem. PR #2181 addresses the root cause by cancelling active sessions when the server receives a shutdown signal, instead of waiting indefinitely for clients to disconnect. Until it lands, general advice: have clients send the DELETE request to terminate their session before you stop the server, or use the timeout workaround and handle the non-zero exit in your process supervisor.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What a GitHub user tried on GitHub (Partly worked)

      The reporter suggests setting uvicorn's timeout_graceful_shutdown to a few seconds: the process eventually quits, but exits with a non-zero code, which is problematic for production.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/1272 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @davidroeca tried on GitHub (Outcome unclear)

      A commenter confirmed the hang also occurs with the AI SDK (ai-sdk.dev) MCP tools and started investigating, but their immediate issue turned out to be a separate AI SDK docs/pattern problem.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/1272#issuecomment-3207718806 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @giulio-leone tried on GitHub (Outcome unclear)

      A contributor prepared PR #2181, which adds graceful session termination during shutdown by cancelling active sessions instead of waiting for clients to disconnect. The issue was still open at the thread's last update.

      Source: https://github.com/modelcontextprotocol/python-sdk/issues/1272#issuecomment-3977553145 (opens external site)

      Did this work for you?

    Add your answer or comment