Skip to main content
FastMCP provides two powerful ways to integrate with FastAPI applications:
  1. Generate an MCP server FROM your FastAPI app - Convert existing API endpoints into MCP tools
  2. Mount an MCP server INTO your FastAPI app - Add MCP functionality to your web application
Generating MCP servers from OpenAPI is a great way to get started with FastMCP, but in practice LLMs achieve significantly better performance with well-designed and curated MCP servers than with auto-converted OpenAPI servers. This is especially true for complex APIs with many endpoints and parameters.We recommend using the FastAPI integration for bootstrapping and prototyping, not for mirroring your API to LLM clients. See the post Stop Converting Your REST APIs to MCP for more details.
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 the Copy button to copy it for use with other code blocks):
All subsequent code examples in this guide assume you have the above FastAPI application code already defined. Each example builds upon this base application, app.

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 a RouteMap to map all GET requests to MCP resources, and all POST/PUT/DELETE requests to MCP tools:
To learn more about customizing the conversion process, see the OpenAPI Integration guide.

Authentication and Headers

You can configure headers and other client options via the httpx_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 the http_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:
This approach lets you maintain a single codebase while offering both traditional REST endpoints and MCP-compatible endpoints for LLM clients.

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:
If you’re mounting an authenticated MCP server under a path prefix, see Mounting Authenticated Servers for important OAuth routing considerations.

CORS Middleware

If your FastAPI app uses CORSMiddleware 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:
This pattern ensures both your app’s initialization logic and the MCP server’s session manager are properly managed. The key is using nested 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

  1. Use in-memory transport for testing - Pass MCP servers directly to clients
  2. Design purpose-built MCP tools - Better than auto-converting complex APIs
  3. Keep tool parameters simple - LLMs perform better with focused interfaces
For more details on configuration options, see the OpenAPI Integration guide.