What is FastAPI-MCP?
FastAPI-MCP is a native extension for the FastAPI framework that exposes your API endpoints as MCP tools, which can be dynamically discovered and invoked by MCP-compatible LLM agents. Unlike traditional converters, FastAPI-MCP deeply integrates with FastAPI's ASGI stack, supports authentication and documentation preservation out of the box, and enables both unified and separate deployment models for greater flexibility.
How to Configure FastAPI-MCP
- Install FastAPI-MCP using your preferred package manager:
- With uv:
uv add fastapi-mcp
- With pip:
pip install fastapi-mcp
- With uv:
- Import FastAPI-MCP and initialize it with your FastAPI application instance.
- Use the
.mount()
method to expose your MCP server, which will auto-generate MCP tool endpoints based on your existing FastAPI routes. - Optionally, configure authentication using FastAPI's dependency injection (e.g.,
Depends()
), and customize documentation or deployment according to your requirements.
How to Use FastAPI-MCP
- Add the following to your FastAPI application:
from fastapi import FastAPI from fastapi_mcp import FastApiMCP app = FastAPI() mcp = FastApiMCP(app) mcp.mount()
- Once mounted, your MCP server will be available at
/mcp
under your app's base URL. - All compatible FastAPI endpoints will automatically become MCP tools, ready for discovery and invocation by MCP clients, including LLM agents.
- Endpoints retain their original schema, validation, and authentication logic, and any changes to your FastAPI application will be reflected in your MCP tools.
- Explore documentation at fastapi-mcp.tadata.com and review the examples for practical demonstrations.
Key Features
- Authentication integration: Reuse FastAPI's existing dependency and security model for MCP endpoints.
- Minimal configuration: Instantly expose all your FastAPI endpoints as MCP tools with one method call.
- Native FastAPI support: Leverages ASGI transport for efficient and direct communication.
- Schema and documentation preservation: Maintains OpenAPI docs, models, and field validation.
- Flexible deployment: Deploy the MCP server within your app or as a standalone service.
- Comprehensive ecosystem: Integrate LLMs and external MCP-compatible tools easily.
Use Cases
- Instantly make your business's REST APIs available as secure, LLM-accessible tools for smart agent workflows.
- Build custom workflow automation by allowing LLMs to call your FastAPI endpoints as MCP tools.
- Prototype and share new AI-powered features without manual endpoint wrapping or duplication.
- Connect enterprise data and external APIs into a unified, model-accessible toolset.
FAQ
Q1: Do I need to change my FastAPI code to use FastAPI-MCP?
A: No, you can expose your existing endpoints as MCP tools with no or minimal changes by simply initializing and mounting FastAPI-MCP.
Q2: Can I use FastAPI's authentication and dependencies with MCP tools?
A: Yes, FastAPI-MCP fully supports authentication and dependency injection via FastAPI's Depends()
, so your security model remains intact.
Q3: Is it possible to deploy the MCP server separately from my FastAPI application?
A: Yes, FastAPI-MCP supports both unified and separate deployment modes, allowing you to mount the MCP server within your app or as a standalone ASGI service.
Q4: How are request and response schemas handled?
A: Schemas and documentation for all endpoints are preserved, ensuring consistent input validation and discoverability through MCP tools.