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)
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It only states the action without mentioning any traits like error handling, performance limits, output format, or side effects. This leaves the agent with insufficient information about how the tool behaves beyond its basic function.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is very concise with a single sentence, which is efficient and front-loaded. However, it is overly brief to the point of under-specification, lacking necessary details that would make it more helpful without becoming verbose.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (a calculation function with no annotations, no output schema, and low schema coverage), the description is incomplete. It fails to address key aspects like return values, error cases, or usage examples, making it inadequate for an agent to effectively invoke the tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 1 parameter with 0% description coverage, and the description does not compensate by explaining what 'expression' entails (e.g., syntax, supported operators, constraints). This leaves the parameter semantics entirely undocumented, failing to add value beyond the minimal schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Calculates/evaluates the given expression' is tautological, essentially restating the tool name 'calculate' without adding specificity. It mentions a verb ('calculates/evaluates') and resource ('expression'), but lacks details about what type of expressions are supported or the calculation context, making it vague.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool, such as for mathematical expressions, logical evaluations, or specific domains. Since there are no sibling tools, the lack of alternatives doesn't penalize heavily, but the description fails to offer any contextual usage hints or prerequisites.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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