Skip to main content
Glama
gogouravr

FastMCP Demo

by gogouravr

calculate

Perform basic arithmetic calculations including addition, subtraction, multiplication, and division with two numbers.

Instructions

Perform basic arithmetic calculations

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationYesThe arithmetic operation to perform
aYesFirst number
bYesSecond number

Implementation Reference

  • Handler for the 'calculate' tool in the main MCP demo server. Performs arithmetic operations (add, subtract, multiply, divide) on two numbers, handles division by zero, and returns the result as text content.
    if (name === "calculate") {
      const operation = args?.operation as string;
      const a = args?.a as number;
      const b = args?.b as number;
    
      let result: number;
      switch (operation) {
        case "add":
          result = a + b;
          break;
        case "subtract":
          result = a - b;
          break;
        case "multiply":
          result = a * b;
          break;
        case "divide":
          if (b === 0) {
            throw new Error("Division by zero is not allowed");
          }
          result = a / b;
          break;
        default:
          throw new Error(`Unknown operation: ${operation}`);
      }
    
      return {
        content: [
          {
            type: "text",
            text: `${a} ${operation} ${b} = ${result}`,
          },
        ],
      };
    }
  • Input schema definition for the 'calculate' tool, defining operation (enum), and numbers a and b as required parameters.
    {
      name: "calculate",
      description: "Perform basic arithmetic calculations",
      inputSchema: {
        type: "object",
        properties: {
          operation: {
            type: "string",
            enum: ["add", "subtract", "multiply", "divide"],
            description: "The arithmetic operation to perform",
          },
          a: {
            type: "number",
            description: "First number",
          },
          b: {
            type: "number",
            description: "Second number",
          },
        },
        required: ["operation", "a", "b"],
      },
    },
  • Handler (execute function) for the 'calculate' tool in the FastMCP example server. Performs arithmetic operations and returns a formatted string result.
    execute: async (args) => {
      const { operation, a, b } = args;
      
      let result: number;
      switch (operation) {
        case "add":
          result = a + b;
          break;
        case "subtract":
          result = a - b;
          break;
        case "multiply":
          result = a * b;
          break;
        case "divide":
          if (b === 0) {
            throw new Error("Division by zero is not allowed");
          }
          result = a / b;
          break;
      }
      
      return `${a} ${operation} ${b} = ${result}`;
    },
  • Registration of the 'calculate' tool using FastMCP's addTool method, including schema via Zod and handler.
    server.addTool({
      name: "calculate",
      description: "Perform basic arithmetic calculations",
      parameters: z.object({
        operation: z.enum(["add", "subtract", "multiply", "divide"]).describe("The arithmetic operation to perform"),
        a: z.number().describe("First number"),
        b: z.number().describe("Second number"),
      }),
      execute: async (args) => {
        const { operation, a, b } = args;
        
        let result: number;
        switch (operation) {
          case "add":
            result = a + b;
            break;
          case "subtract":
            result = a - b;
            break;
          case "multiply":
            result = a * b;
            break;
          case "divide":
            if (b === 0) {
              throw new Error("Division by zero is not allowed");
            }
            result = a / b;
            break;
        }
        
        return `${a} ${operation} ${b} = ${result}`;
      },
    });
  • Handler for the 'calculate' tool in the basic MCP server implementation. Identical logic to the main demo.
    if (name === "calculate") {
      const operation = args?.operation as string;
      const a = args?.a as number;
      const b = args?.b as number;
    
      let result: number;
      switch (operation) {
        case "add":
          result = a + b;
          break;
        case "subtract":
          result = a - b;
          break;
        case "multiply":
          result = a * b;
          break;
        case "divide":
          if (b === 0) {
            throw new Error("Division by zero is not allowed");
          }
          result = a / b;
          break;
        default:
          throw new Error(`Unknown operation: ${operation}`);
      }
    
      return {
        content: [
          {
            type: "text",
            text: `${a} ${operation} ${b} = ${result}`,
          },
        ],
      };
    }
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure but only states the basic function. It doesn't mention error handling (e.g., division by zero), performance characteristics, or any side effects, leaving significant gaps in understanding how the tool behaves beyond its core operation.

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

Conciseness5/5

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

The description is extremely concise with a single, clear sentence that directly states the tool's purpose without any unnecessary words or structural fluff. It's front-loaded and wastes no space.

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

Completeness2/5

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

Given the lack of annotations and output schema, the description is incomplete for a tool with 3 parameters and mathematical operations. It doesn't explain return values, error conditions, or behavioral nuances, making it inadequate for full contextual understanding despite the simple nature of the tool.

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

Parameters3/5

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

The schema description coverage is 100%, with clear descriptions for all three parameters, so the description doesn't need to add parameter details. The description mentions 'basic arithmetic calculations,' which aligns with the parameters but doesn't provide additional semantic context beyond what the schema already covers.

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

Purpose4/5

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

The description clearly states the tool's purpose with a specific verb ('perform') and resource ('basic arithmetic calculations'), making it immediately understandable. However, it doesn't differentiate from the only sibling tool 'hello', which appears unrelated, so it doesn't fully address sibling distinction.

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?

The description provides no guidance on when to use this tool versus alternatives or any contextual prerequisites. It simply states what the tool does without indicating appropriate use cases or limitations.

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

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/gogouravr/fast-mcp'

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