Agents and MCP
Coding agents need your keys to run tests and dev servers. With Latchkey they get them through latchkey run: the values reach the command, not the agent’s files, and you decide what an agent may use without asking.
The pattern
- Keep references in env files:
latchkey import .env.local --rewrite. - Run anything that needs secrets through
latchkey run. - Decide what an agent may read without you: agent-safe items for values that aren’t secret, grants for a folder and a time. Everything else asks you, showing the agent’s process chain, folder and command.
Fresh git worktrees don’t have your .env (it’s gitignored). latchkey run --env PROJECT/ENV loads an environment’s items directly, and a grant for a repository’s folder covers its worktrees.
lk run, and you allow the folder until the agent quits. Its next run goes through. A production item isn't covered, so it asks again, and a denial is exit 77. Example data; the output is what Latchkey prints.Skills and plugins
The Latchkey skill tells an agent to run commands through lk run, check before a slow run, never print or write a value, put references in env files, give you the lk set command for a missing key, suggest a session grant when you step away, and stop on 77 or 66.
- Claude Code: the plugin installs the skill and registers
lk mcpas an MCP server.~/.local/binmust be on thePATHClaude Code starts with. - Codex CLI, Gemini CLI, Cursor and other tools that read the Agent Skills format: copy the
plugins/latchkey/skills/latchkeyfolder into the skills folder your tool reads, such as.agents/skills/for Codex. - Claude Desktop and other MCP clients without a shell: add
lk mcpas a local MCP server. Chat apps that only reach MCP servers on the internet can’t use Latchkey, since its MCP server runs on your Mac.
/plugin install latchkey --marketplace zachshallbetter/latchkeyThe repository is public, so the marketplace install should work, though it hasn’t been tried end to end yet. You can also add the plugin from a local checkout with /plugin marketplace add /path/to/latchkey.
Instructions for agents
Without the skill, put this in CLAUDE.md, AGENTS.md or your agent’s equivalent:
Secrets are in Latchkey. Env files hold latchkey:// references, not values.
Run anything that needs secrets as
`latchkey run --env-file .env.local -- <command>` or
`latchkey run --env <project>/<environment> -- <command>`.
Never run `latchkey get`, never print the environment, and never read or
write values in .env files. If latchkey exits with 77 (refused) or 66
(missing), stop and ask; don't retry.Exit codes to act on
| Code | Meaning | What an agent should do |
|---|---|---|
| 0 | Done | |
| 64 | Wrong usage | Fix the command |
| 66 | No item with that ID | Stop; ask the person to add it |
| 69 | The Latchkey app can’t be reached | Stop; ask the person to open Latchkey |
| 73 | A file couldn’t be rewritten | Stop and report |
| 77 | Refused: not agent-safe, not approved, no grant | Stop; ask the person to approve or grant. Don’t retry |
| 127 | The command couldn’t start | Fix the command |
On 77 and 66 the message names the items and, for 77, the exact latchkey grant command that would allow them, for the person to run.
Leaving an agent working
latchkey grant 'shop/development/*' --session --for 4h # ends when the agent quits
latchkey grant 'shop/development/*' --command "npm test" --for 2h # only this command
latchkey grant 'shop/development/*' --folder ~/Projects/shop --for 2h # the folder, for a timeRun them inside the agent’s session (in Claude Code, start the line with !) so --session ties to the agent. The agent’s reads go through without asking until the grant ends, and each one is in the app’s Activity. End it early from Grants in the app or with latchkey revoke ID.
Keys an agent can use but never read
For OpenAI and Anthropic keys, latchkey run --proxy (or proxy: true on latchkey_run) gives the command a placeholder and a local base URL, and adds the real key only to requests to the provider. An agent’s command can call the API but can’t print, encode or send the key anywhere else. See --proxy.
Only the command and the processes it starts can use the proxy; a request from any other program gets a 403, and the proxy stops when the command exits. To narrow what the command may do with the key, add --proxy-models A,B (only requests naming a listed model are forwarded) and --proxy-requests N (refuse requests after N). On latchkey_run these are proxy_models, an array of model names, and proxy_requests, a number; both need proxy: true. Latchkey says on standard error that the limits are enforced.
latchkey run --proxy --proxy-models gpt-4o-mini --proxy-requests 100 --env shop/development -- npm testAfter an agent session
See what the agent was given, and replace anything you’d rather it no longer knew:
latchkey touched --since 4h --by claude
latchkey rotate --touched --since 4h --by claude --open --pushFrom another Mac over SSH
Forward Latchkey’s socket when you connect, then point latchkey on the other Mac at it:
ssh -o StreamLocalBindUnlink=yes -R /tmp/latchkey-$USER.sock:"$HOME/Library/Application Support/Latchkey/broker.sock" devmacexport LATCHKEY_SOCKET=/tmp/latchkey-$USER.sock
latchkey run --env shop/development -- npm test$USER expands to your login name on each Mac: use the same name on both, or put a fixed name in both commands. Forwarding needs the direct-download app on your Mac; the Mac App Store version doesn’t support it yet, and if the other Mac has the App Store version installed, set LATCHKEY_BROKER=direct there.
- Each read that is not agent-safe asks. Your Mac sees only your
sshclient, so grants don’t apply and can’t be made over the connection. - The approval panel says so, and shows what the tool on the other computer says it’s running, marked as unchecked.
- The tool there can’t check the app’s signature through a forwarded socket;
latchkey statussays so. - The other computer needs
latchkeytoo; the tool is macOS only.
Latchkey’s MCP server
latchkey mcp gives agents Latchkey’s tools over MCP: for clients without a shell, like Claude Desktop, and for structured refusals instead of exit codes. It has no direct secret-read tool. Child output masks exact secret values; transformed secrets can still leak.
claude mcp add latchkey -- ~/.local/bin/latchkey mcp{
"mcpServers": {
"latchkey": { "command": "/Users/you/.local/bin/latchkey", "args": ["mcp"] }
}
}| Tool | What it does | Asks you? |
|---|---|---|
latchkey_status | Where values are kept, how you approve, item and grant counts | No |
latchkey_list | Item IDs with kind, service and agent-safe, by project or environment. Never values | No |
latchkey_check | What a run would get without asking, what would ask, and what’s missing, for env_files, environments, files or ids | No |
latchkey_run | Runs command (an argument array) in cwd with references filled in. Returns the exit code and the output, masked. With proxy, proxy_models and proxy_requests limit what the command may do with the key (see above) | When an item isn’t agent-safe or granted |
latchkey_request_grant | Asks you for a grant on ids in the agent’s folder for duration | Always |
latchkey_project | Which project the folder’s checkout is, from its Git remotes, and its environments | No |
latchkey_export_references | Writes an environment’s env file of references and plain settings, never values, inside its cwd only | No |
- Output is always masked, and only the last 64 KB of each stream is returned, with a flag when it was cut. Standard input is empty.
- Commands stop after
timeout_seconds: 600 by default, at most 3600. Use it for tests, builds and scripts; run dev servers in a shell withlatchkey run. A cancelled request stops its command. - Grants an agent asks for cover only the folder it runs in (and that repository’s worktrees), last for the time it asked for (which you see and approve), end when the agent quits, and can’t include production environments or a whole project. Grant those yourself with
latchkey grant. - Approval panels for requests through the MCP server say they come from an agent.
- Several requests in a row from the same agent in the same folder share one panel while it’s up, and one touch answers them; a grant until the agent quits stops the asking for the rest of the session.
- Reading values, changing or deleting items, earlier values, remote locations and pushes aren’t offered.
{
"content": [{ "type": "text", "text": "exit code 0
--- stdout ---
12 passing" }],
"structuredContent": {
"exitCode": 0, "timedOut": false,
"stdout": "12 passing", "stdoutTruncated": false,
"stderr": "", "stderrTruncated": false
},
"isError": false
}A refusal is a tool error (isError: true) whose structuredContent has error (refused, missing, unreachable, usage), the ids, and for a refusal the grantCommand for the person to run.
Other MCP servers
An MCP server that your agent starts as a command can get its keys through Latchkey: wrap the command, and keep references in its environment.
{
"mcpServers": {
"some-server": {
"command": "latchkey",
"args": ["run", "--no-masking", "--", "npx", "-y", "some-mcp-server"],
"env": { "SOME_API_KEY": "latchkey://SOME_API_KEY" }
}
}
}- Use
--no-masking: MCP speaks over standard output, which must pass through untouched. - The server’s process chain includes the agent, so make its items agent-safe or grant them, or each start asks.
- Servers reached over HTTP with a header can’t be wrapped this way.
Agents need the absolute path when ~/.local/bin isn’t on their PATH: use "command": "/Users/you/.local/bin/latchkey".
Key files
latchkey run --file ASC_KEY_PATH=shop/production/ASC_P8 -- fastlane betaThe command gets the path of a temporary file only you can read, deleted when it exits. In an env file, ASC_KEY_PATH=latchkey-file://shop/production/ASC_P8 does the same.
What Latchkey can’t do for you
A value you release to a command is in that command’s memory and environment. A command run by an agent can write it to a file, send it over the network, or encode it so masking doesn’t catch it. Masking only hides exact copies, 6 characters or longer, in what the command prints. Agent-safe and grants decide what reaches the command; they can’t control what the command does with it.