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

ToolDescriptionExample
calculateEvaluate mathematical expressions"2 + 3 * 4" โ†’ 14
addAdd two numbersadd(5, 3) โ†’ 8
subtractSubtract two numberssubtract(10, 4) โ†’ 6
multiplyMultiply two numbersmultiply(6, 7) โ†’ 42
divideDivide two numbersdivide(15, 3) โ†’ 5
powerRaise to powerpower(2, 8) โ†’ 256
square_rootCalculate square rootsquare_root(64) โ†’ 8
factorialCalculate factorialfactorial(5) โ†’ 120
trigonometryTrig functionssin(90ยฐ) โ†’ 1
get_constantsList 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

  1. Multiple Tool Types: Implements 9 different calculation tools
  2. Complex Logic: Safe expression parsing with security considerations
  3. Error Handling: Comprehensive error management for edge cases
  4. Type Safety: Proper type hints and validation
  5. Real-world Utility: Actually useful for AI assistants
  6. 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