calculate
Evaluate any mathematical expression from basic arithmetic to advanced functions including trigonometry, matrices, unit conversions, complex numbers, and statistics.
Instructions
Evaluate a mathematical expression using MathJS. Supports arithmetic, algebra, trigonometry, matrices, units, complex numbers, statistics and all other MathJS built-in functions.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expression | Yes | Math expression to evaluate, e.g. "sin(pi/4)", "2 km to mile", "det([1,2;3,4])" |
Implementation Reference
- src/index.ts:87-100 (handler)The async handler function for the 'calculate' tool. It calls evaluate(expression) and returns the result as text content, or an error message with isError flag.
async ({ expression }) => { try { const text = await evaluate(expression); return { content: [{ type: 'text', text }], }; } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); return { content: [{ type: 'text', text: `Error: ${message}` }], isError: true, }; } }, - src/index.ts:78-85 (schema)Input schema for the 'calculate' tool. Defines a single 'expression' string parameter (min length 1) with a description of supported MathJS features.
inputSchema: { expression: z .string() .min(1) .describe( 'Math expression to evaluate, e.g. "sin(pi/4)", "2 km to mile", "det([1,2;3,4])"', ), }, - src/index.ts:73-101 (registration)Registration of the tool named 'calculate' using server.registerTool() with its schema and handler.
server.registerTool( 'calculate', { description: 'Evaluate a mathematical expression using MathJS. Supports arithmetic, algebra, trigonometry, matrices, units, complex numbers, statistics and all other MathJS built-in functions.', inputSchema: { expression: z .string() .min(1) .describe( 'Math expression to evaluate, e.g. "sin(pi/4)", "2 km to mile", "det([1,2;3,4])"', ), }, }, async ({ expression }) => { try { const text = await evaluate(expression); return { content: [{ type: 'text', text }], }; } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); return { content: [{ type: 'text', text: `Error: ${message}` }], isError: true, }; } }, ); - src/index.ts:36-63 (helper)The evaluate() helper function that runs math.evaluate() inside a Worker thread with a timeout. Used by the calculate handler to safely compute expressions.
function evaluate(expression: string): Promise<string> { return new Promise<string>((resolve, reject) => { const worker = new Worker(WORKER_PATH, { workerData: { expression, maxMatrixSize: MAX_MATRIX_SIZE }, execArgv: isTsNode ? ['-r', 'ts-node/register'] : [], }); const timer = setTimeout(() => { void worker.terminate(); reject(new Error(`Calculation timed out after ${TIMEOUT_MS} ms`)); }, TIMEOUT_MS); worker.once('message', (msg: WorkerMessage) => { clearTimeout(timer); if (msg.ok) { resolve(msg.value); } else { reject(new Error(msg.error)); } }); worker.once('error', (err) => { clearTimeout(timer); void worker.terminate(); reject(err); }); }); } - src/worker.ts:31-40 (helper)The worker thread implementation that actually calls math.evaluate(expression) and checks matrix size limits before posting the result back.
const { expression, maxMatrixSize } = workerData as WorkerInput; try { const result = math.evaluate(expression); checkSize(result, maxMatrixSize); parentPort!.postMessage({ ok: true, value: math.format(result, { precision: 14 }) }); } catch (err: unknown) { const message = err instanceof Error ? err.message : String(err); parentPort!.postMessage({ ok: false, error: message }); }