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

    MCP Python SDK streamable-http: why 307 redirect from /mcp to /mcp/ and how to fix?

    I start an MCP server built with the modelcontextprotocol/python-sdk using mcp.run(transport='streamable-http') and the log shows POST /mcp returning '307 Temporary Redirect', then POST /mcp/ returning 200 OK. Why does every request redirect from /mcp to /mcp/, and how do I avoid it?

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

    4 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      Explained: the 307 redirect is not a bug in the MCP Python SDK. As a contributor explained in the thread, Starlette/FastAPI automatically turns a route without a trailing slash (/mcp) into one with it (/mcp/), and the SDK's streamable_http_client does not handle redirects. The log in the issue body shows the client hitting POST /mcp, getting the 307, then retrying POST /mcp/ and getting 200 OK. A commenter attributed this to FastAPI behavior and suggested the issue may be closed in this project.

      Two workarounds came out of the thread:

      1. Client side (confirmed by the contributor): call the client with the trailing slash:

        streamablehttp_client("http://myserver.com/mcp/")
        

        The version without the trailing slash (http://myserver.com/mcp) triggers the redirect errors.

      2. Server side (suggested by a commenter, not explicitly confirmed): set streamable_http_path='/mcp/' when configuring the server so the route matches the direct path.

      The contributor also noted that not all MCP clients handle redirects, mentioning a VS Code issue as an example, so using the trailing-slash URL from the start is the safer option. General advice: keep the server path and the client URL consistent (both with or both without the trailing slash) so you don't rely on redirect behavior.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What a GitHub user tried on GitHub (Outcome unclear)

      Reporter started the server with mcp.run(transport='streamable-http') and reported the log showing POST /mcp returning 307 Temporary Redirect, followed by POST /mcp/ returning 200 OK.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @pamelafox tried on GitHub (Worked)

      A contributor tested streamablehttp_client("http://myserver.com/mcp/") and confirmed it resolves the redirect errors, while the URL without the trailing slash triggers them.

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

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @chi2liu tried on GitHub (Outcome unclear)

      A commenter suggested setting streamable_http_path='/mcp/' to avoid the redirect; the thread contains no confirmation that this fixed it.

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

      Did this work for you?

    Add your answer or comment