Fix example for multiple stateless servers (#720)
This commit is contained in:
parent
13f018264c
commit
c2f8730d6d
1 changed files with 10 additions and 3 deletions
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())
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue