calculate
Perform basic arithmetic operations (add, subtract, multiply, divide) on two numbers to get calculation results.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"operation": "<operation>",
"a": "<a>",
"b": "<b>"
},
"name": "calculate"
}
}Input 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}`, }, ], }; } );