Convert Your FastAPI Backend to an MCP Server in 5 Minutes
Today, we're exploring fastapi-mcp, a powerful package that seamlessly transforms a standard FastAPI backend into a fully functional MCP (Multi-Content Prompt) server. This allows AI agents to interact with your existing API endpoints as if they were native tools.
From Endpoints to AI Tools: The Core Concept
In traditional web development, a frontend application, perhaps built with React.js, communicates with a FastAPI backend through defined API endpoints like create-todo, get-todo, and delete-todo.
However, in the new era of agentic AI, the interaction model is shifting. Instead of a user clicking buttons on a UI, an AI agent interacts with a backend through a chat interface. This requires the backend to expose its functionalities not as simple endpoints, but as "tools" that the agent can understand and utilize. This is where fastapi-mcp comes in, converting your endpoints into create-tool, get-todo-tool, and delete-tool with minimal effort.
This allows an MCP client, such as Cursor, Windmill, or a custom client built with a library like mcp-use, to call your server's functions directly.
Effortless Conversion in Just a Few Steps
The beauty of fastapi-mcp lies in its simplicity. You can convert an entire FastAPI server by adding just a few lines of code.
Let's walk through an example using a Pokémon-themed FastAPI server with several endpoints: - Compare two Pokémon. - Get details about a specific Pokémon. - View a trainer's Pokémon team. - Add a Pokémon to the team. - Remove a Pokémon from the team.
Here’s how to convert it:
Step 1: Install the Package First, add the package to your project.
uv add fastapi-mcp
Step 2: Modify Your FastAPI App
In your main application file (e.g., main.py), import the library and initialize it.
# main.py
from fastapi import FastAPI
from fastapi_mcp import MCP # 1. Import the library
app = FastAPI()
# ... your existing FastAPI endpoints and logic ...
# Example:
# @app.get("/pokemon/{name}")
# def get_pokemon_details(name: str):
# # ... logic to fetch pokémon data
# return {"name": name, "type": "Electric"}
# 2. Initialize MCP after the app instance
mcp = MCP(app)
# 3. Set up the MCP server at the end of the file
mcp.setup_server()
That's it! Your FastAPI server is now also an MCP server.
Step 3: Configure Your MCP Client
The final step is to inform your MCP client about the new server. For a client like mcp-use, you would update its configuration JSON file to include the local server's URL.
{
"mcp_servers": {
"pokemon_api": "http://127.0.0.1:8000/mcp"
}
}
A Practical Demonstration
With the server running and the client configured, you can start interacting with your API through a natural language chat interface.
Let's start the FastAPI server:
bash
uvicorn main:app --reload
Now, running the MCP client allows for the following interactions:
Querying Pokémon Details:
User: Tell me about Pikachu
The client calls the appropriate tool on the MCP server, which corresponds to the original /pokemon/{name} endpoint.
Agent: ID: 25, Name: Pikachu, Type: Electric. Pikachu is an Electric-type Pokémon known for its lightning-fast speed and powerful electric attacks.
Comparing Two Pokémon:
User: compare Charizard with Mewtwo
The agent uses the compare tool to fetch stats for both Pokémon and provide a summary.
Agent: Charizard Stats: HP: 78, Attack: 84, ... Mewtwo Stats: HP: 106, Attack: 110, ... Summary: Mewtwo has superior stats. As a legendary Pokémon, it is significantly more powerful than Charizard in almost every category.
Managing a Pokémon Team:
User: add Mewtwo to my team with ID 1
The agent uses the add_to_team tool.
Agent: Success! You currently have one Pokémon in your team: Mewtwo.
User: I also want to add Pikachu and Bulbasaur in my team
The agent processes the request, making multiple tool calls to add both Pokémon.
Agent: Your team has been updated. You now have Mewtwo, Pikachu, and Bulbasaur in your team.
As you can see, the FastAPI backend behaves like a native MCP server, with its endpoints acting as callable tools for the AI agent. This demonstrates how easily you can bridge existing infrastructure with the world of agentic AI.