@@ -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+
13971399from starlette.applications import Starlette
13981400from 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
14131422app = 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+
14341446from starlette.applications import Starlette
14351447from 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
14501469app = 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+
14711493from starlette.applications import Starlette
14721494from starlette.routing import Mount
14731495
@@ -1495,12 +1517,23 @@ def send_message(message: str) -> str:
14951517api_mcp.settings.streamable_http_path = " /"
14961518chat_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
14991531app = 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
@@ -2208,12 +2241,12 @@ Run from the repository root:
22082241import asyncio
22092242
22102243from mcp import ClientSession
2211- from mcp.client.streamable_http import streamablehttp_client
2244+ from mcp.client.streamable_http import streamable_http_client
22122245
22132246
22142247async def main ():
22152248 # Connect to a streamable HTTP server
2216- async with streamablehttp_client (" http://localhost:8000/mcp" ) as (
2249+ async with streamable_http_client (" http://localhost:8000/mcp" ) as (
22172250 read_stream,
22182251 write_stream,
22192252 _,
@@ -2337,11 +2370,12 @@ cd to the `examples/snippets` directory and run:
23372370import asyncio
23382371from urllib.parse import parse_qs, urlparse
23392372
2373+ import httpx
23402374from pydantic import AnyUrl
23412375
23422376from mcp import ClientSession
23432377from mcp.client.auth import OAuthClientProvider, TokenStorage
2344- from mcp.client.streamable_http import streamablehttp_client
2378+ from mcp.client.streamable_http import streamable_http_client
23452379from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
23462380
23472381
@@ -2395,15 +2429,16 @@ async def main():
23952429 callback_handler = handle_callback,
23962430 )
23972431
2398- async with streamablehttp_client(" http://localhost:8001/mcp" , auth = oauth_auth) as (read, write, _):
2399- async with ClientSession(read, write) as session:
2400- await session.initialize()
2432+ async with httpx.AsyncClient(auth = oauth_auth, follow_redirects = True ) as custom_client:
2433+ async with streamable_http_client(" http://localhost:8001/mcp" , http_client = custom_client) as (read, write, _):
2434+ async with ClientSession(read, write) as session:
2435+ await session.initialize()
24012436
2402- tools = await session.list_tools()
2403- print (f " Available tools: { [tool.name for tool in tools.tools]} " )
2437+ tools = await session.list_tools()
2438+ print (f " Available tools: { [tool.name for tool in tools.tools]} " )
24042439
2405- resources = await session.list_resources()
2406- print (f " Available resources: { [r.uri for r in resources.resources]} " )
2440+ resources = await session.list_resources()
2441+ print (f " Available resources: { [r.uri for r in resources.resources]} " )
24072442
24082443
24092444def run ():
0 commit comments