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

    MCP SSE server: "Received request before initialization was complete" after redeploy

    I serve a FastMCP SSE server (modelcontextprotocol/python-sdk, April 2025) with uvicorn. On first run, the MCP Inspector lists tools fine, but after I reload the server with the same command, the next tools/list or tools/call times out and the server logs 'RuntimeError: Received request before initialization was complete'. What causes this and what are the workarounds?

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

    6 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      Still open upstream as of the thread's last comment (May 2025). The error means the server received a JSON-RPC request (tools/list, tools/call, ...) for a session that never completed the required initialize handshake. Commenters traced this to clients/proxies that send initialize twice, or post to a session that died when the server was restarted.

      What worked in the thread:

      1. Python client: call initialize() explicitly. If you connect with mcp.client.sse.sse_client + ClientSession, add await session.initialize() right after creating the ClientSession and before any tool call. One commenter confirmed this reliably fixed the error.

      2. Use mcp-proxy instead of supergateway. Per a commenter, supergateway sends its own initialize message on launch and then forwards the client's real one, so the server receives initialize twice. The same example worked flawlessly with mcp-proxy. A later comment noted the double-initialization bug was patched in supergateway 2.7.0, but another user still saw the error after a server reload even with 2.7.0, because some clients (anything-llm, fastagent) don't re-initialize the session after a disconnect.

      3. Temporary server-side patch. Monkeypatch ServerSession._received_request to swallow the RuntimeError:

      python
      from mcp.server.fastmcp import FastMCP
      from mcp.server.session import ServerSession
      
      old = ServerSession._received_request
      
      async def _received_request(self, *args, **kwargs):
          try:
              return await old(self, *args, **kwargs)
          except RuntimeError:
              pass
      
      ServerSession._received_request = _received_request
      

      The author of this patch warned it is not a long-term fix, since it hides other RuntimeErrors. A follow-up comment confirmed the error no longer appeared with it.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @wilson-urdaneta tried on GitHub (Worked)

      Added an explicit await session.initialize() right after creating the ClientSession in a Python SSE client before calling tools; the error stopped occurring.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @mheubi tried on GitHub (Worked)

      Switched from supergateway to mcp-proxy; the same FastMCP example then worked flawlessly, confirming supergateway was sending a second initialize against the protocol spec.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @folkvir tried on GitHub (Worked)

      Monkeypatched ServerSession._received_request to catch and ignore the RuntimeError; a follow-up comment confirmed the error no longer appeared.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @Nedomas tried on GitHub (Partly worked)

      Upgraded to supergateway 2.7.0, which patched the double-initialization bug; a later comment still got the error after reloading the server with that version.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

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

      Reproduced with the FastMCP BMI/weather example under uv run uvicorn main:app --port=8001 --host=0.0.0.0 --timeout-graceful-shutdown 5: the Inspector listed tools on first run, but after reloading the server, listing tools timed out with 'Received request before initialization was complete'.

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

      Did this work for you?

    Add your answer or comment