calculate
Compute mathematical expressions to solve calculations, from basic arithmetic to trigonometric functions, within the Slim MCP server.
Instructions
Calculate the result of a mathematical expression. Args: expression: A mathematical expression as a string (e.g. "2 + 2", "sin(30)")
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes |
Implementation Reference
- src/claude_tools/calculator.py:3-25 (handler)The handler function for the 'calculate' tool. It safely evaluates mathematical expressions using a restricted eval environment with math functions.def calculate(expression: str) -> str: """Calculate the result of a mathematical expression. Args: expression: A mathematical expression as a string (e.g. "2 + 2", "sin(30)") """ try: # Replace common math functions with their python equivalents expression = expression.replace("^", "**") # Create a safe namespace with only math functions safe_dict = { 'abs': abs, 'round': round, 'sin': math.sin, 'cos': math.cos, 'tan': math.tan, 'asin': math.asin, 'acos': math.acos, 'atan': math.atan, 'sqrt': math.sqrt, 'log': math.log, 'log10': math.log10, 'pi': math.pi, 'e': math.e } # Evaluate the expression in the safe namespace result = eval(expression, {"__builtins__": {}}, safe_dict) return f"Result: {result}" except Exception as e: return f"Error: {str(e)}"
- src/claude_tools/calculator.py:27-30 (registration)Registers the 'calculate' tool using mcp.tool() decorator.def register_calculator_tools(mcp): """Register all calculator tools with the MCP server.""" mcp.tool()(calculate)
- src/claude_tools/calculator.py:3-7 (schema)Type hints and docstring defining the input schema for the 'calculate' tool.def calculate(expression: str) -> str: """Calculate the result of a mathematical expression. Args: expression: A mathematical expression as a string (e.g. "2 + 2", "sin(30)") """