Skip to content

Commit c605a70

Browse files
committed
Update README
1 parent 7bc7183 commit c605a70

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,8 @@ Run from the repository root:
13941394
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
13951395
"""
13961396

1397+
import contextlib
1398+
13971399
from starlette.applications import Starlette
13981400
from starlette.routing import Mount
13991401

@@ -1409,11 +1411,19 @@ def hello() -> str:
14091411
return "Hello from MCP!"
14101412

14111413

1414+
# Create a lifespan context manager to run the session manager
1415+
@contextlib.asynccontextmanager
1416+
async def lifespan(app: Starlette):
1417+
async with mcp.session_manager.run():
1418+
yield
1419+
1420+
14121421
# Mount the StreamableHTTP server to the existing ASGI server
14131422
app = Starlette(
14141423
routes=[
14151424
Mount("/", app=mcp.streamable_http_app()),
1416-
]
1425+
],
1426+
lifespan=lifespan,
14171427
)
14181428
```
14191429

@@ -1431,6 +1441,8 @@ Run from the repository root:
14311441
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
14321442
"""
14331443

1444+
import contextlib
1445+
14341446
from starlette.applications import Starlette
14351447
from starlette.routing import Host
14361448

@@ -1446,11 +1458,19 @@ def domain_info() -> str:
14461458
return "This is served from mcp.acme.corp"
14471459

14481460

1461+
# Create a lifespan context manager to run the session manager
1462+
@contextlib.asynccontextmanager
1463+
async def lifespan(app: Starlette):
1464+
async with mcp.session_manager.run():
1465+
yield
1466+
1467+
14491468
# Mount using Host-based routing
14501469
app = Starlette(
14511470
routes=[
14521471
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
1453-
]
1472+
],
1473+
lifespan=lifespan,
14541474
)
14551475
```
14561476

@@ -1468,6 +1488,8 @@ Run from the repository root:
14681488
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
14691489
"""
14701490

1491+
import contextlib
1492+
14711493
from starlette.applications import Starlette
14721494
from starlette.routing import Mount
14731495

@@ -1495,12 +1517,23 @@ def send_message(message: str) -> str:
14951517
api_mcp.settings.streamable_http_path = "/"
14961518
chat_mcp.settings.streamable_http_path = "/"
14971519

1520+
1521+
# Create a combined lifespan to manage both session managers
1522+
@contextlib.asynccontextmanager
1523+
async def lifespan(app: Starlette):
1524+
async with contextlib.AsyncExitStack() as stack:
1525+
await stack.enter_async_context(api_mcp.session_manager.run())
1526+
await stack.enter_async_context(chat_mcp.session_manager.run())
1527+
yield
1528+
1529+
14981530
# Mount the servers
14991531
app = Starlette(
15001532
routes=[
15011533
Mount("/api", app=api_mcp.streamable_http_app()),
15021534
Mount("/chat", app=chat_mcp.streamable_http_app()),
1503-
]
1535+
],
1536+
lifespan=lifespan,
15041537
)
15051538
```
15061539

0 commit comments

Comments
 (0)