@@ -1261,6 +1261,7 @@ Basic example showing how to mount StreamableHTTP server in Starlette.
12611261Run from the repository root:
12621262 uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
12631263"""
1264+
12641265import contextlib
12651266
12661267from starlette.applications import Starlette
@@ -1277,13 +1278,15 @@ def hello() -> str:
12771278 """ A simple hello tool"""
12781279 return " Hello from MCP!"
12791280
1281+
12801282# Create lifespan context manager to initialize the session manager
12811283@contextlib.asynccontextmanager
12821284async def lifespan (app : Starlette):
12831285 """ Context manager for managing MCP session manager lifecycle."""
12841286 async with mcp.session_manager.run():
12851287 yield
12861288
1289+
12871290# Mount the StreamableHTTP server to the existing ASGI server
12881291app = Starlette(
12891292 routes = [
@@ -1306,6 +1309,7 @@ Example showing how to mount StreamableHTTP server using Host-based routing.
13061309Run from the repository root:
13071310 uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
13081311"""
1312+
13091313import contextlib
13101314
13111315from starlette.applications import Starlette
@@ -1322,13 +1326,15 @@ def domain_info() -> str:
13221326 """ Get domain-specific information"""
13231327 return " This is served from mcp.acme.corp"
13241328
1329+
13251330# Create lifespan context manager to initialize the session manager
13261331@contextlib.asynccontextmanager
13271332async def lifespan (app : Starlette):
13281333 """ Context manager for managing MCP session manager lifecycle."""
13291334 async with mcp.session_manager.run():
13301335 yield
13311336
1337+
13321338# Mount using Host-based routing
13331339app = Starlette(
13341340 routes = [
@@ -1351,6 +1357,7 @@ Example showing how to mount multiple StreamableHTTP servers with path configura
13511357Run from the repository root:
13521358 uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
13531359"""
1360+
13541361import contextlib
13551362
13561363from starlette.applications import Starlette
@@ -1380,6 +1387,7 @@ def send_message(message: str) -> str:
13801387api_mcp.settings.streamable_http_path = " /"
13811388chat_mcp.settings.streamable_http_path = " /"
13821389
1390+
13831391# Create lifespan context manager to initialize both session managers
13841392@contextlib.asynccontextmanager
13851393async def lifespan (app : Starlette):
@@ -1389,6 +1397,7 @@ async def lifespan(app: Starlette):
13891397 await stack.enter_async_context(chat_mcp.session_manager.run())
13901398 yield
13911399
1400+
13921401# Mount the servers
13931402app = Starlette(
13941403 routes = [
@@ -1412,6 +1421,7 @@ Example showing path configuration during FastMCP initialization.
14121421Run from the repository root:
14131422 uvicorn examples.snippets.servers.streamable_http_path_config:app --reload
14141423"""
1424+
14151425import contextlib
14161426
14171427from starlette.applications import Starlette
@@ -1437,6 +1447,7 @@ async def lifespan(app: Starlette):
14371447 async with mcp_at_root.session_manager.run():
14381448 yield
14391449
1450+
14401451# Mount at /process - endpoints will be at /process instead of /process/mcp
14411452app = Starlette(
14421453 routes = [
0 commit comments