Skip to content

Commit 72a3400

Browse files
TheMailmansTheMailmansKludex
authored
fix: add lifespan context manager to StreamableHTTP mounting examples (#1669)
Co-authored-by: TheMailmans <tyler@example.com> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
1 parent 8e02fc1 commit 72a3400

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
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

examples/snippets/servers/streamable_http_basic_mounting.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
66
"""
77

8+
import contextlib
9+
810
from starlette.applications import Starlette
911
from starlette.routing import Mount
1012

@@ -20,9 +22,17 @@ def hello() -> str:
2022
return "Hello from MCP!"
2123

2224

25+
# Create a lifespan context manager to run the session manager
26+
@contextlib.asynccontextmanager
27+
async def lifespan(app: Starlette):
28+
async with mcp.session_manager.run():
29+
yield
30+
31+
2332
# Mount the StreamableHTTP server to the existing ASGI server
2433
app = Starlette(
2534
routes=[
2635
Mount("/", app=mcp.streamable_http_app()),
27-
]
36+
],
37+
lifespan=lifespan,
2838
)

examples/snippets/servers/streamable_http_host_mounting.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
66
"""
77

8+
import contextlib
9+
810
from starlette.applications import Starlette
911
from starlette.routing import Host
1012

@@ -20,9 +22,17 @@ def domain_info() -> str:
2022
return "This is served from mcp.acme.corp"
2123

2224

25+
# Create a lifespan context manager to run the session manager
26+
@contextlib.asynccontextmanager
27+
async def lifespan(app: Starlette):
28+
async with mcp.session_manager.run():
29+
yield
30+
31+
2332
# Mount using Host-based routing
2433
app = Starlette(
2534
routes=[
2635
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
27-
]
36+
],
37+
lifespan=lifespan,
2838
)

examples/snippets/servers/streamable_http_multiple_servers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
66
"""
77

8+
import contextlib
9+
810
from starlette.applications import Starlette
911
from starlette.routing import Mount
1012

@@ -32,10 +34,21 @@ def send_message(message: str) -> str:
3234
api_mcp.settings.streamable_http_path = "/"
3335
chat_mcp.settings.streamable_http_path = "/"
3436

37+
38+
# Create a combined lifespan to manage both session managers
39+
@contextlib.asynccontextmanager
40+
async def lifespan(app: Starlette):
41+
async with contextlib.AsyncExitStack() as stack:
42+
await stack.enter_async_context(api_mcp.session_manager.run())
43+
await stack.enter_async_context(chat_mcp.session_manager.run())
44+
yield
45+
46+
3547
# Mount the servers
3648
app = Starlette(
3749
routes=[
3850
Mount("/api", app=api_mcp.streamable_http_app()),
3951
Mount("/chat", app=chat_mcp.streamable_http_app()),
40-
]
52+
],
53+
lifespan=lifespan,
4154
)

0 commit comments

Comments
 (0)