Skip to content

Request redirection for the MCP server mounted with FastAPI #951

Open
@Archerll

Description

@Archerll

Question

While trying to use the code recommended in the README, I found that I couldn't access the /echo and /math endpoints.

Here is the environment I'm using:

  • fastapi = 0.115.12
  • mcp = 1.9.3

and I followed the example code exactly as provided.

# echo.py
from mcp.server.fastmcp import FastMCP

mcp = FastMCP(name="EchoServer", stateless_http=True)


@mcp.tool(description="A simple echo tool")
def echo(message: str) -> str:
    return f"Echo: {message}"
# math.py
from mcp.server.fastmcp import FastMCP

mcp = FastMCP(name="MathServer", stateless_http=True)


@mcp.tool(description="A simple add tool")
def add_two(n: int) -> int:
    return n + 2

# main.py
import contextlib
from fastapi import FastAPI
from mcp.echo import echo
from mcp.math import math


# Create a combined lifespan to manage both session managers
@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
    async with contextlib.AsyncExitStack() as stack:
        await stack.enter_async_context(echo.mcp.session_manager.run())
        await stack.enter_async_context(math.mcp.session_manager.run())
        yield


app = FastAPI(lifespan=lifespan)
app.mount("/echo", echo.mcp.streamable_http_app())
app.mount("/math", math.mcp.streamable_http_app())

After running the ​​main​​ application, when requesting:

http://localhost:8000/echo
http://localhost:8000/math

I encountered the following error:

INFO:     "POST /echo HTTP/1.1" 307 Temporary Redirect
INFO:     "POST /echo/ HTTP/1.1" 404 Not Found
INFO:     "POST /math HTTP/1.1" 307 Temporary Redirect
INFO:      "POST /math/ HTTP/1.1" 404 Not Found

I'm not sure how to resolve this issue. Perhaps this section could be explained in more detail in the README?

Additional Context

mcp = 1.9.3
python = 3.12.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions