mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 14:54:24 +01:00
Fix example for multiple stateless servers (#720)
This commit is contained in:
13
README.md
13
README.md
@@ -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())
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user