# Gate Claude agent tools

Add approvals to a Claude Agent SDK agent in TypeScript or Python, so each risky tool call is authorized and verified on its exact input, with a person's approval when your policy asks, before it runs.

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](https://provenanceauth.com/docs/concepts/policies.md) asks for one.

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

```bash
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](https://provenanceauth.com/docs/get-started/sandbox.md).

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

```bash
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
```

```bash
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:

**Node**

```ts
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);
}
```

**Python**

```python
import os
from claude_agent_sdk import ClaudeAgentOptions, query
from provenanceauth_claude_agent_sdk import provenance_hooks
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])

options = ClaudeAgentOptions(
    mcp_servers={"billing": billing},
    hooks=provenance_hooks(
        tools=["mcp__billing__pay_invoice", "Bash"],
        client=prov,
        device_id="dev_00000000000000000000000000000001",
    ),
)

async for message in query(prompt="Pay invoice INV-2041 from Brindlecote Supplies", options=options):
    print(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:

| Outcome | What happens |
| --- | --- |
| Verified | The hook allows the call, and the tool runs with the input that was verified |
| Denied | The hook denies the call, and the model reads a sentence such as "The person declined this action." |
| Waiting for approval | The 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:

**Node**

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

**Python**

```python
hooks = provenance_hooks(
    tools={
        "mcp__billing__pay_invoice": {
            "describe": lambda a: f"Pay invoice {a['invoice']}: ${a['amount']} to {a['payee']}",
            "context": lambda a: {"action_type": "payment", "transaction_amount": a["amount"]},
        },
    },
    client=prov,
    device_id="dev_00000000000000000000000000000001",
    timeout_ms=60_000,
)
```

- `describe` (function): The action bound into the token and shown to the person.
- `payload` (function): The payload bound into the token. Default: the tool name and its whole input.
- `context` (function): Facts for your [policy](https://provenanceauth.com/docs/concepts/policies.md), read at verify.
- `runId` (string): An [agent-team run](https://provenanceauth.com/docs/mandate/guides/halt-a-run.md) to bind each call to (`run_id` in Python).
- `mandateToken` (string): A grant to authorize under, such as a [subagent's own limits](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md) (`mandate_token`).
- `agentId` (string): The registered agent making the calls (`agent_id`).
- `timeoutMs` (number): 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:

```ts
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](https://provenanceauth.com/docs/get-started/sandbox.md).

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

```bash
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:

| Field | Environment variable | What it sets |
| --- | --- | --- |
| `api_key` | `PROVENANCE_KEY` | The key the hook authorizes with |
| `device_id` | `PROVENANCE_DEVICE_ID` | The person's enrolled device |
| `tools` | `PROVENANCE_HOOK_TOOLS` | The tools to gate, comma-separated in the variable |
| `timeout_ms` | `PROVENANCE_HOOK_TIMEOUT_MS` | How long to wait for the person (default 120 000) |
| `api_base` | `PROVENANCE_API` | The 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](https://provenanceauth.com/docs/get-started/sandbox.md).

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

```bash
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](https://provenanceauth.com/help/personal/guides/connect-your-assistant#from-a-terminal).

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| The model reads "Waiting for approval" | The person hadn't answered within `timeoutMs` | Raise `timeoutMs`, and the matcher or command `timeout` with it. |
| The model reads "This agent session was turned off in Provenance." | The agent session was revoked | Start a new session. |
| A new approval on every call | The tool's input changes between calls | Retry with the same input, or describe the action with stable fields. |
| A listed tool is never gated | The name doesn't match | MCP tools are named `mcp__<server>__<tool>`; use `mcp__<server>__*` for a whole server. |

## See also

- [Framework integrations](https://provenanceauth.com/docs/mandate/guides/framework-integrations.md)
- [Step-up approval](https://provenanceauth.com/docs/mandate/guides/step-up-approval.md)
- [Provenance MCP](https://provenanceauth.com/docs/provenance-mcp.md)
- [Execution binding](https://provenanceauth.com/docs/mandate/guides/execution-binding.md)
