FastAPI 🤝 FastMCP
Integrate FastMCP with FastAPI applications
FastMCP provides two powerful ways to integrate with FastAPI applications, both of which are documented below.
- You can generate an MCP server FROM your FastAPI app by converting existing API endpoints into MCP tools. This is useful for bootstrapping and quickly attaching LLMs to your API.
- You can mount an MCP server INTO your FastAPI app by adding MCP functionality to your web application. This is useful for exposing your MCP tools alongside regular API endpoints.
You can even combine both approaches to create a single FastAPI app that serves both regular API endpoints and MCP tools!
Generating MCP servers from FastAPI apps 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 FastAPI servers. This is especially true for complex APIs with many endpoints and parameters.
FastMCP does not include FastAPI as a dependency; you must install it separately to use this integration.
Generating an MCP Server
New in version: 2.0.0
FastMCP can directly convert your existing FastAPI applications into MCP servers, allowing AI models to interact with your API endpoints through the MCP protocol.
Under the hood, the FastAPI integration is built on top of FastMCP’s OpenAPI integration. See the OpenAPI docs for more details.
Create a Server
The simplest way to convert a FastAPI app is using the FastMCP.from_fastapi()
method:
Component Mapping
By default, FastMCP converts every endpoint in your FastAPI app into an MCP Tool. This provides maximum compatibility with LLM clients that primarily support MCP tools.
You can customize this behavior using route maps to control which endpoints become tools, resources, or resource templates:
The FastMCP.from_fastapi()
method accepts all the same configuration options as FastMCP.from_openapi()
, including route maps, custom tags, component naming, timeouts, and component customization functions. For comprehensive configuration details, see the OpenAPI Integration guide.
Key Considerations
Operation IDs
FastMCP uses your FastAPI operation IDs to name MCP components. Ensure your endpoints have meaningful operation IDs:
Pydantic Models
Your Pydantic models are automatically converted to JSON schema for MCP tool parameters:
The MCP tool will have properly typed parameters matching your Pydantic model.
Error Handling
FastAPI error handling carries over to the MCP server. HTTPExceptions are automatically converted to appropriate MCP errors.
Since FastAPI integration is built on OpenAPI, all the same configuration options are available including authentication setup, timeout configuration, and request parameter handling. For detailed information on these features, see the OpenAPI Integration guide.
Mounting an MCP Server
New in version: 2.3.1
You can also mount an existing FastMCP server into your FastAPI application, adding MCP functionality to your web application. This is useful for exposing your MCP tools alongside regular API endpoints.
Basic Integration
The MCP endpoint will be available at /mcp-server/mcp/
of your FastAPI application.
For Streamable HTTP transport, you must pass the lifespan context from the FastMCP app to the FastAPI app. Otherwise, the FastMCP server’s session manager will not be properly initialized.
Advanced Integration
You can combine both approaches - generate an MCP server from your FastAPI app AND mount additional MCP servers:
Now you have:
- API endpoints converted to MCP tools (via
api_mcp
) - Additional MCP tools available at
/tools/mcp/
- Regular FastAPI endpoints at their original paths
Authentication and Middleware
When mounting MCP servers into FastAPI, you can leverage FastAPI’s authentication and middleware:
For more advanced ASGI integration patterns, see the ASGI Integration guide.