Fix example for multiple stateless servers (#720)

This commit is contained in:
ihrpr
2025-05-15 09:57:12 +01:00
committed by GitHub
parent 13f018264c
commit c2f8730d6d

View File

@@ -437,15 +437,22 @@ def add_two(n: int) -> int:
```python
# main.py
import contextlib
from fastapi import FastAPI
from mcp.echo import echo
from mcp.math import math
app = FastAPI()
# 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
# Use the session manager's lifespan
app = FastAPI(lifespan=lambda app: echo.mcp.session_manager.run())
app = FastAPI(lifespan=lifespan)
app.mount("/echo", echo.mcp.streamable_http_app())
app.mount("/math", math.mcp.streamable_http_app())
```