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:
parent
8e02fc17e1
commit
72a34002aa
4 changed files with 72 additions and 6 deletions
39
README.md
39
README.md
|
|
@ -1394,6 +1394,8 @@ Run from the repository root:
|
||||||
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
|
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Mount
|
from starlette.routing import Mount
|
||||||
|
|
||||||
|
|
@ -1409,11 +1411,19 @@ def hello() -> str:
|
||||||
return "Hello from MCP!"
|
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
|
# Mount the StreamableHTTP server to the existing ASGI server
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Mount("/", app=mcp.streamable_http_app()),
|
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
|
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Host
|
from starlette.routing import Host
|
||||||
|
|
||||||
|
|
@ -1446,11 +1458,19 @@ def domain_info() -> str:
|
||||||
return "This is served from mcp.acme.corp"
|
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
|
# Mount using Host-based routing
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
|
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
|
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Mount
|
from starlette.routing import Mount
|
||||||
|
|
||||||
|
|
@ -1495,12 +1517,23 @@ def send_message(message: str) -> str:
|
||||||
api_mcp.settings.streamable_http_path = "/"
|
api_mcp.settings.streamable_http_path = "/"
|
||||||
chat_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
|
# Mount the servers
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Mount("/api", app=api_mcp.streamable_http_app()),
|
Mount("/api", app=api_mcp.streamable_http_app()),
|
||||||
Mount("/chat", app=chat_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
|
uvicorn examples.snippets.servers.streamable_http_basic_mounting:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Mount
|
from starlette.routing import Mount
|
||||||
|
|
||||||
|
|
@ -20,9 +22,17 @@ def hello() -> str:
|
||||||
return "Hello from MCP!"
|
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
|
# Mount the StreamableHTTP server to the existing ASGI server
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Mount("/", app=mcp.streamable_http_app()),
|
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
|
uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Host
|
from starlette.routing import Host
|
||||||
|
|
||||||
|
|
@ -20,9 +22,17 @@ def domain_info() -> str:
|
||||||
return "This is served from mcp.acme.corp"
|
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
|
# Mount using Host-based routing
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Host("mcp.acme.corp", app=mcp.streamable_http_app()),
|
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
|
uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
|
||||||
from starlette.applications import Starlette
|
from starlette.applications import Starlette
|
||||||
from starlette.routing import Mount
|
from starlette.routing import Mount
|
||||||
|
|
||||||
|
|
@ -32,10 +34,21 @@ def send_message(message: str) -> str:
|
||||||
api_mcp.settings.streamable_http_path = "/"
|
api_mcp.settings.streamable_http_path = "/"
|
||||||
chat_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
|
# Mount the servers
|
||||||
app = Starlette(
|
app = Starlette(
|
||||||
routes=[
|
routes=[
|
||||||
Mount("/api", app=api_mcp.streamable_http_app()),
|
Mount("/api", app=api_mcp.streamable_http_app()),
|
||||||
Mount("/chat", app=chat_mcp.streamable_http_app()),
|
Mount("/chat", app=chat_mcp.streamable_http_app()),
|
||||||
]
|
],
|
||||||
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue