From c2f8730d6db50e23c88609e16ae610476b85bbe6 Mon Sep 17 00:00:00 2001 From: ihrpr Date: Thu, 15 May 2025 09:57:12 +0100 Subject: [PATCH] Fix example for multiple stateless servers (#720) --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 57088f9..2611e25 100644 --- a/README.md +++ b/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()) ```