calculate
Process mathematical expressions instantly to compute results. Input an expression as a string (e.g., "2 + 2" or "sin(30)") and receive the calculated output, designed for integration with automated workflows or AI agents.
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 a mathematical expression provided as a string using eval with a restricted namespace of math functions to prevent code injection.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:3-7 (schema)Input/output schema defined by function signature (expression: str -> str) and docstring specifying the parameter format and examples.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)") """
- src/claude_tools/calculator.py:29-29 (registration)Registers the 'calculate' function as an MCP tool using the mcp.tool() decorator within the register_calculator_tools function.mcp.tool()(calculate)
- src/claude_tools/main.py:23-23 (registration)Invokes the registration of calculator tools (including 'calculate') during MCP server initialization in the main entry point.register_calculator_tools(mcp)