calculator
Perform addition, subtraction, multiplication, and division operations using inputs provided. Utilize this tool for quick and precise mathematical calculations on the MCP server.
Instructions
Perform basic mathematical operations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | The first number | |
| b | Yes | The second number | |
| operation | Yes | The mathematical operation to perform |
Implementation Reference
- src/index.ts:110-146 (handler)Handler function for the 'calculator' tool that parses input, performs arithmetic operations (add, subtract, multiply, divide), handles division by zero, and returns the result.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)Zod schema for validating calculator tool inputs: operation (enum), a and b (numbers).// 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:57-79 (registration)Registration of the 'calculator' tool in the listTools response, including name, description, and JSON input schema.{ 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'], }, },