The short answer
- Keep the key in an environment variable (or the app's own secret prompt), never typed into a file in your project.
- In any config you commit, write the variable's name in the client's syntax (
${NAME}for Claude Code,${env:NAME}for Cursor, VS Code and Windsurf), not the value. - Add
.env*.localto.gitignoreand check it withgit check-ignore -v .env.local. - Search your history once:
git log --oneline -S "<the first 10 characters of the key>" --all. - If a key ever reached a remote, rotate it. Deleting the commit is not enough.
A key in a config file is not a problem by itself. A key in a file that gets committed, pushed, pasted into an issue or shown on a stream is. The trick is knowing which files each tool writes and which of them travel.
Where each client saves it
When a shell fills in $NAME before the client sees the command, the value gets saved. When the config holds ${NAME} and the client fills it in at start, only the name is saved.
| Client | File | What lands there | Travels with the repo? |
|---|---|---|---|
| Claude Code, local or user scope | ~/.claude.json |
the value (the shell filled it in) | no, it is in your user folder, readable only by you |
| Claude Code, project scope | .mcp.json in the project |
whatever you wrote: use ${NAME} |
yes, it is meant to be committed |
| Cursor | ~/.cursor/mcp.json or .cursor/mcp.json |
whatever you wrote: use ${env:NAME} |
the project file does |
| VS Code | .vscode/mcp.json |
${input:…} makes VS Code ask once and keep it in its secret storage |
the project file does |
| Windsurf | mcp_config.json in your user folder |
use ${env:NAME} |
no |
| Claude Desktop | claude_desktop_config.json in your user folder |
values in env are plain text |
no |
| Grok CLI | ~/.grok/config.toml (user) or .grok/config.toml (project) |
the value, unless you add it with '${NAME}' in single quotes |
the project file does |
Two details from our own test machines: Claude Code saved ~/.claude.json readable by you only (mode 600), while the Grok CLI saved ~/.grok/config.toml readable by other users of the machine (664). On a shared Mac or Linux box, tighten it:
chmod 600 ~/.grok/config.tomlSafe to commit: the name, never the value
A committed config that carries the name works for everyone on the team, each with their own key. (The example is Clinamen's hosted server, whose keys come with Clinamen Signature when sales open at launch; the shape is the same for any keyed server.)
{
"mcpServers": {
"clinamen": {
"type": "http",
"url": "https://mcp.clinamen.dev/mcp",
"headers": { "Authorization": "Bearer ${CLINAMEN_LICENCE_KEY}" }
}
}
}Each person sets CLINAMEN_LICENCE_KEY in their own shell. If the variable is not set, the server receives the literal text ${CLINAMEN_LICENCE_KEY} and Clinamen's says unexpanded. Set the variable in the terminal you start the agent from (or with setx on Windows, then open a new window) and restart the agent.
The live demo loads here when scripts run. The text below says what it shows.
Paste an MCP config or a command line. Each line is checked in your browser (nothing is sent or saved) for a value that looks like a real key, versus a reference such as ${env:NAME}, ${NAME}, ${input:id}, $env:NAME or %NAME%. The example holds a made-up key on purpose. It is a first look, not a secret scanner: use git log -S (below) for history.
.env files: ignore them before you create them
Frameworks read keys from .env.local (Next.js, Vite) and many tools read .env. Ignore the local ones first, then check that git agrees:
.env.local
.env*.localgit check-ignore -v .env.localThat prints the .gitignore line that matches. No output means the file is not ignored.
Did a key already get in?
Search every commit on every branch for part of the key (ten characters are plenty, and you never have to paste the whole thing):
git log --oneline -S "clm_abc123" --allEach commit listed added or removed that text. If the commits were only ever on your machine, you can rewrite them. If they were ever pushed, assume the key is public: bots scan public repos within minutes, and forks and caches keep old commits. Rotate the key (most providers have a revoke or rotate button in their dashboard; otherwise write to their support and say the key leaked), update the variable on every machine that uses it, then clean the history at leisure.
Places keys leak that are not git
claude mcp get <name>prints the server's headers in plain text, key included. Fine in your own terminal, not in a bug report.- A screen recording or stream of your terminal while you
echoa variable or open a config file. - An agent transcript you share: if the agent read a config file, the key is in the transcript.
- A cmd line that sets and prints a variable at once:
set NAME=value & echo %NAME%prints the value from before the line ran, because cmd fills in%NAME%when it reads the line, so people try again with the real key on screen. Put the two commands on separate lines.
What a careful server does with your key
You can expect a server to keep the key in a header, never in the URL (URLs end up in logs and browser history), and to say why it refused a key without echoing it back. Clinamen's hosted server, for example, records each call as the tool, the entry, the tier and the time, and stores a hash of the key with a support ticket, never the key itself.
Something here did not work?
Every command on this page was run before it was published, on the systems named in "How we tested this". If a step fails for you, write to [email protected] with what you ran and what you saw, and we will fix the page.