calculate
Perform basic arithmetic calculations including addition, subtraction, multiplication, and division with two numbers.
Instructions
Perform basic arithmetic calculations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | The arithmetic operation to perform | |
| a | Yes | First number | |
| b | Yes | Second number |
Implementation Reference
- src/index.ts:100-134 (handler)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}`, }, ], }; }
- src/index.ts:57-79 (schema)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"], }, },
- src/fastmcp-example/server.ts:39-62 (handler)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}`; },
- src/fastmcp-example/server.ts:31-63 (registration)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}`; }, });
- src/basic-mcp/server.ts:98-132 (handler)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}`, }, ], }; }