I’ve been moving the plaintext tokens out of my zsh env file and into 1Password, leaving only op:// secret references behind, which worked fine for the scripts I already run through op run, but it broke the MCP servers my coding agent uses at work in a way that looked like a bad token. This is a note on why that happens, the small change in the MCP config that fixes it, and what replaced that change once 1Password started asking for approval in every new session, mostly so I don’t have to work it out again.
The setup
The agent (Claude Code, in the desktop app) has a few MCP servers configured in the project’s .mcp.json. Some of them are HTTP servers that sign in with OAuth, and the rest are stdio servers, which the app starts as child processes and passes their credentials through environment variables. The Bitbucket one looked like this:
{
"mcpServers": {
"bitbucket": {
"command": "npx",
"args": ["-y", "<bitbucket-mcp-package>"],
"env": {
"BITBUCKET_EMAIL": "${ATLASSIAN_EMAIL}",
"BITBUCKET_TOKEN": "${ATLASSIAN_BB}"
}
}
}
}
The ${...} values are expanded from the app’s own environment, which comes from the shell when the app is started from one (mine is, through a shell alias), and the shell reads them from ~/.zshenv. Rotating the Bitbucket token seemed like a good moment to replace the token and the account email there with references to 1Password items (the item names below are made up):
export ATLASSIAN_EMAIL="op://Private/Atlassian/email"
export ATLASSIAN_BB="op://Private/Bitbucket MCP/token"
Why only some servers broke
After restarting the app, every Bitbucket call failed with an authentication error (401) asking to check the username and token, while the Atlassian server, which talks to the same account, kept working. A freshly rotated token that gets a 401 looks like a bad token, so I spent a while on the token itself (and on stat, which said ~/.zshenv hadn’t changed in a year, because it’s a symlink and macOS stat describes the link itself unless you pass -L), but the app had been restarted after the edit, and a plain curl with the same values got a 401 as well, which pointed at the values rather than at the app or the server.
To see what the variable holds without printing any of it, we can test its prefix and print only the length:
zsh -c '[[ $ATLASSIAN_BB == op://* ]] && print "reference, length ${#ATLASSIAN_BB}" || print "not a reference, length ${#ATLASSIAN_BB}"'
# reference, length 46
This shows what a fresh shell gets from the env file, which here is also what the app got, since the app was started after the edit. A 46-character op:// string is not a token (a scoped Bitbucket API token is 192 characters), and the email turned out to be a reference too, so the server was sending the references themselves as the username and password, and Bitbucket rejected them, as it should. The Atlassian server never noticed because it signs in with OAuth and doesn’t read these variables at all.
An
op://reference is just a string until something resolves it, and nothing between the env file and the MCP server does that. On the command line, that’s the job ofop run,op readorop inject.
Starting the server through op run
op run scans its own environment for op:// references and replaces each one with the secret it points to before it starts the command after --. So instead of starting the server directly, we let the app start op, and op starts the server:
{
"mcpServers": {
"bitbucket": {
"command": "/opt/homebrew/bin/op",
"args": ["run", "--", "npx", "-y", "<bitbucket-mcp-package>"],
"env": {
"BITBUCKET_EMAIL": "${ATLASSIAN_EMAIL}",
"BITBUCKET_TOKEN": "${ATLASSIAN_BB}"
}
}
}
}
The app still expands ${ATLASSIAN_BB} to the op:// string and puts it in the child’s environment, op run finds it there and swaps in the real token, and the server starts with the same values it had before the cleanup. Two other stdio servers read their credentials from the same env file, so they got the same wrapper.
op run resolves every reference it finds in its environment, not only the two the server needs, so if the server inherits the rest of the app’s environment, it gets every secret you moved into the env file. It also masks resolved secrets in the child’s output by default, and for a stdio server that output is the protocol stream, so a tool result that contains the account email comes back with the email concealed (one more reason the email went back to a plain value later on).
The same goes for anything else that reads those variables directly, like a curl one-liner for the Bitbucket API, which went from 401 to 200 once it was wrapped. If you write one, pass the credentials to curl as a config on stdin, so the real token doesn’t end up in curl’s arguments, where anyone running ps could see it:
op run -- sh -c 'printf "user = \"%s:%s\"\n" "$ATLASSIAN_EMAIL" "$ATLASSIAN_BB" |
curl -s -K - -o /dev/null -w "%{http_code}\n" \
"https://api.bitbucket.org/2.0/repositories/<workspace>/<repo>/pullrequests"'
The sh -c with single quotes is needed here. Without it, our own shell expands $ATLASSIAN_BB to the op:// string while building the command line, and op run only resolves environment variables, not arguments, so curl would get the reference again.
The other way to do this is export ATLASSIAN_BB="$(op read 'op://...')" in the env file, which works, but every zsh reads .zshenv, so it runs op read on every shell start, which is slow and prompts a lot, and it puts the real secret into the environment of every process the shell launches, which is more or less what the cleanup was meant to stop.
Reading the tokens from the Keychain instead
op run fixed the 401, but it brought a 1Password approval prompt into every new agent session. The CLI’s approval belongs to the terminal it was given in and runs out after ten idle minutes, while the MCP servers the app starts have no terminal at all, so in my setup every new session asked again, which got annoying fast, and when nobody was there to approve it, the server didn’t start.
What works better for MCP servers is a copy of each token in the macOS login Keychain, made once from 1Password, and a small launcher that reads the copies and starts the server. The env file still exports the op:// references, so 1Password stays the place the tokens come from. The copying is a loop that has to run in zsh, in a terminal, where one 1Password approval covers every op read in it (the service names are made up):
for pair in ATLASSIAN_BB:bitbucket-mcp-token OTHER_TOKEN:other-mcp-token; do
var=${pair%%:*}; svc=${pair#*:}
if tok=$(op read "${(P)var:?}") && [[ -n $tok ]]; then
security add-generic-password -U -a "$USER" -s "$svc" -w "$tok"
else
echo "skipped $svc" >&2
fi
done
${(P)var} is zsh for the value of the variable whose name is in var, and the :? makes it fail instead of expanding to nothing, so a locked 1Password or a typo in a variable name skips that item rather than writing an empty password over a good copy (bash doesn’t know (P) at all, so there every item fails the op read line and gets skipped). -U updates an item that already exists. The token does sit in security’s arguments for a moment, where other processes running as you could see it in ps, so if you’d rather paste it, ending the command with a bare -w makes security ask for the value with hidden input.
The launcher takes VAR=service pairs, reads each one from the Keychain, exports only those and then execs the server, so each server gets its own tokens and no others:
#!/usr/bin/env bash
# Usage: keychain-run.sh VAR=service [VAR=service ...] -- command [args ...]
set -euo pipefail
while [[ $# -gt 0 && "$1" != "--" ]]; do
[[ "$1" == *=* ]] || { echo "keychain-run: expected VAR=service, got '$1'" >&2; exit 2; }
var="${1%%=*}"
service="${1#*=}"
value="$(security find-generic-password -a "$USER" -s "$service" -w)" && [[ -n "$value" ]] ||
{ echo "keychain-run: could not read '$service' from the Keychain (needed for $var)" >&2; exit 1; }
export "$var=$value"
shift
done
[[ $# -ge 2 ]] || { echo "usage: keychain-run.sh VAR=service ... -- command [args ...]" >&2; exit 2; }
shift
exec "$@"
The app runs it directly, so it has to be executable (chmod +x keychain-run.sh). In .mcp.json the launcher becomes the command and the token leaves the env block:
{
"mcpServers": {
"bitbucket": {
"command": "/Users/me/bin/keychain-run.sh",
"args": ["BITBUCKET_TOKEN=bitbucket-mcp-token", "--", "npx", "-y", "<bitbucket-mcp-package>"],
"env": {
"BITBUCKET_EMAIL": "${ATLASSIAN_EMAIL}"
}
}
}
}
The email went back to a plain value in the env file (export ATLASSIAN_EMAIL="[email protected]"), since it was never a secret. That change needs the app quit and started again, because the app reads its environment only when it starts, so a new session alone still sends the old op:// string and gets the 401 from the beginning of this post. The loop also has to run before the config changes, otherwise the launcher has nothing to read and the server doesn’t start. The other two servers got the same change.
security documents that the application that creates an item is trusted to read it without a warning, so reading the copies back with security doesn’t prompt, and as long as the login keychain is unlocked, which it normally is for as long as you’re logged in, new sessions start their servers without asking anything. The flip side is that anything running as you can read the tokens the same way, including the agent’s own shell commands, which under op run would at least have needed an approval, and the Keychain’s encryption only helps while it’s locked. For a signing key that’s a trade I wouldn’t make, but these tokens are scoped to a couple of APIs and revoking one takes a minute, so for tokens the agent’s tools use all day I’m fine with it, and in a terminal I still use op run, where one prompt per window is no bother. The best version would be OAuth for every server, the way the Atlassian one already works, but some servers only take a token.
Rotating a token now means updating the 1Password item, running the loop again and restarting the servers, which a new session does, while servers in a session that’s already open keep the token they started with until they’re reconnected. If a server gets a 401 right after a rotation, a stale Keychain copy is the first thing to suspect.
Summary
Moving credentials to op:// references is worth doing, but it changes what every reader of those variables gets, so before you switch, it pays to list who reads them (MCP servers that take them in env, scripts, one-liners from your notes). Putting op run -- in front of a command is enough for anything started by hand, while MCP servers, which every new agent session starts again, are better off with a copy in the Keychain and a small launcher, at the cost of the tokens being readable by anything running as you, the agent included. Servers that sign in on their own with OAuth won’t notice any of this. When a token gets a 401, checking whether the variable holds a reference or a token takes a second, and doing it before suspecting the token would have saved me a few rounds of guessing. With the launcher in place, a 401 right after a rotation usually means the Keychain copy is stale.