FastAPI with MCP
About
A FastAPI application demonstrating MCP integration for mathematical operations and tool registration.
Details
- Author
- hannguyendd
- Categories
- Developer Tools
Jump to
Setup
Install FastAPI with MCP in your MCP client (Claude Desktop, Cursor, Windsurf, and others).
Repository: https://github.com/hannguyendd/fastapi-with-mcp
Follow the installation instructions in the repository README, then restart your MCP client.
A FastAPI application demonstrating MCP integration for mathematical operations and tool registration.
FastAPI with Model Context Protocol (MCP)
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 web framework integration with MCP
- Multiple mathematical operations (add, multiply, subtract)
- Different tool registration patterns (decorators vs functions)
- Server-Sent Events (SSE) support for real-time communication
- Example configurations for MCP client integration
- Pandas integration for data manipulation demonstrations
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
- Python 3.12+
- FastAPI[standard]
- FastMCP
- Pandas
- Pydantic
- MCP
git clone <repository-url> cd fastapi-with-mcp
- Create a virtual environment (using uv or standard Python):
# 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 .
uvicorn fastapi_mcp:app --reload --port 8000
- FastAPI docs:http://localhost:8000/docs
- MCP SSE endpoint:http://localhost:8000/mcp-server/sse
- MCP server mount:http://localhost:8000/mcp-server
The application provides several mathematical tools that demonstrate Pandas integration:
- Add- Add two numbers using pandas DataFrame operations
- Multiply- Multiply two numbers using pandas DataFrame operations
- Subtract- Subtract two numbers using pandas DataFrame operations
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)
Theconfig.jsonfile contains example configuration for MCP clients:
{ "mcpServers": { "math-tools": { "type": "http", "url": "http://localhost:8000/mcp-server/sse", "env": {} } } }
- Uses"math-tools"as the server identifier (reflecting the mathematical operations provided)
- Sets type to"http"for HTTP-based communication
- Points to the mounted MCP server endpoint at"/mcp-server/sse"
- GET /add?a={int}&b={int}- Add two numbers
- GET /multiply?a={int}&b={int}- Multiply two numbers
- GET /docs- Interactive API documentation
- GET /redoc- ReDoc API documentation
- GET /mcp-server/sse- Server-Sent Events endpoint for MCP communication
- MCP tools are accessible through the MCP protocol via the mounted server at/mcp-server
Run the tool registration test to see different registration methods:
For testing tool registration methods, seetest_tool_registration.py:
- FastAPI[standard]: Modern, fast web framework for building APIs with standard extras
- FastMCP: FastAPI integration for Model Context Protocol
- Pandas: Data manipulation library (used for mathematical operations)
- Pydantic: Data validation library
- MCP: Model Context Protocol implementation
This project usesdotenvto load environment variables. Create a.envfile for environment-specific configurations:
# Add any environment variables here if needed # Example: # DEBUG=true # LOG_LEVEL=info
- Fork the repository
- Create a feature branch (git checkout -b feature/amazing-feature)
- Commit your changes (git commit -m 'Add some amazing feature')
- Push to the branch (git push origin feature/amazing-feature)
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- FastAPI Documentation
- Model Context Protocol
- FastMCP Documentation
- Module name conflicts: Avoid naming filesmcp.pyas it conflicts with the MCP package
- Port conflicts: Ensure port 8000 is available or change the port in uvicorn command
- Python version: This project requires Python 3.12+
- Dependencies: Make sure all dependencies are installed with the correct versions
- Check the FastAPI docs at/docsendpoint
- Verify all dependencies are installed correctly
- Ensure Python 3.12+ is being used
- Check server logs for detailed error messages
- Verify that the MCP SSE endpoint is accessible at/mcp-server/sse
This is a web browser that enables your coding agent, such as Claude Code, to visit websites on your behalf and assist you in identifying bugs or creating UI test cases.
Create crafted UI components inspired by the best 21st.dev design engineers.
Bring agent evaluations, observability, and synthetic test set generation directly into your IDE for free with Galileo's new MCP server
An MCP server to help AI assistants to answer questions and generate AccelByte Extend SDK code more effectively .
MCP server for AI Diagram Maker — generate beautiful software engineering diagrams directly inside Cursor, Claude Desktop, Claude Code, or any MCP-compatible AI agent
ALAPI MCP Tools,Call hundreds of API interfaces via MCP
AI-powered SVG animation generator that transforms static files into animated SVG components using the Allyson platform
MCP server that gives AI assistants on-demand access to 1,500+ amCharts docs, ~300 code examples, and 1000+ class API references.
APIMatic MCP Server is used to validate OpenAPI specifications using APIMatic. The server processes OpenAPI files and returns validation summaries by leveraging APIMatic’s API.
One shared context layer for AI agents and humans — live API specs, DB schemas, and versioned contracts across repos so every agent and teammate works from the same source of truth.
Build and deploy full-stack Next.js apps with 98 tools for React, AWS, and MongoDB
Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.





