One line of code turns a FastAPI application into an MCP server in seconds!

Written by
Iris Vance
Updated on:July-09th-2025
Recommendation

Simplify FastAPI applications, transform them into intelligent API servers with one click, and seamlessly connect with AI agents.

Core content:
1. Introduction to FastAPI-MCP tools: zero-configuration integration with MCP
2. Installation and quick start: one line of code to achieve MCP integration
3. Advanced usage: custom API configuration to enhance interaction capabilities

Yang Fangxian
Founder of 53AI/Most Valuable Expert of Tencent Cloud (TVP)

 

FastAPI-MCP is a zero-configuration tool that allows FastAPI applications to automatically expose all endpoints and be compatible with  the Model Context Protocol (MCP) .

This means that your FastAPI application can not only be called by traditional clients, but also seamlessly connect to AI agent tools such as Claude Desktop and Cursor  to achieve smarter API interaction. 


? What is FastAPI-MCP?

FastAPI-MCP is a zero-configuration tool that makes FastAPI applications automatically compatible with the Model Context Protocol (MCP) , thereby:

✅One  -line code integration  ——Mount the MCP server directly on FastAPI without changing the existing code

✅Automatically  discover APIs  — no additional configuration required, all endpoints are automatically converted to MCP tools

✅  Keep API documentation  - Your Swagger documentation is directly accessible in the MCP ecosystem

✅Support  custom extensions  - Allows additional MCP tools to be defined and enhances API capabilities

Your FastAPI application can no longer only be called by traditional clients, but can also be transformed into an intelligent API server for efficient interaction with AI agent tools !


Install FastAPI-MCP

You can   install it via pip  or  uv :

# Use uv (recommended)
uv add fastapi-mcp

# or use pip
pip install fastapi-mcp

Get started quickly

Assuming you already have a  FastAPI  application, now**Just one line of code**, you can make it support  MCP :

from  fastapi  import  FastAPI
from  fastapi_mcp  import  add_mcp_server

# Create a FastAPI application
app = FastAPI()

# Mount the MCP server
add_mcp_server(
    app,                   # FastAPI application
    mount_path = "/mcp" ,     # MCP server path
    name = "My API MCP" ,     # MCP server name
)

In this way, your MCP server can https://your-app.com/mcp Visited! ?


Advanced Usage

FastAPI-MCP also supports custom API configuration , such as:

Enable full JSON Schema to adapt to different LLM tools

Show all possible response modes to make the API more intuitive

Manually expand MCP tools to enhance interaction capabilities

Sample code:

from  fastapi  import  FastAPI
from  fastapi_mcp  import  add_mcp_server

app = FastAPI()

# Create an MCP server and enable advanced configuration
mcp_server = add_mcp_server(
    app,
    mount_path= "/mcp" ,
    name= "My API MCP" ,
    describe_all_responses = True ,    # Display all possible response modes
    describe_full_response_schema = True # Provide complete JSON Schema
)

#Add an additional MCP tool (get server time)
@mcp_server.tool()
async def get_server_time () ->  str :
    """Get the current server time"""
    from  datetime  import  datetime
    return  datetime.now().isoformat()

This way, your MCP server not only supports automatic API conversion, but can also be extended with custom AI tools!

? Connect to MCP server

FastAPI-MCP supports  SSE (Server-Sent Events)  and  mcp-proxy  , making it easy to connect to AI tools:

1️⃣ SSE method (applicable to Cursor)

  1. 1. Run the FastAPI application
  2. 2. In  Cursor  Settings, enter the MCP server URL (such as http://localhost:8000/mcp
  3. 3. Cursor will automatically discover all API endpoints

2️⃣ mcp-proxy method (for Claude Desktop)

If the MCP client does not support SSE (such as Claude Desktop), you can use mcp-proxy acting:

Install mcp-proxy

uv tool install mcp-proxy

Configuring Claude Desktop

 Add MCP proxy configuration in claude_desktop_config.json  :

Windows

{
  "mcpServers" : { 
    "my-api-mcp-proxy" : { 
        "command" : "mcp-proxy" , 
        "args" : [ "http://127.0.0.1:8000/mcp" ] 
    }
  }
}

MacOS

{
  "mcpServers" : { 
    "my-api-mcp-proxy" : { 
        "command" : "/Full/Path/To/Your/Executable/mcp-proxy" , 
        "args" : [ "http://127.0.0.1:8000/mcp" ] 
    }
  }
}

turn up mcp-proxy Path:

which mcp-proxy

Claude Desktop will automatically discover and connect to all API endpoints! ?


Summarize

FastAPI-MCP allows your  FastAPI application to seamlessly integrate with the MCP ecosystem . With just one line of code , the API endpoint can be automatically converted into an MCP tool that can be used by the AI ​​agent tool , while also retaining the Swagger document .

Applies to :

 ✅ Developers who want FastAPI  to automatically expose APIs ✅   Scenarios that need to interact with AI agents (Claude / Cursor) ✅   Projects that want to connect to the MCP ecosystem with zero configuration