A FastAPI application demonstrating MCP integration for mathematical operations and tool registration.
A FastAPI application integrated with Model Context Protocol (MCP) for mathematical operations and tool registration examples. This project demonstrates how to build MCP servers using FastAPI and shows different approaches to registering tools.
fastapi-with-mcp/
├── fastapi_mcp.py # Main FastAPI + MCP application
├── test_tool_registration.py # Tool registration testing examples
├── config.json # MCP client configuration
├── pyproject.toml # Python project configuration
├── .python-version # Python version specification
├── .gitignore # Git ignore rules
├── uv.lock # UV lock file for dependencies
└── README.md # This file
git clone <repository-url>
cd fastapi-with-mcp
# Using uv (recommended)
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Or using standard Python
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Using uv
uv pip install -e .
# Or using pip
pip install -e .
Start the main application:
uvicorn fastapi_mcp:app --reload --port 8000
The server will be available at:
http://localhost:8000/docs
http://localhost:8000/mcp-server/sse
http://localhost:8000/mcp-server
The application provides several mathematical tools that demonstrate Pandas integration:
Each operation creates a pandas DataFrame to perform the calculation, demonstrating how to integrate data manipulation libraries with MCP tools.
This project demonstrates multiple ways to register MCP tools:
@mcp.tool
@app.get("/add", operation_id="add_two_numbers")
async def add(a: int, b: int):
"""Add two numbers and return the sum."""
summ = pd.DataFrame({"a": [a], "b": [b], "sum": [a + b]})
result = int(summ.loc[0, "sum"])
return {"sum": result}
def multiply(a: int, b: int):
"""Multiply two numbers and return the product."""
product = pd.DataFrame({"a": [a], "b": [b], "product": [a * b]})
result = int(product.loc[0, "product"])
return {"product": result}
# Register the function as a tool
mcp.tool(multiply)
def subtract(a: int, b: int):
"""Subtract two numbers and return the difference."""
diff = pd.DataFrame({"a": [a], "b": [b], "difference": [a - b]})
result = int(diff.loc[0, "difference"])
return {"difference": result}
# Register using the decorator syntax as a function
mcp.tool()(subtract)
@app.get("/multiply", operation_id="multiply_two_numbers")
async def multiply_endpoint(a: int, b: int):
"""FastAPI endpoint that also works as MCP tool."""
return multiply(a, b)
# Register the same function as an MCP tool
mcp.tool(multiply_endpoint)
The config.json
file contains example configuration for MCP clients:
{
"mcpServers": {
"math-tools": {
"type": "http",
"url": "http://localhost:8000/mcp-server/sse",
"env": {}
}
}
}
This configuration:
"math-tools"
as the server identifier (reflecting the mathematical operations provided)"http"
for HTTP-based communication"/mcp-server/sse"
GET /add?a={int}&b={int}
- Add two numbersGET /multiply?a={int}&b={int}
- Multiply two numbersGET /docs
- Interactive API documentationGET /redoc
- ReDoc API documentationGET /mcp-server/sse
- Server-Sent Events endpoint for MCP communication/mcp-server
Run the tool registration test to see different registration methods:
python test_tool_registration.py
For testing tool registration methods, see test_tool_registration.py
:
python test_tool_registration.py
This project uses dotenv
to load environment variables. Create a .env
file for environment-specific configurations:
# Add any environment variables here if needed
# Example:
# DEBUG=true
# LOG_LEVEL=info
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the MIT License - see the LICENSE file for details.
mcp.py
as it conflicts with the MCP packageIf you encounter issues:
/docs
endpoint/mcp-server/sse
A platform-agnostic server for scalable mobile automation and development across iOS, Android, simulators, and emulators.
Retrieves essential network information from devices using gNMI and OpenConfig models.
Make your AI agent speak every language on the planet, using Lingo.dev Localization Engine.
An autonomous memory management system for Claude AI, featuring multi-provider LLM integration and a persistent memory database.
An AI agent using the Model Context Protocol (MCP) with a Node.js server providing REST resources for users and messages.
An interactive sandbox to safely execute Python code and install packages in isolated Docker containers.
Query A/B test data using the Hackle API.
Provides a sleep/wait tool to add delays between operations, such as waiting between API calls or testing eventually consistent systems.
Beautiful HTML and PNG diff visualization using diff2html, designed for filesystem edit_file dry-run output with high-performance Bun runtime.
iOS Swift Package Manager server written in Swift