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.7.4.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.7.4-cp38-abi3-win_amd64.whl (339.5 kB view details)

Uploaded CPython 3.8+Windows x86-64

capnagent-0.7.4-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.7.4-cp38-abi3-macosx_11_0_arm64.whl (444.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: capnagent-0.7.4.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.7.4.tar.gz
Algorithm Hash digest
SHA256 3096730390245511fecbbcd525ff390d3b2af0492a0f42b7cdc8ee97b1c62c65
MD5 0728e08151b0cce40960cc49d29f97f3
BLAKE2b-256 0165634aa08da5b3442cf63dba8d3b21232642bfaa79c591975451a2d89b6555

See more details on using hashes here.

File details

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

File metadata

  • Download URL: capnagent-0.7.4-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 339.5 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.7.4-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f49d206d3650eb903248d7d6a8016284319a805a7941a5a16e7139148813ea22
MD5 5bf065e592c25fcda7f7105daf4860f4
BLAKE2b-256 68be44617b77e9905af346877f42a75a5a831c489f41ae4bf62b1c3604d2cdfb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for capnagent-0.7.4-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 704e6e76a674b2f93f4296b6449f958ee66c1ec75bfdc4134e4917feb410dfb3
MD5 e2e2cc7751ad1c948bd279a38884e283
BLAKE2b-256 c9e260e95b0310d4ebf5c6ee28e24932de87d30c9f0f1d501a6cce230a5cfd3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for capnagent-0.7.4-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1eb7bf063f5785a040943faa7c0c9fd69a0d86e99757712bb50a478664f52273
MD5 ab2cca33c5560d7b0292a71fa780f6a7
BLAKE2b-256 842a4239dccea73855a81d3538ed9007d09f2d8581da42fb3126595b79298499

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