MCP server entries often need an API key, and the tempting place to put it is in the config file next to the server command. That config file is exactly the file that gets copied to another machine, pasted into a help request, or committed to a shared repository. The safe pattern is to put the secret in an environment variable and reference it from the config, so the file can be shared without the secret travelling with it.
What you need
- The API key the server needs
- The client config file for the server
- A way to set environment variables on your machine
Step 1: Store the key in the environment
Set the key as an environment variable on your machine, for example in your shell profile or the operating system secret store, and confirm it is set by echoing only its name, never its value.
Step 2: Reference the variable from the config
In the server entry, put the key in the env field for that server only, referencing the variable rather than pasting the key value into the file.
Step 3: Check the file is safe to share
Open the config file and confirm no secret value appears in it. If the file is committed to a repository, check the repository history too: a key removed from the current file may still sit in an old commit.
Worked example: synthetic config check for one server
The table below is a synthetic example. It shows the expected result of checking a config file; no key was used.
| Check | Found | Result |
|---|---|---|
| Key in config file | No value present | Pass |
| Key in env field | Variable reference only | Pass |
| Key in git history | Not found | Pass |
| Server starts | Reads key from environment | Pass |
Expected result (UNTESTED). the config file holds only a variable reference, the key lives in the environment, and the server starts. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- A project-scope .mcp.json is shared with everyone who clones the repository, so secrets in it reach everyone.
- Never paste a token into a shared log, screenshot or example.
- Rotating a leaked key is the only fix; deleting it from the file does not unshare it.
- Each server should get its own key where the service allows it, so one leak does not compromise every server.
