Skip to content

Commit c2f8730

Browse files
authored
Fix example for multiple stateless servers (modelcontextprotocol#720)
1 parent 13f0182 commit c2f8730

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,22 @@ def add_two(n: int) -> int:
437437

438438
```python
439439
# main.py
440+
import contextlib
440441
from fastapi import FastAPI
441442
from mcp.echo import echo
442443
from mcp.math import math
443444

444445

445-
app = FastAPI()
446+
# Create a combined lifespan to manage both session managers
447+
@contextlib.asynccontextmanager
448+
async def lifespan(app: FastAPI):
449+
async with contextlib.AsyncExitStack() as stack:
450+
await stack.enter_async_context(echo.mcp.session_manager.run())
451+
await stack.enter_async_context(math.mcp.session_manager.run())
452+
yield
446453

447-
# Use the session manager's lifespan
448-
app = FastAPI(lifespan=lambda app: echo.mcp.session_manager.run())
454+
455+
app = FastAPI(lifespan=lifespan)
449456
app.mount("/echo", echo.mcp.streamable_http_app())
450457
app.mount("/math", math.mcp.streamable_http_app())
451458
```

0 commit comments

Comments
 (0)