calculate
Perform basic arithmetic operations (add, subtract, multiply, divide) on two numbers to get calculation results.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | ||
| a | Yes | ||
| b | Yes |
Implementation Reference
- src/tools/calculatorTool.ts:16-48 (handler)The handler function that executes the 'calculate' tool logic, performing basic arithmetic operations (add, subtract, multiply, divide) on two numbers and returning the result or error for division by zero.
async ({ operation, a, b }) => { let result: number | undefined; 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: Division by zero is not allowed.' }], isError: true, }; } result = a / b; break; } return { content: [ { type: 'text', text: `Result of ${operation}(${a}, ${b}) = ${result}`, }, ], }; } - src/tools/calculatorTool.ts:11-15 (schema)Input schema for the 'calculate' tool, defining the operation type and two numeric operands using Zod.
{ operation: z.enum(['add', 'subtract', 'multiply', 'divide']), a: z.number(), b: z.number(), }, - src/tools/calculatorTool.ts:9-49 (registration)Registration of the 'calculate' tool on the MCP server instance, including schema and handler.
server.tool( 'calculate', { operation: z.enum(['add', 'subtract', 'multiply', 'divide']), a: z.number(), b: z.number(), }, async ({ operation, a, b }) => { let result: number | undefined; 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: Division by zero is not allowed.' }], isError: true, }; } result = a / b; break; } return { content: [ { type: 'text', text: `Result of ${operation}(${a}, ${b}) = ${result}`, }, ], }; } );