Dev.to Security πŸ” Cybersecurity πŸ‘ 2 πŸ“– 6 min read

One claude mcp add saved my API key in plain text in two places

I searched my shell history for my Notion token. It was there three times. Then I opened ~/.claude.json and found it a fourth time. TL;DR If you pass an API key straight to claude mcp add, it ends up in plai

I searched my shell history for my Notion token. It was there three times. Then I opened ~/.claude.json and found it a fourth time.

TL;DR

  • If you pass an API key straight to claude mcp add, it ends up in plain text in two places: your shell history and ~/.claude.json.
  • The new "read the config from stdin" option for claude mcp add-json only fixes the first one. The key still lands in ~/.claude.json.
  • The fix that actually works is boring: register a reference like ${NOTION_TOKEN} instead of the key itself, in single quotes. Neither file ever sees the real value.

Short on time? Everything below is free. If you'd rather have Claude Code find the leaks for you and hand you the exact fix commands, I packaged it as a skill: mcp-key-check on Gumroad.

The copy-paste that started it

Here's how most of us add an MCP server. I copied this pattern from a README, pasted my token over the x's and hit Enter:

claude mcp add notion -e NOTION_TOKEN=ntn_xxxxxxxx -- npx -y @notionhq/notion-mcp-server

It connected. Great. Done.

A few weeks later I was searching my history for something unrelated and saw my token. The whole thing. Three times, actually, because the first two attempts didn't connect and I kept retyping the command.

It felt like hiding the house key under the doormat and then realizing I'd left it on top of the doormat.

So I sat down and tested a few ways of registering an MCP server, with fake tokens, and checked where the key ends up each time. Here's what I found.

Place #1: your shell history

zsh saves every command you type to ~/.zsh_history. If the key is in the command, the key is in the file. Obvious once you say it out loud, but I never think about it while I'm typing.

The scary part is where that file travels. Dotfiles synced to GitHub. Full home folder backups in the cloud. A screen share where you hit Ctrl+R to find an old command. None of these are attacks. They're all just "oops".

Place #2: ~/.claude.json

This one I hadn't expected. When you run claude mcp add, Claude Code writes the server's config to ~/.claude.json in your home folder. I opened it, and there was my key in the server's env block, as plain as could be.

To be fair, the file's permissions were set so only my user could read it. Nobody is reading it right now. But plain text is plain text, and "I cleaned my history" doesn't help at all if the same key is sitting in a second file.

What about the new stdin option?

A recent Claude Code release (2.1.327) lets claude mcp add-json read the JSON config from stdin if you pass - instead of the JSON. You pipe the config in, so it never appears in your command line or your history.

That's a genuinely nice improvement. But two things surprised me when I tried it.

First, if the JSON you pipe in contains the real key, the real key still goes into ~/.claude.json. Only the history problem is solved.

Second, when I asked Claude Code to do it for me ("register this MCP server via stdin"), it refused. Claude Code won't change MCP config from inside a session, because it can't show you a confirmation screen there. You have to run it in your own terminal. That's a sensible safety rule, and it's also why the fix below has to be run by you, not by the agent.

The fix: register a reference, not the key

Here's what I settled on. Instead of the key's value, you register the name of an environment variable:

claude mcp add notion -e 'NOTION_TOKEN=${NOTION_TOKEN}' -- npx -y @notionhq/notion-mcp-server

The single quotes are the whole trick. Without them, your shell replaces ${NOTION_TOKEN} with its value before Claude Code ever sees it, and you're back to storing the real key. I checked both: without quotes the real value was saved, with quotes the literal text ${NOTION_TOKEN} was saved.

Now your history contains ${NOTION_TOKEN}. So does ~/.claude.json. The real key is in neither.

Does it actually work, though? I registered a tiny server that just writes the env value it receives to a file. The value from my environment arrived correctly. When I started it with the variable unset, Claude Code warned me that an environment variable was missing, so you'll notice if you forget.

The same ${VAR} syntax is documented for project-level .mcp.json files. If your team commits that file to a repo, this isn't optional.

Getting the key into the environment without leaking it again

Typing export NOTION_TOKEN=... in the terminal puts it right back into your history. I created a small file containing only the exports, made it readable only by me (chmod 600), and sourced it from ~/.zshrc. I wrote the file in an editor, so nothing touched my history.

One catch: this assumes you start Claude Code from a terminal, so it inherits your shell's environment. If the server doesn't connect, check that first.

Bonus: a leading space

If you really must type a key once, add setopt HIST_IGNORE_SPACE to ~/.zshrc. Commands that start with a space then aren't saved to history. I tested it, and only the space-prefixed line was missing. Remember that this protects your history and nothing else.

Already leaked? Rotate first

If you just found your key in one of these files, the most important step isn't deleting the line. It's issuing a new key at the provider and revoking the old one.

Once a key has been written to a file, you don't know where backups and sync have carried it. "I deleted it" isn't the same as "it's safe". Treat it like a house key you might have lost: change the lock. Then clean the history line and re-register the server with ${VAR}.

Wrap-up

  • A key passed directly to claude mcp add is stored in plain text in both ~/.zsh_history and ~/.claude.json
  • The stdin option only fixes the history, and it has to be run in your own terminal
  • Register '${VAR}' in single quotes and the real key is stored nowhere; it's expanded when the server starts
  • If a key was already in plain text, rotate it before anything else

Want to check right now? Paste this into Claude Code. It only shows the first 4 characters of anything it finds:

Check ~/.zsh_history and ~/.claude.json for API keys or tokens stored in plain text. Never print the values, only the first 4 characters. List file, line (or MCP server name), and what kind of key it looks like. Skip ${...} references, they're safe. Don't modify any files, just suggest fixes.

Over to you: how many did you find? I found mine three times. Tell me in the comments, no judgment.

Want it to hand you the exact fix commands?

Thanks for reading this far! If you're itching to go check your own machine now, I turned the whole process into a Claude Code skill, including the details I didn't have room for above.

  • Scans shell history, ~/.claude.json (user and per-project servers) and .mcp.json in one go
  • Recognizes common key formats (GitHub, OpenAI, Anthropic, Notion, Slack, Stripe, AWS, Google and more) plus anything named like a key or token
  • Checks env, headers (Bearer tokens) and args, and ignores safe ${VAR} references
  • Never prints a full key, only the first 4 characters and the length
  • Prints the exact claude mcp remove + claude mcp add-json pair for each leaky server, already rewritten to use ${VAR}, for you to run in your own terminal
  • Ends with a rotation checklist of every provider it found

If you've only ever registered servers with ${VAR} references, you don't need it.

πŸ‘‰ Get the skill: https://gennekiengineer.gumroad.com/l/mcp-key-check

If this was useful, a ❀️ and a follow would mean a lot. I write about practical ways to put AI to work in indie development.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.