Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@FastAPI MCP Math and Tool Registration Demomultiply 15 by 24"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
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.
Features
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
Project Structure
Requirements
Python 3.12+
FastAPI[standard]
FastMCP
Pandas
Pydantic
MCP
Installation
Clone the repository:
Create a virtual environment (using uv or standard Python):
Install dependencies:
Usage
Running the FastAPI + MCP Server
Start the main application:
The server will be available at:
FastAPI docs:
http://localhost:8000/docsMCP SSE endpoint:
http://localhost:8000/mcp-server/sseMCP server mount:
http://localhost:8000/mcp-server
Available Tools
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.
Tool Registration Methods
This project demonstrates multiple ways to register MCP tools:
Method 1: Decorator Approach
Method 2: Function Registration
Method 3: Decorator as Function
Method 4: Combined FastAPI + MCP
MCP Client Configuration
The config.json file contains example configuration for MCP clients:
This configuration:
Uses
"math-tools"as the server identifier (reflecting the mathematical operations provided)Sets type to
"http"for HTTP-based communicationPoints to the mounted MCP server endpoint at
"/mcp-server/sse"
API Endpoints
FastAPI Endpoints
GET /add?a={int}&b={int}- Add two numbersGET /multiply?a={int}&b={int}- Multiply two numbersGET /docs- Interactive API documentationGET /redoc- ReDoc API documentation
MCP Endpoints
GET /mcp-server/sse- Server-Sent Events endpoint for MCP communicationMCP tools are accessible through the MCP protocol via the mounted server at
/mcp-server
Development
Testing Tool Registration
Run the tool registration test to see different registration methods:
Simple MCP Server Example
For testing tool registration methods, see test_tool_registration.py:
Dependencies
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
Environment Variables
This project uses dotenv to load environment variables. Create a .env file for environment-specific configurations:
Contributing
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
License
This project is licensed under the MIT License - see the LICENSE file for details.
Learn More
Troubleshooting
Common Issues
Module name conflicts: Avoid naming files
mcp.pyas it conflicts with the MCP packagePort 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
Getting Help
If you encounter issues:
Check the FastAPI docs at
/docsendpointVerify 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