|
2 | 2 | <img alt="Agent Client Protocol" src="https://zed.dev/img/acp/banner-dark.webp"> |
3 | 3 | </a> |
4 | 4 |
|
5 | | -# Agent Client Protocol (Python) |
| 5 | +# Agent Client Protocol — Python SDK |
6 | 6 |
|
7 | | -Build ACP-compliant agents and clients in Python with generated schema models, asyncio transports, and runnable demos. |
| 7 | +[](https://pypi.org/project/agent-client-protocol/) |
| 8 | +[](https://pypi.org/project/agent-client-protocol/) |
| 9 | +[](https://github.com/agentclientprotocol/python-sdk/actions/workflows/main.yml) |
| 10 | +[](https://agentclientprotocol.github.io/python-sdk/) |
8 | 11 |
|
9 | | -> Releases track the upstream ACP schema; contributions that tighten coverage or tooling are always welcome. |
| 12 | +Python SDK for building [Agent Client Protocol (ACP)](https://agentclientprotocol.com/) agents and clients with generated Pydantic v2 models, asyncio JSON-RPC transports, and runnable examples. |
10 | 13 |
|
11 | | -## Install |
| 14 | +## Installation |
12 | 15 |
|
13 | 16 | ```bash |
14 | 17 | pip install agent-client-protocol |
15 | 18 | # or |
16 | 19 | uv add agent-client-protocol |
17 | 20 | ``` |
18 | 21 |
|
19 | | -## At a glance |
| 22 | +Optional extras: |
20 | 23 |
|
21 | | -- **Spec parity:** Generated Pydantic models in `acp.schema` track every ACP release so payloads stay valid. |
22 | | -- **Direct construction:** Generated discriminator fields have defaults, so models can be instantiated directly. |
23 | | -- **Runtime ergonomics:** Async base classes, stdio JSON-RPC plumbing, and lifecycle helpers keep custom agents tiny. |
24 | | -- **Examples ready:** Streaming, permissions, Gemini bridge, and duet demos live under `examples/`. |
25 | | -- **Contrib utilities:** Session accumulators, tool call trackers, and permission brokers share patterns from real deployments. |
| 24 | +```bash |
| 25 | +# Streamable HTTP and WebSocket transports (experimental) |
| 26 | +pip install "agent-client-protocol[http]" |
| 27 | + |
| 28 | +# Logfire / OpenTelemetry instrumentation |
| 29 | +pip install "agent-client-protocol[logfire]" |
| 30 | +``` |
| 31 | + |
| 32 | +Requires Python 3.10–3.15. |
| 33 | + |
| 34 | +## Quick example |
| 35 | + |
| 36 | +### Build an agent |
| 37 | + |
| 38 | +```python |
| 39 | +import asyncio |
| 40 | +from typing import Any |
| 41 | +from uuid import uuid4 |
| 42 | + |
| 43 | +from acp import PROTOCOL_VERSION, Agent, InitializeResponse, NewSessionResponse, PromptResponse, run_agent |
| 44 | +from acp.interfaces import Client |
| 45 | +from acp.schema import AgentMessageChunk, TextContentBlock |
| 46 | + |
| 47 | + |
| 48 | +class EchoAgent(Agent): |
| 49 | + def on_connect(self, conn: Client) -> None: |
| 50 | + self._conn = conn |
26 | 51 |
|
27 | | -## Who benefits |
| 52 | + async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeResponse: |
| 53 | + return InitializeResponse(protocol_version=PROTOCOL_VERSION) |
28 | 54 |
|
29 | | -- Agent authors who need typed models and event-stream ergonomics for ACP-compatible assistants. |
30 | | -- Client integrators embedding ACP parties inside Python applications or wrapping existing CLIs via stdio. |
31 | | -- Tooling teams experimenting with permission flows, streaming UX, or Gemini bridges without re-implementing transports. |
32 | | -See real adopters like kimi-cli in the [Use Cases list](https://agentclientprotocol.github.io/python-sdk/use-cases/). |
| 55 | + async def new_session(self, cwd: str, **kwargs: Any) -> NewSessionResponse: |
| 56 | + return NewSessionResponse(session_id=uuid4().hex) |
33 | 57 |
|
34 | | -## How to get started |
| 58 | + async def prompt(self, session_id: str, prompt: list[Any], **kwargs: Any) -> PromptResponse: |
| 59 | + for block in prompt: |
| 60 | + text = getattr(block, "text", "") |
| 61 | + await self._conn.session_update( |
| 62 | + session_id=session_id, |
| 63 | + update=AgentMessageChunk(content=TextContentBlock(text=text)), |
| 64 | + ) |
| 65 | + return PromptResponse(stop_reason="end_turn") |
35 | 66 |
|
36 | | -- 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. |
37 | | -- Browse the [example gallery](https://github.com/agentclientprotocol/python-sdk/tree/main/examples) to see progressively richer integrations you can copy or extend. |
38 | | -- Skim the [docs hub](https://agentclientprotocol.github.io/python-sdk/) for focused references on contrib helpers, releasing, and transport details. |
39 | 67 |
|
40 | | -## Quick links |
| 68 | +if __name__ == "__main__": |
| 69 | + asyncio.run(run_agent(EchoAgent())) |
| 70 | +``` |
| 71 | + |
| 72 | +### Connect from a client |
| 73 | + |
| 74 | +```python |
| 75 | +import asyncio |
| 76 | +import sys |
| 77 | +from typing import Any |
| 78 | + |
| 79 | +from acp import PROTOCOL_VERSION, Client, spawn_agent_process |
| 80 | +from acp.schema import TextContentBlock |
| 81 | + |
41 | 82 |
|
42 | | -| Need | Link | |
43 | | -|--------------------|------------------------------------------------------------------------| |
44 | | -| Docs hub | <https://agentclientprotocol.github.io/python-sdk/> | |
45 | | -| Quickstart | <https://agentclientprotocol.github.io/python-sdk/quickstart/> | |
46 | | -| Use cases | <https://agentclientprotocol.github.io/python-sdk/use-cases/> | |
47 | | -| Contrib helpers | <https://agentclientprotocol.github.io/python-sdk/contrib/> | |
48 | | -| Releasing workflow | <https://agentclientprotocol.github.io/python-sdk/releasing/> | |
49 | | -| Examples | <https://github.com/agentclientprotocol/python-sdk/tree/main/examples> | |
50 | | -| Tests | <https://github.com/agentclientprotocol/python-sdk/tree/main/tests> | |
51 | | -| PyPI | <https://pypi.org/project/agent-client-protocol/> | |
| 83 | +class SimpleClient(Client): |
| 84 | + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: |
| 85 | + print(session_id, update) |
52 | 86 |
|
53 | | -## Project layout |
54 | 87 |
|
55 | | -- `src/acp/`: runtime package (agents, clients, transports, schema bindings, contrib utilities) |
56 | | -- `schema/`: upstream JSON schema sources (regenerate via `make gen-all`) |
57 | | -- `docs/`: MkDocs content backing the published documentation |
58 | | -- `examples/`: runnable scripts covering stdio orchestration patterns |
59 | | -- `tests/`: pytest suite with golden fixtures and optional Gemini coverage |
| 88 | +async def main() -> None: |
| 89 | + async with spawn_agent_process(SimpleClient(), sys.executable, "examples/echo_agent.py") as (conn, _proc): |
| 90 | + await conn.initialize(protocol_version=PROTOCOL_VERSION) |
| 91 | + session = await conn.new_session(cwd=".", mcp_servers=[]) |
| 92 | + await conn.prompt(session_id=session.session_id, prompt=[TextContentBlock(text="Hello ACP!")]) |
60 | 93 |
|
61 | | -## Developer commands |
62 | 94 |
|
63 | | -- `make install` provisions the `uv` virtualenv and installs pre-commit hooks. |
64 | | -- `make check` runs Ruff formatting/linting, type analysis, dependency hygiene, and lock verification. |
65 | | -- `make test` executes `pytest` (with doctests) inside the managed environment. |
66 | | -- `ACP_SCHEMA_VERSION=<ref> make gen-all` refreshes protocol artifacts when the schema advances. |
| 95 | +asyncio.run(main()) |
| 96 | +``` |
| 97 | + |
| 98 | +## Protocol and transport support |
| 99 | + |
| 100 | +| Capability | Import / Extra | Status | Reference | |
| 101 | +| --- | --- | --- | --- | |
| 102 | +| ACP v1 (`schema-v1.35.0`) | `acp`, `acp.schema` | Stable | [Quickstart](https://agentclientprotocol.github.io/python-sdk/quickstart/) · [Protocol Features](https://agentclientprotocol.github.io/python-sdk/protocol-features/) | |
| 103 | +| ACP v2 (`schema-v2.0.0-beta.4`) + dual-stack router | `acp.experimental.v2`, `acp.experimental.AgentProtocolRouter` | Preview | [Experimental v2](https://agentclientprotocol.github.io/python-sdk/experimental-v2/) | |
| 104 | +| Stdio JSON-RPC (NDJSON) | Built-in (`run_agent`, `connect_to_agent`, `spawn_agent_process`) | Stable | [Quickstart](https://agentclientprotocol.github.io/python-sdk/quickstart/) | |
| 105 | +| Streamable HTTP & WebSocket | `acp.http`, `acp.ws` (`[http]`) | Experimental | [Web Transport](https://agentclientprotocol.github.io/python-sdk/web-transport/) | |
| 106 | +| Stateless MCP-over-ACP | `acp` / `acp.experimental.v2` | Unstable / Preview | [Protocol Features](https://agentclientprotocol.github.io/python-sdk/protocol-features/#stateless-mcp-over-acp) | |
| 107 | +| Session, tool call, and permission helpers | `acp.contrib` | Experimental | [Contrib Utilities](https://agentclientprotocol.github.io/python-sdk/contrib/) | |
| 108 | + |
| 109 | +## Documentation |
| 110 | + |
| 111 | +| Topic | Link | |
| 112 | +| --- | --- | |
| 113 | +| Documentation hub | <https://agentclientprotocol.github.io/python-sdk/> | |
| 114 | +| Quickstart | <https://agentclientprotocol.github.io/python-sdk/quickstart/> | |
| 115 | +| Protocol features (v1) | <https://agentclientprotocol.github.io/python-sdk/protocol-features/> | |
| 116 | +| Web transport (HTTP & WebSocket) | <https://agentclientprotocol.github.io/python-sdk/web-transport/> | |
| 117 | +| Experimental protocol v2 | <https://agentclientprotocol.github.io/python-sdk/experimental-v2/> | |
| 118 | +| Contrib utilities | <https://agentclientprotocol.github.io/python-sdk/contrib/> | |
| 119 | +| Use cases | <https://agentclientprotocol.github.io/python-sdk/use-cases/> | |
| 120 | +| Testing & conformance | <https://agentclientprotocol.github.io/python-sdk/testing/> | |
| 121 | +| Releasing | <https://agentclientprotocol.github.io/python-sdk/releasing/> | |
| 122 | +| Runnable examples | <https://github.com/agentclientprotocol/python-sdk/tree/main/examples> | |
| 123 | + |
| 124 | +## Development |
| 125 | + |
| 126 | +```bash |
| 127 | +make install # Create uv environment and install pre-commit hooks |
| 128 | +make check # Lockfile check, Ruff lint/format, ty type check, deptry |
| 129 | +make test # Run pytest with doctests |
| 130 | +make gen-check # Verify generated schema bindings match schema/ |
| 131 | +make docs-test # Build MkDocs site strictly |
| 132 | +``` |
67 | 133 |
|
68 | | -Keep docs and examples current whenever you ship public API or transport changes, and prefer Conventional Commits (`feat:`, `fix:`, etc.) when submitting patches. |
| 134 | +To regenerate protocol bindings when upstream schemas advance, run `ACP_SCHEMA_VERSION=<ref> make gen-all` (or `make gen-v2` for v2). |
69 | 135 |
|
70 | | -## Community & support |
| 136 | +## Community |
71 | 137 |
|
72 | | -- File issues or feature requests at <https://github.com/agentclientprotocol/python-sdk>. |
73 | | -- Discuss ideas or get help via GitHub Discussions: <https://github.com/agentclientprotocol/python-sdk/discussions>. |
74 | | -- Join the broader ACP conversations at <https://agentclientprotocol.com/>, the Zed community channels, or the community Zulip: <https://agentclientprotocol.zulipchat.com/>. |
75 | | -- Shared learnings, integrations, or third-party transports are welcome additions to the documentation—open a PR! |
| 138 | +- Issues and pull requests: <https://github.com/agentclientprotocol/python-sdk> |
| 139 | +- Discussions: <https://github.com/agentclientprotocol/python-sdk/discussions> |
| 140 | +- ACP specification and ecosystem: <https://agentclientprotocol.com/> |
| 141 | +- Zulip chat: <https://agentclientprotocol.zulipchat.com/> |
0 commit comments