- 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
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. Instead, you need to create a new lifespan function that manages both contexts. This ensures that both your appās initialization logic and the MCP serverās session manager run properly:async with statements - the inner context (MCP) will be initialized after the outer context (your app), and cleaned up before it. This maintains the correct initialization and cleanup order for all your resources.
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

