Exposes math operations as REST API endpoints for addition and subtraction with request/response validation
Math Operations API & MCP Server
A FastAPI application with MCP (Model Context Protocol) server integration for performing basic math operations. This project exposes addition and subtraction operations both as REST APIs and as MCP tools for AI applications.
Project Structure
Features
FastAPI REST API: Traditional REST endpoints for math operations
MCP Server: Expose operations as tools for AI applications (Claude Desktop, etc.)
Router-based Architecture: Clean, scalable code organization
Type Safety: Pydantic models for request/response validation
Installation
Prerequisites
Python 3.10 or higher
uv
package manager (recommended) orpip
Install Dependencies
Using uv
:
Using pip
:
Usage
1. Running the FastAPI Server
Start the REST API server:
The server will be available at:
API Base:
http://localhost:8000
Interactive Docs:
http://localhost:8000/docs
Add endpoint:
POST http://localhost:8000/api/add
Subtract endpoint:
POST http://localhost:8000/api/subtract
Example API Requests
Add two numbers:
Subtract two numbers:
2. Running the MCP Server
The MCP server allows AI applications to use the math operations as tools.
Testing MCP Server Locally
You can test the MCP server using the MCP Inspector tool:
Install MCP Inspector:
npm install -g @modelcontextprotocol/inspectorRun the inspector:
mcp-inspector uv run mcp_server.pyTest the tools in the web interface:
Open the URL provided by the inspector (usually
http://localhost:5173
)You'll see the available tools:
add
andsubtract
Click on a tool to test it with sample inputs
View the responses in real-time
Alternative Testing with stdio
You can also test the MCP server directly via stdio:
Then send JSON-RPC requests manually. Example:
Integration with AI Applications
For Claude Desktop:
Locate your Claude Desktop configuration file:
Windows:
%APPDATA%\Claude\claude_desktop_config.json
macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
Linux:
~/.config/Claude/claude_desktop_config.json
Add the MCP server configuration:
Important Configuration Notes:
cwd
: MUST be the absolute path to your project directoryWindows: Use double backslashes
\\
or forward slashes/
macOS/Linux: Use absolute path like
/home/user/projects/MCP
command
: The executable to run (uv
,python
, etc.)args
: Arguments passed to the commandenv
: Optional environment variables (empty object{}
for none)
Verify the configuration:
The
cwd
path must exist and containmcp_server.py
Ensure
uv
is installed and accessible from your PATHOn Windows, you can verify the path by running:
dir "d:\Projects\MCP\mcp_server.py"
On macOS/Linux, verify with:
ls -la /path/to/MCP/mcp_server.py
Restart Claude Desktop completely:
Close Claude Desktop entirely (check system tray/menu bar)
Reopen Claude Desktop
The math operations tools should now appear
Verify the connection:
In Claude Desktop, check the settings or tools panel
Look for "math-operations" server status (should show as connected)
If there's an error, check the logs (see Troubleshooting section)
For Other AI Applications:
Most MCP-compatible AI applications use similar configuration. Copy from mcp.json and adapt:
For Cline/VSCode: Add to your VSCode settings or Cline configuration:
For Continue.dev:
Add to ~/.continue/config.json
:
MCP Tools Documentation
Tool: add
Adds two numbers together.
Parameters:
a
(number, required): First number to addb
(number, required): Second number to add
Returns:
result
: The sum of a and boperation
: "addition"inputs
: The input values
Example:
Response:
Tool: subtract
Subtracts b from a.
Parameters:
a
(number, required): Number to subtract fromb
(number, required): Number to subtract
Returns:
result
: The difference (a - b)operation
: "subtraction"inputs
: The input values
Example:
Response:
Testing & Validation
1. Testing FastAPI Endpoints
Using Python requests:
Using the interactive docs:
Navigate to
http://localhost:8000/docs
Click on an endpoint to expand it
Click "Try it out"
Enter test values
Click "Execute"
2. Testing MCP Server
Method 1: Using MCP Inspector (Recommended)
Method 2: Manual stdio Testing
Method 3: Using Python MCP Client
3. Validating Claude Desktop Integration
After configuring Claude Desktop:
Check server status:
Open Claude Desktop settings
Look for MCP Servers section
Verify "math-operations" shows as "Connected" (green indicator)
Test the tools:
Start a new conversation in Claude Desktop
Ask: "Can you add 15 and 25 for me?"
Claude should use the
add
tool from your MCP serverCheck the response includes tool usage indicator
View logs:
Windows:
%APPDATA%\Claude\logs\mcp-*.log
macOS:
~/Library/Logs/Claude/mcp-*.log
Linux:
~/.config/Claude/logs/mcp-*.log
Development
Adding New Operations
To add new math operations (e.g., multiply, divide):
Create API endpoint in
api/multiply.py
:
Add router to main.py:
Add MCP tool in mcp_server.py:
Update list_tools()
:
Update call_tool()
:
Project Dependencies
fastapi: Modern web framework for building APIs
uvicorn: ASGI server for running FastAPI
pydantic: Data validation using Python type annotations
mcp: Model Context Protocol SDK for AI integrations
Troubleshooting
MCP Server Issues
Problem: Server not connecting in Claude Desktop
Verify configuration path:
# Windows type "%APPDATA%\Claude\claude_desktop_config.json" # macOS/Linux cat ~/Library/Application\ Support/Claude/claude_desktop_config.jsonCheck the cwd path exists:
# Windows dir "d:\Projects\MCP\mcp_server.py" # macOS/Linux ls -la /path/to/MCP/mcp_server.pyVerify uv is installed:
uv --versionTest server manually:
cd d:\Projects\MCP uv run mcp_server.pyCheck Claude Desktop logs:
Look for error messages in MCP log files
Common issues: wrong path, missing dependencies, permission errors
Problem: Tools not appearing in Claude Desktop
Completely quit Claude Desktop (check system tray)
Verify JSON syntax in config file (use JSONLint.com)
Ensure no trailing commas in JSON
Restart Claude Desktop
Wait 10-30 seconds for server initialization
Problem: "Module not found" errors
FastAPI Server Issues
Problem: Port 8000 already in use
Problem: Import errors
Ensure you're running from the correct directory:
Testing Issues
Problem: MCP Inspector won't start
Problem: stdio communication hangs
Check for print statements or logging that might interfere with stdio
Ensure JSON-RPC messages are properly formatted
Use
logger.info()
instead ofprint()
for debugging
Resources
License
This project is provided as-is for educational and development purposes.
Contributing
Feel free to extend this project with additional math operations or features!
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
Enables AI applications to perform basic mathematical operations like addition and subtraction through MCP tools. Also provides REST API endpoints for the same operations.