All projects

smcp-gateway

A security gateway for MCP tool calls, built from a paper that shipped no code, and tested with real LLM agents in the agent's seat.

Role
Author
When
Oct 2026 - Present
Status
v0.3.0, alpha
Context
Implementation of arXiv:2602.01129
Stack
  • Python
  • MCP
  • Ed25519
  • Streamable HTTP
  • pytest
  • GitHub Actions

The problem

MCP lets an LLM agent call tools on real systems. It reads files, queries databases and sends email. The SMCP paper (Hou, Wang et al., Feb 2026) proposes five security layers for it: a trusted registry, mutual authentication, a security context on every call, a policy engine and a signed audit log. It describes them with JSON examples. It ships no code and runs no experiments, so there was no way to know what the design actually stops.

I built all five layers as a gateway that sits in front of any unmodified MCP server, over stdio or Streamable HTTP. Then I attacked it, first with the paper's own list of 26 MCP risks, then with real LLM agents.

How it works

  1. 1

    Registry

    A YAML registry of agents, servers and tools. Each tool's name, description and input schema is pinned by SHA-256, so a lookalike or rug-pulled tool is hidden from the agent.

  2. 2

    Handshake

    Agent and gateway trade Ed25519-signed hellos and derive a session key with X25519. Every call then carries an HMAC and a strictly increasing sequence number, so a forged or replayed call is refused.

  3. 3

    Security context

    Built from delegation tokens the gateway checks itself. It ignores any chain the agent asserts, and scopes can only narrow along the chain.

  4. 4

    Policy

    Built-in checks plus YAML rules, where deny wins. Obligations redact secrets and fields and cap rows before a result reaches the agent.

  5. 5

    Extensions

    Argument guards, Ed25519-signed user approval, session flow labels and tool-description scanning. Each one is off unless configured, and each one faces variants written to beat it.

  6. 6

    Audit

    A hash-chained JSONL log with every entry signed, plus a signed checkpoint of its head, so a cut or edited log is caught.

Every SMCP field travels in MCP's existing _meta field. The gateway adds no JSON-RPC methods, and the server behind it never sees an SMCP field. Two checks use code I didn't write: the gateway runs in front of the official @modelcontextprotocol/server-filesystem with its 14 real tools, and the official MCP Inspector CLI drives it over Streamable HTTP.

The finding

The paper's design answers one question well: who is calling, on whose behalf, and may they call this tool. Forged identities, replayed sessions, typosquatted and rug-pulled tools and cross-tenant calls all get blocked.

A trusted caller doing something harmful gets through. The policy engine reads riskLevel from the calling agent, so a rule like "sensitive tools only at medium risk or below" denies an honest agent that declares high and permits a fooled agent that declares low. With real models, the paper's design stopped 1 of the 4 attacks that worked, and only because a redaction rule hid the key the model read.

The extensions take the risk level as the higher of what the agent declares and what the gateway assesses from the session, and they send high-stakes calls to the user for a signed approval.

What the real models did

  • With no gateway, gpt-oss-120b reached the attacker's goal in 20 of 35 attack runs and gpt-oss-20b in 19 of 35. Qwen 3.8 27B reached it in none of 35. It often named the planted instruction and refused it.
  • The HTML comment that says "ignore your previous instructions" fooled no model in 45 runs. The same request written as a plain business note fooled both gpt-oss models in every run with no gateway.
  • With the extensions on, gpt-oss-120b got around the email domain guard twice. It emailed an internal address and asked them to forward the customer list to the attacker. The flow labels caught it, because the email carried text from a confidential result.
  • Behind the gateway, the models tried the prose and ticket attacks about as often as with no gateway. The gateway stopped the calls. The models didn't get safer.

Decisions and tradeoffs

  • Approval is a signature over the exact call. The root user signs the session, tool and argument digest with Ed25519. It's single use, so a fooled agent can't replay an approval or move it to other arguments.
  • Rules score all 405 runs instead of a judge model. An attack counts only if its goal happened in a call that ran. Every transcript is published, and --rescore rebuilds each table without an API call.
  • Every extension faces variants written to beat it. A variant counts only if it works on the bare server first, and a row where one gets through is scored partial. That's where the 5 partial rows come from.
  • Hiding a poisoned tool has a cost. The scanner hid the poisoned weather tool, so the user's weather question went unanswered in all 15 runs. The repo reports that row next to the security numbers.

What broke

A security review of my own extensions found gaps, and I fixed them before v0.2.

  • A reused JSON-RPC request id let a second response skip redaction, flow labels and the gateway's signature. The gateway now tracks every forwarded request by id and drops a response nobody asked for.
  • Eight parallel connections against a session cap of three opened seven sessions. The cap is now checked and taken under one lock.
  • In Python, a guard pattern's $ also matched just before a final newline. Patterns now follow ECMA-262, and the bench has a trailing-newline variant for command injection.

Results

  • 39 to 0

    Attack runs, out of 105, where an open-weight LLM agent reached the attacker's goal, with no gateway vs. with the extensions

    docs/LLM_RESULTS.md, gpt-oss-120b, gpt-oss-20b and Qwen 3.8 27B on Groq, 405 runs in all. 12 of the stops rely on a simulated user who declines.

  • 33 of 105

    Attack runs that still reached the goal through the paper's own design

    Same run. It stopped only tool poisoning, through its redaction rule.

  • 10 to 18

    Of the paper's 26 MCP risks, blocked by the paper's design vs. with the extensions

    Scripted bench on a simulated server. With the extensions, 5 are partial, 1 is missed and 2 are not tested.

  • 30 of 30

    Normal tasks finished in every profile, with 15 approval prompts under the extensions

    docs/LLM_RESULTS.md

  • 0 of 45

    Runs where the textbook "ignore your previous instructions" comment fooled a model. The same ask as a business note fooled both gpt-oss models in every run with no gateway.

    docs/LLM_RESULTS.md, indirect-comment and indirect-prose

  • 1.4 ms

    Median cost per call through the gateway with every extension on, against 0.04 ms for a plain MCP call

    smcp-gateway bench, Apple M1, Python 3.12

  • 136

    Tests, one of which pins every bench outcome, run in CI on Python 3.10 to 3.12 and against the official filesystem server

    pytest collection, Oct 14 2026

I wrote both the attacks and the defenses, so this isn't an independent red team. The bench server is simulated, the approving user is simulated and careful, and the models are open-weight models on one provider on one day. With four or five trials per cell, a difference of one or two runs is noise.

Limits

Credential theft stays missed. With the agent's key, the attacker is the agent until someone revokes it. Argument guards are lexical, so a symlink inside an allowed root escapes them. The scanner looks for known markers, and text reworded to avoid them passes. The reworded variants fooled no model, so the real-model run doesn't test that gap. Streamable HTTP has no TLS, so bind it to loopback or put a TLS proxy in front.

Next project

minja-repro

An independent reimplementation of MINJA, a memory-poisoning attack on LLM agents that needs nothing but ordinary queries, plus the confidence intervals the paper left out.