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

  1. Keep references in env files: latchkey import .env.local --rewrite.
  2. Run anything that needs secrets through latchkey run.
  3. 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.

An agent runs its tests through 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
/plugin install latchkey --marketplace zachshallbetter/latchkey

The 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:

CLAUDE.md / AGENTS.md
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

CodeMeaningWhat an agent should do
0Done
64Wrong usageFix the command
66No item with that IDStop; ask the person to add it
69The Latchkey app can’t be reachedStop; ask the person to open Latchkey
73A file couldn’t be rewrittenStop and report
77Refused: not agent-safe, not approved, no grantStop; ask the person to approve or grant. Don’t retry
127The command couldn’t startFix 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

Terminal
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 time

Run 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.

Terminal
latchkey run --proxy --proxy-models gpt-4o-mini --proxy-requests 100 --env shop/development -- npm test

After an agent session

See what the agent was given, and replace anything you’d rather it no longer knew:

Terminal
latchkey touched --since 4h --by claude
latchkey rotate --touched --since 4h --by claude --open --push

From another Mac over SSH

Forward Latchkey’s socket when you connect, then point latchkey on the other Mac at it:

Terminal
ssh -o StreamLocalBindUnlink=yes -R /tmp/latchkey-$USER.sock:"$HOME/Library/Application Support/Latchkey/broker.sock" devmac
On the other Mac
export 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.

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.

Terminal
claude mcp add latchkey -- ~/.local/bin/latchkey mcp
Other clients
{
  "mcpServers": {
    "latchkey": { "command": "/Users/you/.local/bin/latchkey", "args": ["mcp"] }
  }
}
ToolWhat it doesAsks you?
latchkey_statusWhere values are kept, how you approve, item and grant countsNo
latchkey_listItem IDs with kind, service and agent-safe, by project or environment. Never valuesNo
latchkey_checkWhat a run would get without asking, what would ask, and what’s missing, for env_files, environments, files or idsNo
latchkey_runRuns 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_grantAsks you for a grant on ids in the agent’s folder for durationAlways
latchkey_projectWhich project the folder’s checkout is, from its Git remotes, and its environmentsNo
latchkey_export_referencesWrites an environment’s env file of references and plain settings, never values, inside its cwd onlyNo
A latchkey_run result
{
  "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.

.mcp.json
{
  "mcpServers": {
    "some-server": {
      "command": "latchkey",
      "args": ["run", "--no-masking", "--", "npx", "-y", "some-mcp-server"],
      "env": { "SOME_API_KEY": "latchkey://SOME_API_KEY" }
    }
  }
}

Agents need the absolute path when ~/.local/bin isn’t on their PATH: use "command": "/Users/you/.local/bin/latchkey".

Key files

Terminal
latchkey run --file ASC_KEY_PATH=shop/production/ASC_P8 -- fastlane beta

The 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.