- Generate an MCP server FROM your FastAPI app - Convert existing API endpoints into MCP tools
- Mount an MCP server INTO your FastAPI app - Add MCP functionality to your web application
When generating an MCP server from FastAPI, FastMCP uses OpenAPIProvider (v3.0.0+) under the hood to source tools from your FastAPI app’s OpenAPI spec. See Providers to understand how FastMCP sources components.
FastMCP does not include FastAPI as a dependency; you must install it separately to use this integration.
Example FastAPI Application
Throughout this guide, we’ll use this e-commerce API as our example (click theCopy button to copy it for use with other code blocks):
Generating an MCP Server
One of the most common ways to bootstrap an MCP server is to generate it from an existing FastAPI application. FastMCP will expose your FastAPI endpoints as MCP components (tools, by default) in order to expose your API to LLM clients.Basic Conversion
Convert the FastAPI app to an MCP server with a single line:Adding Components
Your converted MCP server is a full FastMCP instance, meaning you can add new tools, resources, and other components to it just like you would with any other FastMCP instance.Interacting with the MCP Server
Once you’ve converted your FastAPI app to an MCP server, you can interact with it using the FastMCP client to test functionality before deploying it to an LLM-based application.Custom Route Mapping
Because FastMCP’s FastAPI integration is based on its OpenAPI integration, you can customize how endpoints are converted to MCP components in exactly the same way. For example, here we use aRouteMap to map all GET requests to MCP resources, and all POST/PUT/DELETE requests to MCP tools:
Authentication and Headers
You can configure headers and other client options via thehttpx_client_kwargs parameter. For example, to add authentication to your FastAPI app, you can pass a headers dictionary to the httpx_client_kwargs parameter:
Mounting an MCP Server
In addition to generating servers, FastMCP can facilitate adding MCP servers to your existing FastAPI application. You can do this by mounting the MCP ASGI application.Basic Mounting
To mount an MCP server, you can use thehttp_app method on your FastMCP instance. This will return an ASGI application that can be mounted to your FastAPI application.
Offering an LLM-Friendly API
A common pattern is to generate an MCP server from your FastAPI app and serve both interfaces from the same application. This provides an LLM-optimized interface alongside your regular API:Key Considerations
Operation IDs
FastAPI operation IDs become MCP component names. Always specify meaningful operation IDs:Lifespan Management
When mounting MCP servers, always pass the lifespan context:CORS Middleware
If your FastAPI app usesCORSMiddleware and you’re mounting an OAuth-protected FastMCP server, avoid adding application-wide CORS middleware. FastMCP and the MCP SDK already handle CORS for OAuth routes, and layering CORS middleware can cause conflicts (such as 404 errors on .well-known routes or OPTIONS requests).
If you need CORS on your own FastAPI routes, use the sub-app pattern: mount your API and FastMCP as separate apps, each with their own middleware, rather than adding top-level CORSMiddleware to the combined application.
Combining Lifespans
If your FastAPI app already has a lifespan (for database connections, startup tasks, etc.), you can’t simply replace it with the MCP lifespan. Usecombine_lifespans to run both:
combine_lifespans enters lifespans in order and exits in reverse order.
Performance Tips
- Use in-memory transport for testing - Pass MCP servers directly to clients
- Design purpose-built MCP tools - Better than auto-converting complex APIs
- Keep tool parameters simple - LLMs perform better with focused interfaces

