How do you share an MCP server through a Claude Code project's `.mcp.json` without committing anyone's token, and where can `${VAR}` expansion fail you?
answer
- share the shape, not the secret
- a fallback after a colon and dash
- five fields that expand
- unset means literal text, plus a warning
- some names arrive empty on purpose
basics
~20 sReference each developer's own variable as ${VAR} or ${VAR:-default} in command, args, env, url or headers. Unset without a default, it stays literal text with a warning; credential names like ANTHROPIC_API_KEY read as empty in remote url and headers.
solid answer
~40 s`.mcp.json` supports `${VAR}` and `${VAR:-default}` in `command`, `args`, `env`, `url` and `headers`, so the committed file names a variable and each developer exports their own value. Three failure modes matter. An unset variable with no default doesn't stop the server: it loads with the literal `${VAR}` text and `claude mcp list` and `/mcp` warn, naming the variable. In a remote server's `url` and `headers`, credential names — Claude Code's own `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN`, cloud-provider credentials, `NPM_TOKEN`, `HTTPS_PROXY` — always read as empty, even with a default, so `Bearer ${ANTHROPIC_AUTH_TOKEN}` reaches the server as `Bearer ` and fails with a 401; copy the value into a variable with your own name. And `CLAUDE_PROJECT_DIR` is set in the server's environment, not Claude Code's, so use `${CLAUDE_PROJECT_DIR:-.}` in `command` or `args`.
code
json · 15 lines{
"mcpServers": {
"tracker": {
"type": "http",
"url": "${TRACKER_URL:-https://tracker.example.com}/mcp",
"headers": { "Authorization": "Bearer ${TRACKER_TOKEN}" }
},
"orders-db": {
"type": "stdio",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR:-.}/tools/db-mcp-server.js"],
"env": { "DB_URL": "${ORDERS_DB_URL}" }
}
}
}go deeper
Know that .mcp.json can say ${VAR} so the committed file holds a variable name instead of a token.
Name the five fields that expand, the :-default syntax, and that an unset variable without a default loads as literal text with a warning.
Diagnose the 401 from a credential name read as empty, explain why Claude Code does that, and handle CLAUDE_PROJECT_DIR living only in the server's environment.
Set a team convention for which values may live in shared configuration, which are per-developer variables, and when OAuth removes the shared secret altogether.
## The problem expansion solves A project-scope server lives in `.mcp.json`, which is committed so the whole team gets the same configuration. But servers often need things that differ per person or per machine: an API token, a base URL for a staging environment, a path. **Environment variable expansion** lets the committed file name a variable while each developer supplies the value from their own environment, so the file carries the shape of the configuration and never the secret. ## Syntax and where it applies - `${VAR}` expands to the value of the environment variable `VAR`. - `${VAR:-default}` expands to `VAR` if it is set, otherwise to `default`. Expansion happens in five fields: | Field | Typical use | |---|---| | `command` | The server executable path | | `args` | Command-line arguments | | `env` | Variables passed to a stdio server | | `url` | A remote server's endpoint | | `headers` | A remote server's authentication headers | Local- and user-scope entries in `~/.claude.json` use the same entry shape and can carry the same `${VAR}` references. ## Failure mode 1: an unset variable without a default If a referenced variable isn't set and has no default, the configuration **still loads**. Claude Code warns in `claude mcp list` output and in `/mcp`, naming the missing variable, and uses the unexpanded `${VAR}` text as-is. A header ends up as the literal string `Bearer ${TRACKER_TOKEN}`, and the server rejects it. Either set the variable or add a `:-default` fallback so the server starts with a value you intended. ## Failure mode 2: credential variables read as empty In a **remote** server's `url` and `headers`, Claude Code deliberately reads certain credential variables as empty instead of expanding them. This stops a repository's `.mcp.json`, or a plugin, from sending your Claude Code or cloud-provider credentials to a server it names. The covered names include: - Claude Code's own credentials, such as `ANTHROPIC_API_KEY` and `ANTHROPIC_AUTH_TOKEN`; - cloud-provider credentials, such as `AWS_BEARER_TOKEN_BEDROCK`; - other credentials an environment carries, such as `HTTPS_PROXY` and `NPM_TOKEN`. A covered name reads as empty **whether or not it is set**, and a `:-default` on it is ignored, with no missing-variable warning. `Bearer ${ANTHROPIC_AUTH_TOKEN}` reaches the server as `Bearer `, which usually yields a `401` that Claude Code reports as a failed connection. A provider base URL such as `ANTHROPIC_BASE_URL` still expands, unless its value embeds a credential. To give a server one of the covered credentials on purpose, copy it into a variable with a name of your own and reference that. When you have set a covered variable that a remote `url` or `headers` references, `claude --debug-file <path>` logs a `never expanded toward a remote server` line naming it. ## Failure mode 3: a variable that only the server sees Claude Code sets `CLAUDE_PROJECT_DIR` in a stdio server's **own** environment, not in Claude Code's. Expansion runs in Claude Code before the server starts, so `${CLAUDE_PROJECT_DIR}` in `command` or `args` finds nothing; write `${CLAUDE_PROJECT_DIR:-.}` instead, or read the variable inside the server. Plugin-provided configurations substitute it directly. ## Seeing what was configured without leaking it For local, project and user servers, `claude mcp list`, `claude mcp get` and the `/mcp` detail view show a `${VAR}` reference **by name**, not as its resolved value, so sharing that output in a ticket doesn't leak the token. The duplicate-name warning likewise quotes endpoints unexpanded. ## A workable team pattern 1. Commit `.mcp.json` with `${VAR}` references and sensible `:-default` values for non-secrets. 2. Document the variable names each developer must export, using your own names for anything that would collide with a covered credential. 3. Prefer OAuth through `/mcp` where the server supports it, so no shared variable is needed at all. 4. When a server fails, read the warnings in `claude mcp list` before suspecting the server. ## Common mistakes - Committing a literal token because "the repository is private". - Expecting a missing variable to block startup. - Reusing `ANTHROPIC_API_KEY` or `NPM_TOKEN` in a remote header and chasing the 401. - Using `${CLAUDE_PROJECT_DIR}` without a default in `args`.
- A teammate's `tracker` server fails with a 401 while yours works with the same `.mcp.json`. What do you check first?Run `claude mcp list` on their machine: if `TRACKER_TOKEN` is unset there, it shows a missing-variable warning naming it, and the header went out as the literal `${TRACKER_TOKEN}` text. The committed `.mcp.json` is the same for both of you, so the difference is their environment: export the variable and start a new session.
- Is it safe to paste `claude mcp get tracker` output into a support ticket?For local, project and user servers, yes as far as expansion goes: `claude mcp list`, `claude mcp get` and the `/mcp` detail view show `${VAR}` references by name rather than their resolved values. A literal token written into the file, though, is shown as written.
- An unset ${VAR} without a default stops the server from loading
- A :-default makes ANTHROPIC_API_KEY usable in a remote header
- Expansion works only in a remote server's url field
- claude mcp list prints the resolved token values
- ${CLAUDE_PROJECT_DIR} always expands in args without a default