Integrating FastMCP in ASGI Applications
Integrate FastMCP servers into existing Starlette, FastAPI, or other ASGI applications
While FastMCP provides standalone server capabilities, you can also integrate your FastMCP server into existing web applications. This approach 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
Please note that all FastMCP servers have a run()
method that can be used to start the server. This guide focuses on integration with broader ASGI frameworks.
ASGI Server
FastMCP servers can be created as Starlette ASGI apps for straightforward hosting or integration into existing applications.
The first step is to obtain a Starlette application instance from your FastMCP server using the http_app()
method:
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.
Both approaches return a Starlette application that can be integrated with other ASGI-compatible web frameworks.
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:
Running the Server
To run the FastMCP server, you can use the uvicorn
ASGI server:
Or, from the command line:
Custom Middleware
New in 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:
Starlette Integration
New in version: 2.3.1
You can 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 of the resulting Starlette app.
For Streamable HTTP transport, you must pass the lifespan context from the FastMCP app to the outer Starlette app, as nested lifespans are not recognized. Otherwise, the FastMCP server’s session manager will not be properly initialized.
FastAPI Integration
New in version: 2.3.1
FastAPI is built on Starlette, so you can mount your FastMCP server in a similar way:
The MCP endpoint will be available at /mcp-server/mcp
of the resulting FastAPI app.
For Streamable HTTP transport, you must pass the lifespan context from the FastMCP app to the resulting FastAPI app, as nested lifespans are not recognized. Otherwise, the FastMCP server’s session manager will not be properly initialized.
Custom Routes
In addition to adding your FastMCP server to an existing ASGI app, you can also add custom web routes to your FastMCP server, which will be exposed alongside the MCP endpoint. To do so, use the @custom_route
decorator. Note that this is less flexible than using a full ASGI framework, but can be useful for adding simple endpoints like health checks to your standalone server.
These routes will be included in the FastMCP app when mounted in your web application.