calc_eval
Evaluate mathematical expressions with configurable decimal precision. Perform accurate arithmetic computations for calculations and problem-solving.
Instructions
Alias of calc.eval
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expr | Yes | ||
| precision | No |
Implementation Reference
- src/tools/calc.ts:20-33 (handler)The main handler function `calcEval` that evaluates a math expression using mathjs. It normalizes exponent notation (**, mũ, lũy thừa -> ^), evaluates the expression, and optionally formats the result with configurable precision (capped at 0-20). Returns {ok, result} on success or {ok, error} on failure.
export async function calcEval(expr: string, precision?: number) { try { const norm = normalizeExponent(expr); const res = math.evaluate(norm); let out = String(res); if (typeof res === 'number' && Number.isFinite(res) && typeof precision === 'number') { const p = Math.max(0, Math.min(precision, 20)); out = res.toFixed(p); } return { ok: true, result: out }; } catch (e: any) { return { ok: false, error: e?.message || 'calc error' }; } } - src/tools/calc.ts:5-17 (helper)Helper function `normalizeExponent` that normalizes various exponent notations (Python **, Vietnamese 'mũ'/'lũy thừa') into the caret ^ operator used by mathjs.
function normalizeExponent(s: string): string { let t = s; // Python style t = t.replace(/\*\*/g, '^'); // "mũ", "mu" (viết không dấu), "lũy thừa" // ví dụ: "5 mũ 10" -> "5 ^ 10" t = t.replace(/\s*(m[ũu]|lu[ũu]y\s*thừa)\s*/gi, ' ^ '); // tuỳ chọn: gom nhiều khoảng trắng t = t.replace(/\s+/g, ' ').trim(); return t; - src/server.ts:29-36 (registration)Registration of the 'calc_eval' tool as an alias of 'calc.eval'. Uses Zod schema for input validation (expr: string, precision: optional integer 0-64), calls calcEval, and returns JSON-stringified result as text content.
server.tool('calc_eval', 'Alias of calc.eval', { expr: z.string(), precision: z.number().int().min(0).max(64).optional() }, OPEN, async ({ expr, precision }) => { const res = await calcEval(expr, precision); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } ); - src/server.ts:30-30 (schema)Input schema for calc_eval: 'expr' (required string) and 'precision' (optional integer, min 0, max 64).
{ expr: z.string(), precision: z.number().int().min(0).max(64).optional() },