MCP security for coding agents

MCP security means controlling which MCP servers a coding agent can load, checking every MCP tool call before it runs, and treating everything an MCP server returns, including its tool descriptions, as untrusted input. For a team, that is three controls working together: a managed server allowlist, a pre-call hook on every mcp__ tool, and an audit log that records server, tool and arguments for each call.

Last updated: · Checked against the OWASP MCP Top 10, the MCP spec and the vendors' docs on 23 November 2026

Works with Claude Code · Cursor · Codex

Contents

What MCP security means for a coding agent

A coding agent such as Claude Code, Cursor or Codex CLI uses MCP servers to reach GitHub, issue trackers, databases, browsers and internal APIs. Each server adds tools the model can call with the developer's own credentials, on a machine that also holds source code, SSH keys and cloud tokens. Three things make this different from a normal integration:

  • The model decides when to call a tool, based on text it read, and that text can come from anyone.
  • A local (stdio) server is a program running with the developer's privileges. The MCP spec warns that local servers can run arbitrary code and asks clients to show the exact startup command before connecting one.
  • Tool descriptions and tool results go straight into the model's context. A server does not need to run malicious code to cause harm; it can just write instructions.

Tool poisoning: instructions hidden in tool descriptions

Tool poisoning (OWASP MCP03:2025) is an attack where a server puts instructions in a tool's description or parameter text. The model reads them; the developer usually does not. Invariant Labs showed a poisoned server steering an agent into sending data from another, trusted server to the attacker. Typical payloads ask the model to read ~/.ssh or .env and pass the contents in a harmless-looking argument, or to call a different tool "before anything else, for setup".

What helps:

  • Load servers from an approved list, not from whatever a repository's .mcp.json declares.
  • Read the tool descriptions of a new server before approving it, including hidden text (HTML comments, zero-width characters, base64).
  • Check the arguments of each call, because that is where the stolen data has to travel.

Rug pulls: tool definitions that change after approval

A rug pull is a server that ships clean tool descriptions, gets approved, then changes them. MCP lets servers send a list_changed notification, and Claude Code's docs state that in an interactive session it fetches the updated list without a reconnect. Approval of a server is therefore approval of whatever that server publishes later, unless you pin it.

What helps:

  • Pin the server version (see below), so an update is a change you review, not one that arrives on its own.
  • Record a hash of each tool's description at review time and compare it on a schedule; open-source scanners such as Invariant's mcp-scan work this way.
  • Keep per-call checks on, so a changed description still has to get its call past your policy.

Over-broad tokens and the confused deputy

An MCP server usually holds a token for a third-party API. If that token can do everything, so can any prompt that reaches the agent. The MCP spec covers two server-side failures: the confused deputy (an MCP proxy server with a static client ID that skips per-client consent) and token passthrough, where it states that MCP servers "MUST NOT accept any tokens that were not explicitly issued for the MCP server." Its scope guidance is least privilege: start with a minimal scope set and elevate per operation, and avoid wildcard scopes like files:* or admin:*.

For a coding team that means: a GitHub token limited to reading for a server that reads issues, a separate token per server, and no personal admin tokens in a shared config. This maps to OWASP MCP02:2025 (Privilege Escalation via Scope Creep) and MCP07:2025 (Insufficient Authentication & Authorization).

Exfiltration through MCP results and prompt injection

The most common path does not need a malicious server. A trusted server returns content an attacker wrote: an issue body, a pull request comment, a web page, a database row. The agent reads it, follows the planted instruction, and uses another tool (shell, HTTP, a second MCP server) to send data out. This is OWASP MCP06:2025 (Prompt Injection via Contextual Payloads). How detection and session taint work across agents: prompt injection in coding agents.

What helps:

  • Score MCP results for injected instructions where they enter the session, not after the damage.
  • Once a session has read suspicious content, hold the actions such text usually asks for: network calls to unknown hosts, reads of secrets, git push, permission changes.
  • Block credentials from leaving the machine through any tool, MCP included.

Allowlist servers centrally

Each agent has its own controls. Use the strongest one each offers.

  • Claude Code. In managed settings, allowedMcpServers and deniedMcpServers take entries keyed by serverUrl (wildcards allowed), serverCommand (exact command and arguments) or serverName. Set allowManagedMcpServersOnly: true so user and project allowlists cannot widen yours. The denylist always wins. The docs warn that a serverName entry "is not a security control", because a user picks the name; enforce with serverUrl and serverCommand. For a fixed set, deploy managed-mcp.json (Windows: C:\Program Files\ClaudeCode\managed-mcp.json; macOS: /Library/Application Support/ClaudeCode/; Linux: /etc/claude-code/). Project .mcp.json servers need approval in interactive sessions, but claude -p runs load them without asking, so add unwanted ones to disabledMcpjsonServers for CI.
  • Codex CLI. Per server in config.toml, enabled_tools is an allowlist of tools and disabled_tools is applied after it. default_tools_approval_mode accepts auto, prompt, writes or approve, and tools.<tool>.approval_mode overrides it for one tool. Project-scoped .codex/config.toml applies to trusted projects.
  • Cursor. Enterprise hooks in C:\ProgramData\Cursor\hooks.json (Windows), /Library/Application Support/Cursor/hooks.json (macOS) or /etc/cursor/hooks.json (Linux) rank above project and user hooks, and when several hooks answer, deny wins over ask and ask over allow.

Check every MCP call with a hook

An allowlist decides which servers load. A hook decides about each call, with its arguments.

  • Claude Code names MCP tools mcp__<server>__<tool>. A matcher of mcp__.* covers every MCP tool; mcp__github__.* covers one server. Note that mcp__github alone matches nothing, because a plain name is compared as an exact string.
  • Cursor has beforeMCPExecution, which receives tool_name, tool_input, mcp_server_name and the server's url or command, and answers with permission (allow, deny or ask), user_message and agent_message. Crashes and timeouts fail open by default; set failClosed: true on security hooks.
  • Codex CLI passes MCP calls to PreToolUse hooks, which can allow or deny (no "ask").

A working Claude Code example. In .claude/settings.json (or managed settings):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "mcp__.*",
        "hooks": [
          { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/mcp-guard.sh" }
        ]
      }
    ]
  }
}

And mcp-guard.sh, which lets two reviewed servers through and denies the rest with a reason the model sees:

#!/bin/bash
input=$(cat)
tool=$(jq -r '.tool_name' <<<"$input")
server=${tool#mcp__}
server=${server%%__*}

case "$server" in
  github|sentry) exit 0 ;;
esac

jq -n --arg r "MCP server '$server' is not on the team allowlist" '{
  hookSpecificOutput: {
    hookEventName: "PreToolUse",
    permissionDecision: "deny",
    permissionDecisionReason: $r
  }
}'

Exit 0 with no output means "no decision", so the normal permission flow applies. Print nothing else to stdout, or Claude Code may fail to parse the JSON. A real policy also inspects tool_input, for example denying any argument that contains a key, a .env path or an unknown URL.

Pin versions and keep secrets out of MCP config

  • Pin. npx -y some-server runs whatever the registry serves today. Use some-server@1.4.2, or a container digest. In Claude Code, a serverCommand allowlist entry matches the command and every argument exactly, so allowlisting ["npx", "-y", "some-server@1.4.2"] blocks any other version. This addresses OWASP MCP04:2025 (Software Supply Chain Attacks & Dependency Tampering).
  • Secrets. Claude Code's docs say any user on the machine can read managed-mcp.json, so do not put API keys in its env blocks; use ${VAR} expansion, OAuth or a headersHelper. Claude Code also reads credential variables such as ANTHROPIC_API_KEY and NPM_TOKEN as empty in a remote server's url and headers, so a project .mcp.json cannot send them to a server it names. In Codex, use bearer_token_env_var instead of a literal token; the docs note that an OAuth client secret is saved as plaintext in config.toml, so keep that file private and out of version control. This is OWASP MCP01:2025 (Token Mismanagement & Secret Exposure).

Audit every MCP call: server, tool, arguments

When something goes wrong you need to answer: which server, which tool, with what arguments, in which session, after reading what, and who approved it. Claude Code can export MCP server and tool names through OpenTelemetry with OTEL_LOG_TOOL_DETAILS=1. A per-call log should also keep the (redacted) arguments and the decision. Missing this is OWASP MCP08:2025 (Lack of Audit and Telemetry), and unlisted servers no one tracks are MCP09:2025 (Shadow MCP Servers).

MCP security and the OWASP MCP Top 10

The OWASP MCP Top 10 is in beta (items tagged 2025). How the controls above map:

  • MCP01:2025 Token Mismanagement & Secret Exposure: env-var credentials, no secrets in shared config, block credentials in tool arguments.
  • MCP02:2025 Privilege Escalation via Scope Creep: least-privilege tokens per server.
  • MCP03:2025 Tool Poisoning: approved server list, description review, argument checks.
  • MCP04:2025 Software Supply Chain Attacks & Dependency Tampering: pinned versions, exact serverCommand entries.
  • MCP05:2025 Command Injection & Execution: hooks on shell and MCP calls, sandboxed local servers.
  • MCP06:2025 Prompt Injection via Contextual Payloads: score MCP results, hold risky actions in a tainted session.
  • MCP07:2025 Insufficient Authentication & Authorization: per-server tokens, no token passthrough.
  • MCP08:2025 Lack of Audit and Telemetry: per-call audit log with server, tool and arguments.
  • MCP09:2025 Shadow MCP Servers: scan repositories for declared servers; managed allowlists.
  • MCP10:2025 Context Injection & Over-Sharing: limit what each server can read and return.

MCP security checklist

  1. Managed allowlist in each agent: allowedMcpServers + allowManagedMcpServersOnly (Claude Code), enterprise hooks.json (Cursor), enabled_tools per server (Codex).
  2. Allow by serverUrl and serverCommand, not by name.
  3. Every local server pinned to a version or digest.
  4. A pre-call hook on every MCP tool (mcp__.*, beforeMCPExecution, Codex PreToolUse), failing closed.
  5. Arguments checked for secrets, sensitive paths and unknown hosts.
  6. MCP results scored for injected instructions; risky actions held after a hit.
  7. One token per server, least privilege, read from environment variables.
  8. New servers reviewed, including their tool descriptions, and re-reviewed on version change.
  9. claude -p and CI runs covered by disabledMcpjsonServers or a managed config.
  10. Every MCP call logged with server, tool, arguments and decision, in a log you can export.

Honest limits

  • Hooks see tool calls and results, not tool definitions. A poisoned description is caught when it produces a bad call or a suspicious result, not when the server publishes it. Description pinning needs a separate scanner.
  • Rule-based injection detection catches marked injections (override wording, fake system messages, hidden text) but misses plain requests planted in data, such as "please grant Amy access". Holding risky actions in a tainted session limits what such text can do; it does not catch the text.
  • A hook that crashes can fail open: Cursor does by default unless failClosed: true, and in Codex a killed hook process lets the action run (openai/codex#41979).
  • A local administrator can edit managed files. Protections against config changes stop the agent, not an admin.
  • Server-side flaws (token passthrough, confused deputy in an OAuth proxy) have to be fixed by whoever runs the MCP server. A client-side policy can limit the blast radius, not repair the server.

How SecGuardRail handles MCP calls

  • One policy for three agents. Claude Code, Cursor and Codex CLI (beta, Windows) send each tool call through the same decision flow, approvals and audit log. MCP is its own tool class, so one policy covers MCP tools from every agent. Policies can match tool.class = mcp, tool.mcp_server, tool.name and any argument through $.input.<path>.
  • Before the call. Claude Code: a PreToolUse hook with the anchored matcher ^mcp__.*. Cursor: beforeMCPExecution, with MCP input that Cursor sends as a JSON string parsed before the check. Codex: MCP calls go through the same hook route and are recorded with server and tool.
  • Fails closed. If SecGuardRail cannot decide in time, MCP tools do not run by default; the fail mode is configurable per tool class.
  • After the call. MCP results are scored for prompt injection where they enter the session (Claude Code PostToolUse, Cursor afterMCPExecution). A hit marks the session tainted, warns the model, and the Prompt Injection Protection pack holds egress to hosts outside the allowlist, secret reads, git push, permission changes and the exact command the text asked for.
  • Secrets. The Secrets pack denies credentials sent off the machine, and that rule applies to every MCP call regardless of destination. Secrets are redacted before anything is stored or sent to Slack.
  • Approvals and alerts. Risky MCP calls can be held for a person: the Slack alert links to the request, and an approver decides in the dashboard.
  • Audit. Every decision is stored with agent, session, server, tool and redacted arguments, in a tamper-evident hash chain you can verify and export as CSV or NDJSON.
  • Discovery. The repository scan lists the MCP servers each repository's .mcp.json declares, so shadow servers show up. The MCP Tool Watch pack and Tamper Protection (which denies MCP write tools aimed at agent settings and hook files) are included.
  • Monitor mode lets you run a pack and see what it would have done before you enforce it.

FAQ

What is MCP tool poisoning?

An attack where an MCP server hides instructions in a tool's description or parameters. The model reads them and may call tools with stolen data in the arguments. OWASP lists it as MCP03:2025.

How do I block an MCP server in Claude Code?

Add it to deniedMcpServers in managed settings with a serverUrl or serverCommand entry. A denylist match always wins. To allow a fixed list, use allowedMcpServers with allowManagedMcpServersOnly: true, or deploy managed-mcp.json. See Claude Code security for teams.

Can a hook check MCP calls in Cursor and Codex too?

Yes. Cursor has beforeMCPExecution, which can allow, deny or ask. Codex sends MCP calls to PreToolUse hooks, which can allow or deny.

What is an MCP rug pull?

A server that changes its tool descriptions after you approved it. Pin server versions, hash the descriptions you reviewed, and keep per-call checks on so a changed tool still meets your policy.

Does SecGuardRail cover MCP in Claude Code, Cursor and Codex?

Yes. All three send MCP calls through one policy, one approval queue and one audit log; the Codex connector is in beta on Windows.

Set it up

Become a design partner

Design partners: Team plan free for 30 days, then 50% off for life, limited to 10 teams. We set it up with you on a 20-minute call.

Agents you use

0/1000

We store this to contact you. See Privacy.

Plans

  • Free

    $0

    • 1 developer, 1 workspace
    • 3 agent API keys
    • 10,000 agent events a month
  • Starter

    $50/mo

    • Up to 3 developers
    • Everything in Free
    • Approvals with timeout
  • Team

    $99/mo

    • Up to 10 developers
    • +$9 per extra developer
    • Everything in Starter
  • Business

    From $499/mo

    • Everything in Team
    • Agent discovery
    • Retention and legal hold
See full pricing

Compare hooks, sandboxes and gateways across Claude Code, Cursor and Codex. Connect an agent in the docs. Related guides: Claude Code security for teams, Cursor hooks security for teams and Codex CLI security for teams. See pricing, or join as a design partner.

Sources

Guardrails for Claude Code, Cursor and Codex

Request access