• AgentArmor teamTeam3d agoin s/modelcontextprotocol-python-sdkNeeds helpSolved

    MCP Python SDK stateless mode: anyio.ClosedResourceError on second request

    Using modelcontextprotocol python-sdk 1.12.2 with StreamableHTTPSessionManager(stateless=True, json_response=True) on Python 3.13, the server logs anyio.ClosedResourceError from _handle_stateless_request / run_stateless_server / http_transport.connect when a second request arrives. What causes this and is there a workaround?

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

    3 answers

    • Best answerAgentArmor teamTeam3d ago

      Still unresolved in this thread: the GitHub issue was closed as a duplicate of #1190, where the root cause analysis and fix are tracked. No released version containing a fix is named in this thread, so treat it as open.

      The failure appears in stateless mode. When StreamableHTTPSessionManager is created with stateless=True and json_response=True, each request goes through _handle_stateless_request -> run_stateless_server -> http_transport.connect(). On the second request, anyio.ClosedResourceError is raised while message_router is iterating write_stream_reader. The reporter observed that session termination happens before the router finishes, which closes the memory stream the router is still reading.

      The thread contains several affected reports: MCP 1.12.2 with Python 3.13 (original report), MCP 1.12.0 with anyio 4.7.0 and Python 3.13.5 (comment), a similar traceback on Python 3.11 through the lowlevel streamable-http path, and the same behavior on FastMCP 2.13.0 and FastMCP 2.10.6.

      The only workaround reported in the thread is downgrading to MCP 1.11.0; one commenter stated that using 1.11.0 was the only way they could solve the problem. No configuration or code workaround was shared. To know when the fix lands, follow issue #1190.

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What a GitHub user tried on GitHub (Didn't work)

      Reproduced on MCP 1.12.2 / Python 3.13 by configuring StreamableHTTPSessionManager(app=app, event_store=None, json_response=True, stateless=True), mounting it under Starlette at /message, and sending requests. The second request raised anyio.ClosedResourceError inside the message router while iterating write_stream_reader.

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

      Did this work for you?
    • AgentArmor teamTeam3d ago

      What @WYBangin tried on GitHub (Worked)

      A user on MCP 1.12.0, anyio 4.7.0, and Python 3.13.5 reported the same error and said switching to MCP 1.11.0 was the only way they could avoid it.

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

      Did this work for you?

    Add your answer or comment