calculate
Evaluate mathematical expressions with support for arithmetic, trigonometry, logarithms, and variable substitutions 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
TableJSON 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}) |
Implementation Reference
- src/vibe_math_mcp/tools/basic.py:14-47 (registration)MCP tool decorator registering the 'calculate' tool with name, comprehensive description of capabilities and examples, and tool annotations indicating it's read-only and idempotent.@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, ), )
- src/vibe_math_mcp/tools/basic.py:48-76 (handler)The core handler function for the 'calculate' tool. It uses SymPy to parse and simplify the expression, substitutes variables if provided, evaluates it numerically, formats the result, and handles evaluation errors gracefully.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}}" )
- Pydantic model defining strict input validation schema for the calculate tool, including whitespace stripping, length limits, and type checking for expression and variables.class CalculateInput(BaseModel): """Input model for mathematical expression evaluation.""" model_config = ConfigDict(str_strip_whitespace=True) expression: str = Field( ..., description="Mathematical expression to evaluate (e.g., '2+2', 'sin(pi/2)', 'x^2+1')", min_length=1, max_length=2000, ) variables: Optional[Dict[str, float]] = Field( default=None, description="Variable substitutions as dict (e.g., {'x': 5, 'y': 10})" )