Skip to main content

CrewAI integration for the Grantex delegated authorization protocol — scope-enforced, audited agent tools

Project description

grantex-crewai

CrewAI integration for the Grantex delegated authorization protocol — scope-enforced, audited agent tools.

Wrap any CrewAI tool with Grantex grant token verification so your agents can only use tools they've been authorized for, with a full audit trail.

PyPI Python License

Install

pip install grantex-crewai

You also need CrewAI installed (it's a peer dependency):

pip install crewai

Quick start

from grantex_crewai import create_grantex_tool
from pydantic import BaseModel

# Define the tool's input schema
class FetchParams(BaseModel):
    url: str

# Create a scope-enforced tool
tool = create_grantex_tool(
    name="fetch_data",
    description="Fetches data from the given URL.",
    grant_token="eyJhbGciOiJSUzI1NiIs...",  # Grantex JWT
    required_scope="data:read",
    func=lambda url: requests.get(url).text,
    args_schema=FetchParams,
)

# Use it in a CrewAI agent — if the token doesn't have
# the "data:read" scope, create_grantex_tool raises immediately

The tool performs an offline scope check at creation time by decoding the JWT's scp claim. No network call is needed — if the required scope is missing, a PermissionError is raised before the tool is ever used.

Audit logging

Wrap any Grantex tool with audit logging to record every invocation to the Grantex audit trail:

from grantex import Grantex
from grantex_crewai import create_grantex_tool, with_audit_logging

client = Grantex(api_key="YOUR_API_KEY")

tool = create_grantex_tool(
    name="send_email",
    description="Sends an email.",
    grant_token=token,
    required_scope="email:send",
    func=send_email_fn,
)

# Wrap with audit logging — every call is recorded
tool = with_audit_logging(
    tool, client,
    agent_id="ag_01HXYZ...",
    grant_id="grnt_01HXYZ...",
)

# Now use the tool in your crew
# Successful calls log status="success", failures log status="failure"

API reference

create_grantex_tool

create_grantex_tool(
    *,
    name: str,
    description: str,
    grant_token: str,
    required_scope: str,
    func: Callable[..., str],
    args_schema: type[BaseModel] | None = None,
) -> BaseTool

Creates a CrewAI BaseTool with Grantex scope enforcement.

Parameter Description
name Tool name (used in audit log entries)
description Human-readable description shown to the LLM
grant_token Grantex grant token (RS256 JWT)
required_scope Scope that must be present in the token's scp claim
func The function to execute when the tool is called
args_schema Optional Pydantic BaseModel describing tool inputs

Raises:

  • PermissionError — if the grant token doesn't contain required_scope
  • ValueError — if the grant token can't be decoded
  • ImportError — if crewai is not installed

with_audit_logging

with_audit_logging(
    tool: BaseTool,
    client: Grantex,
    *,
    agent_id: str,
    grant_id: str,
) -> BaseTool

Patches the tool's _run method to log audit entries via the Grantex client.

Parameter Description
tool A CrewAI BaseTool (from create_grantex_tool)
client A grantex.Grantex client instance
agent_id Grantex agent ID to attribute the action to
grant_id Grant ID authorizing this tool invocation

get_tool_scopes

get_tool_scopes(grant_token: str) -> list[str]

Returns the scopes embedded in a grant token. Purely offline — no network call, no signature check.

Requirements

  • Python 3.9+
  • grantex >= 0.1.0
  • crewai >= 0.28.0 (peer dependency)

Links

License

Apache 2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grantex_crewai-0.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grantex_crewai-0.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file grantex_crewai-0.1.1.tar.gz.

File metadata

  • Download URL: grantex_crewai-0.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for grantex_crewai-0.1.1.tar.gz
Algorithm Hash digest
SHA256 987ed5b6766f92794b31014daeef13965e8161bcba9ed2dab87374c406990bb2
MD5 25e4df387ee75827bfa32e7e63726f22
BLAKE2b-256 1fec69f7401f700b801ded68227385d1abb0ec6ca93858a52a08ea861454e037

See more details on using hashes here.

File details

Details for the file grantex_crewai-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: grantex_crewai-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for grantex_crewai-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8db77391c37de4c7c6097b8ee48824c24164a0b248f7ee71cd50bfe48a610ff7
MD5 6db40914ba549a3c006c7369b1ae479f
BLAKE2b-256 a5a16ece544694aef91f716965d11f9d77dbd77e0c6166f009060fa204d2c9b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page