Protect MCP servers
Wrap the MCP servers you already use with zn-gate, all at once (init) or one by one (shield).
zn-gate shield is a local proxy between your agent and an MCP server. For every message it:
- checks
tools/listresponses: tool titles and descriptions, and every text in a tool's schema (parameter names and descriptions, default, example and enum values, patterns), are checked for hidden instructions, and a tool that carries one is neutralized before the agent reads it; - checks tool-call arguments before they reach the server: a blocked call never runs;
- checks tool output before the agent reads it: secrets are redacted, and output that carries an injection is replaced by a short notice. Results are checked in full up to 32 MB;
- checks the other text the server sends to the agent: notifications, sampling requests, resource and prompt text, error messages (see "What the shield checks" below);
- records each decision in the local audit log.
All of it is local. Nothing is sent anywhere, in any configuration.
All servers at once: zn-gate init --wrap-mcp
init --wrap-mcp finds the MCP servers in your agent config files, writes a backup of each file it changes, and wraps each local (stdio) server with shield:
# Preview what would change
npx -y zn-gate init --dry-run
# Wrap in shadow mode first: log detections, change nothing in the traffic
npx -y zn-gate init --shadow
# Wrap and enforce
npx -y zn-gate init --wrap-mcp
# Restore the original configs from the latest backups
npx -y zn-gate init --revert
Start with --shadow, review the decisions (npx -y zn-gate evidence --tail 20), then enforce.
| Agent | File | Format |
|---|---|---|
| Claude Desktop | claude_desktop_config.json (macOS ~/Library/Application Support/Claude/, Windows %APPDATA%\Claude\, Linux ~/.config/Claude/) |
mcpServers JSON |
| Claude Code | ~/.claude.json (user scope) |
mcpServers JSON |
| Cursor | ~/.cursor/mcp.json |
mcpServers JSON |
| Codex CLI | ~/.codex/config.toml (or $CODEX_HOME/config.toml) |
[mcp_servers.<name>] TOML |
| OpenCode | ~/.config/opencode/opencode.json |
mcp JSON, command array |
| Antigravity, Hermes | ~/.gemini/antigravity/mcp_config.json, ~/.gemini/config/mcp_config.json, ~/.hermes/mcp.json |
mcpServers JSON (paths not yet verified against official docs) |
Remote (URL) servers are left as they are. init never rewrites a file it cannot round-trip safely: OpenCode files with comments and Codex entries with multi-line args are reported as "wrap manually".
One server: zn-gate shield
Put zn-gate in front of any MCP server command:
npx -y zn-gate shield -- uvx mcp-server-fetch
npx -y zn-gate shield -- npx -y @modelcontextprotocol/server-postgres postgresql://localhost/db
In a client config, replace the server command with npx and put -y zn-gate shield -- before the original command and arguments. To log without blocking, add --shadow before the --: npx -y zn-gate shield --shadow -- uvx mcp-server-fetch.
What the shield checks
The checks on server messages, tool schemas and large results below are available from zn-gate 1.14.0.
Tool lists
For every tool the server lists, the shield reads the title and the description, and every string in the input and output schema: parameter names and descriptions, default, const, enum and examples values (also inside nested objects and arrays), pattern, format, $ref and extension keys. A tool that carries an injection in any of them is neutralized as a whole. A schema that is too deep or too large to check (more than 32 levels or 5,000 entries) is not passed on unchecked either: the tool is neutralized, and zn-gate prints the name of the tool and the limit on stderr.
Everything else the server sends
The shield also checks the other text a server can put in front of the agent: notifications, requests such as sampling, the contents of resources and prompts (text blobs too), the descriptions in resource and prompt lists, the instructions the server sends when it starts, completion values, error messages, and the structured content, embedded resources and resource links of tool results. Text that carries an injection is replaced by a notice, and secrets are redacted. In structured content the keys are checked as well as the values.
- Each text is checked in full up to 32 MB, and blocked when it is larger.
- A message with many text fields is checked string by string up to 500 strings. The strings after that are checked together in chunks of up to 256 KB, so a large harmless result passes unchanged. Only a chunk that holds an injection or a secret is checked string by string, and only the strings that need it change. If one message holds more than 8 such chunks, the strings of the further ones are replaced by a notice.
- A line from the server that is not valid JSON is dropped instead of being passed on unchecked. In shadow mode it is passed on and reported.
- A line from the agent that is not valid JSON (for example one that contains
NaN) is not sent to the server either, because it cannot be checked (from 1.17.0). The agent gets a parse error when the request id can be read, and the line is dropped and logged otherwise. Shadow mode passes it on and reports it. - Messages keep the order the server sent them in.
- JSON-RPC batches are checked message by message, in both directions. A batch that contains a blocked request is not sent, and each request in it gets an answer.
Blocked text from a server message is logged at most 50 times per message. The first 50 blocked texts in one server message each get their own stderr line and audit log event. Every further blocked text in that message is still checked and handled the same way, but it is only counted. When the message is done, the shield writes one stderr line and one server-message:summary event that give the number of uncounted texts and a count per rule. In shadow mode the text is forwarded, so the log is the only record of what would have been blocked: past the 50th block in a message, it keeps only the per-rule count.
Large results and invisible text
Tool results are checked in full up to 32 MB. A larger result is removed, and the agent gets a notice that asks for a smaller part (a page or a line range). The audit log records the rule size:truncated.
Output that is mostly invisible characters, such as zero-width spaces, is blocked at once, with the rule size:invisible. The shield counts those characters over the whole result, including embedded resources and structured content, so splitting them across many small items does not avoid the limit.
In shadow mode, both cases are only recorded.
When the wrapped program cannot start
If the program after -- is not installed or not on your PATH (for example uvx without uv), zn-gate shield says so in plain words on stderr: it names the program and, for uvx, npx, node, docker and python, points to where to install it. If the file exists but is not allowed to run, it says that instead. Fix it and restart your editor. When your agent closes its input, the shield stops the wrapped server instead of leaving it running.
Tool pinning
Available from zn-gate 1.8.0.
A server can change a tool after you started using it: a new description that tells the agent to send files somewhere, or a new parameter. Tool pinning catches that. The first time zn-gate shield sees a server's tools, it records a fingerprint (a SHA-256 hash) of each tool definition. Every later tool list is compared with those fingerprints.
What you see when a tool changes:
warn(the default): the tool stays usable, but its description starts with a notice that tells the agent to ask you before using it. The shield also writes[zn-shield TOOLS CHANGED]to the MCP server's log (stderr, which your agent app keeps, not your terminal) and records the event in the local audit log. This relies on the agent reading the notice.block: the changed or new tool is removed from the list, and calls to any tool that is not approved are refused, even when the agent calls it from a list it saved earlier. Use this mode if you want a hard stop.
Tools with a duplicated name, or without a valid name, are flagged in both modes. They cannot be approved, so the server has to fix its tool list. In block mode they are removed.
Set the mode in ~/.zn/config.json:
{ "mcpPinning": "block" }
or for one run, with the environment variable ZN_MCP_PINNING=block. The modes are off, warn (default) and block. The mode is the strictest of your config file and the environment. The environment of an MCP server can make it stricter, never weaker. Only "mcpPinning": "off" in your own ~/.zn/config.json turns pinning off.
With --shadow, pinning only logs: it prints the warning and records the event, and it does not remove, mark or refuse any tool.
Approving a change
# List the pinned servers and the tools that changed since they were approved
npx -y zn-gate pins
# Accept the changes of one server (use the key that `pins` prints)
npx -y zn-gate pins approve <server-key>
# Accept the changes of every server
npx -y zn-gate pins approve --all
Approval runs only in your own terminal: zn-gate asks you to type the server key (or all) again. Your agent cannot approve: the zn-gate agent hooks refuse that command. Before you approve, look at the changed tool in your agent or in the server's source. pins shows tool names, not their text, and approval accepts the tools as the shield last saw them; if a tool changes again later, it is flagged again.
What is stored
Pins live in ~/.zn/mcp-pins/, one file per server, readable only by your account on Linux and macOS. Each file holds the program name, its arguments with secrets removed, the tool names and their fingerprints. It never holds tool text. A server's key comes from its command line, so a changed command line (for example a new package version) is a new server, and its first list is trusted again.
Limits
- Pinning covers tool definitions: names, descriptions and parameters. It does not check the server's code.
- The first list a server returns is trusted. If that first list was already harmful, pinning will not catch it.
- A server started as
npx some-tool@latestcan change its code while its tool list stays the same, and pinning will not notice. Pin the package version in your MCP configuration, for examplenpx -y some-tool@1.4.2, and review each new version. - An agent that can run commands on your machine could edit the pin files or start a server without the shield. The hooks stop the commands they recognize, not every possible one.
- Pinning protects only servers that run through
zn-gate shield(seeinitabove).
Keep tool access narrow
zn-gate checks what goes in and out of a tool; it does not decide which tools an agent should have. After wrapping, review each server and keep only the tools your agent needs. Put destructive operations behind explicit approval.