Skip to main content
Glama
dhinojosac

TypeScript MCP Server Template

by dhinojosac

calculate

Perform basic arithmetic operations such as addition, subtraction, multiplication, and division using this utility tool on the TypeScript MCP Server Template.

Instructions

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

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The asynchronous handler function for the 'calculate' tool that validates arguments using Zod, performs the specified arithmetic operation (add, subtract, multiply, divide), and returns the 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/server.ts:51-57 (registration)
    Registers the 'calculate' tool on the MCP server with its name, title, and description.
    server.registerTool( 'calculate', { title: 'Calculate', description: 'Performs basic arithmetic calculations (add, subtract, multiply, divide)', },
  • Zod schema defining and validating the input arguments for the 'calculate' tool: operation (enum: add/subtract/multiply/divide), a (number), b (number), with a 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'], } );
  • Utility helper function to validate any tool arguments against a Zod schema, providing formatted error messages on validation failure. Used in the 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; } }

Other Tools

Related Tools

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