
Give AI Agents the Key, Not the Keyring: Least-Privilege Secrets for MCP with cmdn-locker
After aws-vault, I moved every other secret into a locked Keychain vault and gave AI agents only the key they need.
Table of Contents16 sections
In my previous article, Stop Keeping AWS Keys in Plaintext, I moved my AWS keys out of ~/.aws/credentials and into aws-vault. But AWS keys were only one part of the problem. When I looked at my Mac again, I still found API keys in .env files, tokens inside MCP configuration for Claude Code, and a broad GitHub token sitting in gh. aws-vault cannot help with those, because it only knows about AWS.
With so many stories about compromised engineer laptops these days, I asked myself a simple question: if my Mac were compromised tomorrow, what would the attacker take first? The honest answer was "every plaintext secret, in a few seconds". So I set one rule for myself: no plaintext secrets on my laptop, including the ones my AI agents use.
To follow that rule without making daily work painful, I built a small internal tool called cmdn-locker. It applies the aws-vault idea to every other secret: keep values in a locked macOS Keychain, and give each command, MCP server or agent only the key it needs, only when it runs. This article explains why I built it, how it works with Claude Code, how I use it every day, and what it does not protect against.
Version note: This article is based on
cmdn-lockerv0.3.0, an internal tool at Classmethod Danang, as of November 2026. It runs on macOS only. Claude Code behavior is based on the official documentation at the time of writing.
TL;DR
cmdn-lockerstores secrets in a dedicated, password-protected macOS Keychain, separate from the login Keychain. It starts locked, locks on sleep, after 15 minutes idle, and after a 1-hour hard session cap by default.Configuration files such as
.mcp.jsoncontain only names (a project group and a binding), never values. They are safe to commit.Each consumer gets only what it asked for:
execinjects selected keys into one command,runstarts a bound stdio MCP server,headersreturns exactly one auth header to Claude Code'sheadersHelper, andghruns the GitHub CLI with a repo-scoped token.For GitHub, it signs in to a fine-grained GitHub App via Device Flow and refreshes the 8-hour token automatically. No client secret and no long-lived personal access token on the Mac.
When the vault is locked and Claude Code needs a secret, macOS shows one native password dialog, even if several MCP servers start at the same time.
It is not magic: while the vault is unlocked, it trusts your user account, and any delivered secret can still be misused. Combine it with scoped tokens, short sessions and a sandbox.
Why plaintext secrets are still a problem after aws-vault
I covered the general threat model in the aws-vault article, so here I focus on the secrets that remain after AWS keys are gone.
Risk | How it gets exposed | What cmdn-locker changes |
|---|---|---|
Supply chain malware | A compromised npm package, PyPI package or extension reads | No plaintext file to read. Values sit in an encrypted vault that is locked most of the time. |
Infostealers | Commodity malware collects well-known paths such as | No ambient |
Accidental commits | A token pasted into |
|
AI coding agents | An agent running under your user can read every | Each MCP server or command receives only the keys bound to it, at launch time. |
Too-broad access | One | Per-consumer bindings and repo-scoped GitHub Apps limit what any single leak can do. |

Figure 1: Before, secrets live in plaintext files that any process can read. After, they live in a locked vault and are delivered on demand.
Why not just aws-vault, or a plain Keychain item?
Before building anything, I checked whether existing options were enough. As a Technical Leader, I also wanted something simple enough for every engineer on the team to adopt.
Option | What it does well | Why it was not enough for me |
|---|---|---|
aws-vault | Excellent model: OS keychain, short-lived sessions, inject into one command. | Only handles AWS credentials. |
A generic item in the login Keychain | Encrypted at rest, built into macOS. | The login Keychain stays unlocked for the whole desktop session, so any process running as me can read it most of the day. |
| Simple, every tool understands them. | Plaintext on disk, readable 24/7, and every script gets every key. |
Pasting tokens into MCP config | Fast to set up. | Plaintext, easy to commit, and the token is usually broad and long-lived. |
What I needed was the aws-vault model, plus three things none of the options above had: a separate vault that locks itself, native support for Claude Code's MCP integrations (stdio servers and headersHelper), and GitHub App tokens that refresh themselves.
How cmdn-locker works
Similar to the "Store → Exchange → Deliver" flow of aws-vault, cmdn-locker works in three steps:
Store.
cmdn-locker setsaves a value at a hidden prompt into a dedicated Keychain file,~/Library/Keychains/cmdn-locker.keychain-db. Secrets are organized by project group (for examplebilling,github,default), sobilling/SEARCH_API_KEYanddefault/SEARCH_API_KEYare two independent values.Check. When something asks for a secret,
cmdn-lockerloads the matching binding from a localmetadata.json(names only,0600), checks that the request matches it, and checks that the vault policy has not been weakened.Deliver. It reads only the bound keys and hands them to one consumer: a child process, a stdio MCP server, an HTTP header, or the GitHub CLI.

Figure 2: The vault holds the whole keyring. cmdn-locker checks each request and hands every consumer only the key bound to it.
The vault policy
cmdn-locker does not use the login Keychain. The dedicated vault has its own rules:
Setting | Default | Notes |
|---|---|---|
Master password | Chosen at | Minimum 12 characters, separate from your macOS password |
Idle auto-lock | 15 minutes | Enforced by macOS |
Hard session cap | 1 hour from unlock | Locks even if you keep using it |
Lock on sleep | Always on | Cannot be disabled |
Locked + no terminal |
| Show the native macOS password dialog (alternative: |
The vault starts locked and does not unlock when you log in. Before reading any secret, cmdn-locker also checks the Keychain settings. If something outside the tool turned off lock-on-sleep or raised the timeout, it locks the vault and refuses to continue until you unlock it again. Changing the policy with cmdn-locker config also requires an unlocked vault, so a background process cannot quietly weaken it.
Getting started
Create the vault once, then store secrets at a hidden prompt. Nothing is echoed to the screen or written to shell history:
$ cmdn-locker init
Create vault password:
Confirm vault password:
Initialized vault at /Users/<you>/Library/Keychains/cmdn-locker.keychain-db (idle TTL: 15m, max session: 1h, on-locked: gui)
$ cmdn-locker set SEARCH_API_KEY --project billing
$ cmdn-locker set DATABASE_URL --project billingListing a group shows names only and never reads secret values:
$ cmdn-locker list --project billing
Project: billing
Keys:
DATABASE_URL
SEARCH_API_KEY
Bindings:
(none)And status shows the lock state and the remaining session time:
$ cmdn-locker status
Vault: unlocked
Path: /Users/<you>/Library/Keychains/cmdn-locker.keychain-db
Settings status: ok
Keychain idle TTL: 15m
Lock on sleep: enabled
Configured idle TTL: 15m
Configured max session: 1h
Non-TTY on-locked: gui
Session remaining: 58m12sAfter migrating a secret, delete the plaintext copy. As I wrote in the aws-vault article: if you keep the plaintext file "just in case", you have not actually solved the problem.
Four ways to deliver a secret
1. Local commands: exec
Inject selected keys into one child process only. Your shell never holds them:
$ cmdn-locker exec --project billing --env SEARCH_API_KEY -- npm test
$ cmdn-locker exec --project billing --env DATABASE_URL,SEARCH_API_KEY -- npm run migrate--all injects every key in the group, but you must ask for it explicitly. If a key is missing or the vault cannot be unlocked, the command never starts.
2. Local stdio MCP servers: bind + run
Bind an MCP server binary to specific keys once on your Mac. The binding needs an absolute executable path and explicit DEST_ENV=VAULT_KEY mappings:
$ cmdn-locker bind --project billing --name ledger-local \
--env CLIENT_ID=LEDGER_ID \
--env CLIENT_SECRET=LEDGER_SECRET \
-- /opt/tools/ledger-mcp serve --stdioThen .mcp.json contains no secrets at all:
{
"mcpServers": {
"ledger": {
"type": "stdio",
"command": "cmdn-locker",
"args": ["run", "--project", "billing", "--binding", "ledger-local"]
}
}
}cmdn-locker keeps all of its own messages off stdout, so the MCP JSON-RPC stream stays clean.
3. Remote HTTP MCP servers: bind + headers
This is my favorite part for Claude Code. Claude Code supports a headersHelper: a command it runs to get auth headers for a remote MCP server. cmdn-locker headers plays that role.
$ cmdn-locker bind --project github --name github-remote \
--key GITHUB_PAT \
--server github \
--url https://api.githubcopilot.com/mcp/ \
--header Authorization \
--format bearer{
"mcpServers": {
"github": {
"type": "http",
"url": "https://api.githubcopilot.com/mcp/",
"headersHelper": "cmdn-locker headers --project github --binding github-remote"
}
}
}When Claude Code calls the helper, it passes the server name and URL in CLAUDE_CODE_MCP_SERVER_NAME and CLAUDE_CODE_MCP_SERVER_URL. cmdn-locker compares them character for character with the binding. If they do not match, it exits with an error before reading anything from Keychain. If they match, it reads exactly one key and prints one header. You can test it from the terminal before opening Claude Code:
$ CLAUDE_CODE_MCP_SERVER_NAME=github \
CLAUDE_CODE_MCP_SERVER_URL=https://api.githubcopilot.com/mcp/ \
cmdn-locker headers --project github --binding github-remote
{"Authorization":"Bearer <token>"}4. GitHub CLI with a repo-scoped GitHub App: github login + gh
A classic personal access token in gh is broad and long-lived. Instead, cmdn-locker signs in to a fine-grained GitHub App with OAuth Device Flow. The App is installed only on the repositories you choose, with only the permissions you need, for example Issues: Read-only.
$ cmdn-locker github login --project acme-issues \
--client-id Iv23liXXXXXXXXXXXXXX \
--repo "acme-org/billing-*" \
--open
$ gh auth logout # remove the ambient token
$ cmdn-locker --project acme-issues gh issue list --state openA few details I care about:
No client secret on the Mac. Device Flow needs only the App's public Client ID.
Short-lived tokens with auto-refresh. The user token (
ghu_...) expires after 8 hours. When it is close to expiry,cmdn-lockerexchanges the stored refresh token and updates the vault, forgh,exec,runandheaders.Group selection from Git remotes. Inside a repository,
cmdn-locker ghchecks all remotes (for exampleoriginas your fork andupstreamas the organization repo) and picks the most specific matching pattern. If nothing matches, it fails closed and never falls back to another token.
Combined with GitHub's read-only issues endpoint for the remote MCP server (https://api.githubcopilot.com/mcp/x/issues/readonly), this gives Claude Code exactly one capability: reading issues in the approved repositories.
My own routine
I do not unlock the vault before starting Claude Code. I just start working. When Claude Code launches an MCP server or calls the headersHelper while the vault is locked, macOS shows its native password dialog, I type the vault password once, and everything continues. If several MCP servers start at the same time, cmdn-locker serializes the unlock with a lock file, so I only see one dialog, not one per server.
I keep the default policy: 15 minutes idle, 1 hour hard cap, lock on sleep. I do not have to remember to lock the vault, because it locks itself, and the cost of unlocking again is one password dialog.
For SSH sessions, CI, or anyone who prefers no pop-ups, there is a strict mode. It exits with an error and empty output until you run cmdn-locker unlock in a terminal:
$ cmdn-locker config set-on-locked failWhat this does NOT protect against
cmdn-locker reduces what an attacker can collect, but it is not a complete isolation boundary:
While the vault is unlocked, it trusts your user account. Malware running as you during that window can call the tool or read the vault too. The short idle TTL, the hard session cap and lock-on-sleep make that window small, but they do not remove it.
A delivered secret is out of the tool's control. Once a process receives a token, it can use or leak it. Least privilege limits the damage. It does not prevent misuse.
Binding checks catch mistakes, not attackers. Matching the server name and URL prevents sending a token to the wrong endpoint by accident. It does not authenticate the caller, because a local process can set those environment variables itself.
Revocation still happens at the provider. Removing a binding stops future delivery. To invalidate a token that was already delivered, revoke it in the provider's console.
macOS only, single user.
This is why I see cmdn-locker as one layer, not the whole answer. It limits which secrets an agent receives. To limit what the agent can reach on your machine, combine it with the isolation I described in Claude Code Sandbox, or run unattended agents inside a container or VM.
Troubleshooting
Symptom | Cause and fix |
|---|---|
Claude Code cannot connect to an HTTP MCP server, and | The server name or URL in |
No password dialog appears and the MCP server fails over SSH | The |
| The Keychain settings were changed outside the tool (for example lock-on-sleep turned off). Run |
| No Git remote matches a bound repository pattern. Add one with |
A command started without a key you expected |
|
Conclusion
Moving AWS keys into aws-vault was my first step. cmdn-locker is the second one: the same idea applied to every other secret on my Mac, with first-class support for Claude Code and MCP. Daily work still feels the same. Claude Code connects to its MCP servers as usual, and the only new thing is one password dialog when the vault is locked.
The two ideas I would like you to take away are:
Configs hold names, never values.
.mcp.json, scripts and team templates should reference a secret, not contain it. Then they are safe to commit and share, and each engineer provisions their own values locally.Give each agent one key, for a short time. Bind each MCP server or command to the minimum set of keys, prefer short-lived and scoped tokens such as GitHub App tokens, and let the vault lock itself.
cmdn-locker is an internal tool for now, but the pattern does not depend on it. A locked OS keychain, configs that reference names, per-consumer bindings and short-lived tokens can be built with many tools. I hope this article pushes you to look at your own laptop and ask what an attacker would take first. If you manage secrets for AI agents in a different way, I would love to hear about it.
References
Stop Keeping AWS Keys in Plaintext (my previous article)
Claude Code Sandbox (my previous article)
Claude Code documentation: MCP, dynamic headers for custom authentication
GitHub Docs: Generating a user access token for a GitHub App (Device Flow)
ByteNess/aws-vault (the tool that inspired cmdn-locker)


