> ## Documentation Index
> Fetch the complete documentation index at: https://docs.grantex.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# CrewAI Agent

> Scoped tools with audit logging using CrewAI.

## What it does

This example demonstrates the `grantex-crewai` integration by building CrewAI tools with Grantex scope enforcement:

1. **Register an agent** and obtain a grant token via the sandbox flow
2. **Create scoped tools** using `create_grantex_tool` -- each tool is bound to a specific scope (`calendar:read`, `email:send`)
3. **Wrap tools with `with_audit_logging`** to automatically log every invocation to the Grantex audit trail
4. **Invoke tools** directly (in a full CrewAI crew, the LLM would select and call these tools)
5. **Demonstrate scope enforcement** -- attempting to create a tool with `account:delete` (a scope not in the grant) raises a `PermissionError`
6. **Inspect the audit trail** to see the logged actions

## Prerequisites

* **Python 3.9+**
* **Docker** (Docker Desktop or Docker Engine with Compose)

## Run

Start the local Grantex stack from the repository root:

```bash theme={null}
docker compose up --build
```

In a separate terminal, run the example:

```bash theme={null}
cd examples/crewai-agent
pip install -r requirements.txt
python main.py
```

## Expected output

```text theme={null}
Agent registered: ag_01HXYZ...
Grant token received, grantId: grnt_01HXYZ...
Tools created: read_calendar, send_email
Audit logging attached

--- Invoking read_calendar ---
Result: {"events": [{"title": "Team standup", "time": "9:00 AM", "query": "today"}, {"title": "Design review", "time": "2:00 PM", "query": "today"}]}

--- Invoking send_email ---
Result: Email sent successfully: "Meeting summary: standup at 9 AM, design review at 2 PM"

--- Testing scope enforcement ---
Scope check blocked unauthorized tool: Grant token does not include required scope "account:delete". Granted scopes: calendar:read, email:send

--- Audit trail ---
  [success] read_calendar — 2026-02-28T12:00:00.000Z
  [success] send_email — 2026-02-28T12:00:01.000Z

Done! CrewAI integration demo complete.
```

## Environment variables

| Variable          | Default                 | Description                                  |
| ----------------- | ----------------------- | -------------------------------------------- |
| `GRANTEX_URL`     | `http://localhost:3001` | Base URL of the Grantex auth service         |
| `GRANTEX_API_KEY` | `sandbox-api-key-local` | API key. Use a sandbox key for auto-approval |

## Source code

The full source is in [`examples/crewai-agent/main.py`](https://github.com/mishrasanjeev/grantex/tree/main/examples/crewai-agent).
