0Pricing
Claude Architect · 课时

使用环境变量管理机密

引用 ${GITHUB_TOKEN};永远不要提交令牌。

使用环境变量管理机密 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Secrets Don't Belong in Config

MCP servers connect Claude to real systems — GitHub, databases, internal APIs. Each connection needs credentials: tokens, API keys, passwords.

The architect's rule is absolute: secrets never live in committed files. Your .mcp.json (project scope, shared in version control) declares which servers exist and how to launch them — but a raw token written there is now in your git history forever.

The clean pattern is to reference a secret by environment-variable name and let the runtime resolve it. This lesson shows how MCP does exactly that with ${GITHUB_TOKEN}.

The Two MCP Scopes

MCP configuration lives in two distinct scopes, and the distinction drives where secrets are safe:

  • Project scope — .mcp.json at the repo root. Shared via VCS so the whole team gets the same servers.
  • User scope — ~/.claude.json. Personal, on your machine only, NOT shared.

Because project scope is committed, anything you put in .mcp.json is visible to everyone with repo access — including a leaked clone. That is precisely why a token's value must never appear there; only a reference to an env var may.

Referencing a Secret with ${VAR}

In .mcp.json you pass credentials through the server's env block, using ${VAR} expansion. The literal text ${GITHUB_TOKEN} is committed safely — the real token is resolved from the environment at launch time.

Here the GitHub MCP server receives its token without the secret ever touching the file.

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}

Where the Real Value Comes From

The reference ${GITHUB_TOKEN} is inert until the runtime supplies the actual value. You provide it through your shell environment — typically from a local, git-ignored .env file or your OS secret store.

The token is set outside the repo. The committed config stays clean; each developer (and each CI runner) injects their own value.

# .env — git-ignored, never committed
export GITHUB_TOKEN="ghp_realSecretValueLivesOnlyHere"

# load it into the shell before launching Claude Code
source .env

Lock the Door: .gitignore

Referencing an env var is only half the defense. You must also guarantee the file holding the real value never gets committed.

Add your secret files to .gitignore. Commit an example file with placeholder names so teammates know which variables to set — without ever shipping the values.

# .gitignore
.env
.env.local
*.secret

# .env.example  (THIS one is committed — names only, no values)
GITHUB_TOKEN=
DATABASE_URL=

Personal vs Shared Secrets

Scope also decides whose credential is used:

  • A shared service token the whole team uses can be referenced in project .mcp.json — each member still supplies the value via their own environment.
  • A purely personal token (your individual PAT) fits naturally with user scope ~/.claude.json, which isn't shared anyway.

Either way the principle holds: the file may contain the name; the environment supplies the value. Never invert that.

Prefer Community MCP Servers

For standard integrations — GitHub, Postgres, Slack, filesystem — prefer a maintained community MCP server over a custom one. They already implement the env-based secret handling correctly, expose proper Tools/Resources/Prompts, and return structured errors.

Writing your own server just to call GitHub means re-implementing auth and token handling you could inherit safely. Reserve custom servers for genuinely proprietary systems.

Secrets in CI/CD

In a pipeline there is no developer .env to source. Instead, your CI platform's encrypted secret store injects the variable into the job environment, and the same ${GITHUB_TOKEN} reference resolves identically.

This is why the reference pattern matters: one committed .mcp.json works locally and in CI, with the value sourced differently in each place — and never printed.

# GitHub Actions — value comes from encrypted repo secrets
jobs:
  review:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - run: claude -p "/review" --output-format json

Least Privilege for Tokens

An MCP token inherits whatever permissions you granted it. A GitHub PAT scoped to repo can push code; one scoped to read:org cannot.

Mirror the architect's least-privilege habit from tool and subagent design: grant each token only the scopes its server actually needs. If ${GITHUB_TOKEN} ever leaks despite your safeguards, a tightly scoped, short-lived token limits the blast radius dramatically.

Rotation and Leak Response

Treat any committed secret as compromised — even after you delete it, it remains in git history. The only real fix is to revoke and rotate the credential at its source (GitHub settings), then re-issue a fresh one into your environment.

Because your config references ${GITHUB_TOKEN} rather than embedding a value, rotation is painless: revoke the old token, update the env var, relaunch. No code change, no commit.

# After rotating the token at the provider, just update the env value:
export GITHUB_TOKEN="ghp_freshlyRotatedValue"
# .mcp.json still references ${GITHUB_TOKEN} — nothing else changes

Putting It Together

The complete, exam-grade pattern for MCP secrets:

  • Declare the server in project .mcp.json (shared via VCS).
  • Reference the secret as ${GITHUB_TOKEN} in the env block — never the raw value.
  • Supply the value from a git-ignored .env locally and from an encrypted store in CI.
  • Ignore secret files; commit only an .env.example of names.
  • Scope tokens to least privilege and rotate on any suspicion.

Config is shareable; secrets are not. That separation is the whole game.

Quick Check: Sharing an MCP Config

Apply the secret-handling rules to a real team scenario.

Recap: Secrets with Environment Variables

Key takeaways:

  • MCP config has two scopes: project .mcp.json (shared via VCS) and user ~/.claude.json (personal).
  • Committed config may hold a reference like ${GITHUB_TOKEN} — never the token value.
  • Real values come from a git-ignored .env locally and an encrypted secret store in CI; commit only an .env.example of names.
  • Prefer community MCP servers — they handle env-based secrets correctly out of the box.
  • Scope tokens to least privilege; on any leak, revoke and rotate at the source — the reference pattern makes rotation a one-line env change.

Separate the shareable from the secret, and you've internalized the rule the exam is testing.

常见问题解答

「使用环境变量管理机密」课时是免费的吗?

是的 — 「使用环境变量管理机密」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。

「使用环境变量管理机密」这节课中我会学到什么?

引用 ${GITHUB_TOKEN};永远不要提交令牌。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Claude Architect 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「使用环境变量管理机密」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Claude Architect 课中编写并运行代码吗?

能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 工具、资源与提示词
  2. 项目范围与用户范围
  3. 使用环境变量管理机密
  4. 社区服务器与自定义服务器
← 返回 Claude Architect