Can AI Coding Agents Leak API Keys? 2026 Cursor, Claude Code, MCP JSON Config, and GitGuardian AI Hooks
A coding agent does not guard your secrets. The model sees JSON and text; the host decides which config, file reads, and MCP responses enter context. Cursor and Claude Code can block keys in prompts before send, but once tool output contains a secret, withholding depends on the product.
Before you read
| Leak surface | 2026 public data / products | Response |
|---|---|---|
| MCP config JSON | GitGuardian State of Secrets Sprawl 2026: ~24k unique secrets in public MCP configs; docs often show inline env or headers | Env references, OAuth, or a vault; do not commit project mcp.json with values |
| Prompts and file reads | ggshield AI Hooks scan 600+ secret types at prompt and pre-tool and block | deny Read(.env); never paste .env into chat |
| Tool args and MCP output | Bearer tokens in arguments and log JSON enter sessions; many assistants only notify on MCP output, not withhold | Redact logs and rotate; ggshield 1.55+ can withhold shell output on Claude Code / Codex |
| Commits and CI | GitGuardian: Claude Code–assisted commits leak secrets at ~3.2%, about 2× baseline | pre-commit / CI secret scan; demos use fictional keys only |
sk_demo_not_real, orders_api, and A-1042 are fictional. GitGuardian AI Hooks docs from 2026-09-25 describe post-tool differences; Cursor often notifies on MCP output rather than withholding.
They can leak—risk lives in config JSON, context, and logs, not in whether the model memorizes passwords
GitGuardian’s 2026 Secrets Sprawl report and AI Coding Agents Are Leaking Credentials on Endpoints are explicit: AI assistants speed up secret creation and copying without matching governance. Typical coding-agent paths: you put an API key in MCP or Cursor project config; the agent Read-opens that file; or shell/MCP returns JSON with Authorization; that becomes a tool result in the next prompt. If a key reached the model provider’s context, treat it as potentially exposed—unless docs say it was withheld and you verified no upload.
- Config layer:
.cursor/mcp.json, Claude Code project MCP, remote serverheaders—all JSON, easy to commit or share. - Interaction layer: developers paste keys into prompts; or the agent reads
.env, kubeconfig, private keys. ggshield can block at pre-tool. - Observability layer: local session logs, error reports, debug JSON pasted into third-party pages. Redact before sharing.
MCP JSON-RPC and tools/call policy: MCP and JSON-RPC. File and shell boundaries: coding agent permissions. This piece does not replay attack stories—only what config and log JSON should look like.
Config, read call, raw log, redacted export—four JSON shapes
Project-level MCP config is plain JSON. Putting a key under env feels safer than headers, but once the file is in Git, history keeps it:
{
"mcpServers": {
"orders_api": {
"command": "npx",
"args": ["-y", "@demo/orders-mcp"],
"env": {
"ORDERS_API_KEY": "sk_demo_not_real"
}
}
}
}
The agent does not know it is a secret—only the path. A Read tool JSON is valid; the host permission model decides harm:
{
"type": "tool_use",
"id": "call_read_cfg",
"name": "Read",
"input": {
"path": ".cursor/mcp.json"
}
}
MCP or HTTP tool arguments often land in debug logs. Unredacted JSON is as risky as sending it to the model:
{
"event": "mcp.tools/call",
"server": "orders_api",
"arguments": {
"order_id": "A-1042",
"authorization": "Bearer sk_demo_not_real"
}
}
Before tickets, posts, or local formatting, produce a readable redacted copy—keep field names, replace values:
{
"event": "mcp.tools/call",
"server": "orders_api",
"arguments": {
"order_id": "A-1042",
"authorization": "Bearer [REDACTED]"
}
}
Same detection, different aftermath
GitGuardian documents prompt, pre-tool, and post-tool stages. The first two are mostly aligned: block. The third decides whether you rotate:
| Stage | Cursor | Claude Code |
|---|---|---|
| Secret in prompt | ggshield blocks; not sent | Same |
| Before command / read | Blocks Bash and Read that contain secrets | Same |
| Shell output with secret | Mostly desktop notify; model may already have seen it | ggshield 1.55+ can withhold; transcript gets replacement text |
| MCP tool output | Notify; treat as exposed and rotate | Mostly notify; MCP output usually not withheld |
So: no inline production keys in MCP config; prefer OAuth or short-lived tokens on remote MCP; for keys that appear in output, audit with redacted JSON instead of replaying raw tool results into chat.
Six steps to pull keys out of the agent workflow
From prevention to already leaked:
- Inventory JSON config. Every mcp.json and plugin file in project and home dirs; list keys under
env,headers,apiKey. - Reference, don’t embed. Env placeholders and vault injection; repos keep
${ORDERS_API_KEY}style references only. - Tighten Read / Bash. deny
.envand credential globs; mirror {perms} as JSON rules. - Endpoint scanning.
ggshield machine setupregisters AI Hooks for Cursor / Claude Code (docs: ggshield 1.49+, withhold 1.55+). - Default redaction on export. Redact before copying tool results; decode JWT claims only, never paste full tokens into issues.
- Assume MCP output exposed on notify-only. Per GitGuardian, rotate when output reached the assistant; “the model did not repeat it” is not rotation.
For shared MCP, use a reviewable JSON manifest without secrets plus per-developer local secret files—not a committable token in plugin.json.
Spread three JSON kinds before you ship agents
Most incidents are one env line in config or one Bearer in logs—not the model exfiltrating on purpose.
- Use format on mcp.json and tool arguments; no literal long
sk_orBearerstrings. - Use JSON redaction on log JSON before tickets or drafts.
- If the token is a JWT, use JWT decoder for alg and exp—do not publish the signature segment.
- Use JSONPath to pull
$.mcpServers.*.envfrom large config; use JSON Schema validation to forbidapi_keyin tool arguments. - Check coding agent permissions: even valid JSON should not bypass deny rules on secret files.
JSONSort runs locally in the browser—good for preview before redaction; do not put real repo paths in examples.
Common mistakes
- “Only GitHub upload counts.” Model context, local session files, and support attachments are all exposure.
- Bearer in MCP headers for quick debugging. Remote MCP examples get copied verbatim; use OAuth or short credentials.
- Believing .gitignore fixes committed mcp.json. History remains; rotate and scrub or use BFG.
- Ignoring user-level MCP config. Cursor home config is not in the repo but still leaks via sync or screenshots.
- Treating withhold as universal. MCP output is often notify-only; do not skip rotation.
- Pasting production tool results into public formatters. Use local tools or redact first; demos use sk_demo_not_real only.
FAQ
Can I commit Cursor project mcp.json?
Commit the shape, not secret values. Use env references and CI secret scanning. GitGuardian notes inline tokens in project files spread with every clone.
Is Claude Code login the same as a third-party API key?
No. Anthropic docs put login tokens in the OS keychain, but agents can still read third-party API keys in the project. Manage both separately.
After ggshield blocks, must I rotate?
If blocked at prompt or pre-tool and never reached the model, usually no rotation for that event. If post-tool was notify-only and output entered context, docs say treat as exposed and rotate.
MCP env vs shell export—which is safer?
Both beat inline committable JSON, but agents can still Read env files or echo. Prefer a vault, short-lived tokens, and deny sensitive paths.
Can I paste a JWT into the agent like an API key?
No. All three JWT segments are sensitive; decode only for debugging claims. Leak before expiry equals stolen session—rotate signing keys.
Conclusion
AI coding agents can leak API keys—almost always through JSON config, tool arguments, and logs—not because the model “chooses” to exfiltrate. GitGuardian’s 2026 data highlights MCP config and AI-assisted commits; ggshield AI Hooks guard the front door, but MCP output is often notify-only.
Move secrets out of committable JSON, deny file reads with policy, redact before sharing logs. Next time you add env to mcp.json, ask: will this JSON hit Git, chat, or a screenshot?
Further reading
- Coding agents calling MCP JSON-RPC, tools/call, and arguments
- Coding agent permissions Read / Bash deny rules
- JSON redaction Mask logs and tool results
- JWT decoder Check exp and claims, not full tokens
- All posts More on JSON and AI development
Changelog: First published