Skip to main content
Glama
dhinojosac

TypeScript MCP Server Template

by dhinojosac

calculate

Perform basic arithmetic operations including addition, subtraction, multiplication, and division within the TypeScript MCP Server Template.

Instructions

Performs basic arithmetic calculations (add, subtract, multiply, divide)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • 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}`, }, ], }; }
  • 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}`, }, ], }; } );
  • 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; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dhinojosac/calendar-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server