Skip to main content
Glama
githejie

Calculator MCP Server

calculate

Evaluate mathematical expressions with precision using this calculation tool, which processes user-provided formulas to deliver accurate numerical results.

Instructions

Calculates/evaluates the given expression.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expressionYes

Implementation Reference

  • The MCP tool handler for the 'calculate' tool. It is registered via the @mcp.tool() decorator and implements the core logic by calling the evaluate helper function to safely compute the mathematical expression.
    @mcp.tool()
    async def calculate(expression: str) -> str:
        """Calculates/evaluates the given expression."""
        return evaluate(expression)
  • The evaluate helper function that parses and evaluates the mathematical expression using Python's ast module, supporting safe operations with allowed operators and math constants/functions to prevent code injection.
    def evaluate(expression: str) -> str:
        allowed_operators = {
            ast.Add: operator.add,
            ast.Sub: operator.sub,
            ast.Mult: operator.mul,
            ast.Div: operator.truediv,
            ast.FloorDiv: operator.floordiv,
            ast.Mod: operator.mod,
            ast.Pow: operator.pow,
            ast.USub: operator.neg,
        }
        allowed_names = {
            k: getattr(math, k)
            for k in dir(math)
            if not k.startswith("__")
        }
        allowed_names.update({
            "pi": math.pi,
            "e": math.e,
        })
    
        def eval_expr(node):
            if isinstance(node, ast.Constant):
                return node.value
            elif isinstance(node, ast.Name):
                if node.id in allowed_names:
                    return allowed_names[node.id]
                raise ValueError(f"Unknown identifier: {node.id}")
            elif isinstance(node, ast.BinOp):
                left = eval_expr(node.left)
                right = eval_expr(node.right)
                if type(node.op) in allowed_operators:
                    return allowed_operators[type(node.op)](left, right)
            elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
                return -eval_expr(node.operand)
            elif isinstance(node, ast.Call):
                func = eval_expr(node.func)
                args = [eval_expr(arg) for arg in node.args]
                return func(*args)
            raise ValueError(f"Unsupported operation: {ast.dump(node)}")
    
        expression = expression.replace('^', '**').replace('×', '*').replace('÷', '/')
        parsed_expr = ast.parse(expression, mode='eval')
        result = eval_expr(parsed_expr.body)
        return str(result)
Install Server

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/githejie/mcp-server-calculator'

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