Jeremiah Lowin commited on
Commit
86a15b9
·
1 Parent(s): e1a5c52

Store server instance as fastmcp_server

Browse files
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
- return create_base_app(
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
- return create_base_app(
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,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from fastmcp.server import FastMCP
3
+ from fastmcp.server.http import create_sse_app, create_streamable_http_app
4
+
5
+
6
+ def test_http_app_sets_mcp_server_state():
7
+ server = FastMCP(name="StateTest")
8
+ app = server.http_app()
9
+ assert app.state.fastmcp_server is server
10
+
11
+
12
+ def test_http_app_sse_sets_mcp_server_state():
13
+ server = FastMCP(name="StateTest")
14
+ app = server.http_app(transport="sse")
15
+ assert app.state.fastmcp_server is server
16
+
17
+
18
+ def test_create_streamable_http_app_sets_state():
19
+ server = FastMCP(name="StateTest")
20
+ app = create_streamable_http_app(server, "/mcp")
21
+ assert app.state.fastmcp_server is server
22
+
23
+
24
+ def test_create_sse_app_sets_state():
25
+ server = FastMCP(name="StateTest")
26
+ app = create_sse_app(server, message_path="/message", sse_path="/sse")
27
+ assert app.state.fastmcp_server is server