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
- server.js:94-123 (handler)Handler function for the 'calculate' tool, performing arithmetic operations (add, subtract, multiply, divide) on inputs a and b, handling division by zero, and returning the result as text.case 'calculate': let result; switch (args.operation) { case 'add': result = args.a + args.b; break; case 'subtract': result = args.a - args.b; break; case 'multiply': result = args.a * args.b; break; case 'divide': if (args.b === 0) { throw new Error('Division by zero is not allowed'); } result = args.a / args.b; break; default: throw new Error(`Unknown operation: ${args.operation}`); } return { content: [ { type: 'text', text: `${args.a} ${args.operation} ${args.b} = ${result}`, }, ], };
- server.js:44-66 (registration)Registration of the 'calculate' tool in the ListTools response, defining name, description, and input schema for arithmetic operations.{ 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'], }, },
- server.js:47-65 (schema)Input schema definition for the 'calculate' tool, specifying operation (enum), numbers a and b as required properties.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'], },