DEV Community

Renato Marinho
Renato Marinho

Posted on

Stop letting LLMs guess your math: Why eval() is a death sentence for agents

I watched an agent attempt to solve (15 + 4) * 2 / sqrt(9) the other day. It didn't fail because it couldn't do the math. It failed because it tried to simulate the order of operations mentally, hallucinated the intermediate step, and gave me a confidently incorrect answer.

If you are building agentic workflows, you eventually hit this wall: LLMs are incredible linguistic engines but probabilistic calculators. They don't "calculate"—they predict the next most likely character in a sequence representing a calculation. When the expression gets non-trivial, probability fails precision.

The lazy fix? Passing the string to a JavaScript eval() call or a Python exec().

That is a catastrophic mistake.

If you expose an LLM to a runtime via eval(), you aren't just giving it a calculator; you are handing over Remote Code Execution (RCE). If a user manages to prompt the model to evaluate something like process.exit() or attempts to scrape local environment variables through injected code, your entire infrastructure becomes a liability. Most developers skip this realization until they see an unauthorized shell command executing in their logs.

This is exactly why we built the Deterministic Math Expression Evaluator.

Beyond Prompt Engineering

You can spend hours refining system prompts, telling the LLM to "think step by step" or "use PEMDAS." You can even implement few-shot prompting with dozens of examples. None of it works as well as moving the heavy lifting from the transformer layers to a dedicated execution unit.

The Deterministic Math Expression Evaluator doesn't rely on the LLM's internal weights to decide if multiplication comes before addition. Instead, it uses a pure Recursive Descent Parser to build an Abstract Syntax Tree (AST).

When you send an expression like (15 + 5) * 2^3 through this MCP tool, here is what actually happens:

  1. Lexical Analysis: The string is broken down into tokens.
  2. Parsing: The Lexer builds an AST based on strict mathematical grammar.
  3. Evaluation: The tree is traversed deterministically according to precedence rules.
  4. Result: A precise numerical value is returned.

The LLM isn't doing math anymore; it’s acting as the interface that decides when to call the specialized tool. This separation of concerns transforms the agent from a shaky mathematician into an efficient orchestrator.

Security via Architecture, Not Filters

A common question I get when talking about agent tools is: "Can't we just sanitize the input?"\string["Can't we just sanitize the input?"]\redacted text replacement below...\r much easier said than done at scale. Regex filters for malicious keywords are notoriously easy to bypass with obfuscation techniques.\r
The architecture here solves this at the structural level. Because we use an AST parser rather than evaluating code within a runtime context, malicious payloads like process.exit() simply fail during lexical analysis. There is no interpreter seeing those commands and thinking "this looks like code I should run." To our parser, process is just an unrecognized identifier that violates the mathematical grammar ruleset.\r

The debugger scan for this server returns an A+ grade with a score of 100 precisely because there is zero risk of instruction leakage or unexpected side effects during evaluation.\r

Capabilities & Limitations\r

\r
You get several built-in functions out of the box:\r
sqrt, abs, sin, cos, tan, log, exp, round, ceil,\r
and floor.\r\
Please note: while it handles deeply nested parentheses perfectly due to its recursive nature, it remains a focused mathematical engine—it won't perform matrix transformations or handle complex integrals itself unless paired with more advanced modules like our Matrix Operations Engine or Wolfram Alpha integration options available on Vinkius.\r\
to keep things lean and fast (average latency around 855ms), we stripped away any unnecessary dependencies so it stays lightweight within any MCP host like Claude or Cursor.\r\
\r
The goal shouldn't be making models smarter at everything; it should be making them better at knowing when they are out of their depth and calling for help.


MCPs are the music of AI Agents. We built the catalog. Discover Vinkius MCP Catalog.

Top comments (0)