ChainlessChain Agent SDK for Python
chainlesschain-agent-sdk is the zero-runtime-dependency Python client for
the same Agent Protocol v1 used by @chainlesschain/agent-sdk. It starts one
cc agent subprocess, frames its NDJSON stream safely, exposes frozen typed
events, and performs approval, question, and MCP elicitation round trips.
Version 0.2.7 is the current release candidate for
PyPI. Python 3.10 or
newer is required. It adds the bounded App Server pilot client and generated
Context/Memory protocol validators while preserving lossless unknown-event
delivery and the zero-runtime-dependency package boundary.
Install
Install the SDK and a compatible cc CLI:
python -m pip install "chainlesschain-agent-sdk==0.2.7"
npm install --global "chainlesschain@0.166.11"
The Python distribution has no runtime dependencies. The CLI is installed
separately because AgentSession controls it as a subprocess:
python -c "import chainlesschain_agent_sdk as sdk; print(sdk.__version__)"
cc --version
Basic session
import asyncio
from chainlesschain_agent_sdk import (
AgentSession,
AgentSessionOptions,
ElicitationResponse,
ResultEvent,
UnknownAgentEvent,
)
async def main() -> None:
session = AgentSession(
AgentSessionOptions(
cwd=".",
session_id="ci-fix-1042", # declare new sessions that must be resumable
permission_mode="acceptEdits",
),
on_approval=lambda request: (
{"kind": "acceptOnce"}
if request.tool == "run_shell"
else {"kind": "decline", "reason": "Tool is not allow-listed"}
),
on_question=lambda request: None, # cancel in non-interactive hosts
on_elicitation=lambda request: ElicitationResponse("decline"),
)
await session.start()
await session.send("Run the focused tests and fix failures.")
async for event in session:
# Every wire object is yielded. A newer CLI type is never discarded.
if isinstance(event, UnknownAgentEvent):
print("unknown event preserved:", event.to_dict())
elif isinstance(event, ResultEvent):
print(event.subtype, event.result)
await session.end()
await session.wait()
asyncio.run(main())
Approval callbacks may still return booleans for source compatibility. Direct
respond_approval(id, bool) calls retain the legacy boolean wire; structured
decisions echo the request binding and can express scoped turn/session grants.
SystemInitEvent.session_id is the authoritative live ID. Persist it and
resume later with AgentSessionOptions(resume=that_id). Anonymous stream
sessions are not persisted by CLI design; use session_id when creating a
session that must be resumable. resume takes precedence over session_id,
matching the TypeScript SDK.
Event and callback guarantees
- The generated
AgentStreamEventPayloadis the authoritative closed wire union.KNOWN_EVENT_CLASSESis discovered from the open runtime class hierarchy and exists only for ergonomic dataclass dispatch; it no longer mirrors or redefines the schema discriminator inventory. - Every event retains its original object in the read-only
rawmapping;to_dict()returns a deep mutable copy, including unknown additive fields. - Unknown outer
typevalues are delivered asUnknownAgentEventthrough bothon_eventand async iteration. CC_AGENT_STREAM_EVENT_TYPESandvalidate_agent_stream_eventexpose the canonical known discriminator inventory without changing that lossless unknown-event behavior.AgentStreamEventPayload,CanonicalAgentStreamEvent, andvalidate_canonical_agent_stream_eventexpose the generated strict contract for hosts that must reject a malformed payload with a known discriminator.- NDJSON decoding carries split lines and split UTF-8 code points across chunks, accepts CRLF, and flushes a final line without a newline.
- Approval callback errors answer
approve:false(fail closed). Question callbacks automatically echo the opaque runtimebindingon both normal andnullanswers, so stale/cross-turn answers remain fail closed. MCP elicitation accepts only an explicitElicitationResponse("accept", content)(or equivalent mapping); all other outcomes cancel. URL requests exposemetadata.url/metadata.url_host; the host must show the full HTTPS URL and obtain explicit consent before opening it. URL completion and non-interactive fallback arrive as typedElicitationCompleteEvent/ElicitationDeferredEventobjects. stderris diagnostics only. It is available throughon_stderrand is never parsed as protocol data.
Callbacks may be synchronous functions or coroutines. Keep them bounded: the CLI also applies its own interaction timeouts.
CI consumer
examples/ci_gate.py is an executable consumer with an
explicit handler for every current event class. It journals every raw event
before dispatch and preserves unknown events in the same artifact:
python examples/ci_gate.py \
--prompt "Run unit tests and fix only the failing implementation" \
--provider openai \
--session-id "ci-${GITHUB_RUN_ID}" \
--output agent-events.ndjson
The script denies approvals unless a tool is explicitly repeated with
--approve-tool. examples/github-actions.yml
is a manual-dispatch GitHub Actions template with read-only repository
permissions and a short-lived event artifact. Raw events can contain prompts,
tool output, and model responses, so do not publish that artifact publicly.
The same script has a hermetic replay mode used by this repository's CI:
python examples/ci_gate.py \
--replay ../agent-sdk/__fixtures__/protocol/*.ndjson \
--output protocol-events.ndjson
Conformance and tests
Python tests read the canonical fixtures in
packages/agent-sdk/__fixtures__/protocol/ directly; there is no copied Python
fixture set to drift from TypeScript and Java consumers.
cd packages/agent-sdk-python
PYTHONPATH=src python -m unittest discover -s tests -v
The language-neutral contract remains
packages/agent-sdk/docs/PROTOCOL.md.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chainlesschain_agent_sdk-0.2.7.tar.gz.
File metadata
- Download URL: chainlesschain_agent_sdk-0.2.7.tar.gz
- Upload date:
- Size: 52.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08279665d66a119604ecc449d501c8b4e38f1b1988d6dcdb8213649f6c5f1150
|
|
| MD5 |
dc6b634c1cc397be5c3b1b8ec9aed70f
|
|
| BLAKE2b-256 |
665600751de641ec3cb38217349c4973b6a62d1cda25d1061c03d9615b8928f9
|
Provenance
The following attestation bundles were made for chainlesschain_agent_sdk-0.2.7.tar.gz:
Publisher:
python-agent-sdk-release.yml on chainlesschain/chainlesschain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainlesschain_agent_sdk-0.2.7.tar.gz -
Subject digest:
08279665d66a119604ecc449d501c8b4e38f1b1988d6dcdb8213649f6c5f1150 - Sigstore transparency entry: 2643392860
- Sigstore integration time:
-
Permalink:
chainlesschain/chainlesschain@e93dc817ae7f65159ffa754472ebdac30de34180 -
Branch / Tag:
refs/tags/python-agent-sdk-v0.2.7 - Owner: https://github.com/chainlesschain
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-agent-sdk-release.yml@e93dc817ae7f65159ffa754472ebdac30de34180 -
Trigger Event:
push
-
Statement type:
File details
Details for the file chainlesschain_agent_sdk-0.2.7-py3-none-any.whl.
File metadata
- Download URL: chainlesschain_agent_sdk-0.2.7-py3-none-any.whl
- Upload date:
- Size: 44.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c7bf52618495f1f69c1803d6f421a5c57ed450531dca41387e72e0fb496859f
|
|
| MD5 |
3ef8ba6fd447a15c1594c57e0220e188
|
|
| BLAKE2b-256 |
98e0894ad8f43e20fda0116e9a1fec7c43d8c2e514fe57a0ea83b0a2b3ed2b01
|
Provenance
The following attestation bundles were made for chainlesschain_agent_sdk-0.2.7-py3-none-any.whl:
Publisher:
python-agent-sdk-release.yml on chainlesschain/chainlesschain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
chainlesschain_agent_sdk-0.2.7-py3-none-any.whl -
Subject digest:
5c7bf52618495f1f69c1803d6f421a5c57ed450531dca41387e72e0fb496859f - Sigstore transparency entry: 2643393351
- Sigstore integration time:
-
Permalink:
chainlesschain/chainlesschain@e93dc817ae7f65159ffa754472ebdac30de34180 -
Branch / Tag:
refs/tags/python-agent-sdk-v0.2.7 - Owner: https://github.com/chainlesschain
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-agent-sdk-release.yml@e93dc817ae7f65159ffa754472ebdac30de34180 -
Trigger Event:
push
-
Statement type: