@@ -1175,21 +1175,23 @@ For more information on mounting applications in Starlette, see the [Starlette d
1175
1175
1176
1176
You can mount the StreamableHTTP server to an existing ASGI server using the ` streamable_http_app ` method. This allows you to integrate the StreamableHTTP server with other ASGI applications.
1177
1177
1178
- <!-- snippet-source examples/snippets/servers/streamable_http_mounting.py -->
1178
+ ##### Basic mounting
1179
+
1180
+ <!-- snippet-source examples/snippets/servers/streamable_http_basic_mounting.py -->
1179
1181
``` python
1180
1182
"""
1181
- Example showing how to mount StreamableHTTP servers in Starlette applications .
1183
+ Basic example showing how to mount StreamableHTTP server in Starlette.
1182
1184
1183
1185
Run from the repository root:
1184
- uvicorn examples.snippets.servers.streamable_http_mounting :app --reload
1186
+ uvicorn examples.snippets.servers.streamable_http_basic_mounting :app --reload
1185
1187
"""
1186
1188
1187
1189
from starlette.applications import Starlette
1188
- from starlette.routing import Host, Mount
1190
+ from starlette.routing import Mount
1189
1191
1190
1192
from mcp.server.fastmcp import FastMCP
1191
1193
1192
- # Basic example - mounting at root
1194
+ # Create MCP server
1193
1195
mcp = FastMCP(" My App" )
1194
1196
1195
1197
@@ -1205,11 +1207,64 @@ app = Starlette(
1205
1207
Mount(" /" , app = mcp.streamable_http_app()),
1206
1208
]
1207
1209
)
1210
+ ```
1208
1211
1209
- # or dynamically mount as host
1210
- app.router.routes.append(Host(" mcp.acme.corp" , app = mcp.streamable_http_app()))
1212
+ _ Full example: [ examples/snippets/servers/streamable_http_basic_mounting.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_basic_mounting.py ) _
1213
+ <!-- /snippet-source -->
1214
+
1215
+ ##### Host-based routing
1216
+
1217
+ <!-- snippet-source examples/snippets/servers/streamable_http_host_mounting.py -->
1218
+ ``` python
1219
+ """
1220
+ Example showing how to mount StreamableHTTP server using Host-based routing.
1221
+
1222
+ Run from the repository root:
1223
+ uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
1224
+ """
1225
+
1226
+ from starlette.applications import Starlette
1227
+ from starlette.routing import Host
1228
+
1229
+ from mcp.server.fastmcp import FastMCP
1230
+
1231
+ # Create MCP server
1232
+ mcp = FastMCP(" MCP Host App" )
1233
+
1234
+
1235
+ @mcp.tool ()
1236
+ def domain_info () -> str :
1237
+ """ Get domain-specific information"""
1238
+ return " This is served from mcp.acme.corp"
1239
+
1240
+
1241
+ # Mount using Host-based routing
1242
+ app = Starlette(
1243
+ routes = [
1244
+ Host(" mcp.acme.corp" , app = mcp.streamable_http_app()),
1245
+ ]
1246
+ )
1247
+ ```
1248
+
1249
+ _ Full example: [ examples/snippets/servers/streamable_http_host_mounting.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_host_mounting.py ) _
1250
+ <!-- /snippet-source -->
1251
+
1252
+ ##### Multiple servers with path configuration
1253
+
1254
+ <!-- snippet-source examples/snippets/servers/streamable_http_multiple_servers.py -->
1255
+ ``` python
1256
+ """
1257
+ Example showing how to mount multiple StreamableHTTP servers with path configuration.
1258
+
1259
+ Run from the repository root:
1260
+ uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
1261
+ """
1262
+
1263
+ from starlette.applications import Starlette
1264
+ from starlette.routing import Mount
1265
+
1266
+ from mcp.server.fastmcp import FastMCP
1211
1267
1212
- # Advanced example - multiple servers with path configuration
1213
1268
# Create multiple MCP servers
1214
1269
api_mcp = FastMCP(" API Server" )
1215
1270
chat_mcp = FastMCP(" Chat Server" )
@@ -1227,31 +1282,59 @@ def send_message(message: str) -> str:
1227
1282
return f " Message sent: { message} "
1228
1283
1229
1284
1230
- # Default behavior: endpoints will be at /api/mcp and /chat/mcp
1231
- default_app = Starlette(
1232
- routes = [
1233
- Mount(" /api" , app = api_mcp.streamable_http_app()),
1234
- Mount(" /chat" , app = chat_mcp.streamable_http_app()),
1235
- ]
1236
- )
1237
-
1238
- # To mount at the root of each path (e.g., /api instead of /api/mcp):
1239
- # Configure streamable_http_path before mounting
1285
+ # Configure servers to mount at the root of each path
1286
+ # This means endpoints will be at /api and /chat instead of /api/mcp and /chat/mcp
1240
1287
api_mcp.settings.streamable_http_path = " /"
1241
1288
chat_mcp.settings.streamable_http_path = " /"
1242
1289
1243
- configured_app = Starlette(
1290
+ # Mount the servers
1291
+ app = Starlette(
1244
1292
routes = [
1245
1293
Mount(" /api" , app = api_mcp.streamable_http_app()),
1246
1294
Mount(" /chat" , app = chat_mcp.streamable_http_app()),
1247
1295
]
1248
1296
)
1297
+ ```
1298
+
1299
+ _ Full example: [ examples/snippets/servers/streamable_http_multiple_servers.py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_multiple_servers.py ) _
1300
+ <!-- /snippet-source -->
1301
+
1302
+ ##### Path configuration at initialization
1303
+
1304
+ <!-- snippet-source examples/snippets/servers/streamable_http_path_config.py -->
1305
+ ``` python
1306
+ """
1307
+ Example showing path configuration during FastMCP initialization.
1308
+
1309
+ Run from the repository root:
1310
+ uvicorn examples.snippets.servers.streamable_http_path_config:app --reload
1311
+ """
1312
+
1313
+ from starlette.applications import Starlette
1314
+ from starlette.routing import Mount
1315
+
1316
+ from mcp.server.fastmcp import FastMCP
1249
1317
1250
- # Or configure during initialization
1318
+ # Configure streamable_http_path during initialization
1319
+ # This server will mount at the root of wherever it's mounted
1251
1320
mcp_at_root = FastMCP(" My Server" , streamable_http_path = " /" )
1321
+
1322
+
1323
+ @mcp_at_root.tool ()
1324
+ def process_data (data : str ) -> str :
1325
+ """ Process some data"""
1326
+ return f " Processed: { data} "
1327
+
1328
+
1329
+ # Mount at /process - endpoints will be at /process instead of /process/mcp
1330
+ app = Starlette(
1331
+ routes = [
1332
+ Mount(" /process" , app = mcp_at_root.streamable_http_app()),
1333
+ ]
1334
+ )
1252
1335
```
1253
1336
1254
- _ Full example: [ examples/snippets/servers/streamable_http_mounting .py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_mounting .py ) _
1337
+ _ Full example: [ examples/snippets/servers/streamable_http_path_config .py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_path_config .py ) _
1255
1338
<!-- /snippet-source -->
1256
1339
1257
1340
#### SSE servers
0 commit comments