DEV Community

RecallRun
RecallRun

Posted on Originally published at recallrun.dev

A fail-closed tool gate for AI agents: a denylist let 25 of 29 bad calls through, an allowlist 0

Tested on: 2 vCPU GitHub runner, Python 3.12.16, pytest 9.2.0, standard library only. 33 hand-written tool calls (4 legitimate, 29 that should be denied) against a 6-tool registry. No LLM and no real tools were called, so this tests the gate logic, not whether a model would try these calls. The test set is mine, written alongside the gate, so 0 failures is expected; it shows the design handles these classes, not that it is complete. Not tested: real MCP servers, concurrency, approval UX.

An agent calls tools. Some tools only read; some change things. How do you stop a write from happening that nobody approved?

Short answer: don't block what looks dangerous. Allow only what you have listed, check the arguments, and deny on any error. On 33 test calls, a denylist on tool names let 25 of the 29 bad ones through. A fail-closed gate let 0 through and blocked none of the 4 legitimate reads.

Why a denylist fails

A denylist asks "does this name look destructive?" Names like delete_user get caught. purge_customer, transfer_funds and update_order_status don't contain the words you thought of. Names also arrive in forms you didn't plan for: trailing whitespace, zero-width characters, fullwidth Unicode.

Then there are tools whose name is fine and whose arguments are the attack. A run_sql_readonly tool is only read-only if something checks the SQL.

MCP tool annotations such as readOnlyHint and destructiveHint don't solve this. Per secondary sources I found in a search on 2026-11-20, the spec says clients must treat annotations as untrusted unless the server is trusted, and a server can mislabel a destructive tool. Use the hints for approval prompts. Enforce on the client side with your own registry. For the wider MCP picture see MCP clients & security.

The gate

Four rules, in gate.py (full file linked at the end):

  1. Exact registry match. The name must equal a registered name byte for byte after Unicode normalization. Anything else is denied, including tools from a new MCP server nobody has reviewed.
  2. Per-tool argument validation. SQL must be a single SELECT. Paths must sit under /data/public/ with no ... Refunds are capped at 100. Emails go only to @example.com.
  3. Writes need two things: the session is in read_write mode, and a human approved this call.
  4. Any exception means deny. Bad types, missing keys and parse errors all return False.
def closed_gate(name, args, approved=False, mode="read_only"):
    try:
        if not isinstance(name, str) or name != norm(name).strip():
            return False
        tool = REGISTRY.get(name)
        if tool is None:
            return False
        if not isinstance(args, dict) or not tool.validate(args):
            return False
        if tool.effect == "write":
            return mode == "read_write" and approved and tool.needs_approval
        return True
    except Exception:
        return False
Enter fullscreen mode Exit fullscreen mode

Results

The naive gate blocks names containing delete, drop, remove or rm. "Unsafe" means a call that should have been denied was allowed.

Category Calls Naive unsafe Closed unsafe
Legitimate reads 4 0 0
Write in read-only mode 2 2 0
Obvious destructive name 3 0 0
Unlisted destructive name 5 5 0
Case variant 2 1 0
Whitespace variant 1 1 0
Zero-width variant 1 1 0
Fullwidth Unicode variant 1 1 0
Allowed tool, hostile arguments 8 8 0
Malformed call 4 4 0
New unreviewed MCP tool 2 2 0
Total 33 25 0

25 of 29 calls that should be denied got through the denylist (86%). The 4 it caught were the three obvious names plus DELETE_USER. It never saw a write tool it hadn't been told about, such as Refund_Order or purge_customer.

In read_write mode, an approved $20 refund passed, an unapproved one was denied, and an approved $500 refund was denied by the argument cap.

What the numbers mean

The denylist's failures are not exotic. Eight of the 25 are plain tool names with hostile arguments, and five are destructive tools with names nobody listed. The allowlist wins because it inverts the default: unknown means no.

Caveats:

  • I wrote the cases and the gate together. The closed gate scoring 0 unsafe is expected. A fresh attacker or a fresh tool shape may find a hole, so treat this as a regression suite you extend, not a proof.
  • The "malformed call" rows count the naive gate as unsafe for a None name only because my harness passes it through as allowed; a real naive gate would more likely crash. Remove those 4 and the denylist still let through 21 of 25.
  • Regex-checking SQL is weak. In production, use a database role with read-only grants, and let the gate be the second layer.
  • Argument validators are per-tool work. That cost is the price of the guarantee.

Rule of thumb

Register every tool by exact name, tag it read or write, validate its arguments, and require approval for writes. Deny on unknown names, bad arguments and any exception. Put the same cases in a test suite (test_gate.py, 35 tests) so a new tool can't ship without a registry entry. Related: prompt injection through documents, which is how a model gets talked into these calls, and guardrails. The basics of tool-using agents are in AI Engineering: agents.


The gate, the 33 test calls and test_gate.py are in the experiment files on recallrun.dev.

Top comments (2)

Collapse
 
jsbry profile image
Joshua Bryan •

On "only read-only if something enforces it": we've watched the enforcement itself fail open. GitHub's MCP server reads GITHUB_READ_ONLY with Go's ParseBool, so =yes silently leaves read-only off (github/github-mcp-server#3747). Several database servers enforce read-only by keyword-filtering the SQL, which comments and multi-statement tricks get past (GHSA-mwwr-p57h-56pf). Your rule covers both: enforce it at the source, with a read-only role or token, then list the tools after switching it on and check it took.

Collapse
 
recallrundev profile image
RecallRun •

Good examples, and they hit a weak spot in my own gate. Rule 2 checks that the SQL is a single SELECT, which is the same string-level layer you're describing getting bypassed. In the post that should be the second check, not the guarantee. The guarantee is the database refusing writes: give the tool a role with only SELECT grants, so a comment trick or a second statement fails at the server whatever the gate thought.

Listing the tools after turning read-only on is a good cheap test too. I didn't run real MCP servers in this test, and that check would catch a flag that parsed the wrong way without needing anything else.