scientific_calculator
Evaluate mathematical expressions with support for scientific functions, logarithms, trigonometry, complex numbers, and constants using this calculator tool.
Instructions
Evaluate mathematical expressions using a scientific calculator.
Supports:
Basic arithmetic: +, -, *, /, //, %, **
Scientific functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh
Logarithmic functions: log, log10, log2, ln (natural log)
Exponential functions: exp, sqrt, cbrt
Constants: pi, e, tau
Complex numbers: 1+2j, complex operations
Trigonometric functions work with radians by default
Use degrees(x) to convert radians to degrees, radians(x) to convert degrees to radians
Examples:
"sin(pi/2)" -> 1.0
"log10(100)" -> 2.0
"sqrt(16)" -> 4.0
"2**3" -> 8
"exp(1)" -> 2.718281828459045
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes |
Implementation Reference
- server.py:29-136 (handler)The handler function for the 'scientific_calculator' tool, decorated with @mcp.tool() which also serves as registration. It safely evaluates mathematical expressions using a controlled eval environment with math functions, constants, and complex number support. Includes comprehensive error handling and result formatting.@mcp.tool() def scientific_calculator(expression: str) -> str: """ Evaluate mathematical expressions using a scientific calculator. Supports: - Basic arithmetic: +, -, *, /, //, %, ** - Scientific functions: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh - Logarithmic functions: log, log10, log2, ln (natural log) - Exponential functions: exp, sqrt, cbrt - Constants: pi, e, tau - Complex numbers: 1+2j, complex operations - Trigonometric functions work with radians by default - Use degrees(x) to convert radians to degrees, radians(x) to convert degrees to radians Examples: - "sin(pi/2)" -> 1.0 - "log10(100)" -> 2.0 - "sqrt(16)" -> 4.0 - "2**3" -> 8 - "exp(1)" -> 2.718281828459045 """ try: # Create a safe namespace with mathematical functions and constants safe_dict = { # Mathematical functions 'sin': math.sin, 'cos': math.cos, 'tan': math.tan, 'asin': math.asin, 'acos': math.acos, 'atan': math.atan, 'atan2': math.atan2, 'sinh': math.sinh, 'cosh': math.cosh, 'tanh': math.tanh, 'asinh': math.asinh, 'acosh': math.acosh, 'atanh': math.atanh, 'log': math.log, 'log10': math.log10, 'log2': math.log2, 'ln': math.log, # Natural logarithm alias 'exp': math.exp, 'sqrt': cmath.sqrt, 'cbrt': lambda x: x**(1/3), 'pow': pow, 'abs': abs, 'ceil': math.ceil, 'floor': math.floor, 'trunc': math.trunc, 'round': round, 'degrees': math.degrees, 'radians': math.radians, 'factorial': math.factorial, 'gcd': math.gcd, 'lcm': math.lcm if hasattr(math, 'lcm') else lambda a, b: abs(a*b) // math.gcd(a, b), # Constants 'pi': math.pi, 'e': math.e, 'tau': math.tau, 'inf': math.inf, 'nan': math.nan, # Complex number functions 'complex': complex, 'real': lambda x: x.real if isinstance(x, complex) else x, 'imag': lambda x: x.imag if isinstance(x, complex) else 0, 'conjugate': lambda x: x.conjugate() if hasattr(x, 'conjugate') else x, 'phase': cmath.phase, 'polar': cmath.polar, 'rect': cmath.rect, # Allow built-in mathematical operations '__builtins__': {} } # Replace common mathematical notation expression = expression.replace('^', '**') # Allow ^ for exponentiation expression = expression.replace('mod', '%') # Allow mod for modulo # Evaluate the expression result = eval(expression, safe_dict) # Format the result nicely if isinstance(result, complex): if abs(result.imag) < 1e-10: # Essentially real real_part = result.real if abs(real_part) < 1e-10: return "0" elif abs(real_part - round(real_part)) < 1e-10: return str(int(round(real_part))) else: return str(real_part) else: real_str = str(int(result.real)) if abs(result.real - round(result.real)) < 1e-10 else str(result.real) imag_str = str(int(result.imag)) if abs(result.imag - round(result.imag)) < 1e-10 else str(result.imag) if result.real == 0: return f"{imag_str}j" if result.imag != 1 else "j" if result.imag == 1 else "-j" else: if result.imag >= 0: imag_part = f" + {imag_str}j" if result.imag != 1 else " + j" else: imag_part = f" - {abs(float(imag_str))}j" if result.imag != -1 else " - j" return f"{real_str}{imag_part}" elif isinstance(result, float): # Round very small numbers to avoid floating point precision issues if abs(result) < 1e-10: return "0" elif abs(result - round(result)) < 1e-10: return str(int(round(result))) else: return str(result) else: return str(result) except ZeroDivisionError: return "Error: Division by zero" except ValueError as e: return f"Error: Invalid mathematical operation - {str(e)}" except OverflowError: return "Error: Result too large to compute" except TypeError as e: return f"Error: Invalid expression type - {str(e)}" except SyntaxError: return "Error: Invalid mathematical expression syntax" except Exception as e: return f"Error: {str(e)}"