Skip to content

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/refunds over 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.

Terminal window
npx @humanauth/cli guard init --target hu_… # writes humanauth.policy.yaml and humanauth.guard.yaml
npx @humanauth/guard secret set stripe # the OS prompts for the key and stores it in the keychain

humanauth.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.yaml
api_key: env:HUMANAUTH_API_KEY
tenant: 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.ai
default: allow
rules:
- match: { tool: "stripe.request", args: { method: "POST", path: "/v1/refunds", "body.amount": "> 5000" } }
require: human
action: "stripe:refund"
Terminal window
npx @humanauth/guard check # resolves every secret and lists the tools; prints no values

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”
Terminal window
npx @humanauth/guard run -- claude

The command runs with the secret variables removed from its environment and HUMANAUTH_RELAY_URL set:

Terminal window
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:

Terminal window
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).

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",
});

A person takes seconds to minutes to answer, so the guard never assumes one short request.

CallerWhile the person decidesIf the caller gives up
MCP client that supports MCP TasksThe call returns a task at once; the client polls tasks/get and the finished task holds the API’s responsetasks/cancel cancels the approval request
Any other MCP clientThe call stays open and reports progress every 5 seconds when asked toCancelling the call or closing the connection cancels the approval request
HTTP (curl, relayFetch, run --)The connection stays openHanging 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_SECRET

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

FormReads from
env:NAMEan environment variable of the guard
file:patha file
keychain:service/accountmacOS Keychain, or the Secret Service on Linux
op://vault/item/fieldthe 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-token

When the provider rejects the grant, calls fail with relay_authorization_required so your code can ask someone to connect the account again.

  • 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 under raw_body_types), or a key sent twice (unless you set allow_repeated_keys: true).
  • Checks the allow list 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 removes Set-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.
  • 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: deny and require: human are for.
  • It protects only what goes through it. If the agent can read the same key from a .env file 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 use default: deny with allow rules, for anything that matters.
  • Keep humanauth.guard.yaml where 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.