Back to Hub

Securing MCP tool calls: what I built and what I learned

What it does

AI assistants can now do things, such as reading files and sending messages, using add-on tools that other people write. This project sits between the two. You list what's allowed, and it checks every tool call against that list before letting it through.

The gateway sits between an AI assistant and its tools, runs four checks, then allows or blocks the request
Where the gateway sits.

The problem

The Model Context Protocol lets an AI agent use tools served by other programs. The agent reads each tool's description to decide what to do, which means a server can write instructions straight into the model's context. It can also change a description after you have approved it, claim the name of a tool you already trust, or return output that tries to redirect the agent.

I wanted something that sits between the two and decides what the agent may see and do, without needing changes to either side.

How it works

The gateway speaks MCP on both sides, over stdio or Streamable HTTP, and denies anything it was not told to permit.

  • Policy. A YAML file lists the allowed tools and rules for each argument: types, patterns, lengths and the directories a path may point into.
  • Pinning. The first approved definition of each tool is hashed. If it changes, calls stop until it is approved again.
  • Scanning. Descriptions and outputs are checked for injected instructions, invisible characters and encoded text.
  • Limits and approval. Rate limits, credential blocking, optional human approval, and an audit log with redaction.
Demo recording: seven attacks blocked, the audit log, and each defence switched off in turn
Made from the output of the demo and the defence check. The demo uses a real MCP client and a test server that behaves maliciously.

Results

CheckResult
Automated tests126 passing
Defences disabled one at a time44 of 44 make a test fail
Added latency, median / 99th percentile0.22 ms / 0.50 ms (trivial server, Ryzen 9 7950X)
Scanner: attack samples flagged21 of 32 (66%)
Scanner: harmless samples wrongly flagged6 of 40 (15%), mostly deliberately hard cases

The scanner numbers need some context. The rules and the samples were written together, so these figures overstate how it would fare against a stranger. Even so it caught none of the attacks that were paraphrased, written in another language, or disguised with spacing and look-alike letters.

What I learned

The first version of this project passed all its tests and was wrong in ways the tests could not see. Its demo showed a poisoned tool being blocked. The scanner had never detected it: its pattern expected "ignore previous instructions" and the sample said "ignore all previous instructions". The tool disappeared only because it was missing from the allowlist. Sending requests as a JSON-RPC batch skipped the policy altogether, and a path such as /home/../etc/passwd passed the directory check.

So the rebuild started with tests that describe what the gateway must guarantee, run against the real process, and fail before the fix. The tests themselves were then checked by removing one defence at a time. The first pass found two with nothing guarding them.

That still was not enough. An automated security review of the rebuilt code found nine more problems that every test and every disabled-defence check had passed over. A server could switch off argument validation with one keyword in its schema. A human approver was shown redacted arguments, not the ones about to be sent. Injected text could travel in fields the scanner never read. Each one now has a test that failed before the fix and passes after it. Tests only cover the attacks someone has thought of, so it is worth having another reviewer look.

For agent security more broadly, pattern matching only catches the obvious attacks. The real protection comes from simpler things: a short allowlist, strict argument rules and refusing anything unexpected.

Disclosure: this project was written with AI coding agents, including its tests and sample attacks. The first version came from one set of agents; an audit with another found the problems above and led to the rebuild.

What's next

Testing against widely used third-party MCP servers, measuring the scanner on attacks written by other people, and per-item rules for resources and prompts. The gateway also cannot stop a malicious local server from acting on its own, so untrusted servers still need a sandbox.

Source code