Skip to main content
Glama
by apetta

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

NameRequiredDescriptionDefault
contextNoOptional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification.
output_modeNoOutput format: full (default), compact, minimal, value, or final. See batch_execute tool for details.full
expressionYesMathematical expression (e.g., '2+2', 'sin(pi/2)', 'x^2+1')
variablesNoVariable substitutions (e.g., {'x': 5, 'y': 10})

Input Schema (JSON Schema)

{ "properties": { "context": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "description": "Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification." }, "expression": { "description": "Mathematical expression (e.g., '2+2', 'sin(pi/2)', 'x^2+1')", "minLength": 1, "type": "string" }, "output_mode": { "default": "full", "description": "Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details.", "enum": [ "full", "compact", "minimal", "value", "final" ], "type": "string" }, "variables": { "anyOf": [ { "additionalProperties": { "type": "number" }, "type": "object" }, { "type": "null" } ], "default": null, "description": "Variable substitutions (e.g., {'x': 5, 'y': 10})" } }, "required": [ "expression" ], "type": "object" }

Implementation Reference

  • 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}}" )
  • 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:
  • 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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/apetta/vibe-math-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server