Calculator
Performs a wide range of mathematical calculations, including basic arithmetic, advanced operations, trigonometry, and safe expression evaluation.
Calculator MCP Server ๐งฎ
A Model Context Protocol (MCP) server that provides comprehensive mathematical calculation capabilities for AI assistants. This intermediate-level implementation demonstrates proper MCP protocol usage with multiple tools, error handling, and security considerations.
๐ Features
๐ข Basic Arithmetic
- Addition: Add two numbers
- Subtraction: Subtract two numbers
- Multiplication: Multiply two numbers
- Division: Divide two numbers (with zero-division protection)
๐งฎ Advanced Operations
- Power: Raise numbers to any power
- Square Root: Calculate square roots (with negative number protection)
- Factorial: Calculate factorials (with overflow protection)
๐ Trigonometry
- Sin, Cos, Tan: Support for both radians and degrees
- Angle conversion: Automatic degree-to-radian conversion
๐ Expression Evaluation
- Safe Expression Parser: Evaluate complex mathematical expressions
- Security: Protected against code injection
- Constants: Built-in mathematical constants (ฯ, e, ฯ)
๐ก๏ธ Safety Features
- Input validation and sanitization
- Protection against dangerous code execution
- Proper error handling and user-friendly messages
- Overflow protection for large calculations
๐ Quick Start
1. Install Dependencies
cd Calculator_Server
pip install -r requirements.txt
2. Run the Server
python server.py
The server will start on http://0.0.0.0:8001
with SSE transport.
3. Test with Client
python client.py
๐ ๏ธ Available Tools
Tool | Description | Example |
---|---|---|
calculate | Evaluate mathematical expressions | "2 + 3 * 4" โ 14 |
add | Add two numbers | add(5, 3) โ 8 |
subtract | Subtract two numbers | subtract(10, 4) โ 6 |
multiply | Multiply two numbers | multiply(6, 7) โ 42 |
divide | Divide two numbers | divide(15, 3) โ 5 |
power | Raise to power | power(2, 8) โ 256 |
square_root | Calculate square root | square_root(64) โ 8 |
factorial | Calculate factorial | factorial(5) โ 120 |
trigonometry | Trig functions | sin(90ยฐ) โ 1 |
get_constants | List available constants | ฯ, e, ฯ |
๐ Usage Examples
Basic Calculations
# Simple arithmetic
add(10, 5) # "10 + 5 = 15"
multiply(7, 8) # "7 ร 8 = 56"
divide(20, 4) # "20 รท 4 = 5"
# Advanced operations
power(2, 10) # "2^10 = 1024"
square_root(144) # "โ144 = 12"
factorial(6) # "6! = 720"
Expression Evaluation
# Complex expressions
calculate("2 + 3 * 4") # "2 + 3 * 4 = 14"
calculate("sqrt(16) + pow(2, 3)") # "sqrt(16) + pow(2, 3) = 12"
calculate("sin(pi/2)") # "sin(pi/2) = 1"
calculate("2^3 + sqrt(9)") # "2^3 + sqrt(9) = 11"
Trigonometry
# Degrees and radians
trigonometry("sin", 90, "degrees") # "sin(90 degrees) = 1"
trigonometry("cos", 0, "radians") # "cos(0 radians) = 1"
trigonometry("tan", 45, "degrees") # "tan(45 degrees) = 1"
๐ง Integration with MCP Clients
Claude Desktop Configuration
Add to your Claude Desktop config:
{
"mcpServers": {
"calculator": {
"command": "python",
"args": ["path/to/Calculator_Server/server.py"],
"env": {}
}
}
}
Cursor IDE Configuration
Add to your Cursor MCP config:
{
"calculator-server": {
"command": "python",
"args": ["server.py"],
"cwd": "path/to/Calculator_Server"
}
}
๐ก๏ธ Security Features
- Safe Expression Evaluation: Only allows mathematical operations
- Input Sanitization: Removes dangerous patterns and functions
- Error Boundaries: Graceful error handling with informative messages
- Overflow Protection: Prevents calculations that could cause system issues
๐ฏ Why This is Intermediate Level
- Multiple Tool Types: Implements 9 different calculation tools
- Complex Logic: Safe expression parsing with security considerations
- Error Handling: Comprehensive error management for edge cases
- Type Safety: Proper type hints and validation
- Real-world Utility: Actually useful for AI assistants
- MCP Best Practices: Follows official MCP protocol standards
๐ Transport Protocols
- SSE (Server-Sent Events): Default transport for web integration
- Stdio: Available for command-line integration
๐ Example Output
๐งฎ Testing Calculator MCP Server...
==================================================
๐ Available tools: 9
โข calculate: Evaluate a mathematical expression safely
โข add: Add two numbers
โข subtract: Subtract two numbers
โข multiply: Multiply two numbers
โข divide: Divide two numbers
โข power: Raise a number to a power
โข square_root: Calculate the square root of a number
โข factorial: Calculate the factorial of a number
โข trigonometry: Calculate trigonometric functions
๐งช Test 1: add({'a': 5, 'b': 3})
โ
Result: 5 + 3 = 8
๐งช Test 2: calculate({'expression': 'sin(pi/2)'})
โ
Result: sin(pi/2) = 1.0
๐ Calculator MCP Server testing completed!
๐ค Contributing
This calculator server demonstrates intermediate MCP development concepts. Feel free to extend it with:
- More mathematical functions (logarithms, hyperbolic functions)
- Unit conversions
- Statistical calculations
- Matrix operations
Built with โค๏ธ using the Model Context Protocol
Related Servers
ONLYOFFICE DocSpace
Interact with ONLYOFFICE DocSpace for document collaboration and management.
Claude MCP Trello
Interact with Trello boards and cards via the Trello API, with built-in rate limiting and error handling.
Google Workspace MCP Server
Interact with Google Workspace services like Gmail and Google Calendar.
Coda
Interact with the Coda API to manage documents and pages, including creating, reading, updating, and deleting.
MCP Notes
A simple note-taking server for recording and managing notes with AI models, using AWS DynamoDB for storage.
Fibery
Perform queries and entity operations in your Fibery workspace.
Xero
Interact with the accounting data in your business using our official MCP server
Work Memory MCP Server
Manages work memories and shares context between AI tools using a local SQLite database.
Twenty MCP Server
Enables AI assistants to seamlessly interact with your Twenty CRM data through its API.
Time
Time and timezone conversion capabilities