Skip to main content
Glama

calc.eval

Evaluate mathematical expressions locally to perform calculations without external dependencies. Supports precision control for accurate results.

Instructions

Evaluate math expression (no external calls).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
exprYes
precisionNo

Implementation Reference

  • Core implementation of calc.eval: normalizes expression (e.g., exponents), evaluates using mathjs, handles precision and errors.
    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/server.ts:20-28 (registration)
    Registration of the 'calc.eval' tool with schema, description, and inline handler wrapper calling calcEval.
    // ===== calc.eval + alias ===== server.tool('calc.eval', 'Evaluate math expression (no external calls).', { 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) }] }; } );
  • Helper function to normalize exponent notation in Vietnamese and Python-style (** -> ^).
    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)
    Alias registration for 'calc_eval' using the same schema and handler as 'calc.eval'.
    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) }] }; } );

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/khanhs-234/tool4lm'

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