calculate
Perform arithmetic operations like addition, subtraction, multiplication, and division using specified numerical inputs. Part of the MCP Server Boilerplate for standardized context in LLM applications.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| a | Yes | ||
| b | Yes | ||
| operation | Yes |
Implementation Reference
- src/tools/calculatorTool.ts:16-48 (handler)The handler function that executes the arithmetic calculation based on the specified operation ('add', 'subtract', 'multiply', 'divide'), handling inputs a and b, with division by zero error check.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)Zod input schema for the 'calculate' tool, validating operation as one of the arithmetic enums and a, b as numbers.{ operation: z.enum(['add', 'subtract', 'multiply', 'divide']), a: z.number(), b: z.number(), },
- src/tools/calculatorTool.ts:9-49 (registration)The server.tool call that registers the 'calculate' tool with its name, input schema, and handler function on the MCP server instance.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}`, }, ], }; } );