|
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 models and asyncio transports over stdio, Streamable HTTP, and WebSocket. |
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 | +For experimental HTTP/WebSocket transports, install the `http` extra (`pip install "agent-client-protocol[http]"`). Requires Python 3.10–3.15. |
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 | +## Quick example |
26 | 25 |
|
27 | | -## Who benefits |
| 26 | +### Build an agent |
28 | 27 |
|
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/). |
| 28 | +```python |
| 29 | +import asyncio |
| 30 | +from typing import Any |
| 31 | +from uuid import uuid4 |
33 | 32 |
|
34 | | -## How to get started |
| 33 | +from acp import PROTOCOL_VERSION, Agent, InitializeResponse, NewSessionResponse, PromptResponse, run_agent |
| 34 | +from acp.interfaces import Client |
| 35 | +from acp.schema import AgentMessageChunk, TextContentBlock |
35 | 36 |
|
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 | 37 |
|
40 | | -## Quick links |
| 38 | +class EchoAgent(Agent): |
| 39 | + def on_connect(self, conn: Client) -> None: |
| 40 | + self._conn = conn |
41 | 41 |
|
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/> | |
| 42 | + async def initialize(self, protocol_version: int, **kwargs: Any) -> InitializeResponse: |
| 43 | + return InitializeResponse(protocol_version=PROTOCOL_VERSION) |
52 | 44 |
|
53 | | -## Project layout |
| 45 | + async def new_session(self, cwd: str, **kwargs: Any) -> NewSessionResponse: |
| 46 | + return NewSessionResponse(session_id=uuid4().hex) |
54 | 47 |
|
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 |
| 48 | + async def prompt(self, session_id: str, prompt: list[Any], **kwargs: Any) -> PromptResponse: |
| 49 | + for block in prompt: |
| 50 | + text = getattr(block, "text", "") |
| 51 | + await self._conn.session_update( |
| 52 | + session_id=session_id, |
| 53 | + update=AgentMessageChunk(content=TextContentBlock(text=text)), |
| 54 | + ) |
| 55 | + return PromptResponse(stop_reason="end_turn") |
60 | 56 |
|
61 | | -## Developer commands |
62 | 57 |
|
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. |
| 58 | +if __name__ == "__main__": |
| 59 | + asyncio.run(run_agent(EchoAgent())) |
| 60 | +``` |
| 61 | + |
| 62 | +### Drive an agent from Python |
| 63 | + |
| 64 | +```python |
| 65 | +import asyncio |
| 66 | +import sys |
| 67 | +from typing import Any |
| 68 | + |
| 69 | +from acp import PROTOCOL_VERSION, Client, spawn_agent_process |
| 70 | +from acp.schema import TextContentBlock |
| 71 | + |
| 72 | + |
| 73 | +class SimpleClient(Client): |
| 74 | + async def session_update(self, session_id: str, update: Any, **kwargs: Any) -> None: |
| 75 | + print(session_id, update) |
| 76 | + |
| 77 | + |
| 78 | +async def main() -> None: |
| 79 | + async with spawn_agent_process(SimpleClient(), sys.executable, "examples/echo_agent.py") as (conn, _proc): |
| 80 | + await conn.initialize(protocol_version=PROTOCOL_VERSION) |
| 81 | + session = await conn.new_session(cwd=".", mcp_servers=[]) |
| 82 | + await conn.prompt(session_id=session.session_id, prompt=[TextContentBlock(text="Hello ACP!")]) |
67 | 83 |
|
68 | | -Keep docs and examples current whenever you ship public API or transport changes, and prefer Conventional Commits (`feat:`, `fix:`, etc.) when submitting patches. |
69 | 84 |
|
70 | | -## Community & support |
| 85 | +asyncio.run(main()) |
| 86 | +``` |
| 87 | + |
| 88 | +## Documentation |
| 89 | + |
| 90 | +- [Quickstart](https://agentclientprotocol.github.io/python-sdk/quickstart/) — Connect your agent to Zed, Toad, Lody, or T3 Code, or drive agents programmatically. |
| 91 | +- [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`). |
| 92 | +- [Web Transport](https://agentclientprotocol.github.io/python-sdk/web-transport/) — Serve and consume ACP over Streamable HTTP and WebSocket (`[http]`). |
| 93 | +- [Experimental Protocol v2](https://agentclientprotocol.github.io/python-sdk/experimental-v2/) — Preview v2 runtime (`schema-v2.0.0-beta.4`) and dual-stack `AgentProtocolRouter`. |
| 94 | +- [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/) |
| 95 | + |
| 96 | +## Development |
| 97 | + |
| 98 | +```bash |
| 99 | +make install # Set up uv environment and pre-commit hooks |
| 100 | +make check # Format, lint, type-check, and dependency check |
| 101 | +make test # Run pytest suite |
| 102 | +``` |
| 103 | + |
| 104 | +## Community |
71 | 105 |
|
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! |
| 106 | +Join the conversation on [GitHub Discussions](https://github.com/agentclientprotocol/python-sdk/discussions), [Zulip](https://agentclientprotocol.zulipchat.com/), or [agentclientprotocol.com](https://agentclientprotocol.com/). |
0 commit comments