|
| 1 | +# Experimental Protocol v2 |
| 2 | + |
| 3 | +> **Experimental.** Protocol v2 is a draft. Import it from `acp.experimental` and |
| 4 | +> expect its API and generated models to change with the upstream schema. |
| 5 | +
|
| 6 | +The v2 runtime is separate from the stable v1 API. Its methods accept and return |
| 7 | +generated request and response models directly: |
| 8 | + |
| 9 | +```python |
| 10 | +from acp.experimental import v2 |
| 11 | + |
| 12 | +connection = v2.connect_to_agent(MyClient(), transport) |
| 13 | +initialized = await connection.initialize( |
| 14 | + v2.schema.InitializeRequest( |
| 15 | + protocol_version=v2.PROTOCOL_VERSION, |
| 16 | + info=v2.schema.Implementation(name="my-client", version="1.0.0"), |
| 17 | + ) |
| 18 | +) |
| 19 | +session = await connection.open_session( |
| 20 | + v2.schema.NewSessionRequest(cwd="/workspace") |
| 21 | +) |
| 22 | +``` |
| 23 | + |
| 24 | +`open_session()` returns an `ActiveSession`. A v2 prompt is accepted before the |
| 25 | +agent finishes it, so consume session updates until `SessionStop` marks the |
| 26 | +`running` to `idle` transition: |
| 27 | + |
| 28 | +```python |
| 29 | +await session.prompt( |
| 30 | + v2.schema.PromptRequest( |
| 31 | + session_id=session.session_id, |
| 32 | + prompt=[v2.schema.TextContentBlock(text="Hello")], |
| 33 | + ) |
| 34 | +) |
| 35 | +stopped = await session.wait_for_idle() |
| 36 | +``` |
| 37 | + |
| 38 | +## Negotiate v1 or v2 |
| 39 | + |
| 40 | +Use `ClientNegotiator` when the same client can speak both versions. It sends |
| 41 | +exactly one `initialize` request and returns a version-tagged connection: |
| 42 | + |
| 43 | +```python |
| 44 | +from acp.experimental import ( |
| 45 | + ClientNegotiator, |
| 46 | + NegotiatedV2, |
| 47 | + V1ClientConfig, |
| 48 | + V2ClientConfig, |
| 49 | +) |
| 50 | + |
| 51 | +negotiator = ClientNegotiator( |
| 52 | + transport, |
| 53 | + v1=V1ClientConfig(client=v1_client, initialize=v1_initialize), |
| 54 | + v2=V2ClientConfig(client=v2_client, initialize=v2_initialize), |
| 55 | +) |
| 56 | +negotiated = await negotiator.negotiate() |
| 57 | + |
| 58 | +if isinstance(negotiated, NegotiatedV2): |
| 59 | + session = await negotiated.connection.open_session(v2_new_session) |
| 60 | +else: |
| 61 | + session = await negotiated.connection.new_session(cwd="/workspace") |
| 62 | +``` |
| 63 | + |
| 64 | +Agents that serve both versions use `AgentProtocolRouter`: |
| 65 | + |
| 66 | +```python |
| 67 | +from acp.experimental import AgentProtocolRouter |
| 68 | + |
| 69 | +router = AgentProtocolRouter(v1=v1_agent, v2=v2_agent) |
| 70 | +await router.run() |
| 71 | +``` |
| 72 | + |
| 73 | +The selected runtime remains strict after initialization: v1 messages are not |
| 74 | +accepted by a v2 connection, and v2 messages are not translated into v1 calls. |
| 75 | +Only the initial v2 request is reduced to the common v1 initialization fields |
| 76 | +when an agent selects v1. |
0 commit comments