Starlette / ASGI 🤝 FastMCP
Integrate FastMCP servers into ASGI applications
New in version: 2.3.1
FastMCP servers can be integrated into existing ASGI applications, allowing you to add MCP functionality to your web applications. This is useful for:
- Adding MCP functionality to an existing website or API
- Mounting MCP servers under specific URL paths
- Combining multiple services in a single application
- Leveraging existing authentication and middleware
Basic Usage
To integrate a FastMCP server into an ASGI application, use the http_app()
method to obtain a Starlette application instance:
The http_app()
method is new in FastMCP 2.3.2. In older versions, use sse_app()
for SSE transport or streamable_http_app()
for Streamable HTTP transport.
The returned Starlette application can be integrated with other ASGI-compatible web frameworks. The MCP server’s endpoint is mounted at /mcp/
for Streamable HTTP transport and /sse/
for SSE transport.
Configuration Options
You can customize the endpoint path and access the FastMCP server instance:
Adding Custom Routes
You can add custom web routes directly to your FastMCP server using the @custom_route
decorator:
Health Check Endpoints
Health checks are commonly needed for monitoring and load balancing:
The health endpoint will be available at /health
alongside your MCP endpoint at /mcp/
.
Starlette Integration
Mount your FastMCP server in another Starlette application:
The MCP endpoint will be available at /mcp-server/mcp/
of the resulting Starlette app.
For Streamable HTTP transport, you must pass the lifespan context from the FastMCP app to the resulting Starlette app, as nested lifespans are not recognized. Otherwise, the FastMCP server’s session manager will not be properly initialized.
Nested Mounts
You can create complex routing structures by nesting mounts:
In this setup, the MCP server is accessible at the /outer/inner/mcp/
path.
Custom Middleware
New in version: 2.3.2
Add custom Starlette middleware to your FastMCP ASGI apps by passing a list of middleware instances:
Running the Server
To run your ASGI application, use an ASGI server like uvicorn
:
Or from the command line:
Framework-Specific Integration
FastAPI
For FastAPI-specific integration patterns including both mounting MCP servers into FastAPI apps and generating MCP servers from FastAPI apps, see the FastAPI Integration guide.
Other ASGI Frameworks
The patterns shown here work with any ASGI-compatible framework. The key requirements are:
- Mount the FastMCP ASGI app at your desired path
- Pass the lifespan context to your root application
- Configure any necessary middleware or authentication