Skip to main content

Python SDK scaffold for the Causal Agent Protocol

Project description

CAP Python SDK

Official Python SDK for building and integrating CAP-compatible systems.

If you are new to CAP itself, start with the cap repository for the protocol overview, getting-started guides, and normative specification. This repository is the Python implementation layer for people who want to call CAP servers or expose CAP-compatible services in Python.

Choose The Right Repo

  • cap: learn CAP, read the docs, and track the protocol specification
  • python-sdk: build CAP clients and CAP-compatible Python servers
  • cap-reference: inspect a working reference server that exposes CAP over HTTP

What This SDK Gives You

This repository ships three Python-facing layers that match the code today:

  • cap.core: typed CAP envelopes, verb contracts, builders, capability-card models, canonical constants, and error models
  • cap.client: an async HTTP client for calling CAP servers through the standard single-entry CAP route
  • cap.server: a thin FastAPI adapter for validating requests, dispatching by verb, and returning CAP-shaped responses

Use this SDK when you want to:

  • call a CAP server from Python without hand-writing request and response envelopes
  • publish a CAP-compatible endpoint in a FastAPI service
  • share one typed protocol layer across clients, servers, tests, and internal tooling

Installation

Install the published package:

pip install cap-protocol

This base install includes the typed protocol models and the HTTP client. It does not install FastAPI.

If you want to use the optional cap.server FastAPI adapter:

pip install "cap-protocol[server]"

For local development from source:

pip install -e ".[server,dev]"

The distribution name is cap-protocol. The Python import package is cap.

Quickstart: Call A CAP Server

import asyncio

from cap.client import AsyncCAPClient


async def main() -> None:
    client = AsyncCAPClient("https://example.com")
    try:
        capabilities = await client.meta_capabilities()
        print(capabilities.result.name)

        neighbors = await client.graph_neighbors(
            node_id="revenue",
            scope="parents",
            max_neighbors=5,
        )
        print(neighbors.result.neighbors)
    finally:
        await client.aclose()


asyncio.run(main())

The client currently provides first-class methods for:

  • meta.capabilities
  • graph.neighbors
  • graph.markov_blanket
  • graph.paths
  • observe.predict
  • intervene.do
  • traverse.parents
  • traverse.children

If you need a custom or extension verb, use request_verb(...) or request_route(...).

Quickstart: Expose A CAP Endpoint In FastAPI

from fastapi import FastAPI, Request

from cap.server import (
    CAPHandlerSuccessSpec,
    CAPProvenanceContext,
    CAPProvenanceHint,
    CAPVerbRegistry,
    GRAPH_NEIGHBORS_CONTRACT,
    build_fastapi_cap_dispatcher,
    register_cap_exception_handlers,
)

app = FastAPI()
registry = CAPVerbRegistry()


@registry.core(GRAPH_NEIGHBORS_CONTRACT)
async def graph_neighbors(payload, request: Request):
    del request
    return CAPHandlerSuccessSpec(
        result={
            "node_id": payload.params.node_id,
            "scope": payload.params.scope,
            "neighbors": [],
            "truncated": False,
            "reasoning_mode": "graph_propagation",
            "identification_status": "identified",
            "assumptions": [],
        },
        provenance_hint=CAPProvenanceHint(algorithm="handwritten-demo"),
    )


async def provenance_context_provider(payload, request: Request) -> CAPProvenanceContext:
    del payload, request
    return CAPProvenanceContext(
        graph_version="dev",
        server_name="example-cap-server",
        server_version="0.1.0",
    )


dispatch = build_fastapi_cap_dispatcher(
    registry=registry,
    provenance_context_provider=provenance_context_provider,
)
register_cap_exception_handlers(app)


@app.post("/cap")
async def cap_endpoint(payload: dict, request: Request):
    return await dispatch(payload, request)

This adapter stays intentionally small. It handles CAP-shaped validation and dispatch, but it does not impose a larger server framework or hide your business logic.

Current Surface Area

The implementation in this repository currently includes:

  • typed request and response models for the verbs listed above
  • capability-card schema models for meta.capabilities
  • canonical constants for reasoning modes, assumptions, mechanism families, and related protocol strings
  • an async httpx-based client with CAP error normalization
  • a FastAPI registry and dispatcher that validate both inbound requests and outbound responses

For code-level details, use the docs below rather than treating the README as the full API inventory.

Where To Go Next

Contributing And Community

Use repo-local docs for repo-specific workflows:

Use the CAP organization docs for shared community policy:

License

This project is licensed under the Apache License 2.0. See LICENSE.

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

cap_protocol-1.0.1.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

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

cap_protocol-1.0.1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file cap_protocol-1.0.1.tar.gz.

File metadata

  • Download URL: cap_protocol-1.0.1.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for cap_protocol-1.0.1.tar.gz
Algorithm Hash digest
SHA256 030d390c6c68c02fb424f20bbc1e3a1437e97cbd56a49aafc2ec9d9133ec6918
MD5 3b543f6734df2eb9d446f67dfcffe667
BLAKE2b-256 722c0ac6497cbd016bfa59290a76fa3df42a0807cd5853332844d709ea901fa2

See more details on using hashes here.

File details

Details for the file cap_protocol-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: cap_protocol-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for cap_protocol-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0f34a8a56298b4ed7fa02b144ab7bf949de35a59013ad73efe477450f0e9d9a6
MD5 684e30d14765f04fc15fef05b79b1409
BLAKE2b-256 100ad295c3c1123a2ee13f6988efd5672d6609a649f76bd016403728958daea2

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