# Gate CrewAI tools

Add approvals to a CrewAI crew, so each gated tool call is authorized and verified on its exact arguments, with a person's approval when your policy asks, before the tool runs.

Use `provenanceauth-crewai` to add approvals to the tools your CrewAI agents use. Each call is authorized and verified on its exact arguments, after CrewAI validates them against the tool's schema, then the tool runs with those arguments.

**Before you begin:** install the package, 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 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
pip install ./provenance/python/sdk ./provenance/python/crewai-provenance
```

**Availability:** the package is available to beta customers. It supports `crewai` 1.x.

## 1. Gate a tool

`authorize_tool` takes a tool built with `@tool`, or your own `BaseTool` subclass, and returns a gated copy:

**Python**

```python
import os
from crewai import Agent, Crew, Task
from crewai.tools import tool
from provenanceauth_crewai import authorize_tool
from provenanceauth import Provenance

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

@tool("pay_invoice")
def pay_invoice(invoice: str, amount: float, payee: str) -> str:
    """Pay a supplier invoice."""
    return f"paid {invoice}"

clerk = Agent(
    role="Billing clerk",
    goal="Pay supplier invoices",
    backstory="Pays approved invoices for the finance team.",
    tools=[authorize_tool(
        pay_invoice,
        client=prov,
        device_id="dev_00000000000000000000000000000001",
        describe=lambda a: f"Pay invoice {a['invoice']}: ${a['amount']} to {a['payee']}",
    )],
)
```

The gated copy is an instance of the tool's own class, so `isinstance` checks, fields and methods stay the same. Without `describe`, the action description is the tool name and the canonical JSON of its arguments.

## 2. Read the outcome

| Outcome | What happens |
| --- | --- |
| Verified | The tool runs with the arguments that were verified |
| Denied | The tool returns a sentence instead, such as "The person declined this action.", and the agent reads it as the observation |
| Waiting for approval | The tool waits for the person, up to `timeout_ms` (default 2 minutes), then runs or returns the sentence |

If the person hasn't answered when the wait ends, the agent reads "Waiting for approval (appr_…)", and the next call with the same arguments reuses the token that opened the approval. The agent session is CrewAI's run id for the kickoff when the release exposes one, or one id per gated tool; pass `agent_session_id` to set it.

CrewAI's `before_tool_call` hooks block a call with a fixed message, so the package gates the tool itself, where the agent can read why a call didn't run.

## 3. Pass the rest of the options

- `context` (function): Facts for your [policy](https://provenanceauth.com/docs/concepts/policies.md), read at verify.
- `payload` (function): The payload bound into the token. Default: the tool name and its whole input.
- `run_id` (string): An [agent-team run](https://provenanceauth.com/docs/mandate/guides/halt-a-run.md) to bind each call to.
- `mandate_token` (string): A grant to authorize under.
- `agent_id` (string): The registered agent making the calls.
- `timeout_ms` (number): How long to wait for the person. `0` answers at once.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `TypeError: is already gated` | The tool was passed to `authorize_tool` twice | Gate the original tool once. |
| The agent reads "Waiting for approval" | The person hadn't answered within `timeout_ms` | Raise `timeout_ms`, or run the task again after they approve. |

## 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)
