Skip to main content
Glama

calculate

Compute mathematical expressions to solve calculations, from basic arithmetic to trigonometric functions, within the Slim MCP server.

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 mathematical expressions using a restricted eval environment with math functions.
    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)}"
  • Registers the 'calculate' tool using mcp.tool() decorator.
    def register_calculator_tools(mcp):
        """Register all calculator tools with the MCP server."""
        mcp.tool()(calculate)
  • Type hints and docstring defining the input schema for the 'calculate' tool.
    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)")
        """

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