Skip to content

Commit 362b00e

Browse files
committed
use starlette
1 parent 925b22f commit 362b00e

File tree

4 files changed

+32
-41
lines changed

4 files changed

+32
-41
lines changed

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -902,22 +902,23 @@ if __name__ == "__main__":
902902
_Full example: [examples/snippets/servers/streamable_config.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_config.py)_
903903
<!-- /snippet-source -->
904904

905-
You can mount multiple FastMCP servers in a FastAPI application:
905+
You can mount multiple FastMCP servers in a Starlette application:
906906

907-
<!-- snippet-source examples/snippets/servers/streamable_fastapi_mount.py -->
907+
<!-- snippet-source examples/snippets/servers/streamable_starlette_mount.py -->
908908
```python
909-
"""Example of mounting multiple FastMCP servers in a FastAPI application.
909+
"""Example of mounting multiple FastMCP servers in a Starlette application.
910910
911911
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.
913913
914914
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
916916
"""
917917

918918
import contextlib
919919

920-
from fastapi import FastAPI
920+
from starlette.applications import Starlette
921+
from starlette.routing import Mount
921922

922923
from mcp.server.fastmcp import FastMCP
923924

@@ -943,20 +944,24 @@ def add_two(n: int) -> int:
943944

944945
# Create a combined lifespan to manage both session managers
945946
@contextlib.asynccontextmanager
946-
async def lifespan(app: FastAPI):
947+
async def lifespan(app):
947948
async with contextlib.AsyncExitStack() as stack:
948949
await stack.enter_async_context(echo_mcp.session_manager.run())
949950
await stack.enter_async_context(math_mcp.session_manager.run())
950951
yield
951952

952953

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+
)
957962
```
958963

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)_
960965
<!-- /snippet-source -->
961966

962967
For low level server with Streamable HTTP implementations, see:

examples/snippets/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ description = "MCP Example Snippets"
55
requires-python = ">=3.10"
66
dependencies = [
77
"mcp",
8-
"fastapi",
98
]
109

1110
[build-system]

examples/snippets/servers/streamable_fastapi_mount.py renamed to examples/snippets/servers/streamable_starlette_mount.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
"""Example of mounting multiple FastMCP servers in a FastAPI application.
1+
"""Example of mounting multiple FastMCP servers in a Starlette application.
22
33
This example shows how to create multiple MCP servers and mount them
4-
at different endpoints in a single FastAPI application.
4+
at different endpoints in a single Starlette application.
55
66
Run from the repository root:
7-
uvicorn examples.snippets.servers.streamable_fastapi_mount:app --reload
7+
uvicorn examples.snippets.servers.streamable_starlette_mount:app --reload
88
"""
99

1010
import contextlib
1111

12-
from fastapi import FastAPI
12+
from starlette.applications import Starlette
13+
from starlette.routing import Mount
1314

1415
from mcp.server.fastmcp import FastMCP
1516

@@ -35,14 +36,18 @@ def add_two(n: int) -> int:
3536

3637
# Create a combined lifespan to manage both session managers
3738
@contextlib.asynccontextmanager
38-
async def lifespan(app: FastAPI):
39+
async def lifespan(app):
3940
async with contextlib.AsyncExitStack() as stack:
4041
await stack.enter_async_context(echo_mcp.session_manager.run())
4142
await stack.enter_async_context(math_mcp.session_manager.run())
4243
yield
4344

4445

45-
# Create the FastAPI app and mount the MCP servers
46-
app = FastAPI(lifespan=lifespan)
47-
app.mount("/echo", echo_mcp.streamable_http_app())
48-
app.mount("/math", math_mcp.streamable_http_app())
46+
# Create the Starlette app and mount the MCP servers
47+
app = Starlette(
48+
routes=[
49+
Mount("/echo", echo_mcp.streamable_http_app()),
50+
Mount("/math", math_mcp.streamable_http_app()),
51+
],
52+
lifespan=lifespan,
53+
)

uv.lock

Lines changed: 1 addition & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)