Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Add docs

Jeremiah Lowin042a3852b1b1
diff --git a/docs/deployment/asgi.mdx b/docs/deployment/asgi.mdx
index f68f19435..e3a66d5ea 100644
--- a/docs/deployment/asgi.mdx
+++ b/docs/deployment/asgi.mdx
@@ -63,10 +63,34 @@ Or, from the command line:
uvicorn path.to.your.app:http_app --host 0.0.0.0 --port 8000
```
+### Custom Middleware
+
+<VersionBadge version="2.3.2" />
+
+You can add custom Starlette middleware to your FastMCP ASGI apps by passing a list of middleware instances to the app creation methods:
+
+```python
+from fastmcp import FastMCP
+from starlette.middleware import Middleware
+from starlette.middleware.cors import CORSMiddleware
+
+# Create your FastMCP server
+mcp = FastMCP("MyServer")
+
+# Define custom middleware
+custom_middleware = [
+ Middleware(CORSMiddleware, allow_origins=["*"]),
+]
+
+# Create ASGI app with custom middleware
+http_app = mcp.streamable_http_app(middleware=custom_middleware)
+```
## Starlette Integration
+<VersionBadge version="2.3.1" />
+
You can mount your FastMCP server in another Starlette application using the `Mount` class.
```python
@@ -127,6 +151,8 @@ For Streamable HTTP transport, you **must** pass the lifespan context from the F
</Warning>
## FastAPI Integration
+<VersionBadge version="2.3.1" />
+
FastAPI is built on Starlette, so you can mount your FastMCP server in a similar way:
```python
diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py
index 85be37412..37e97d79f 100644
--- a/src/fastmcp/server/server.py
+++ b/src/fastmcp/server/server.py
@@ -747,7 +747,14 @@ class FastMCP(Generic[LifespanResultT]):
message_path: str | None = None,
middleware: list[Middleware] | None = None,
) -> Starlette:
- """Return an instance of the SSE server app."""
+ """
+ Create a Starlette app for the SSE server.
+
+ Args:
+ path: The path to the SSE endpoint
+ message_path: The path to the message endpoint
+ middleware: A list of middleware to apply to the app
+ """
return create_sse_app(
server=self,
message_path=message_path or self.settings.message_path,
@@ -764,7 +771,13 @@ class FastMCP(Generic[LifespanResultT]):
path: str | None = None,
middleware: list[Middleware] | None = None,
) -> Starlette:
- """Return an instance of the StreamableHTTP server app."""
+ """
+ Create a Starlette app for the StreamableHTTP server.
+
+ Args:
+ path: The path to the StreamableHTTP endpoint
+ middleware: A list of middleware to apply to the app
+ """
from fastmcp.server.http import create_streamable_http_app
return create_streamable_http_app(