Welcome to FastAPI-MCP

Expose your FastAPI endpoints as Model Context Protocol (MCP) tools, with Auth!

fastapi-to-mcp

Built by Tadata

FastAPI-MCP is a library that seamlessly integrates your FastAPI application with the Model Context Protocol (MCP), the emerging standard for how AI agents communicate with applications. It allows you to expose your existing API endpoints as tools for Large Language Models (LLMs) with minimal configuration.

Creating a secured MCP server for your application takes only a few lines of code:

from fastapi import FastAPI
from fastapi_mcp import FastApiMCP

# Create or import your FastAPI app
app = FastAPI()

mcp = FastApiMCP(app)

# Mount the MCP server using the recommended HTTP transport
mcp.mount_http()

That's it! Your auto-generated MCP server is now available at https://app.base.url/mcp.

Key Features

  • Built-in Authentication: Secure your MCP tools using your existing FastAPI dependencies and full OAuth 2.0 support. See the Authentication Guide for details.
  • FastAPI-Native: Designed as a native extension of FastAPI, not just an OpenAPI converter. It uses FastAPI's internal mechanisms for maximum compatibility and performance.
  • Zero/Minimal Configuration: Works out of the box by pointing it at your FastAPI app.
  • Schema Preservation: Automatically preserves and translates your Pydantic request and response models into type-safe MCP tool schemas.
  • Documentation Preservation: Endpoint descriptions and docstrings from your FastAPI app are used to generate rich, informative tool descriptions for the LLM.
  • Flexible Deployment: Mount the MCP server on the same FastAPI application or deploy it separately.
  • ASGI Transport: Communicates directly with your FastAPI app's ASGI interface, eliminating the overhead of internal HTTP calls.

The FastAPI-First Approach

FastAPI-MCP is more than just a converter; it's a native integration that respects and leverages the FastAPI ecosystem. This philosophy provides several key advantages:

  • Native Dependencies: Secure your MCP endpoints using the familiar Depends() system from FastAPI for authentication and authorization. This means you can reuse your existing security logic without modification.
  • ASGI Transport: By default, FastApiMCP communicates with your application's logic directly through the ASGI interface. This is highly efficient as it bypasses the network stack for internal calls, unlike traditional approaches that require the MCP server to make HTTP requests back to the API.
  • Unified Infrastructure: You don't need to run a separate process or container for your MCP server. It can be mounted directly onto your main FastAPI app, simplifying your deployment architecture. However, deploying separately is still fully supported for more complex setups.