Calculate
calculatePerform basic arithmetic operations including addition, subtraction, multiplication, and division within the TypeScript MCP Server Template.
Instructions
Performs basic arithmetic calculations (add, subtract, multiply, divide)
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "calculate"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:58-93 (handler)The calculate tool handler: validates args, performs arithmetic operation (add, subtract, multiply, divide), and returns formatted result as text content.
async (args: { [x: string]: any }) => { const { operation, a, b }: CalculateArgs = validateToolArgs( CalculateSchema, args ); let result: number; switch (operation) { case 'add': result = a + b; break; case 'subtract': result = a - b; break; case 'multiply': result = a * b; break; case 'divide': result = a / b; break; default: // This should never happen due to Zod validation, but keeping for safety throw new Error( `Unknown operation: ${operation}. Supported: add, subtract, multiply, divide` ); } return { content: [ { type: 'text', text: `${a} ${operation} ${b} = ${result}`, }, ], }; } - src/schemas/toolSchemas.ts:20-43 (schema)Zod schema defining CalculateArgs: operation enum, numbers a/b, with refinement to prevent division by zero.
export const CalculateSchema = z .object({ operation: z .enum(['add', 'subtract', 'multiply', 'divide'], { errorMap: () => ({ message: 'Operation must be one of: add, subtract, multiply, divide', }), }) .describe('The arithmetic operation to perform'), a: z.number().describe('First number for the calculation'), b: z.number().describe('Second number for the calculation'), }) .refine( data => { if (data.operation === 'divide' && data.b === 0) { return false; } return true; }, { message: 'Division by zero is not allowed', path: ['b'], } ); - src/server.ts:51-94 (registration)Registers the 'calculate' tool on the MCP server with title, description, and handler function.
server.registerTool( 'calculate', { title: 'Calculate', description: 'Performs basic arithmetic calculations (add, subtract, multiply, divide)', }, async (args: { [x: string]: any }) => { const { operation, a, b }: CalculateArgs = validateToolArgs( CalculateSchema, args ); let result: number; switch (operation) { case 'add': result = a + b; break; case 'subtract': result = a - b; break; case 'multiply': result = a * b; break; case 'divide': result = a / b; break; default: // This should never happen due to Zod validation, but keeping for safety throw new Error( `Unknown operation: ${operation}. Supported: add, subtract, multiply, divide` ); } return { content: [ { type: 'text', text: `${a} ${operation} ${b} = ${result}`, }, ], }; } ); - src/schemas/toolSchemas.ts:65-77 (helper)Utility function to validate tool arguments using Zod schema with custom error formatting, used in calculate handler.
export function validateToolArgs<T>(schema: z.ZodSchema<T>, args: unknown): T { try { return schema.parse(args); } catch (error) { if (error instanceof z.ZodError) { const errorMessages = error.errors .map(err => `${err.path.join('.')}: ${err.message}`) .join(', '); throw new Error(`Validation failed: ${errorMessages}`); } throw error; } }