calculate
Perform arithmetic operations including addition, subtraction, multiplication, and division on two numbers using this calculation tool.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | ||
| a | Yes | ||
| b | Yes |
Implementation Reference
- src/index.ts:31-56 (handler)The handler function for the 'calculate' tool that performs add, subtract, multiply, or divide operations on inputs a and b, handling division by zero.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) return { content: [ { type: "text", text: "Error: Cannot divide by zero", }, ], }; result = a / b; break; } return { content: [{ type: "text", text: String(result) }] }; }
- src/index.ts:26-30 (schema)Zod schema defining the input parameters for the 'calculate' tool: operation (enum), a (number), b (number).operation: z.enum(["add", "subtract", "multiply", "divide"]), a: z.number(), b: z.number(), }, async ({ operation, a, b }) => {
- src/index.ts:24-57 (registration)Registration of the 'calculate' tool using this.server.tool, including name, input schema, and handler function."calculate", { operation: z.enum(["add", "subtract", "multiply", "divide"]), a: z.number(), b: z.number(), }, async ({ operation, a, b }) => { 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) return { content: [ { type: "text", text: "Error: Cannot divide by zero", }, ], }; result = a / b; break; } return { content: [{ type: "text", text: String(result) }] }; } );