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}`, }, ], }; }
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