Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 78 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,105 @@
<img alt="Agent Client Protocol" src="https://zed.dev/img/acp/banner-dark.webp">
</a>

# Agent Client Protocol (Python)
# Agent Client Protocol — Python SDK

Build ACP-compliant agents and clients in Python with generated schema models, asyncio transports, and runnable demos.
[![PyPI](https://img.shields.io/pypi/v/agent-client-protocol)](https://pypi.org/project/agent-client-protocol/)
[![Python](https://img.shields.io/pypi/pyversions/agent-client-protocol)](https://pypi.org/project/agent-client-protocol/)
[![CI](https://github.com/agentclientprotocol/python-sdk/actions/workflows/main.yml/badge.svg)](https://github.com/agentclientprotocol/python-sdk/actions/workflows/main.yml)
[![Docs](https://img.shields.io/badge/docs-GitHub%20Pages-blue)](https://agentclientprotocol.github.io/python-sdk/)

> Releases track the upstream ACP schema; contributions that tighten coverage or tooling are always welcome.
Python SDK for building [Agent Client Protocol (ACP)](https://agentclientprotocol.com/) agents and clients, with generated Pydantic models and asyncio transports over stdio, Streamable HTTP, and WebSocket.

## Install
## Installation

```bash
pip install agent-client-protocol
# or
uv add agent-client-protocol
```

## At a glance
For experimental HTTP/WebSocket transports, install the `http` extra (`pip install "agent-client-protocol[http]"`). Requires Python 3.10–3.15.

- **Spec parity:** Generated Pydantic models in `acp.schema` track every ACP release so payloads stay valid.
- **Direct construction:** Generated discriminator fields have defaults, so models can be instantiated directly.
- **Runtime ergonomics:** Async base classes, stdio JSON-RPC plumbing, and lifecycle helpers keep custom agents tiny.
- **Examples ready:** Streaming, permissions, Gemini bridge, and duet demos live under `examples/`.
- **Contrib utilities:** Session accumulators, tool call trackers, and permission brokers share patterns from real deployments.
## Quick example

## Who benefits
### Build an agent

- Agent authors who need typed models and event-stream ergonomics for ACP-compatible assistants.
- Client integrators embedding ACP parties inside Python applications or wrapping existing CLIs via stdio.
- Tooling teams experimenting with permission flows, streaming UX, or Gemini bridges without re-implementing transports.
See real adopters like kimi-cli in the [Use Cases list](https://agentclientprotocol.github.io/python-sdk/use-cases/).
```python
import asyncio
from typing import Any
from uuid import uuid4

## How to get started
from acp import PROTOCOL_VERSION, Agent, InitializeResponse, NewSessionResponse, PromptResponse, run_agent
from acp.interfaces import Client
from acp.schema import AgentMessageChunk, TextContentBlock

- Follow the [Quickstart guide](https://agentclientprotocol.github.io/python-sdk/quickstart/) for installation, echo-agent validation, editor wiring (e.g. Zed), and programmatic launch recipes.
- Browse the [example gallery](https://github.com/agentclientprotocol/python-sdk/tree/main/examples) to see progressively richer integrations you can copy or extend.
- Skim the [docs hub](https://agentclientprotocol.github.io/python-sdk/) for focused references on contrib helpers, releasing, and transport details.

## Quick links
class EchoAgent(Agent):
def on_connect(self, conn: Client) -> None:
self._conn = conn

| Need | Link |
|--------------------|------------------------------------------------------------------------|
| Docs hub | <https://agentclientprotocol.github.io/python-sdk/> |
| Quickstart | <https://agentclientprotocol.github.io/python-sdk/quickstart/> |
| Use cases | <https://agentclientprotocol.github.io/python-sdk/use-cases/> |
| Contrib helpers | <https://agentclientprotocol.github.io/python-sdk/contrib/> |
| Releasing workflow | <https://agentclientprotocol.github.io/python-sdk/releasing/> |
| Examples | <https://github.com/agentclientprotocol/python-sdk/tree/main/examples> |
| Tests | <https://github.com/agentclientprotocol/python-sdk/tree/main/tests> |
| PyPI | <https://pypi.org/project/agent-client-protocol/> |
async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeResponse:
return InitializeResponse(protocol_version=PROTOCOL_VERSION)

## Project layout
async def new_session(self, cwd: str, **kwargs: Any) -> NewSessionResponse:
return NewSessionResponse(session_id=uuid4().hex)

- `src/acp/`: runtime package (agents, clients, transports, schema bindings, contrib utilities)
- `schema/`: upstream JSON schema sources (regenerate via `make gen-all`)
- `docs/`: MkDocs content backing the published documentation
- `examples/`: runnable scripts covering stdio orchestration patterns
- `tests/`: pytest suite with golden fixtures and optional Gemini coverage
async def prompt(self, session_id: str, prompt: list[Any], **kwargs: Any) -> PromptResponse:
for block in prompt:
text = getattr(block, "text", "")
await self._conn.session_update(
session_id=session_id,
update=AgentMessageChunk(content=TextContentBlock(text=text)),
)
return PromptResponse(stop_reason="end_turn")

## Developer commands

- `make install` provisions the `uv` virtualenv and installs pre-commit hooks.
- `make check` runs Ruff formatting/linting, type analysis, dependency hygiene, and lock verification.
- `make test` executes `pytest` (with doctests) inside the managed environment.
- `ACP_SCHEMA_VERSION=<ref> make gen-all` refreshes protocol artifacts when the schema advances.
if __name__ == "__main__":
asyncio.run(run_agent(EchoAgent()))
```

### Drive an agent from Python

```python
import asyncio
import sys
from typing import Any

from acp import PROTOCOL_VERSION, Client, spawn_agent_process
from acp.schema import TextContentBlock


class SimpleClient(Client):
async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None:
print(session_id, update)


async def main() -> None:
async with spawn_agent_process(SimpleClient(), sys.executable, "examples/echo_agent.py") as (conn, _proc):
await conn.initialize(protocol_version=PROTOCOL_VERSION)
session = await conn.new_session(cwd=".", mcp_servers=[])
await conn.prompt(session_id=session.session_id, prompt=[TextContentBlock(text="Hello ACP!")])

Keep docs and examples current whenever you ship public API or transport changes, and prefer Conventional Commits (`feat:`, `fix:`, etc.) when submitting patches.

## Community & support
asyncio.run(main())
```

## Documentation

- [Quickstart](https://agentclientprotocol.github.io/python-sdk/quickstart/) — Connect your agent to Zed, Toad, Lody, or T3 Code, or drive agents programmatically.
- [Protocol Features](https://agentclientprotocol.github.io/python-sdk/protocol-features/) — Unstable opt-in methods, session notices, stateless MCP-over-ACP, cancellation, and extensions (`schema-v1.35.0`).
- [Web Transport](https://agentclientprotocol.github.io/python-sdk/web-transport/) — Serve and consume ACP over Streamable HTTP and WebSocket (`[http]`).
- [Experimental Protocol v2](https://agentclientprotocol.github.io/python-sdk/experimental-v2/) — Preview v2 runtime (`schema-v2.0.0-beta.4`) and dual-stack `AgentProtocolRouter`.
- [Examples](https://github.com/agentclientprotocol/python-sdk/tree/main/examples) · [Contrib](https://agentclientprotocol.github.io/python-sdk/contrib/) · [Use Cases](https://agentclientprotocol.github.io/python-sdk/use-cases/) · [Testing](https://agentclientprotocol.github.io/python-sdk/testing/)

## Development

```bash
make install # Set up uv environment and pre-commit hooks
make check # Format, lint, type-check, and dependency check
make test # Run pytest suite
```

## Community

- File issues or feature requests at <https://github.com/agentclientprotocol/python-sdk>.
- Discuss ideas or get help via GitHub Discussions: <https://github.com/agentclientprotocol/python-sdk/discussions>.
- Join the broader ACP conversations at <https://agentclientprotocol.com/>, the Zed community channels, or the community Zulip: <https://agentclientprotocol.zulipchat.com/>.
- Shared learnings, integrations, or third-party transports are welcome additions to the documentation—open a PR!
Join the conversation on [GitHub Discussions](https://github.com/agentclientprotocol/python-sdk/discussions), [Zulip](https://agentclientprotocol.zulipchat.com/), or [agentclientprotocol.com](https://agentclientprotocol.com/).
49 changes: 17 additions & 32 deletions docs/contrib.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
# Experimental Contrib Modules
# Contrib Utilities

The helpers under `acp.contrib` package recurring patterns we saw in integrations such as Toad, kimi-cli, and Gemini. Keep in mind they are experimental—APIs can still shift as we learn. Use this page as a menu:
> **Experimental.** Modules under `acp.contrib` package opt-in stateful helpers for common client and agent workflows. Their APIs may evolve across releases.

- **`session_state.SessionAccumulator`** — build a canonical, immutable snapshot of every session update so UIs can render tool calls and plans without re-implementing state machines.
- **`tool_calls.ToolCallTracker` + `permissions.PermissionBroker`** — coordinate streamed tool call updates and permission prompts from one place.
## Available helpers

## SessionAccumulator (`acp.contrib.session_state`)
- **`acp.contrib.session_state.SessionAccumulator`** — aggregates incoming `SessionNotification` updates into immutable `SessionSnapshot` models for UI rendering.
- **`acp.contrib.tool_calls.ToolCallTracker`** — tracks active tool call lifecycles and emits canonical `ToolCallStart` / `ToolCallProgress` updates.
- **`acp.contrib.permissions.PermissionBroker`** — coordinates `request_permission` prompts using tracked tool call state and default approve/reject options.

**Use it when:** you need a live, merged view of `SessionNotification` events (e.g. tool calls, plan entries, user/agent messages) to drive UI widgets.
## `SessionAccumulator` (`acp.contrib.session_state`)

**Key capabilities**
Feed raw `SessionNotification` events into one accumulator per UI controller and render from immutable snapshots:

- `SessionAccumulator.apply(notification)` reconciles `tool_call` and `tool_call_update` payloads, even if the start event arrives late.
- `snapshot()` returns an immutable `SessionSnapshot` Pydantic model containing the plan, current mode, commands, and ordered message history.
- Raw tool input/output payloads are copied when applying notifications and creating snapshots, so nested payload mutations do not change the accumulated state.
- `subscribe(callback)` lets you push snapshots into stores or UI components whenever state changes.
- Automatic reset on session-change (toggle with `auto_reset_on_session_change`).
- `apply(notification)` merges message chunks, plan updates, mode/config changes, and `tool_call` / `tool_call_update` payloads (including out-of-order updates).
- `snapshot()` returns an immutable `SessionSnapshot` Pydantic model with copied tool input/output payloads.
- `subscribe(callback)` pushes updated snapshots to subscribers whenever state changes.
- `auto_reset_on_session_change` resets accumulated state automatically when a new `session_id` arrives.

> Tip: Create one accumulator per UI controller. Feed every raw `SessionNotification` into it and render from `snapshot.tool_calls` / `snapshot.user_messages` instead of mutating state manually.
## `ToolCallTracker` and `PermissionBroker` (`acp.contrib.tool_calls`, `acp.contrib.permissions`)

## ToolCallTracker & PermissionBroker (`acp.contrib.tool_calls`, `acp.contrib.permissions`)
Share a single `ToolCallTracker` with `PermissionBroker` in your agent loop to keep tool updates and permission requests synchronized:

**Use them when:** your agent runtime synthesises tool call IDs, streams arguments, and prompts the user for approval. The helpers centralise the bookkeeping so you don’t juggle raw Pydantic models.

- `ToolCallTracker.start()/progress()/append_stream_text()` emits canonical `ToolCallStart` / `ToolCallProgress` updates and keeps an in-memory view via `view()` or `tool_call_model()`.
- `PermissionBroker.request_for()` wraps `requestPermission` RPCs. It reuses tracker state (or a provided `ToolCall`), lets you append extra content, and defaults to Approve / Approve for session / Reject options.
- Omit `options` or pass `None` to use broker defaults. An explicit empty option list raises `MissingPermissionOptionsError` before the permission request is sent; the same applies to empty `default_options` when no per-request options are supplied.
- `default_permission_options()` exposes that canonical option triple if you need to customise it.

> Tip: Keep one tracker near the agent event loop. Emit notifications through it and share the tracker with `PermissionBroker` so permission prompts always match the latest tool call state.

## Design Guardrails

To stay aligned with the ACP schema, the contrib layer follows a few rules:

- Schema data classes continue to live in `acp.schema`. Contrib helpers clone them with `.model_copy(deep=True)` before mutation.
- The core runtime never imports contrib modules implicitly—you opt in when they help.
- Helpers focus on painful bookkeeping (tool call aggregation, permission UX) and leave product-specific policy to your application.

Try the contrib modules, then open an issue or PR with feedback so we know which APIs should graduate into the stable surface.
- `ToolCallTracker.start()`, `progress()`, and `append_stream_text()` emit `ToolCallStart` and `ToolCallProgress` updates while maintaining an in-memory view via `view()` and `tool_call_model()`.
- `PermissionBroker.request_for()` builds and sends `request_permission` calls using tracked tool call state (or an explicit `ToolCall`) and optional extra content blocks.
- `default_permission_options()` provides the standard **Approve**, **Approve for session**, and **Reject** option triple. Omitting `options` or passing `None` applies the broker's defaults; passing an empty list raises `MissingPermissionOptionsError` before sending the request.
Loading
Loading