A powerful Model Context Protocol (MCP) server that transforms AI assistants into mathematical powerhouses with 100% deterministic calculations.
Small Language Models (SLMs) are excellent at generating text but struggle with precise mathematical calculations. They predict the next token, which often leads to "hallucinated" answers for complex computations. This MCP server solves that problem by giving AI assistants access to a reliable calculator that computes accurate answers instead of guessing.
The Solution: Instead of asking the AI to guess mathematical answers, we provide it with a Calculator (this Server) so it can compute the answer with precision.
This server provides 30+ mathematical tools across 8 categories:
- Addition, Subtraction, Multiplication, Division
- Modulo operations
- Power calculations
- Square root
- Logarithms (natural, base-10, custom base)
- Factorial
- Greatest Common Divisor (GCD)
- Least Common Multiple (LCM)
- Sine, Cosine, Tangent
- Degree โ Radian conversion
- Mean (average)
- Median
- Standard Deviation
- Variance
- Hypotenuse calculation
- Circle area
- 2D Euclidean distance
- 3D Euclidean distance
- Manhattan distance (n-dimensional)
- Absolute difference
- Absolute value
- Floor, Ceiling, Rounding
- Random number generation
- Mathematical constants (ฯ, e)
- Python 3.10 or higher
- uv package manager (recommended for speed)
# Install uv (if not already installed)
pip install uv
# Clone the repository
git clone <your-repo-url>
cd Unified-Math-Local-MCP-Server
# Install dependencies
uv add fastmcp# Start the server
uv run python main.pyBefore connecting to an AI client, test your tools using the built-in Inspector:
uv run fastmcp dev main.pyThis opens an interactive interface where you can:
- View all available tools
- Test each tool with sample inputs
- See JSON-RPC messages in real-time
- Verify outputs before production use
uv run fastmcp install claude-desktop main.py-
Find your
uvpath:which uv
-
Edit your Claude Desktop configuration file:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
- macOS:
-
Add the server configuration:
{ "mcpServers": { "unified-math-server": { "command": "/path/to/uv", "args": ["run", "python", "/path/to/main.py"] } } } -
Restart Claude Desktop
Once connected to Claude Desktop, you can use natural language:
Example 1: Basic Calculation
User: "Add 15, 27, and 33"
Assistant: [Calls add([15, 27, 33])]
Result: 75
Example 2: Chain of Operations
User: "I have a right triangle with sides 5 and 12. Calculate the hypotenuse,
then tell me the area of a circle with that radius."
Assistant Process:
1. Calls hypotenuse(5, 12) โ 13.0
2. Calls circle_area(13.0) โ 530.93
Final Answer: "The area of the circle is approximately 530.93 square units."
Example 3: Statistics
User: "Calculate the standard deviation of [10, 20, 30, 40, 50]"
Assistant: [Calls stdev([10, 20, 30, 40, 50])]
Result: 15.81
The server includes helper functions (_as_number, _as_list) that sanitize inputs. AI models are non-deterministic and might pass strings like " 42 " instead of the integer 42. This layer prevents crashes due to type errors.
Instead of one monolithic calculate() function, we create specific tools for specific operations. This enables:
- Chain of Thought Reasoning: AI can break complex formulas into steps
- Clear Error Messages: Each tool can provide specific feedback
- Better Discoverability: AI can see exactly what operations are available
We use Python type hints extensively. FastMCP automatically generates JSON schemas from these hints, making the code self-documenting.
| Technology | Purpose | Why? |
|---|---|---|
| uv | Package management | 10-100x faster than pip; instant virtual environment setup |
| FastMCP | MCP abstraction layer | Reduces boilerplate; FastAPI-like decorator syntax |
| Python 3.10+ | Runtime | Native type hints; async/await support |
For a complete list of tools with their signatures and descriptions, run the Inspector or check the main.py file. Each tool includes:
- Clear docstrings
- Type-safe parameters
- Comprehensive error handling
- Input validation
MCP is designed with security in mind:
- Permission-Based: Claude Desktop asks for permission before executing tools
- Local Execution: All computations run on your machine
- No Network Calls: The server doesn't make external requests
- Sandboxed: FastMCP handles input validation and error isolation
Contributions are welcome! To add new mathematical tools:
- Fork the repository
- Add your tool function with the
@mcp.tooldecorator - Include proper type hints and docstrings
- Test using the FastMCP Inspector
- Submit a pull request
This project is open source and available under the MIT License.
Read the full tutorial series on Medium:
- Part 1: Why the era of isolated AI is ending
- Part 1: Understanding MCP architecture
- Part 1: Building this Unified Math Server
- How to Build Remote MCP server
- How to Build MCP client
Found this helpful? โญ Star this repo and share your feedback!
Questions? Open an issue or reach out on [your social media/contact]