0Pricing
Claude Architect · Leçon

Secrets avec des variables d’environnement

Référencez ${GITHUB_TOKEN} ; n’intégrez jamais de tokens dans le dépôt.

Secrets avec des variables d’environnement est une leçon Claude Architect gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Claude Architect, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Claude Architect comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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.

Questions Fréquemment Posées

La leçon « Secrets avec des variables d’environnement » est-elle gratuite ?

Oui — le texte complet de « Secrets avec des variables d’environnement » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Claude Architect, passe à CoddyKit PRO. Le cours Claude Architect comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Secrets avec des variables d’environnement » ?

Référencez ${GITHUB_TOKEN} ; n’intégrez jamais de tokens dans le dépôt. Tu pratiques Claude Architect avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Claude Architect ?

Aucune expérience préalable n'est requise. Claude Architect sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.

Combien de temps prend la leçon « Secrets avec des variables d’environnement » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Claude Architect ?

Oui. Chaque leçon Claude Architect inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Outils, ressources et requêtes
  2. Portée du projet ou de l’utilisateur
  3. Secrets avec des variables d’environnement
  4. Serveurs communautaires ou personnalisés
← Retour à Claude Architect