# Secrets and API keys in Claude Code: keep credentials out of the agent's reach

Source: https://botpassword.com/agents/claude-code
Last checked: 2026-10-25

Claude Code is Anthropic's terminal coding agent. It supports MCP servers over stdio, HTTP (recommended), SSE (deprecated) and WebSocket, configured in `.mcp.json` (project), `~/.claude.json` (user and local scope) or with `claude mcp add`. It reads `CLAUDE.md` for project instructions and restricts file reads with `permissions.deny` rules in `settings.json`.

## MCP support

Yes. Transports: stdio, http, sse, ws.

- project: `.mcp.json`
- user: `~/.claude.json`
- workspace: `~/.claude.json (under the project's path in `projects`; the default "local" scope)`

Add a server with `claude mcp add --transport http <name> <url>  |  claude mcp add [options] <name> -- <command> [args...]`.

## Environment variables and .env files

Variables can be set in your shell or under the `env` key of a settings file (`~/.claude/settings.json`, `.claude/settings.json`, `.claude/settings.local.json` or managed settings); values are copied as written and no shell processes them. In `.mcp.json`, `${VAR}` and `${VAR:-default}` expand in `command`, `args`, `env`, `url` and `headers`. In a remote server's `url` and `headers`, credential-style variables such as `ANTHROPIC_API_KEY` read as empty instead of expanding. The environment-variables reference does not describe automatic `.env` loading. `CLAUDE_CODE_MCP_ALLOWLIST_ENV` limits stdio MCP servers to a safe baseline environment plus their configured `env`.

## File access

Read-only tools (file reads, Grep) run without approval inside the working directory and any additional directories. Bash commands ask first, except a built-in set of read-only commands such as `cat`, `ls`, `head`, `grep` and `find`. Block file reads with `Read` deny rules such as `Read(./.env)` or `Read(./secrets/**)`. Anthropic describes `Read` rules as a best-effort application to built-in tools, and a Bash deny rule "isn't a security boundary" around a program.

Deny or ignore files with: `permissions.deny` with `Read(...)` rules in settings.json (a `.claudeignore` file has no effect).

## Credential risks

- **CVE-2026-21852 (Claude Code before 2.0.65).** A malicious repository's settings file could set `ANTHROPIC_BASE_URL` to an attacker endpoint, and Claude Code sent API requests, including the user's API key, before showing the trust prompt. Fixed in 2.0.65.
- **Project `.mcp.json` servers load without a prompt in non-interactive runs.** Interactive sessions ask before using servers from a repository's `.mcp.json`, but in `claude -p` and Agent SDK runs they load without a prompt unless blocked with `disabledMcpjsonServers` or `--strict-mcp-config`. A stdio server inherits your shell environment unless `CLAUDE_CODE_MCP_ALLOWLIST_ENV` is set.
- **Deny rules are not a hard boundary.** `Read` deny rules are applied best-effort to built-in file tools, and built-in read-only Bash commands such as `cat` run without a prompt. Anthropic recommends pairing deny rules with the sandbox when a restriction must hold.

## FAQ

### Where does Claude Code store MCP server configuration?

Project-scoped servers live in `.mcp.json` in the project root. User and local scopes are stored in `~/.claude.json`. Add one with `claude mcp add`.

### Can Claude Code read my .env file?

By default, yes, if it is inside the working directory. Add `Read(./.env)` and `Read(./.env.*)` to `permissions.deny` in settings.json to stop its file tools from reading it. A Bash command such as `cat .env` is a separate path, so use the sandbox for a stronger guarantee.

### How do I pass an API key to an MCP server in Claude Code?

Reference an environment variable with `${VAR}` in the server's `env` or `headers` in `.mcp.json`, or pass `--env KEY=value` to `claude mcp add`. The first keeps the value out of the committed file.

### How do I give Claude Code an API key without it seeing the key?

Don't put the key in the prompt, in a file Claude Code can read, or in an environment variable it can print. Give it a tool that performs the authenticated step instead: a credential broker exposed over MCP or a CLI that injects the key into the command or HTTP request and returns only the result. BotPassword is that broker, through `bpw mcp` and the `bpw` CLI.

## Sources

- https://code.claude.com/docs/en/mcp
- https://code.claude.com/docs/en/settings
- https://code.claude.com/docs/en/permissions
- https://code.claude.com/docs/en/env-vars
- https://github.com/anthropics/claude-code/security/advisories/GHSA-jh7p-qr78-84p7
