Skip to main content

Capability-based authority tokens for AI agent tool calls. Python bindings.

Project description

capnagent — Python bindings

Python bindings for capnagent, the capability-based authority-token engine for AI agent tool calls.

The bindings cover the same surface as the TypeScript/WASM bindings: Issuer, CapabilityBuilder, Capability, Verifier, Auditor. The Rust core (capnagent-core) is shared 1:1 — same engine, different language surface.

Install

pip install capnagent

(Pre-1.0: built from source via pip install capnagent once the first wheel is published; until then, install from source — see "Build from source" below.)

Quick start

import json
import os
from capnagent import Issuer, Verifier, Auditor

root_key  = os.urandom(32)   # production: KMS / secret manager
audit_key = os.urandom(32)

# Issue a scoped capability.
cap = (
    Issuer.from_key(root_key)
    .issue("checkout")
    .caveat('caller == "agent:planner"')
    .caveat('tool == "checkout.purchase"')
    .caveat("now <= @2099-01-01T00:00:00Z")
    .build()
)

verifier = Verifier(root_key)
auditor  = Auditor(audit_key)

# Verify a call: returns a JSON-encoded receipt.
ctx = {
    "caller": "agent:planner",
    "tool": "checkout.purchase",
    "args": {"sku": "USB-C cable", "amount_cents": 1299},
    "nowMs": 1_700_000_000_000,
}
receipt_json = verifier.verify_with_context(cap, json.dumps(ctx), auditor)
receipt = json.loads(receipt_json)
assert receipt["outcome"]["kind"] == "allowed"

API

Class Method Notes
Issuer from_key(bytes) -> Issuer 32+ bytes from CSPRNG.
issue(identifier) -> CapabilityBuilder
CapabilityBuilder caveat(predicate) DSL: see docs/WEEK2_SPEC.md §2.2.
holder_of_key(pubkey_32_bytes) DPoP-style hok binding.
build() -> Capability Raises if zero caveats (angle C.5).
Capability serialize() -> str URL-safe base64.
parse(token) -> Capability Static.
attenuate(predicate) -> Capability Pre-validates parse (angle B.2).
Verifier Verifier(key_bytes)
verify_with_context(cap, ctx_json, auditor) -> str Receipt as JSON string. Pass json.dumps(ctx) for full A.1 protection.
Auditor Auditor(key_bytes) Empty key raises (angle B.3).
verify(receipt_json) Raises on tampered receipt.

Why the Python binding gets A.1 closure for free

The v0.6 angle finding A.1 (sub-ulp f64 collapse) has a JS-layer artefact: JS's Number IS f64, so JSON.parse collapses sub-ulp digits BEFORE the WASM boundary. v0.6.1 added a JSON-string entry point to the WASM binding to work around this for JS callers.

Python doesn't have that problem. Python's json.dumps preserves arbitrary integer precision (Python ints are unbounded), and float literals round-trip through repr() in a way that preserves syntactic shape. The Rust side parses the JSON string with serde_json::from_str (with arbitrary_precision), keeping the source text past the parse boundary. So the v0.6 integer-domain rule fires correctly end-to-end through the Python binding by default.

The test test_v0_6_integer_caveat_rejects_decimal_arg in tests/test_basic.py exercises this directly.

Build from source

Prerequisites:

  • Rust toolchain (stable)
  • Python 3.8+ with development headers
  • pip install maturin
cd crates/capnagent-py
maturin develop          # editable install into the current venv
python -m pytest tests/ -v

To produce a publishable wheel:

maturin build --release
# Wheel appears under crates/capnagent-py/target/wheels/

License

Apache-2.0 — same as the Rust core.

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

capnagent-0.0.1.tar.gz (95.4 kB view details)

Uploaded Source

Built Distributions

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

capnagent-0.0.1-cp38-abi3-win_amd64.whl (339.4 kB view details)

Uploaded CPython 3.8+Windows x86-64

capnagent-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (503.5 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

capnagent-0.0.1-cp38-abi3-macosx_11_0_arm64.whl (443.9 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file capnagent-0.0.1.tar.gz.

File metadata

  • Download URL: capnagent-0.0.1.tar.gz
  • Upload date:
  • Size: 95.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for capnagent-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8499339c09de62d353d97ee411a9db6691e1100029613d7dec22f33951cea460
MD5 06fab60e781de441dd5e4243cc018c02
BLAKE2b-256 59c85ffccc5a3ddb95384e34ca5815ad7c35525d4c9ad86c0861b13dce2877e7

See more details on using hashes here.

File details

Details for the file capnagent-0.0.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: capnagent-0.0.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 339.4 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for capnagent-0.0.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 55d1f8c2f44de5d2c700f568b50fa76ea9e3ee95506de7e332243260fd13275d
MD5 79e6c35e669d912d0f6fbba8042e1422
BLAKE2b-256 ddfccb1da4d2674ac193380dcaaf8017dadf937de05e1448d3c1ff96ab4c7e1b

See more details on using hashes here.

File details

Details for the file capnagent-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for capnagent-0.0.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d6cd4c0dc849c7515ef5ae0a365a55fca00522c9732a6138bb9a72eb2377438
MD5 fd03e48fba28460613ca5bdf2d149d94
BLAKE2b-256 b57c37fbc9aa0689f9f0a47ecb4fdf5e82f990e427bef49bf3a958017be68ea6

See more details on using hashes here.

File details

Details for the file capnagent-0.0.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for capnagent-0.0.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 34f0156e590f75d77589988260b46a75794fb2e2a6d0abd92245d3dea51b8a50
MD5 724dd523e87b8077846368a3e3ac9f0c
BLAKE2b-256 4c2d8562b9024a0080675fa08f728e534e34cf4bff9bcc8b76f3910e2b7eef66

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