- 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
run()
method that can be used to start the server. This guide focuses on integration with broader ASGI frameworks.
ASGI Server
FlashMCP 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 FlashMCP server using thehttp_app()
method:
The
http_app()
method is new in FlashMCP 2.3.2. In older versions, use sse_app()
for SSE transport or streamable_http_app()
for Streamable HTTP transport.FlashMCP
instance on app.state.FlashMCP_server
, so you
can access it from custom middleware or routes via request.app.state.FlashMCP_server
.
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 FlashMCP server, you can use theuvicorn
ASGI server:
Custom Middleware
New in version: 2.3.2
You can add custom Starlette middleware to your FlashMCP 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 FlashMCP server in another Starlette application:
/mcp-server/mcp
of the resulting Starlette app.
For Streamable HTTP transport, you must pass the lifespan context from the FlashMCP app to the resulting Starlette app, as nested lifespans are not recognized. Otherwise, the FlashMCP server’s session manager will not be properly initialized.
Nested Mounts
You can create complex routing structures by nesting mounts:/outer/inner/mcp
path of the resulting Starlette app.
For Streamable HTTP transport, you must pass the lifespan context from the FlashMCP app to the outer Starlette app, as nested lifespans are not recognized. Otherwise, the FlashMCP 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 FlashMCP server in a similar way:
/mcp-server/mcp
of the resulting FastAPI app.
For Streamable HTTP transport, you must pass the lifespan context from the FlashMCP app to the resulting FastAPI app, as nested lifespans are not recognized. Otherwise, the FlashMCP server’s session manager will not be properly initialized.
Custom Routes
In addition to adding your FlashMCP server to an existing ASGI app, you can also add custom web routes to your FlashMCP 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.