Claude Code
Claude Code is Anthropic’s CLI for AI-assisted coding. Use varlock to inject ANTHROPIC_API_KEY at runtime so the key never sits in a plain text .env or shell history.
For install, schema setup, and shared patterns, see the AI Tools overview.
Environment variable: ANTHROPIC_API_KEY (see supported env variables). Use the API key, not CLAUDE_CODE_OAUTH_TOKEN: that (from claude setup-token) only works for headless claude -p requests, not the interactive TUI.
In a project
Section titled “In a project”Add to .env.schema:
# @sensitive @requiredANTHROPIC_API_KEY=op(op://api-local/anthropic/api-key)varlock run -- claudeFrom any directory
Section titled “From any directory”Personal schema at ~/.env.claude:
# @sensitive @requiredANTHROPIC_API_KEY=op(op://api-local/anthropic/api-key)varlock run -p ~/.env.claude -- claudeShell alias (optional)
Section titled “Shell alias (optional)”alias vclaude='varlock run -p ~/.env.claude --no-redact-stdout -- claude'--no-redact-stdout keeps Claude’s own terminal output unredacted while secrets stay out of your shell history.
MCP server secrets
Section titled “MCP server secrets”MCP server configs (.mcp.json, ~/.claude.json) support ${VAR} expansion, but the variables are read from Claude Code’s own process environment. That works if you launch claude via varlock run, but not for the desktop app, which is not launched from your shell. Instead, let varlock resolve secrets at the point where Claude Code uses them.
Remote servers: headersHelper
Section titled “Remote servers: headersHelper”For http (and ws) servers, Claude Code’s headersHelper runs a command that prints a JSON object of headers. It runs when Claude Code connects to the server (once per session that uses it, not per request), and re-runs automatically if a call returns 401 or 403.
Use varlock printenv --template to emit the headers JSON:
{ "mcpServers": { "my-api": { "type": "http", "url": "https://mcp.example.com", "headersHelper": "varlock printenv --template '{\"Authorization\": \"Bearer {{MY_MCP_TOKEN}}\"}' --escape json" } }}The helper runs from the session’s working directory, so varlock picks up the project’s .env.schema automatically. For user-scope servers, or to be independent of the working directory, add -p /absolute/path/to/project (or -p ~/.env.mcp for a personal schema file). For the desktop app, also use an absolute path to the varlock binary. The standalone binary is the safest choice there: the npm-installed CLI needs node on PATH, which the desktop app may not have.
Claude Code gives the helper 10 seconds to run. A warm varlock cache resolves well within that; a cold resolve that prompts for biometric auth may not, so keep caching enabled for schemas used this way.
Local stdio servers: wrap the command
Section titled “Local stdio servers: wrap the command”For stdio servers, wrap the server command with varlock run in the server entry itself. This works no matter how Claude Code was launched, since each MCP server is a child process it spawns:
{ "mcpServers": { "my-local-server": { "command": "varlock", "args": ["run", "-p", "/absolute/path/to/project", "--filter", "MY_SERVER_*", "--inject", "vars", "--", "npx", "-y", "some-mcp-server"] } }}--filterinjects only the vars that server needs, so each server gets least-privilege access--inject varskeeps the__VARLOCK_ENVblob out of a third-party server’s environment- The server’s stdout is a pipe, so redaction applies to sensitive values in its output; add
--no-redact-stdoutif that interferes with your server