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>
This commit is contained in:
@@ -1394,6 +1394,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
|
||||
@@ -1409,11 +1411,19 @@ def hello() -> str:
|
||||
return "Hello from MCP!"
|
||||
|
||||
|
||||
# Create a lifespan context manager to run the session manager
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with mcp.session_manager.run():
|
||||
yield
|
||||
|
||||
|
||||
# Mount the StreamableHTTP server to the existing ASGI server
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Mount("/", app=mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
```
|
||||
|
||||
@@ -1431,6 +1441,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Host
|
||||
|
||||
@@ -1446,11 +1458,19 @@ def domain_info() -> str:
|
||||
return "This is served from mcp.acme.corp"
|
||||
|
||||
|
||||
# Create a lifespan context manager to run the session manager
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with mcp.session_manager.run():
|
||||
yield
|
||||
|
||||
|
||||
# Mount using Host-based routing
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
```
|
||||
|
||||
@@ -1468,6 +1488,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
|
||||
@@ -1495,12 +1517,23 @@ def send_message(message: str) -> str:
|
||||
api_mcp.settings.streamable_http_path = "/"
|
||||
chat_mcp.settings.streamable_http_path = "/"
|
||||
|
||||
|
||||
# Create a combined lifespan to manage both session managers
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with contextlib.AsyncExitStack() as stack:
|
||||
await stack.enter_async_context(api_mcp.session_manager.run())
|
||||
await stack.enter_async_context(chat_mcp.session_manager.run())
|
||||
yield
|
||||
|
||||
|
||||
# Mount the servers
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Mount("/api", app=api_mcp.streamable_http_app()),
|
||||
Mount("/chat", app=chat_mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
|
||||
@@ -20,9 +22,17 @@ def hello() -> str:
|
||||
return "Hello from MCP!"
|
||||
|
||||
|
||||
# Create a lifespan context manager to run the session manager
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with mcp.session_manager.run():
|
||||
yield
|
||||
|
||||
|
||||
# Mount the StreamableHTTP server to the existing ASGI server
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Mount("/", app=mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
@@ -5,6 +5,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Host
|
||||
|
||||
@@ -20,9 +22,17 @@ def domain_info() -> str:
|
||||
return "This is served from mcp.acme.corp"
|
||||
|
||||
|
||||
# Create a lifespan context manager to run the session manager
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with mcp.session_manager.run():
|
||||
yield
|
||||
|
||||
|
||||
# Mount using Host-based routing
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
@@ -5,6 +5,8 @@ Run from the repository root:
|
||||
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.routing import Mount
|
||||
|
||||
@@ -32,10 +34,21 @@ def send_message(message: str) -> str:
|
||||
api_mcp.settings.streamable_http_path = "/"
|
||||
chat_mcp.settings.streamable_http_path = "/"
|
||||
|
||||
|
||||
# Create a combined lifespan to manage both session managers
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(app: Starlette):
|
||||
async with contextlib.AsyncExitStack() as stack:
|
||||
await stack.enter_async_context(api_mcp.session_manager.run())
|
||||
await stack.enter_async_context(chat_mcp.session_manager.run())
|
||||
yield
|
||||
|
||||
|
||||
# Mount the servers
|
||||
app = Starlette(
|
||||
routes=[
|
||||
Mount("/api", app=api_mcp.streamable_http_app()),
|
||||
Mount("/chat", app=chat_mcp.streamable_http_app()),
|
||||
]
|
||||
],
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user