Skip to content

Commit a869767

Browse files
committed
add example of FastAPI app with multiple routes
1 parent 5424832 commit a869767

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,30 @@ mcp = FastMCP("StatelessServer", stateless_http=True)
404404
mcp.run(transport="streamable-http")
405405
```
406406

407+
You can mount multiple FastMCP servers in a FastAPI application:
408+
409+
```python
410+
# echo.py
411+
from mcp.server.fastmcp import FastMCP
412+
413+
mcp = FastMCP(name="EchoServer", stateless_http=True)
414+
415+
@mcp.tool(description="A simple echo tool")
416+
def echo(message: str) -> str:
417+
return f"Echo: {message}"
418+
419+
# main.py
420+
from fastapi import FastAPI
421+
from routers import echo
422+
423+
app = FastAPI()
424+
425+
# Use the session manager's lifespan
426+
app = FastAPI(lifespan=lambda app: echo.mcp.session_manager.run())
427+
app.mount("/echo", echo.mcp.streamable_http_app())
428+
429+
```
430+
407431
For low level server with Streamable HTTP implementations, see:
408432
- Stateful server: [`examples/servers/simple-streamablehttp/`](examples/servers/simple-streamablehttp/)
409433
- Stateless server: [`examples/servers/simple-streamablehttp-stateless/`](examples/servers/simple-streamablehttp-stateless/)

0 commit comments

Comments
 (0)