@@ -1011,21 +1011,23 @@ For more information on mounting applications in Starlette, see the [Starlette d
1011
1011
1012
1012
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.
1013
1013
1014
- <!-- snippet-source examples/snippets/servers/streamable_http_mounting.py -->
1014
+ ##### Basic mounting
1015
+
1016
+ <!-- snippet-source examples/snippets/servers/streamable_http_basic_mounting.py -->
1015
1017
``` python
1016
1018
"""
1017
- Example showing how to mount StreamableHTTP servers in Starlette applications .
1019
+ Basic example showing how to mount StreamableHTTP server in Starlette.
1018
1020
1019
1021
Run from the repository root:
1020
- uvicorn examples.snippets.servers.streamable_http_mounting :app --reload
1022
+ uvicorn examples.snippets.servers.streamable_http_basic_mounting :app --reload
1021
1023
"""
1022
1024
1023
1025
from starlette.applications import Starlette
1024
- from starlette.routing import Host, Mount
1026
+ from starlette.routing import Mount
1025
1027
1026
1028
from mcp.server.fastmcp import FastMCP
1027
1029
1028
- # Basic example - mounting at root
1030
+ # Create MCP server
1029
1031
mcp = FastMCP(" My App" )
1030
1032
1031
1033
@@ -1041,11 +1043,64 @@ app = Starlette(
1041
1043
Mount(" /" , app = mcp.streamable_http_app()),
1042
1044
]
1043
1045
)
1046
+ ```
1044
1047
1045
- # or dynamically mount as host
1046
- app.router.routes.append(Host(" mcp.acme.corp" , app = mcp.streamable_http_app()))
1048
+ _ 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 ) _
1049
+ <!-- /snippet-source -->
1050
+
1051
+ ##### Host-based routing
1052
+
1053
+ <!-- snippet-source examples/snippets/servers/streamable_http_host_mounting.py -->
1054
+ ``` python
1055
+ """
1056
+ Example showing how to mount StreamableHTTP server using Host-based routing.
1057
+
1058
+ Run from the repository root:
1059
+ uvicorn examples.snippets.servers.streamable_http_host_mounting:app --reload
1060
+ """
1061
+
1062
+ from starlette.applications import Starlette
1063
+ from starlette.routing import Host
1064
+
1065
+ from mcp.server.fastmcp import FastMCP
1066
+
1067
+ # Create MCP server
1068
+ mcp = FastMCP(" MCP Host App" )
1069
+
1070
+
1071
+ @mcp.tool ()
1072
+ def domain_info () -> str :
1073
+ """ Get domain-specific information"""
1074
+ return " This is served from mcp.acme.corp"
1075
+
1076
+
1077
+ # Mount using Host-based routing
1078
+ app = Starlette(
1079
+ routes = [
1080
+ Host(" mcp.acme.corp" , app = mcp.streamable_http_app()),
1081
+ ]
1082
+ )
1083
+ ```
1084
+
1085
+ _ 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 ) _
1086
+ <!-- /snippet-source -->
1087
+
1088
+ ##### Multiple servers with path configuration
1089
+
1090
+ <!-- snippet-source examples/snippets/servers/streamable_http_multiple_servers.py -->
1091
+ ``` python
1092
+ """
1093
+ Example showing how to mount multiple StreamableHTTP servers with path configuration.
1094
+
1095
+ Run from the repository root:
1096
+ uvicorn examples.snippets.servers.streamable_http_multiple_servers:app --reload
1097
+ """
1098
+
1099
+ from starlette.applications import Starlette
1100
+ from starlette.routing import Mount
1101
+
1102
+ from mcp.server.fastmcp import FastMCP
1047
1103
1048
- # Advanced example - multiple servers with path configuration
1049
1104
# Create multiple MCP servers
1050
1105
api_mcp = FastMCP(" API Server" )
1051
1106
chat_mcp = FastMCP(" Chat Server" )
@@ -1063,31 +1118,59 @@ def send_message(message: str) -> str:
1063
1118
return f " Message sent: { message} "
1064
1119
1065
1120
1066
- # Default behavior: endpoints will be at /api/mcp and /chat/mcp
1067
- default_app = Starlette(
1068
- routes = [
1069
- Mount(" /api" , app = api_mcp.streamable_http_app()),
1070
- Mount(" /chat" , app = chat_mcp.streamable_http_app()),
1071
- ]
1072
- )
1073
-
1074
- # To mount at the root of each path (e.g., /api instead of /api/mcp):
1075
- # Configure streamable_http_path before mounting
1121
+ # Configure servers to mount at the root of each path
1122
+ # This means endpoints will be at /api and /chat instead of /api/mcp and /chat/mcp
1076
1123
api_mcp.settings.streamable_http_path = " /"
1077
1124
chat_mcp.settings.streamable_http_path = " /"
1078
1125
1079
- configured_app = Starlette(
1126
+ # Mount the servers
1127
+ app = Starlette(
1080
1128
routes = [
1081
1129
Mount(" /api" , app = api_mcp.streamable_http_app()),
1082
1130
Mount(" /chat" , app = chat_mcp.streamable_http_app()),
1083
1131
]
1084
1132
)
1133
+ ```
1134
+
1135
+ _ 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 ) _
1136
+ <!-- /snippet-source -->
1137
+
1138
+ ##### Path configuration at initialization
1139
+
1140
+ <!-- snippet-source examples/snippets/servers/streamable_http_path_config.py -->
1141
+ ``` python
1142
+ """
1143
+ Example showing path configuration during FastMCP initialization.
1144
+
1145
+ Run from the repository root:
1146
+ uvicorn examples.snippets.servers.streamable_http_path_config:app --reload
1147
+ """
1148
+
1149
+ from starlette.applications import Starlette
1150
+ from starlette.routing import Mount
1151
+
1152
+ from mcp.server.fastmcp import FastMCP
1085
1153
1086
- # Or configure during initialization
1154
+ # Configure streamable_http_path during initialization
1155
+ # This server will mount at the root of wherever it's mounted
1087
1156
mcp_at_root = FastMCP(" My Server" , streamable_http_path = " /" )
1157
+
1158
+
1159
+ @mcp_at_root.tool ()
1160
+ def process_data (data : str ) -> str :
1161
+ """ Process some data"""
1162
+ return f " Processed: { data} "
1163
+
1164
+
1165
+ # Mount at /process - endpoints will be at /process instead of /process/mcp
1166
+ app = Starlette(
1167
+ routes = [
1168
+ Mount(" /process" , app = mcp_at_root.streamable_http_app()),
1169
+ ]
1170
+ )
1088
1171
```
1089
1172
1090
- _ Full example: [ examples/snippets/servers/streamable_http_mounting .py] ( https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/streamable_http_mounting .py ) _
1173
+ _ 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 ) _
1091
1174
<!-- /snippet-source -->
1092
1175
1093
1176
#### SSE servers
0 commit comments