The MCP Gateway Security Guide: Why Your AI Agents Need a Guardrail
AI coding assistants have evolved from autocomplete tools to autonomous agents that can read your database schema, write files to disk, query your cloud APIs, and execute shell commands. The Model Context Protocol (MCP) standardizes how these agents connect to servers — but it does not, by itself, provide any security controls.
When you connect Claude Code or Cursor directly to an MCP server, that agent has the same level of access as the server process itself. If the server has credentials to your production database, the agent can query it. If the server can write to your filesystem, the agent can delete files. There is no authentication per-tool-call, no policy engine, and no audit log.
This is where an MCP Gateway comes in. Think of it as a reverse proxy that sits between your AI agents and every MCP server they talk to. Every tool call passes through the gateway, which authenticates the caller, evaluates the request against policies, validates the target, logs the operation, and optionally queues it for human approval.
The Threat Model
Before designing controls, it helps to enumerate what can go wrong when an AI agent has unfettered MCP access:
- SSRF (Server-Side Request Forgery). An agent could be tricked into calling an MCP tool that fetches a URL — and that URL targets internal infrastructure (169.254.169.254 for cloud metadata, 10.x internal IPs, or localhost services).
- Privilege escalation. An agent with database access writes a new admin user. An agent with filesystem access reads SSH keys. An agent with shell access installs a reverse shell.
- Data exfiltration. An agent reads your entire customer database and returns it in its response. No alert fires because there is no audit trail.
- Prompt injection. A malicious website or document in the agent's context tricks it into calling a destructive tool — and the tool call goes through because there is no approval gate.
- Accidental destruction. Even without malice, an agent interpreting a vague instruction could drop a production table. No undo button exists.
How an MCP Gateway Mitigates Each Risk
1. SSRF Protection
The gateway intercepts every URL an agent's tool call would connect to and validates it against a blocklist before the connection is made. Common patterns:
- Deny private IP ranges: 10.x, 172.16-31.x, 192.168.x, 127.x
- Deny cloud metadata endpoints: 169.254.169.254, metadata.google.internal, 100.100.100.200
- Deny internal TLDs: .local, .internal
- Optionally allowlist specific external domains only
2. Per-Tool Policy Enforcement
Not all tools in an MCP server are created equal. A db.query() tool that accepts arbitrary SQL is far riskier than a db.lookup_user() tool with a fixed query. The gateway assigns a policy to each tool:
- allow — unrestricted access
- block — denied with a reason returned to the agent
- readonly — the gateway introspects the call and blocks any operation matching write keywords (INSERT, UPDATE, DELETE, DROP, etc.)
- require-approval — the call is queued and a human must approve or reject it via Slack before execution
3. Immutable Audit Trail
Every proxied tool call is logged with: who made it (the authenticated user), which workspace and server it targeted, the tool name and arguments, the timestamp, whether it was allowed or denied, and the source IP. This log is append-only — not even admins can delete or modify entries. When something goes wrong, you can reconstruct exactly what happened.
4. Human-in-the-Loop Approval
High-risk operations — dropping a table, deploying to production, modifying IAM policies — can be gated behind human approval. The gateway sends a Slack message with the tool details, and the tool call blocks until someone clicks Approve or Reject. A timeout (configurable per workspace) automatically rejects if no one responds.
5. Argument Sanitization
Shell metacharacters and SQL injection patterns in tool arguments are detected and blocked before they reach the MCP server. This adds defense-in-depth even if the server has its own sanitization.
Architecture: Gateway as Proxy
The gateway uses MCP's Streamable HTTP transport, which means agents connect via standard HTTP. The gateway authenticates the request using OAuth (via Clerk), resolves the target MCP server from a registry, applies the security pipeline, and then forwards theCallToolRequest to the upstream server.
AI Client → [OAuth] → Gateway → [SSRF] → [Policy] → [Approval] → [Audit] → MCP Server
Each stage can deny the request. If a policy check or SSRF validation fails, the gateway returns an error to the agent without ever contacting the upstream server. This guarantees that blocked operations never reach the target.
What About Running MCP Servers Directly?
If you are running MCP servers locally for personal use with one agent, you may not need a gateway. But as soon as you share servers across a team — or connect multiple agents to shared infrastructure — the lack of per-call auth and audit becomes a liability.
A gateway also enables features that raw MCP cannot provide:
- Multi-tenant workspaces with isolated policies
- API key rotation that does not require restarting agents
- Usage metrics and rate limiting per team or per tool
- Integration with existing identity providers (SSO, SCIM)
Conclusion
The Model Context Protocol gives AI agents a standardized interface to tools and data. An MCP Gateway gives you control over what those agents can actually do. SSRF protection, per-tool policies, immutable audit logs, and human approval gates turn agent access from an all-or-nothing proposition into something you can safely run in production.