View as Markdown

Mandate · Guide

Gate Claude agent tools

Use @provenanceauth/claude-agent-sdk (TypeScript) or provenanceauth-claude-agent-sdk (Python) to add approvals to a Claude Agent SDK agent. A PreToolUse hook authorizes and verifies each listed tool call on its exact input before the tool runs, and waits for a person when your policy asks for one.

Before you begin: install the package for your language, and export the sandbox account key.

export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5

This package isn't on npm yet. This package isn't on PyPI yet. Until then, every request in these docs runs over HTTP against the sandbox.

With access to the private Provenance repository, which your Provenance contact grants, install from it:

git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-sdk-1.0.0.tgz ../provenance-packs/provenanceauth-agent-adapters-1.0.0.tgz ../provenance-packs/provenanceauth-claude-agent-sdk-1.0.0.tgz
git clone https://github.com/michaelnbane-jpg/provenance.git provenance
pip install ./provenance/python/sdk ./provenance/python/claude-agent-sdk-provenance

Availability: the packages are available to beta customers. They support the Claude Agent SDK 0.2 and 0.3 in TypeScript and 0.2 in Python.

1. Add the hook

provenanceHooks returns the hooks option for query(). List the tools to gate by name; a trailing * matches every tool on an MCP server:

import { query } from "@anthropic-ai/claude-agent-sdk";
import { Provenance } from "@provenanceauth/sdk";
import { provenanceHooks } from "@provenanceauth/claude-agent-sdk";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });

for await (const message of query({
  prompt: "Pay invoice INV-2041 from Brindlecote Supplies",
  options: {
    mcpServers: { billing },
    hooks: provenanceHooks({
      tools: ["mcp__billing__pay_invoice", "Bash"],
      client: prov,
      deviceId: "dev_00000000000000000000000000000001",
    }),
  },
})) {
  console.log(message);
}

Each call is bound to the SDK's own session id as the agent session. By default the action description is the tool name and the canonical JSON of its input, and the payload is { "tool", "input" }, so the whole input is hash-bound to the token.

2. Read the outcome

The hook answers one of three ways:

OutcomeWhat happens
VerifiedThe hook allows the call, and the tool runs with the input that was verified
DeniedThe hook denies the call, and the model reads a sentence such as "The person declined this action."
Waiting for approvalThe hook waits for the person, up to timeoutMs (default 2 minutes), then allows or denies

If the person hasn't answered when the wait ends, the model reads "Waiting for approval (appr_…)", and the next call with the same input reuses the token that opened the approval. Tools you don't list get no decision from the hook, so the SDK's own permissions apply to them. If Provenance can't be reached, a listed tool is denied.

3. Describe the action for the person

Pass per-tool options to name the action in words and to give your policy its facts:

const hooks = provenanceHooks({
  tools: {
    mcp__billing__pay_invoice: {
      describe: (a) => `Pay invoice ${a.invoice}: $${a.amount} to ${a.payee}`,
      context: (a) => ({ action_type: "payment", transaction_amount: Number(a.amount) }),
    },
  },
  client: prov,
  deviceId: "dev_00000000000000000000000000000001",
  timeoutMs: 60_000,
});
describefunction
The action bound into the token and shown to the person.
payloadfunction
The payload bound into the token. Default: the tool name and its whole input.
contextfunction
Facts for your policy, read at verify.
runIdstring
An agent-team run to bind each call to (run_id in Python).
mandateTokenstring
A grant to authorize under, such as a subagent's own limits (mandate_token).
agentIdstring
The registered agent making the calls (agent_id).
timeoutMsnumber
How long to wait for the person (timeout_ms). 0 answers at once.

4. Use canUseTool instead

If your app decides permissions in canUseTool, use provenanceCanUseTool (provenance_can_use_tool in Python). It takes the same options plus agentSessionId, because the callback carries no session id. Tools you don't list go to fallback, which allows them by default:

const canUseTool = provenanceCanUseTool({
  tools: ["mcp__billing__pay_invoice"],
  client: prov,
  deviceId: "dev_00000000000000000000000000000001",
  agentSessionId: "sess_claude_1",
});

5. Gate Claude Code with a command hook

The provenance-hook command runs the same decision as a Claude Code PreToolUse command hook.

This package isn't on npm yet. Until then, every request in these docs runs over HTTP against the sandbox.

With access to the private Provenance repository, which your Provenance contact grants, install from it:

git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-sdk-1.0.0.tgz ../provenance-packs/provenanceauth-agent-adapters-1.0.0.tgz ../provenance-packs/provenanceauth-claude-agent-sdk-1.0.0.tgz

The command reads ~/.provenance/hook.json (or the file in PROVENANCE_HOOK_CONFIG), and environment variables override it:

FieldEnvironment variableWhat it sets
api_keyPROVENANCE_KEYThe key the hook authorizes with
device_idPROVENANCE_DEVICE_IDThe person's enrolled device
toolsPROVENANCE_HOOK_TOOLSThe tools to gate, comma-separated in the variable
timeout_msPROVENANCE_HOOK_TIMEOUT_MSHow long to wait for the person (default 120 000)
api_basePROVENANCE_APIThe API base URL

A tool that isn't listed gets no output, so Claude Code's own permissions apply. A listed tool with no key or device is denied with a sentence that says so.

For your own Claude Code

To ask your own phone instead of an account's approver, connect Claude Code with the Provenance connect tool:

This package isn't on npm yet. Until then, every request in these docs runs over HTTP against the sandbox.

With access to the private Provenance repository, which your Provenance contact grants, install from it:

git clone https://github.com/michaelnbane-jpg/provenance.git provenance
cd provenance && pnpm install && pnpm build
node scripts/check-publishable.mjs --out ../provenance-packs
cd ../your-project
npm install ../provenance-packs/provenanceauth-connect-1.0.0.tgz

It signs you in with a code and writes hook.json with a person_key (your assistant's key) in place of api_key and device_id. Then every tool call goes through Provenance, so you can stop or limit a session from Provenance, and the listed tools wait for your approval. See connect your own assistant.

Errors you might see

SymptomCauseFix
The model reads "Waiting for approval"The person hadn't answered within timeoutMsRaise timeoutMs, and the matcher or command timeout with it.
The model reads "This agent session was turned off in Provenance."The agent session was revokedStart a new session.
A new approval on every callThe tool's input changes between callsRetry with the same input, or describe the action with stable fields.
A listed tool is never gatedThe name doesn't matchMCP tools are named mcp__<server>__<tool>; use mcp__<server>__* for a whole server.

See also