calculate
Evaluate mathematical expressions with support for arithmetic, trigonometry, logarithms, and variables using SymPy.
Instructions
Evaluate mathematical expressions using SymPy.
Supports: - Arithmetic: +, -, *, /, ^ - Trigonometry: sin, cos, tan, asin, acos, atan - Logarithms: log, ln, exp - Constants: pi, e - Functions: sqrt, abs
Examples:
SIMPLE ARITHMETIC: expression="2 + 2" Result: 4
TRIGONOMETRY: expression="sin(pi/2)" Result: 1.0
WITH VARIABLES: expression="x^2 + 2*x + 1", variables={"x": 3} Result: 16
MULTIPLE VARIABLES: expression="x^2 + y^2", variables={"x": 3, "y": 4} Result: 25
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification. | |
| output_mode | No | Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details. | full |
| expression | Yes | Mathematical expression (e.g., '2+2', 'sin(pi/2)', 'x^2+1') | |
| variables | No | Variable substitutions (e.g., {'x': 5, 'y': 10}) |
Input Schema (JSON Schema)
Implementation Reference
- src/vibe_math_mcp/tools/basic.py:48-76 (handler)The handler function that executes the 'calculate' tool logic, evaluating mathematical expressions with SymPy, supporting variables, and formatting the result.async def calculate( expression: Annotated[ str, Field( description="Mathematical expression (e.g., '2+2', 'sin(pi/2)', 'x^2+1')", min_length=1 ), ], variables: Annotated[ Dict[str, float] | None, Field(description="Variable substitutions (e.g., {'x': 5, 'y': 10})"), ] = None, ) -> str: """Evaluate mathematical expressions.""" try: expr = sympify(expression) if variables: result = float(N(expr.subs(variables))) else: result = float(N(simplify(expr))) return format_result(result, {"expression": expression, "variables": variables}) except Exception as e: raise ValueError( f"Failed to evaluate expression '{expression}'. " f"Error: {str(e)}. " f"Example: '2*x + 3' with variables={{'x': 5}}" )
- src/vibe_math_mcp/tools/basic.py:14-47 (registration)The @mcp.tool decorator that registers the 'calculate' tool with name, description, and annotations.@mcp.tool( name="calculate", description="""Evaluate mathematical expressions using SymPy. Supports: - Arithmetic: +, -, *, /, ^ - Trigonometry: sin, cos, tan, asin, acos, atan - Logarithms: log, ln, exp - Constants: pi, e - Functions: sqrt, abs Examples: SIMPLE ARITHMETIC: expression="2 + 2" Result: 4 TRIGONOMETRY: expression="sin(pi/2)" Result: 1.0 WITH VARIABLES: expression="x^2 + 2*x + 1", variables={"x": 3} Result: 16 MULTIPLE VARIABLES: expression="x^2 + y^2", variables={"x": 3, "y": 4} Result: 25""", annotations=ToolAnnotations( title="Expression Calculator", readOnlyHint=True, idempotentHint=True, ), )
- Pydantic schema definitions for the input parameters of the 'calculate' tool using Annotated and Field.expression: Annotated[ str, Field( description="Mathematical expression (e.g., '2+2', 'sin(pi/2)', 'x^2+1')", min_length=1 ), ], variables: Annotated[ Dict[str, float] | None, Field(description="Variable substitutions (e.g., {'x': 5, 'y': 10})"), ] = None, ) -> str:
- src/vibe_math_mcp/tools/__init__.py:2-5 (registration)Import of the basic tools module in tools/__init__.py, which triggers the registration of the 'calculate' tool via its decorator.# Tools are registered via decorators in their respective modules # Import modules to trigger registration from . import basic