@@ -902,22 +902,23 @@ if __name__ == "__main__":
902
902
_ Full example: [ examples/snippets/servers/streamable_config.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_config.py ) _
903
903
<!-- /snippet-source -->
904
904
905
- You can mount multiple FastMCP servers in a FastAPI application:
905
+ You can mount multiple FastMCP servers in a Starlette application:
906
906
907
- <!-- snippet-source examples/snippets/servers/streamable_fastapi_mount .py -->
907
+ <!-- snippet-source examples/snippets/servers/streamable_starlette_mount .py -->
908
908
``` python
909
- """ Example of mounting multiple FastMCP servers in a FastAPI application.
909
+ """ Example of mounting multiple FastMCP servers in a Starlette application.
910
910
911
911
This example shows how to create multiple MCP servers and mount them
912
- at different endpoints in a single FastAPI application.
912
+ at different endpoints in a single Starlette application.
913
913
914
914
Run from the repository root:
915
- uvicorn examples.snippets.servers.streamable_fastapi_mount :app --reload
915
+ uvicorn examples.snippets.servers.streamable_starlette_mount :app --reload
916
916
"""
917
917
918
918
import contextlib
919
919
920
- from fastapi import FastAPI
920
+ from starlette.applications import Starlette
921
+ from starlette.routing import Mount
921
922
922
923
from mcp.server.fastmcp import FastMCP
923
924
@@ -943,20 +944,24 @@ def add_two(n: int) -> int:
943
944
944
945
# Create a combined lifespan to manage both session managers
945
946
@contextlib.asynccontextmanager
946
- async def lifespan (app : FastAPI ):
947
+ async def lifespan (app ):
947
948
async with contextlib.AsyncExitStack() as stack:
948
949
await stack.enter_async_context(echo_mcp.session_manager.run())
949
950
await stack.enter_async_context(math_mcp.session_manager.run())
950
951
yield
951
952
952
953
953
- # Create the FastAPI app and mount the MCP servers
954
- app = FastAPI(lifespan = lifespan)
955
- app.mount(" /echo" , echo_mcp.streamable_http_app())
956
- app.mount(" /math" , math_mcp.streamable_http_app())
954
+ # Create the Starlette app and mount the MCP servers
955
+ app = Starlette(
956
+ routes = [
957
+ Mount(" /echo" , echo_mcp.streamable_http_app()),
958
+ Mount(" /math" , math_mcp.streamable_http_app()),
959
+ ],
960
+ lifespan = lifespan,
961
+ )
957
962
```
958
963
959
- _ Full example: [ examples/snippets/servers/streamable_fastapi_mount .py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_fastapi_mount .py ) _
964
+ _ Full example: [ examples/snippets/servers/streamable_starlette_mount .py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_starlette_mount .py ) _
960
965
<!-- /snippet-source -->
961
966
962
967
For low level server with Streamable HTTP implementations, see:
0 commit comments