Guard & Credential Broker
An agent that holds an API key can leak it, paste it into a log, or use it for something nobody asked for. @humanauth/guard takes the key away from the agent without taking away the API:
- The guard holds the credential. The agent names a method and a path. The guard adds the key and makes the call.
- Risky calls wait for a person. A rule can require approval for
POST /v1/refundsover a certain amount. The approver sees the method, path and body on their phone or Mac, and the call goes out only after the signed receipt for that exact request verifies. - The credential is attached after the receipt check and at no other time. An approval for one request does not release a different one, and each receipt works once.
It runs on your machine or server. Your keys stay there; HumanAuth never receives them.
Set it up
Section titled “Set it up”npx @humanauth/cli guard init --target hu_… # writes humanauth.policy.yaml and humanauth.guard.yamlnpx @humanauth/guard secret set stripe # the OS prompts for the key and stores it in the keychainhumanauth.guard.yaml says where each credential lives and which APIs may use it. It holds no secret, so it can be committed:
policy: humanauth.policy.yamlapi_key: env:HUMANAUTH_API_KEYtenant: ten_…
secrets: stripe: keychain:humanauth-guard/stripe
http: - preset: stripe secret: stripe allow: ["GET /v1/*", "POST /v1/refunds"]humanauth.policy.yaml says which calls need a person:
target: hu_…server: https://api.humanauth.aidefault: allowrules: - match: { tool: "stripe.request", args: { method: "POST", path: "/v1/refunds", "body.amount": "> 5000" } } require: human action: "stripe:refund"npx @humanauth/guard check # resolves every secret and lists the tools; prints no valuesUse it
Section titled “Use it”From an MCP client
Section titled “From an MCP client”Add the guard as a server. The client’s config carries no key:
{ "mcpServers": { "humanauth-guard": { "command": "npx", "args": ["-y", "@humanauth/guard", "--config", "/path/to/humanauth.guard.yaml"] } }}Each http: entry shows up as one tool, <name>.request. MCP servers listed under upstreams: are started by the guard with only the secrets their entry names, and their tools are passed through.
From a script, an SDK or a coding agent’s shell
Section titled “From a script, an SDK or a coding agent’s shell”npx @humanauth/guard run -- claudeThe command runs with the secret variables removed from its environment and HUMANAUTH_RELAY_URL set:
curl "$HUMANAUTH_RELAY_URL/stripe/v1/charges?limit=3"If you already have a full URL, keep it and send it in a header instead:
curl "$HUMANAUTH_RELAY_URL" -H "X-Relay-URL: https://api.stripe.com/v1/charges?limit=3"A call that needs approval holds the connection open until the person answers. If you hang up first, the approval request is cancelled and nothing is sent, even if the approval arrives later. A response that came from the API carries X-Relay-Upstream-Status; without that header, the guard itself answered (for example 403 relay_refused).
From TypeScript
Section titled “From TypeScript”import { relayFetch } from "@humanauth/guard/client";
const res = await relayFetch("stripe", "/v1/refunds", { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body: "charge=ch_123&amount=9000",});How a gated call waits
Section titled “How a gated call waits”A person takes seconds to minutes to answer, so the guard never assumes one short request.
| Caller | While the person decides | If the caller gives up |
|---|---|---|
| MCP client that supports MCP Tasks | The call returns a task at once; the client polls tasks/get and the finished task holds the API’s response | tasks/cancel cancels the approval request |
| Any other MCP client | The call stays open and reports progress every 5 seconds when asked to | Cancelling the call or closing the connection cancels the approval request |
HTTP (curl, relayFetch, run --) | The connection stays open | Hanging up cancels the approval request |
In every row, an approval that lands after the caller has gone releases nothing.
Set how long the approver has, and optionally have the platform post each signed decision to a backend of yours:
approval: ttl: 300 callback_url: https://ops.example.com/hooks/humanauth callback_secret: env:HUMANAUTH_HOOK_SECRETThe webhook is for the system that records or reacts to decisions. The guard itself still polls: it runs on a laptop or inside a private network and has no address the platform could call. See the webhooks guide for the payload and how to check its signature.
Where a secret can live
Section titled “Where a secret can live”| Form | Reads from |
|---|---|
env:NAME | an environment variable of the guard |
file:path | a file |
keychain:service/account | macOS Keychain, or the Secret Service on Linux |
op://vault/item/field | the 1Password CLI |
A pasted value is refused at startup. For an OAuth connection, store the refresh token (or client credentials) and the guard mints and renews the access token itself:
secrets: google: oauth: token_url: https://oauth2.googleapis.com/token client_id: env:GOOGLE_CLIENT_ID client_secret: keychain:humanauth-guard/google-client-secret refresh_token: keychain:humanauth-guard/google-refresh-tokenWhen the provider rejects the grant, calls fail with relay_authorization_required so your code can ask someone to connect the account again.
What the guard does to every HTTP call
Section titled “What the guard does to every HTTP call”- Sends it only to the configured
base_url. The agent supplies a path, never a URL, and each path has one accepted spelling. - Reads a JSON or form body as fields, so a rule can test
body.amount. A body it cannot read the way the API would is refused: invalid JSON, a content type other than JSON or form (unless you list it underraw_body_types), or a key sent twice (unless you setallow_repeated_keys: true). - Checks the
allowlist before anyone is asked to approve. - Drops the caller’s
Authorization, cookies and every header outside a short safe list, then adds its own. The agent cannot swap in a key of its choosing. - Returns redirects to the caller and does not follow them.
- Replaces any copy of a secret in the response with
[secret:<name>], and removesSet-Cookie. - Writes one audit line: what was called, what policy decided, the receipt id if a person approved. No credential and no argument value is written. Each line carries the hash of the one before, and
npx @humanauth/guard audit verify <file>reports where a line was changed or removed.
What it does not do
Section titled “What it does not do”- It keeps a key from being read. It does not stop a permitted call from misusing the access that key has; that is what
allow,default: denyandrequire: humanare for. - It protects only what goes through it. If the agent can read the same key from a
.envfile or a signed-in CLI, it does not need the guard. - It holds one set of credentials. There is no per-user connection store and no hosted consent screen.
- Code has to call the guard’s tool or URL. Programs that hard-code a provider’s host are not intercepted.
run --is not a sandbox. A program with a shell runs as you, and can read the same environment, keychain item or file the guard reads. To hold keys against an agent that has a shell, run the guard as a different OS user or outside the agent’s container, and give the agent only the relay address.- A rule that gates on a field only fires when the field is there, and matching is case-sensitive text. Gate whole operations (
method+path), or usedefault: denywithallowrules, for anything that matters. - Keep
humanauth.guard.yamlwhere the agent cannot write to it. Whoever edits that file decides where the credentials go.
The full reference (every config key, the built-in presets, error codes, the approval plan) is in the package README.