Text size
Dark mode
RecallRunGuides & benchmarks
Learn to build real systemsGuides•Benchmarks•Experiments•Community
Search the site/Free checklist

Blog/agents

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

I ran 33 agent tool calls through a name denylist and a fail-closed allowlist gate. The denylist let 25 of 29 bad calls through; the allowlist let 0 through.

In this post

What this post covers
  1. Why a denylist fails
  2. The gate
  3. Results
  4. What the numbers mean
  5. Rule of thumb
How this was testedhardware, versions, data

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 (see the Experiment files box):

  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

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.

Learn it step by step · free

Go deeper in the RecallRun guides

All guides →

Experiment files

The exact script and raw output behind every number in this post. Download and rerun them.

Get the free RAG production checklist

25 checks to run before real users see your RAG feature: exact-match misses, invented citations, prompt injection through documents, quality that slips after a prompt change. Confirm your email and the PDF comes with the welcome email, plus each new experiment.

One email to confirm. Unsubscribe any time. What's in it · Privacy

Want the production version of this? See ShipRAG: A production RAG + MCP starter kit for FastAPI: hybrid search, validated citations, evals in CI, Docker, 32 tests.

Written and measured by

Alby Tomy

Python/FastAPI Engineer | Data Engineer | AI Engineer building RAG and MCP systems.

X · GitHub · Contact & enquiries