Skip to main content
Glama

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
NameRequiredDescriptionDefault
expressionYes

Implementation Reference

  • 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)}"
  • 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)") """
  • Registers the 'calculate' function as an MCP tool using the mcp.tool() decorator within the register_calculator_tools function.
    mcp.tool()(calculate)
  • Invokes the registration of calculator tools (including 'calculate') during MCP server initialization in the main entry point.
    register_calculator_tools(mcp)

Other Tools

Related Tools

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/webdevtodayjason/slim-MCP'

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