Skip to main content
Glama

batch_calculate

Process multiple calculations in one request using the specified operation (add, subtract, multiply, divide) for efficient batch computation.

Instructions

Perform multiple calculations in a single request

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
calculationsYes

Implementation Reference

  • The main handler function for the batch_calculate tool. It processes an array of calculations, executes each one individually using helper functions, catches errors per calculation to avoid failing the entire batch, logs to history, and returns both a text summary and structured results array.
    async ({ calculations }) => { log.info(`Executing batch calculations: ${calculations.length} operations`); requestCount++; const results = []; for (const calc of calculations) { try { const result = performBasicCalculation(calc.op, calc.a, calc.b); const historyEntry = createHistoryEntry(calc.op, calc.a, calc.b, result); addToHistory(historyEntry); results.push({ success: true as const, expression: historyEntry.expression, value: result, calculationId: historyEntry.id, }); } catch (error) { // NOTE: This is an example of APPLICATION-LEVEL error handling. // Instead of throwing and failing the entire batch (a protocol-level error), // we catch the error for a single item. The tool itself still succeeds, // but its structured output contains the specific error information. // This gives the client detailed feedback on a per-item basis. results.push({ success: false as const, expression: `${calc.a} ${calc.op} ${calc.b}`, error: error instanceof Error ? error.message : String(error), }); } } return { content: [ { type: 'text', text: results .map((r) => (r.success ? r.expression : `Error in ${r.expression}: ${r.error}`)) .join('\n'), }, ], structuredContent: { results }, }; },
  • Zod input schema for array of calculations (each with a, b, op, min 1 max batch size) and output schema for array of discriminated union results (success with value/expression/id or failure with error).
    const batchCalculateInputSchema = { calculations: z .array( z.object({ a: z.number(), b: z.number(), op: z.enum(['add', 'subtract', 'multiply', 'divide']), }), ) .min(1) .max(LIMITS.maxBatchSize), }; const batchCalculateOutputSchema = { results: z.array( z.discriminatedUnion('success', [ z.object({ success: z.literal(true), expression: z.string(), value: z.number(), calculationId: z.string(), }), z.object({ success: z.literal(false), expression: z.string(), error: z.string(), }), ]), ), };
  • src/server.ts:400-451 (registration)
    Registers the batch_calculate tool on the MCP server with name, title, description, schemas, and inline handler function.
    server.registerTool( 'batch_calculate', { title: 'Batch Calculate', description: 'Perform multiple calculations in a single request', inputSchema: batchCalculateInputSchema, outputSchema: batchCalculateOutputSchema, }, async ({ calculations }) => { log.info(`Executing batch calculations: ${calculations.length} operations`); requestCount++; const results = []; for (const calc of calculations) { try { const result = performBasicCalculation(calc.op, calc.a, calc.b); const historyEntry = createHistoryEntry(calc.op, calc.a, calc.b, result); addToHistory(historyEntry); results.push({ success: true as const, expression: historyEntry.expression, value: result, calculationId: historyEntry.id, }); } catch (error) { // NOTE: This is an example of APPLICATION-LEVEL error handling. // Instead of throwing and failing the entire batch (a protocol-level error), // we catch the error for a single item. The tool itself still succeeds, // but its structured output contains the specific error information. // This gives the client detailed feedback on a per-item basis. results.push({ success: false as const, expression: `${calc.a} ${calc.op} ${calc.b}`, error: error instanceof Error ? error.message : String(error), }); } } return { content: [ { type: 'text', text: results .map((r) => (r.success ? r.expression : `Error in ${r.expression}: ${r.error}`)) .join('\n'), }, ], structuredContent: { results }, }; }, );
  • Helper function that performs the basic arithmetic operations (add, subtract, multiply, divide) and throws McpError for invalid op or divide by zero. Used by the batch_calculate handler.
    function performBasicCalculation(op: string, a: number, b: number): number { switch (op) { case 'add': return a + b; case 'subtract': return a - b; case 'multiply': return a * b; case 'divide': if (b === 0) throw new McpError(ErrorCode.InvalidParams, 'Division by zero'); return a / b; default: throw new McpError(ErrorCode.InvalidParams, `Unknown operation: ${op}`); } }
  • Helper function to create a history entry object with generated ID, timestamp, and formatted expression for each calculation. Used in batch_calculate.
    function createHistoryEntry( operation: string, a: number, b: number | undefined, result: number, ): HistoryEntry { const id = Math.random().toString(36).substring(7); const expression = formatExpression(operation, a, b, result); return { id, timestamp: new Date().toISOString(), operation: operation, input_1: a, input_2: b, result, expression, }; }

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/yigitkonur/example-mcp-server-stdio'

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