calculate
Perform basic mathematical operations like addition, subtraction, multiplication, and division using two numerical inputs.
Instructions
Perform basic mathematical operations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | Mathematical operation to perform | |
| x | Yes | First operand | |
| y | Yes | Second operand |
Implementation Reference
- demo.ts:132-188 (handler)The handler for the 'calculate' tool within the CallToolRequestSchema handler. It destructures the arguments, performs the specified arithmetic operation (add, subtract, multiply, divide) on x and y, handles division by zero and invalid operations, and returns a formatted result text.if (name === "calculate") { const { operation, x, y } = args as { operation: string; x: number; y: number; }; let result: number; let operationSymbol: string; switch (operation) { case "add": result = x + y; operationSymbol = "+"; break; case "subtract": result = x - y; operationSymbol = "-"; break; case "multiply": result = x * y; operationSymbol = "×"; break; case "divide": if (y === 0) { return { content: [ { type: "text", text: "Error: Division by zero is not allowed", }, ], }; } result = x / y; operationSymbol = "÷"; break; default: return { content: [ { type: "text", text: `Error: Unknown operation "${operation}"`, }, ], }; } return { content: [ { type: "text", text: `${x} ${operationSymbol} ${y} = ${result}`, }, ], }; }
- demo.ts:65-83 (schema)The input schema for the 'calculate' tool, defining the required parameters: operation (enum of arithmetic ops), and numbers x and y.inputSchema: { type: "object", properties: { operation: { type: "string", description: "Mathematical operation to perform", enum: ["add", "subtract", "multiply", "divide"], }, x: { type: "number", description: "First operand", }, y: { type: "number", description: "Second operand", }, }, required: ["operation", "x", "y"], },
- demo.ts:62-84 (registration)Registration of the 'calculate' tool in the ListToolsRequestSchema handler's tools array, including its name, description, and input schema.{ name: "calculate", description: "Perform basic mathematical operations", inputSchema: { type: "object", properties: { operation: { type: "string", description: "Mathematical operation to perform", enum: ["add", "subtract", "multiply", "divide"], }, x: { type: "number", description: "First operand", }, y: { type: "number", description: "Second operand", }, }, required: ["operation", "x", "y"], }, },