Spaces:
Running
Running
Merge pull request #489 from jlowin/codex/view-issue-487
Browse files- docs/deployment/asgi.mdx +3 -0
- src/fastmcp/server/http.py +10 -2
- tests/server/test_app_state.py +26 -0
docs/deployment/asgi.mdx
CHANGED
|
@@ -45,6 +45,9 @@ sse_app = mcp.http_app(transport="sse")
|
|
| 45 |
|
| 46 |
Both approaches return a Starlette application that can be integrated with other ASGI-compatible web frameworks.
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
The MCP server's endpoint is mounted at the root path `/mcp` for Streamable HTTP transport, and `/sse` for SSE transport, though you can change these paths by passing a `path` argument to the `http_app()` method:
|
| 49 |
|
| 50 |
```python
|
|
|
|
| 45 |
|
| 46 |
Both approaches return a Starlette application that can be integrated with other ASGI-compatible web frameworks.
|
| 47 |
|
| 48 |
+
The returned app stores the `FastMCP` instance on `app.state.fastmcp_server`, so you
|
| 49 |
+
can access it from custom middleware or routes via `request.app.state.fastmcp_server`.
|
| 50 |
+
|
| 51 |
The MCP server's endpoint is mounted at the root path `/mcp` for Streamable HTTP transport, and `/sse` for SSE transport, though you can change these paths by passing a `path` argument to the `http_app()` method:
|
| 52 |
|
| 53 |
```python
|
src/fastmcp/server/http.py
CHANGED
|
@@ -247,11 +247,15 @@ def create_sse_app(
|
|
| 247 |
server_middleware.extend(middleware)
|
| 248 |
|
| 249 |
# Create and return the app
|
| 250 |
-
|
| 251 |
routes=server_routes,
|
| 252 |
middleware=server_middleware,
|
| 253 |
debug=debug,
|
| 254 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
|
| 257 |
def create_streamable_http_app(
|
|
@@ -344,9 +348,13 @@ def create_streamable_http_app(
|
|
| 344 |
yield
|
| 345 |
|
| 346 |
# Create and return the app with lifespan
|
| 347 |
-
|
| 348 |
routes=server_routes,
|
| 349 |
middleware=server_middleware,
|
| 350 |
debug=debug,
|
| 351 |
lifespan=lifespan,
|
| 352 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
server_middleware.extend(middleware)
|
| 248 |
|
| 249 |
# Create and return the app
|
| 250 |
+
app = create_base_app(
|
| 251 |
routes=server_routes,
|
| 252 |
middleware=server_middleware,
|
| 253 |
debug=debug,
|
| 254 |
)
|
| 255 |
+
# Store the FastMCP server instance on the Starlette app state
|
| 256 |
+
app.state.fastmcp_server = server
|
| 257 |
+
|
| 258 |
+
return app
|
| 259 |
|
| 260 |
|
| 261 |
def create_streamable_http_app(
|
|
|
|
| 348 |
yield
|
| 349 |
|
| 350 |
# Create and return the app with lifespan
|
| 351 |
+
app = create_base_app(
|
| 352 |
routes=server_routes,
|
| 353 |
middleware=server_middleware,
|
| 354 |
debug=debug,
|
| 355 |
lifespan=lifespan,
|
| 356 |
)
|
| 357 |
+
# Store the FastMCP server instance on the Starlette app state
|
| 358 |
+
app.state.fastmcp_server = server
|
| 359 |
+
|
| 360 |
+
return app
|
tests/server/test_app_state.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastmcp.server import FastMCP
|
| 2 |
+
from fastmcp.server.http import create_sse_app, create_streamable_http_app
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def test_http_app_sets_mcp_server_state():
|
| 6 |
+
server = FastMCP(name="StateTest")
|
| 7 |
+
app = server.http_app()
|
| 8 |
+
assert app.state.fastmcp_server is server
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def test_http_app_sse_sets_mcp_server_state():
|
| 12 |
+
server = FastMCP(name="StateTest")
|
| 13 |
+
app = server.http_app(transport="sse")
|
| 14 |
+
assert app.state.fastmcp_server is server
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_create_streamable_http_app_sets_state():
|
| 18 |
+
server = FastMCP(name="StateTest")
|
| 19 |
+
app = create_streamable_http_app(server, "/mcp")
|
| 20 |
+
assert app.state.fastmcp_server is server
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_create_sse_app_sets_state():
|
| 24 |
+
server = FastMCP(name="StateTest")
|
| 25 |
+
app = create_sse_app(server, message_path="/message", sse_path="/sse")
|
| 26 |
+
assert app.state.fastmcp_server is server
|