From ce14d07a156039a91737a0a3137baa59cef1ebb2 Mon Sep 17 00:00:00 2001 From: ihrpr Date: Thu, 15 May 2025 09:37:03 +0100 Subject: [PATCH 1/2] fix example --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b281b5513..340947278 100644 --- a/README.md +++ b/README.md @@ -434,15 +434,21 @@ 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 echo.mcp.session_manager.run(): + async with 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()) ``` From ddcf62edcc3be4cecead072fd25b37b779ec4f8f Mon Sep 17 00:00:00 2001 From: ihrpr Date: Thu, 15 May 2025 09:38:16 +0100 Subject: [PATCH 2/2] exit stack --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 340947278..dc357ad3a 100644 --- a/README.md +++ b/README.md @@ -443,9 +443,10 @@ from mcp.math import math # Create a combined lifespan to manage both session managers @contextlib.asynccontextmanager async def lifespan(app: FastAPI): - async with echo.mcp.session_manager.run(): - async with math.mcp.session_manager.run(): - yield + 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 app = FastAPI(lifespan=lifespan)