Skip to main content
Glama

batch_calculate

Perform multiple arithmetic calculations in a single request to process addition, subtraction, multiplication, and division operations efficiently.

Instructions

Perform multiple calculations in a single request

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
calculationsYes

Implementation Reference

  • The handler function for the 'batch_calculate' tool. It processes multiple calculations in batch, calling performBasicCalculation for each, creating history entries, handling individual errors without failing the entire batch, and returning structured results with both text content and detailed 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 schemas defining the input (array of calculations with a, b, op, limited size) and output (array of results, each either success with value/expression/id or failure with error) for the batch_calculate tool.
    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)
    The server.registerTool call that registers the 'batch_calculate' tool, specifying its name, title, description, input/output schemas, and the 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 }, }; }, );

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