calculator
Perform basic mathematical operations including addition, subtraction, multiplication, and division with two numbers to solve calculation problems.
Instructions
Perform basic mathematical operations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| operation | Yes | The mathematical operation to perform | |
| a | Yes | The first number | |
| b | Yes | The second number |
Implementation Reference
- src/index.ts:110-146 (handler)Implements the calculator tool handler: parses input arguments using the schema, performs the specified arithmetic operation (add, subtract, multiply, divide), handles division by zero, and returns the result in the MCP response format.case 'calculator': { const parsed = CalculatorArgsSchema.parse(args); let result: number; switch (parsed.operation) { case 'add': result = parsed.a + parsed.b; break; case 'subtract': result = parsed.a - parsed.b; break; case 'multiply': result = parsed.a * parsed.b; break; case 'divide': if (parsed.b === 0) { throw new Error('Division by zero is not allowed'); } result = parsed.a / parsed.b; break; default: throw new Error(`Unknown operation: ${parsed.operation}`); } return { content: [ { type: 'text', text: `Result is from my Simple MCP Server:=> ${parsed.a} ${parsed.operation} ${parsed.b} = ${result}`, }, { type: 'text', text: 'Ans is from Simple MCP Server.', }, ], }; }
- src/index.ts:29-34 (schema)Defines the Zod validation schema for the calculator tool's input: operation (enum), and two numbers a and b.// Schema for the calculator tool input const CalculatorArgsSchema = z.object({ operation: z.enum(['add', 'subtract', 'multiply', 'divide']).describe('The mathematical operation to perform'), a: z.number().describe('The first number'), b: z.number().describe('The second number'), });
- src/index.ts:58-79 (registration)Registers the calculator tool in the ListTools response, providing name, description, and JSON schema mirroring the Zod schema for input validation.name: 'calculator', description: 'Perform basic mathematical operations', inputSchema: { type: 'object', properties: { operation: { type: 'string', enum: ['add', 'subtract', 'multiply', 'divide'], description: 'The mathematical operation to perform', }, a: { type: 'number', description: 'The first number', }, b: { type: 'number', description: 'The second number', }, }, required: ['operation', 'a', 'b'], }, },