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
  • meta.methods
  • 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.3.tar.gz (28.3 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.3-py3-none-any.whl (31.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cap_protocol-1.0.3.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cap_protocol-1.0.3.tar.gz
Algorithm Hash digest
SHA256 a764617eda9cf3212af0469c7b92df0fce7e2eba0a7a74106c05958a551ee9af
MD5 9e2866c160b6c8769460df56aa5cfbcc
BLAKE2b-256 33defe66d5cd8202635ee3c767b3aad7140edbe49fbb18bc7b35a0df522ef977

See more details on using hashes here.

Provenance

The following attestation bundles were made for cap_protocol-1.0.3.tar.gz:

Publisher: release.yml on CausalAgentProtocol/python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: cap_protocol-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 31.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cap_protocol-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7dbb2a3a41879c6764a31fa9486fbed845acfc534801433c78691edd85423818
MD5 b5cac8a46c785f7ae791c2033ea84dd2
BLAKE2b-256 260aebe9aca0399930685c99d1298886bb610dd76a9de62251d2ca52cdee8958

See more details on using hashes here.

Provenance

The following attestation bundles were made for cap_protocol-1.0.3-py3-none-any.whl:

Publisher: release.yml on CausalAgentProtocol/python-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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