The core SDK can opt into ACP protocol v2 with the unstable_protocol_v2 crate
feature. Protocol v2 has been in Preview since schema 1.20: it is ready to build
on, and it is not frozen. The SDK's v2 API can still change in a minor release.
agent-client-protocol = { version = "...", features = ["unstable_protocol_v2"] }This feature is separate from the broad unstable feature because protocol v2
is a separate protocol version, not an unstable method family.
To start from working code, build and run the companion agent and client in the Runnable Protocol V2 Quickstart. The examples exercise prompt acceptance, independent session updates, and the terminal idle state over a real stdio connection.
Protocol v2 removed JSON-RPC batching, and the SDK no longer supports it in
either protocol version. Every connection answers an incoming JSON array with a
single -32600 (Invalid Request) error with id: null and processes none of
its entries. The SDK never sends a batch. See Transport Architecture: JSON-RPC
Batches.
Every connection names its version: Client.v1(), Agent.v1(), and
Proxy.v1() expose the stable v1 API, and Client.v2(), Agent.v2(), and
Proxy.v2() the v2 API. The SDK types for each version live in
agent_client_protocol::v1 and agent_client_protocol::v2, which export the
same names: ConnectionTo, Builder, Responder, ResponseRouter,
SentRequest, PreparedRequest, Dispatch, SessionBuilder,
ResumeSessionBuilder, SessionSetup, ActiveSession, and schema (plus
ForkSessionBuilder and the send_initialize outcome types in v2). Moving code from v1 to v2 is
mostly a matter of changing which module it imports from.
Fluent typed handlers, spawned tasks, close callbacks, and connect_with
receive v2::ConnectionTo<_>, so the protocol version is reflected in the
high-level Rust API as well as on the wire:
use agent_client_protocol::schema::ProtocolVersion;
use agent_client_protocol::v2::schema;
use agent_client_protocol::{Agent, Client};
fn implementation() -> schema::Implementation {
schema::Implementation::new("example", "0.1.0")
}
# async fn run(agent_transport: impl agent_client_protocol::ConnectTo<agent_client_protocol::Client>) -> agent_client_protocol::Result<()> {
Client
.v2()
.connect_with(agent_transport, async |cx| {
let initialize = cx
.send_request(schema::InitializeRequest::new(
ProtocolVersion::V2,
implementation(),
))
.block_task()
.await?;
assert_eq!(initialize.protocol_version, ProtocolVersion::V2);
Ok(())
})
.await?;
# Ok(())
# }
# async fn serve(client_transport: impl agent_client_protocol::ConnectTo<agent_client_protocol::Agent>) -> agent_client_protocol::Result<()> {
Agent
.v2()
.on_receive_request(
async |initialize: schema::InitializeRequest, responder, _cx| {
responder.respond(schema::InitializeResponse::new(
initialize.protocol_version,
implementation(),
))
},
agent_client_protocol::on_receive_request!(),
)
.connect_to(client_transport)
.await?;
# Ok(())
# }When v2 mode is enabled, application code should use SDK types from
agent_client_protocol::v2 and message types from v2::schema. Importing
agent_client_protocol::v2::{self, schema} brings both into scope without a
name clash. The preceding agent fragment demonstrates version negotiation only.
An agent that advertises session support must also implement the complete
baseline session surface shown in the runnable quickstart.
Neither method has an initialize-time capability. An agent that advertises
capabilities.cancel on a tool call or terminal must implement the corresponding
request. The SDK answers an unhandled method with -32601 (Method not found).
Before initialization these requests get -32600 like any other.
If you implement the method, respond with -32002 and data.sessionId for an
unknown or inactive session. An announced child of an active session is not
unknown. For a known session, respond with -32602 (Invalid params) if the tool
call or terminal is unknown, final, or not cancellable.
sessionId is the session that reports the tool call or terminal. For a
subagent's tool call or terminal, that is the child session. The item's own
capabilities.cancel enables the request even when the child session doesn't
advertise cancel, and it enables nothing else on the child. Send
v2::schema::CancelToolCallRequest or v2::schema::CancelTerminalRequest with
the child's sessionId.
Stable callbacks receive v1::ConnectionTo<_> and expose the protocol v1
build_session*, load_session*, resume_session*,
v1::SessionBuilder, v1::ActiveSession, and v1::SessionMessage APIs.
Callbacks installed through Client.v2() receive v2::ConnectionTo<_> and expose
the v2 build_session* and resume_session* helpers, plus feature-gated
fork_session* helpers when unstable_session_fork is enabled. The shared
names describe the same lifecycle operations while the connection type selects
their schema and return types at compile time. Resume and fork return
v2::ResumeSessionBuilder and v2::ForkSessionBuilder. Call start_session to
publish the request and obtain a v2::SessionSetup containing the command
handle and complete operation-specific response.
Low-level custom with_handler and with_runner implementations continue to
receive the protocol-neutral ConnectionTo<_>, and generic send_request
remains schema-agnostic. These generic APIs do not infer a protocol version from
the Rust payload type: callers on a v2 connection must use v2::schema types,
or deliberately send extension or untyped messages. The connection guard
enforces negotiation and initialization lifecycle, not Rust-type provenance.
Dynamic handlers registered through v2::ConnectionTo::add_dynamic_handler use
the same low-level HandleDispatchFrom interface and therefore also receive
ConnectionTo<_>. Raw code converts explicitly: connection.into_protocol::<V1>()
returns a v1::ConnectionTo (and into_protocol::<V2>() a v2::ConnectionTo),
and either versioned connection's into_raw() returns the version-neutral one.
into_protocol fails with -32603 when the connection's builder guards a
different version, so a connection from Client.v1() never becomes a
v2::ConnectionTo. ConnectionTo::protocol_version() reports the guarded
version, or None for an unguarded Builder::new connection.
spawn_connection returns the connection type the child builder's callbacks
receive, on the raw ConnectionTo and on both versioned connections:
spawning Client.v2() returns a v2::ConnectionTo<_>, spawning
Client.v1() a v1::ConnectionTo<_>, and spawning a raw Builder::new
builder a raw ConnectionTo<_>.
A complete client installs update and interactive-request handlers before
connecting. After session/prompt is accepted, it must keep the connection
alive and consume updates until the matching session reaches idle. The
v2_one_shot_client
example demonstrates the full sequence, while the compiled cookbook
v2_one_shot_prompt recipe shows how to embed it in an application. Permission
handlers should transfer the request and responder to application-owned work
rather than waiting for user input inside the dispatch callback.
V2 deliberately separates prompt submission from session observation:
session/promptreturns aPromptResponseas soon as the agent accepts the prompt. Itsmessage_ididentifies the inserted user message, and liveUserMessageupdates for that prompt use the same ID. Persisting the message in resumable history remains optional. The response does not indicate turn completion.v2::ActiveSession::send_promptreturns that request as aSentRequest<PromptResponse, V2>. Callers must explicitly await it, register a response callback, or detach it.v2::SessionBuilder::start_sessionlikewise returns a mappedSentRequest. Itsv2::SessionSetupresult keeps the command handle separate from the completeNewSessionResponserepresented by the linked schema, rather than reconstructing a selected subset of its fields.v2::ConnectionTo::resume_sessionandresume_session_fromreturn av2::ResumeSessionBuilder. Itsstart_sessionmethod publishessession/resumeand returns av2::SessionSetupcontaining the completeResumeSessionResponsewithout reconstructing it.- With
unstable_session_fork,v2::ConnectionTo::fork_sessionandfork_session_fromreturn av2::ForkSessionBuilder. Itsstart_sessionpublishessession/fork, preserves the completeForkSessionResponse, and uses that response's newly allocated session ID for the command handle. v2::ActiveSessionis a cloneable command handle containing only the session ID and connection. It does not own, buffer, or unregister inbound messages.- Register typed
UpdateSessionNotificationandRequestPermissionRequesthandlers onClient.v2()before connecting. Updates and interactive requests are separate protocol lanes. Permission handlers should transfer responders to application-owned work rather than waiting for user input inside the connection dispatch loop. Without matching handlers, unhandled v2 notifications are ignored and unhandled requests receive a method-not-found response. They are not retained for a later per-session receiver. session/updateevents can arrive before, during, or after a prompt request. They carry a session ID and entity IDs, but no prompt or turn ID. The SDK therefore does not attribute intervening events to a locally submitted prompt or provide a prompt-scoped text accumulator.state_updatedescribes the session-wide foreground state.idlemeans the session can accept ordinary new foreground work. It is not a wire-level boundary assigning previous events to one prompt, and background updates may continue while idle.send_promptmay be called in any state. The agent inserts the prompt right away or holds it and inserts held prompts in received order; it must not reject a prompt only because foreground work is active. A held prompt's response stays pending until insertion.cancel_active_workandclosedrop the prompts the agent has received and not inserted. Cancelling a held prompt'sSentRequestsends$/cancel_request, which is best effort: the agent either answers-32800or inserts the prompt as usual. An inserted prompt cannot be withdrawn.send_prompt_blocks_with_keysends the prompt with anidempotencyKey. While the agent recognizes the key, a retry with the same key does not insert a second user message. A resumed session does not tell the client which keys the agent kept, so a retry after a reconnect can still insert one (see Schema 1.20).send_promptandsend_prompt_blockssend no key.cancel_active_worksends session-widesession/cancel. It covers the session's foreground work and prompts received before it that the agent has not yet inserted; those prompt requests fail with-32800(request cancelled), while a prompt whose user message was already inserted still succeeds with its message ID. A prompt sent after it is not cancelled. Work in the latestbackgroundlist is not covered and keeps running; the agent may stop background activity it does not list. Cancellation of active foreground work completes after the requiredidleupdate with stop reasoncancelled, which the agent sends before it starts new foreground work in the session. Updates for listed background work can still arrive after it. Cancelling an idle session need not emit another idle update, and work that had already ended keeps its stop reason.cancelledis reserved for work the client cancelled withsession/cancelor by closing the session, and for a child session whose parent cancelled its work. Work the agent stops on its own uses another reason, such aserror. A prompt sent while cancellation is in progress is not rejected for that reason: the agent inserts it, or holds it until the cancelled work has reportedidle. The client should immediately mark unfinished tool calls for the active work as cancelled. The agent owns pending permission requests: it stops waiting for those of the cancelled work, treats them as not approved, and must cancel each with$/cancel_request. The agent decides which requests belong to that work, so the client does not answer every pending permission request when it sendssession/cancel. The client should dismiss a cancelled permission request and answer it with thecancelledoutcome, and may answer one it knows belongs to the cancelled work that way without waiting. Permission requests for listed background work stay open. Elicitations are not all cancelled implicitly: the agent must cancel each elicitation it stops waiting for with an individual$/cancel_request, and the client should answer it with thecancelaction. Cancelling or dropping the prompt'sSentRequestis the separate JSON-RPC request-cancellation mechanism; insertion still requires a successful prompt response with its message ID.set_config_optionmust only name aconfigIdthe agent advertised for the session. An unknownconfigIdor invalid value fails with-32602and leaves the configuration unchanged. A successful response is the complete option set as of the response and may report a differentcurrentValuethan requested. Apply responses andconfig_option_updatenotifications in the order received, each replacing the whole list, even though a duplicate notification for the same change may arrive before the response.closereturns the complete close response. Closing is idempotent, soclosealso resolves a-32002response (an unknown or already-closed session) as success. Mutable configuration is not cached on the command handle.
Install connection handlers before session/new, session/resume, and
feature-gated session/fork requests. This is especially important before
calling start_session on a v2::ResumeSessionBuilder: replay updates precede
the resume response on the wire, so preinstalled typed handlers observe them in
order. The agent ends the replayed history with a replay_complete update
(see Schema 1.18). Do not send a second session/resume with a
replayFrom cursor for a session while the first one is pending. If a handler forwards updates to another
task, the application is responsible for any additional projection-drained
barrier it needs before treating replay as locally applied.
See Ordered Application Dispatch for a
single-consumer queue that carries updates, response results, and closure.
For an application-owned prototype that additionally coordinates shared resume
replay, abandonment, and close, see
Session Operation Coordination. It is a
cookbook policy example, not a public SDK coordinator.
Dropping command handles has no network or inbound-routing side effect. For a
session configured with v2::SessionBuilder::with_mcp_server or
v2::ResumeSessionBuilder::with_mcp_server, or feature-gated
v2::ForkSessionBuilder::with_mcp_server, the SDK installs the MCP routes and
initially polls their runner tasks before publishing the corresponding setup
request, so the agent can connect to those servers during setup or resume
replay. Runners may continue asynchronous initialization, so custom connectors
must be able to queue connections and messages once constructed. A successful
setup promotes the attachment to the connection lifetime. A setup failure,
including an error response after cancellation, cleans up the pending
attachment. This attachment requires both unstable_protocol_v2 and
unstable_mcp_over_acp. Fork additionally requires
unstable_session_fork.
A v2 proxy can instead attach one server globally with
Proxy.v2().with_mcp_server(...). The proxy reuses one connection-scoped
server ID and adds its declaration to v2 session/new, session/resume, and
feature-gated session/fork requests. It modifies only the mcpServers field,
preserving unrelated setup fields and extensions for downstream handlers.
MCP server names must be unique within mcpServers. If the client already
declares a server with the proxy server's name, the proxy fails that request
with -32602, naming the server it configures, instead of forwarding a request
the agent must reject. It does not rename or drop either server.
v2::SessionBuilder::on_proxy_session_start and
v2::ResumeSessionBuilder::on_proxy_session_start, plus
v2::ForkSessionBuilder::on_proxy_session_start when enabled, are the
non-blocking setup helpers for a v2 proxy:
use agent_client_protocol::v2::{SessionSetup, schema};
use agent_client_protocol::{Client, Proxy};
Proxy
.v2()
.on_receive_request_from(
Client,
async |request: schema::NewSessionRequest, responder, cx| {
cx.build_session_from(request)
.with_mcp_server(session_server)?
.on_proxy_session_start(responder, async |setup| {
let SessionSetup { session, response, .. } = setup;
record_session(session.session_id(), response);
Ok(())
})
},
agent_client_protocol::on_receive_request!(),
);These helpers forward request cancellation, send an ordered downstream setup
request, and forward the complete operation-specific response without
reconstruction. For session/new and session/fork, routing is installed when
the response makes the new session ID available and before later inbound
traffic is dispatched. Fork routing uses the response's new ID rather than the
source session ID. For session/resume, the ID is already known, so routing and any per-session
MCP attachment are ready before the downstream request is published. Replay
updates can therefore be forwarded upstream before the complete
ResumeSessionResponse, as required by the protocol. A failed or cancelled
downstream response drops pending routing and MCP attachment. Successful setup
keeps them for the connection lifetime. A cancellation signal itself remains
advisory: it is forwarded downstream while the helper awaits that response.
Each helper then spawns the callback outside the ordering barrier with an
v2::SessionSetup containing the command-only session handle and complete setup
response. Updates and interactive requests remain independent connection
traffic and should still be handled by typed callbacks on Proxy.v2().
If an application wants stream ergonomics, it can fan typed updates out from the connection handler with an explicit buffering and subscriber policy.
Proxy authors should make the version boundary explicit. Proxy.v1() is
the stable v1 builder, while Proxy.v2() is v2-only. A proxy negotiates
proxy/initialize exactly as an agent of the same version negotiates
initialize: a v1 proxy selects v1 for every request, keeping only what
parses under v1 for a newer one; a v2 proxy selects v2 for a request for v2 or
newer, keeping only what parses under v2 for a newer one, and rejects a
request below v2 with -32022. A proxy never parses one version's request
through a permissive schema for another.
When one component must expose independently authored v1 and v2 proxies,
compose them with
Proxy.protocol_router().with_v1(v1_proxy).with_v2(v2_proxy). The proxy
router selects an implementation from proxy/initialize exactly as
Agent.protocol_router() does from initialize: a request for v2 or newer
selects v2, and with a v1 implementation configured everything else goes to
v1, which negotiates it as it would alone. It hands over the complete,
unchanged initial transport frame for a request for exactly the selected
version and does not convert later traffic.
Components implementing their own raw version selector can use
Builder::new(Proxy) and keep protocol-neutral ConnectionTo callbacks.
Builder::new applies no protocol-version guard, so it is not a substitute
for selecting Proxy.v2() in an ordinary v2 proxy implementation. A proxy's default forwarding is the same for every builder and
every protocol version: proxy/initialize has the same wire shape in v1 and
v2, so the proxy forwards untyped messages, and fields the SDK does not know
survive. After a forwarded session/new succeeds, the proxy reads the raw
sessionId from the response and routes that session's updates back to the
client.
The unstable_protocol_v2 feature only adds v2 items. Enabling it never
changes how a v1 connection behaves: where one code path serves both versions,
the SDK selects v2 behavior from the connection's protocol version at runtime.
The conductor forwards both versions through one untyped path that does not
depend on the feature or on the selected version. Its connections to the
client and to the final agent have no version guard; the endpoints validate
the traffic of the version the chain negotiated. The conductor rejects a
proxy/initialize that arrives from outside, renames the client's
initialize to proxy/initialize for the first proxy without touching its
params (the proxy restores the ordinary method before the request reaches the
final agent), and forwards everything else unchanged. info, capabilities,
metadata, and unknown extension fields therefore retain their wire shape
across conductor-controlled rewrites in both versions. A proxy implementation
can still deliberately replace the request it forwards.
Before it instantiates the chain, the conductor selects the version from the
raw protocolVersion: with the conductor crate's unstable_protocol_v2
feature, a request for v2 or newer selects v2; every other request selects v1,
keeping v1's fallback-to-latest rule. A request for a newer version than the
selected one keeps only what parses under the selected version, as the SDK's
own agents negotiate (agent_client_protocol::util::initialize_params_for_version).
A conductor acting as a proxy negotiates its proxy/initialize the same way.
The conductor's selection is the highest version the chain may speak, and it
forwards the request at that version. Because each proxy negotiates like an
agent, a proxy that supports only a lower version initializes the rest of the
chain with that version, and the initialize response that reaches the client
carries the version every component accepted. A component that cannot accept
it, such as a v2-only agent behind a v1-only proxy, rejects the
initialization, and the client sees the error.
Proxy implementations use the schema crate's
agent_client_protocol::v2::schema::InitializeProxyRequest. Its response is
v2::schema::InitializeProxyResponse, a transparent wrapper around the v2
InitializeResponse. v1::schema::InitializeProxyRequest is the v1
type. Static conductor component providers can carry either selected
schema, but each supplied component must support that version. Use
Agent.protocol_router() or Proxy.protocol_router() when a static component
has separate implementations.
Custom InstantiateProxiesAndAgent and InstantiateProxies implementations
receive a SelectedInitializeRequest whose variant is the selected version.
An instantiator that supports only a lower version negotiates down by
returning the request for that version (for example with
SelectedInitializeRequest::into_v1, which keeps what parses under v1).
Returning the initialize request unchanged preserves its complete raw
parameters for an exact-version request, including unknown extensions.
Returning a modified typed request makes that serialized request
authoritative. The conductor sets protocolVersion to the version of the
returned variant even if an instantiator attempts to change the field, and
rejects a variant above the selected version.
The concrete
agent_client_protocol_polyfill::mcp_over_acp::McpOverAcpPolyfill can
participate in a v2 conductor chain when its unstable_protocol_v2 feature is
enabled. It selects v1 or v2 from proxy/initialize, uses that version's MCP
capability and wire types, and adapts native McpServer::Acp declarations in
v2 session/new, session/resume, and session/fork requests.
Other declarations and unrelated request fields remain unchanged. See
MCP-over-ACP Compatibility Bridge for placement and feature
configuration.
This feature extends the concrete compatibility proxy only. The core SDK's global MCP attachment and proxy-session helpers support v1 and v2 independently, as described above.
The SDK handles the initialize negotiation at the JSON-RPC boundary:
- Native
Client.v2()andAgent.v2()connections reject ordinary protocol traffic until the initialization response completes.$/cancel_requestremains available while initialization is in progress. The client is the initializer and the agent is the responder. Attempts in the opposite direction are rejected. An initialization error leaves the connection uninitialized so the client can retry. A secondinitializewhile one is in flight is rejected with-32600. - The client may repeat
initializeafter the previous one completed and before it sends any other request, for example to resend its parameters for the version the agent selected. The agent dispatches each repeat to its initialize handler and the latest one is authoritative. While a repeat is in flight, other requests are rejected as they are during the first. Each repeat is negotiated like the firstinitialize: a v2 agent selects v2 for a request for v2 or newer, and answers a request below v2 with-32022anddata: { "requested", "supported": [2] }. A repeat that fails, whether rejected by the SDK or by the handler, keeps the previous initialization in place and the client may repeat again. The first other request closes this window: a laterinitializeis rejected with-32600on both sides and the connection stays ready. Notifications such as$/cancel_requestdo not close it. - V2 request ids must be strings or integers. A v2 connection answers a request
with a
nullid with-32600andid: null. V1 connections still acceptnullids. - Since schema 1.13, v2 has its own
v2::schema::RequestIdwithout aNullvariant. Request handles carry a protocol marker as their last type parameter:Responder<T, V2>,ResponseRouter<T, V2>,SentRequest<T, V2>,PreparedRequest<T, V2>andDispatch<Req, Notif, V2>, namedv2::Responder<T>,v2::ResponseRouter<T>,v2::SentRequest<T>,v2::PreparedRequest<T>andv2::Dispatch<Req, Notif>. Handles fromClient.v2(),Agent.v2(),Proxy.v2()andv2::ConnectionTouseV2, so theirid()returns av2::schema::RequestId, for example to match an elicitation'srequestId. It never fails, because the SDK rejectsnullIDs on v2 connections and never assignsnullitself. v1 builders useV1. Low-levelHandleDispatchFromandMatchDispatchhandlers are not tied to a version: they receive handles with the defaultRawmarker, whoseid()is the version-neutralRawJsonRpcId. Convert one to a v2 handle withinto_protocol::<V2>(), for example to callon_proxy_session_startfrom a raw router. Every handle also hasraw_id(). - Closures registered on a v2 builder that spell out the responder type need
the v2 handle:
responder: v2::Responder<schema::PromptResponse>withuse agent_client_protocol::v2::{self, schema}(orResponder<schema::PromptResponse, V2>). Unannotated closure parameters are inferred. - A v2 client advertises protocol v2 as its latest supported version.
- A v2 client requires a v2 agent. Send initialization with
send_initializeto learn how the agent negotiated: it resolves withv2::InitializeOutcome::Initializedfor a v2 selection, orv2::InitializeOutcome::LowerVersionwith the agent's validated response for the selected version, anInitializationVersionMismatchwhoseresponseis aSelectedInitializeResponse::V1. Both types are at the crate root because they are not tied to one protocol version. TheInitializedandV1variants each hold a boxedInitializeResponsefrom the selected schema. Read its fields directly, or use*responseto take the owned response out of the box. After a lower selection the connection stays uninitialized, and the caller can explicitly fall back to a v1 client implementation if that is acceptable. Errors returned by the agent, including-32022, malformed responses, and transport failures still resolve with errors. A plainsend_requestof the initialize request instead resolves a v1 selection with a local error. - A native v1 agent selects v1 for requests advertising v1 or a newer version,
with or without
unstable_protocol_v2enabled. Newer initialization metadata is normalized into v1 identity and shared capabilities. Terminal authentication is kept: an object-valuedauth.terminalbecomes v1'sauth.terminal: true. Elicitation capabilities and an object_metaare kept when they parse under v1. The SDK never enables v1 file-system or terminal-execution capabilities from unknown newer fields. V1's original fallback-to-latest rule is retained, including a v1 selection when the client requests an unsupported older version. - A v1 agent that registers a typed
initializerequest handler rejects any other request that arrives before initialization completes with-32600. Agents without one, including those with only catch-all handlers, keep serving requests without initialization. V1 agents still accept a repeatedinitializeat any time. - A standalone v2 agent accepts a request for v2 or a newer version and
selects v2. The request delivered to the handler has
protocol_versionnormalized to v2, and the response selects v2. A request for exactly v2 must be a valid v2 request. A request for a newer version was written for a version the agent doesn't know, so the SDK keeps what parses under v2 instead of rejecting it: capabilities that don't parse and a_metathat is not an object are dropped, fields v2 doesn't define are dropped, and a missing or malformedinfois replaced by an implementation namedunknownwith versionunknown. A v1 agent downgrading a newer request applies the same rule to v1's shape, leaving its optionalclientInfounset instead. If a client initializes with v1, the request resolves with-32022(Unsupported protocol version) anddata: { "requested": 1, "supported": [2] }. The v2 initialization handler is not invoked. Use an agent router with a v1 implementation to serve v1 sessions. - If the agent responds with any other unsupported version, the request resolves with an error so the client can close the connection.
- After initialization, the local API version and negotiated wire version must match. The SDK does not convert traffic between v1 and v2.
That means v1 and v2 implementations still need separate handlers.
Agent.v2(), Client.v2(), and Proxy.v2() are v2-only. While protocol v2
stabilizes, the unstable_protocol_v2 crate feature also exposes
Agent.protocol_router(), Proxy.protocol_router(), and
Client.protocol_connector() for composing version-specific implementations.
Keep these protocol handlers as adapters around shared application logic, not as general-purpose conversions between wire types. In particular, v1 prompt responses end a turn, while v2 prompt responses acknowledge insertion and completion arrives through session state updates. Shared storage also does not automatically make sessions resumable through both versions. See the protocol's dual-stack migration guidance for adapter boundaries and a validation checklist.
Agents can add protocol implementations independently, which makes it easy for applications built with v2 support to control v2 rollout with a runtime feature flag:
use agent_client_protocol::{v1, v2};
use agent_client_protocol::{Agent, ConnectTo};
# fn implementation() -> v2::schema::Implementation {
# v2::schema::Implementation::new("example", "0.1.0")
# }
# async fn serve(client_transport: impl agent_client_protocol::ConnectTo<Agent>) -> agent_client_protocol::Result<()> {
# let enable_protocol_v2 = true;
let v1_agent = Agent.v1().on_receive_request(
async |initialize: v1::schema::InitializeRequest, responder, _cx| {
responder.respond(v1::schema::InitializeResponse::new(initialize.protocol_version))
},
agent_client_protocol::on_receive_request!(),
);
let agent = Agent.protocol_router().with_v1(v1_agent);
let agent = if enable_protocol_v2 {
let v2_agent = Agent.v2().on_receive_request(
async |initialize: v2::schema::InitializeRequest, responder, _cx| {
responder.respond(v2::schema::InitializeResponse::new(
initialize.protocol_version,
implementation(),
))
},
agent_client_protocol::on_receive_request!(),
);
agent.with_v2(v2_agent)
} else {
agent
};
agent
.connect_to(client_transport)
.await?;
# Ok(())
# }The agent protocol router reads the initial initialize request and selects
a version-specific implementation. A request for v2 or a newer version selects
the v2 implementation. When a v1 implementation is configured, everything else
is handed to it unchanged: a request for v1 or an older version, a malformed
initialize, and any other first message. A v1 client therefore sees exactly
the v1 implementation, including v1's original fallback-to-latest rule, as it
would without unstable_protocol_v2. A v2-only endpoint rejects a request
below v2 with a structured version-mismatch error. The router
normalizes a v2 initialize request when selecting a v1 implementation, but does
not convert messages between v1 and v2 after routing. The selected
implementation owns the connection: a repeated initialize is forwarded to it
and never re-routed, so it is negotiated like a first initialize against the
selected version alone. A v2 implementation answers a repeat that requests v1
with -32022 listing only v2, because the connection cannot switch to the v1
API, and the v2 initialization stays in effect. A v1 implementation selects v1
for a repeat that requests a newer version. The
router preserves the complete initial frame when handing it to the selected
implementation. A batched first initialize gets one -32600 error with
id: null, and the client may send initialize again. Response-only frames
before initialization are ignored.
A v2-only endpoint rejects a v1 request without requiring v2's Client identity
or manufacturing a higher-version success response. The error code is
-32022, and its data contains requested and the endpoint's supported
versions. This diagnostic is separate from malformed initialization parameters,
which remain -32602. The router answers with the request's ID and does not
start an application. A failed initialize leaves the connection
uninitialized and open, so the client may send initialize again, for
example with a supported version or corrected parameters. Notifications that
arrive before initialization are ignored. The proxy protocol router, whose
proxy/initialize comes from the conductor, also answers a version mismatch
with -32022 but closes after a rejection.
Custom endpoints can construct the same wire error with
v2::schema::Error::unsupported_protocol_version(requested, supported).into().
The code is defined by v2 negotiation, not by a change to the v1 protocol.
The SDK's version-neutral Error carries any numeric code, so a v1 client
receives the initialization error as an unknown code without new v1 schema
definitions.
just test runs the negotiation tests in both the all-features workspace and
the core crate without default features, so v1 negotiation does not depend on
whether the draft v2 API is compiled in.
Clients that cannot use any supported version should close after a version mismatch and display the supported versions. A successful downgrade must also be checked against the Client's actual supported versions, because advertising a latest version does not imply support for every older version.
The proxy protocol router negotiates proxy/initialize like the agent
protocol router negotiates initialize: it selects the highest configured
version at or below the requested one, rewrites a request for a newer version
to keep what parses under the selected one, and hands the initial frame to
that implementation. A request for exactly the selected version is validated
and handed over unchanged.
Clients use a connector because fallback may require opening a new transport. Both client implementations and the agent transport are factories:
use agent_client_protocol::Client;
let connector = Client
.protocol_connector()
.with_v1(|| v1_client())
.with_v2(|| v2_client());
connector.connect_to(|| open_agent_transport()).await?;The connector starts the highest configured implementation. If a successful v2 initialize response negotiates v1 and a v1 implementation is configured, the connector starts the v1 implementation and compares the complete initialize parameters it would send with the normalized v2 request already seen by the agent:
- If they match exactly, the connector reuses the current agent connection and delivers the original response to the v1 implementation with its request ID. It does not send a second initialize request.
- If they differ, the connector closes that connection, calls both factories again as needed, and performs a fresh v1 initialization on a new agent connection. The protocol permits requesting the lower selected version in that initialization.
- If the agent rejects the v2 initialize request, the error is surfaced. A rejected initialize is not treated as permission to retry with v1.
The reuse probe is conservative: if parsing and serializing the raw v2 request would change any parameter, reuse is disabled and fallback opens a fresh connection. That does not turn an otherwise valid v2 request into an error.
The unstable_protocol_v2 API follows the v2 schema as it changes. Schema 1.5 added
semantic newtypes for paths, media types, IDs, and cursors, renamed
DiffPatch.diff to DiffPatch.text, and added terminal state and output update
types. Schema 1.7 removed the former schema-wide v1/v2 conversion API:
versioned implementations should remain separate, with
purpose-specific adapters at runtime boundaries where the required state and
policy are available. These are draft API changes rather than stable v1 wire
changes. See Migrating to
v2.0 for concrete source changes.
Schema 1.6 adds Cancelled tool-call and plan-entry statuses to draft v2.
Schema 1.7 stabilizes elicitation and terminal authentication, so neither
surface requires its former SDK feature flag. It also adds context compaction
updates (stable since schema 1.11). The SDK carries them through its
existing typed session/update routing in both protocol versions. V1 clients
advertise compaction support through ClientSessionCapabilities::compaction.
Schema 1.8 stabilizes optional programmatic tool-call names in both protocol
versions. No tool-name-specific feature is required. Draft v2 users only need
unstable_protocol_v2. In v1, an omitted or null name leaves the existing
value unchanged on updates. In v2, omission leaves it unchanged, null clears
it, and a string replaces it. V1 cannot express the explicit v2 null clear
operation.
Schema 1.11 stabilizes session notices and compaction. No surface-specific
feature is required. Draft v2 users still need unstable_protocol_v2.
In draft v2, failures after a prompt's user message is inserted end active work
with StopReason::Error(ErrorStopReason) on the idle state update, optionally
including a JSON-RPC error object. Failures before insertion remain error
responses to session/prompt. StopReason now serializes as an object tagged
by stopReason, flattened into IdleStateUpdate. Its Other variant takes
OtherStopReason rather than a string and preserves unknown payload fields.
V1 prompt responses are unchanged and do not accept an error stop reason.
Schema 1.11 also rejects malformed payloads that receivers act on rather than
silently dropping invalid entries or substituting defaults. This includes
session setup's MCP servers and additional directories, terminal launch
arguments and environment, and v2 replay cursors. Typed request decoding
reports invalid parameters (-32602) without invoking the handler. null
still means empty for the defaultable acted-on lists. Omission is accepted
only where the field is optional. Capability and display metadata retain their
lenient decoding.
Client-provided system prompts were added behind the
unstable_session_system_prompt feature. Schema 1.16 stabilizes them and the
feature has been removed. Use the complete lifecycle request with _from
helpers and negotiate the agent's capability first; see
Client-provided system prompts.
Next Edit Suggestions, their document events, and their position-encoding
capabilities have been removed from both protocol versions.
Foreground states are aggregate snapshots, not per-prompt events:
- Report
runningwhen work starts, orrequires_actionif it starts blocked on user action. Agent-initiated workflows and local commands use the same lifecycle without needing a model call or a synthetic user prompt. requires_actionmeans the foreground workflow as a whole is blocked. A pending permission or elicitation does not imply this state while other foreground work can progress or when it belongs to background activity. Once reported, sendrunningwhen progress resumes oridlewhen work ends.- When no foreground work remains, report
idle; normally include a stop reason. Repeated snapshots, including an old stop reason, are idempotent, not additional completions, cancellations, or token usage. - New sessions default to idle, but updates can precede the new-session response. Do not overwrite them with idle when that response arrives.
- Every successful resume must send a current-state snapshot after any replay
and its
replay_complete, and before its response, even without a replay cursor. Do not replay historical states as live activity or reset state to idle on the response. Testy and the echo example only resume sessions without foreground work, so their snapshot is idle without a new stop reason. - A resume must also return the session's current
configOptionsand available commands in its response when it has any, because the response replaces the client's lists. It should send current session info throughsession_info_updateafter any replay and before responding. Like the state snapshot, this synchronizes current state rather than replaying historical configuration or metadata changes.
Prompt acceptance means logical user-message insertion with an ID and a
position, not receipt or queueing. Running may precede insertion. Rejection
before insertion requires an error response and no user-message updates for
that submission; it does not alone end foreground work. A pre-insertion
foreground failure can also require an error idle update. After insertion,
the response stays successful and failure is reported on idle instead.
This includes authentication failures: after insertion, -32000
(Authentication required) belongs in the error stop reason, not an error
prompt response.
Authenticating again does not restart ended work, and automatically retrying
an inserted prompt would insert another message and could repeat side effects.
When closing cancels active work, final updates and idle with cancelled
precede the close response. An uninserted prompt cancelled by close gets
-32800; an inserted one still gets its successful message ID.
On session/cancel, the agent must stop waiting for the pending
session/request_permission requests of the cancelled foreground work and
treat them as not approved. It must cancel each of them with
$/cancel_request (a recommendation before schema 1.19): call
SentRequest::cancel or RequestCancellationHandle::cancel, or drop the
SentRequest, which sends $/cancel_request and discards the late answer.
The agent decides which requests belong to the cancelled work. Requests for
work in the latest background list are not affected. Before responding to
session/close, the agent must cancel every pending permission request and
elicitation in the session with $/cancel_request. Protocol v1 is unchanged:
there the client must answer every pending permission request with
cancelled when it sends session/cancel.
Agents reject unsupported replay cursor types with -32602, rather than
substituting another cursor. Retained user messages preserve the prompt
response's ID across replay. Agent and thought message IDs may change across
reconnections, so do not deduplicate a replay against history you already
hold by ID. A client can rebuild its transcript from a full replay, which
holds the history the agent retained and not necessarily everything the client
saw live. A chunk replay's empty-content primer clears only its replay ID.
Since schema 1.19 the primer applies to every entity replayed as chunks, not
only to messages (see Schema 1.19).
Top-level _meta on upsert updates patches stored metadata by key: omission
leaves it unchanged, null clears it, and an object preserves omitted keys,
removes keys whose values are null, and replaces each other value whole.
Nested objects are not merged; nested nulls are ordinary data. An empty object
does not clear metadata. Replay uses the same rules and never implicitly resets
metadata. ToolCallUpdate::apply_update and TerminalUpdate::apply_update
apply this rule; use v2::schema::apply_meta_update for application-owned
message, session, usage, compaction, or subagent projections. Metadata on chunks,
content, snapshots, and plan_update is not an entity patch.
Protocol v2 negotiation does not select every experimental feature. Operations such as provider configuration require their advertised capabilities. Informational notices, compaction, plan updates, subagent updates, and idle token usage can use their documented capability-free fallbacks; accepting an unknown variant is not a support advertisement. Unknown capability fields are ignored, while malformed known discriminators must not fall through to an unknown-variant fallback.
Client projections of tool calls and terminals should follow these rules:
tool_call_updateis an upsert keyed bytoolCallId. On an existing tool call, omission leaves a field unchanged andnullresets it to its default. A new ID starts from the defaults:kindisother,statusispending,contentandlocationsare empty, andtitle,name,rawInput, andrawOutputare unset. Show a generic title whiletitleis unset.- Treat a tool call whose
statusis unrecognized as still in progress. - A
tool_call_content_chunkfor atoolCallIdthe client has not seen creates the tool call with those defaults and then appends. Likewise aterminal_output_chunkfor an unseenterminalIdcreates the terminal with unknown fields and empty output, then appends. - Decode each terminal chunk independently and append the bytes in order.
Agents should send all output before a
concrete
exitStatus, but clients still append output that arrives after it. Clients may cap how much output they retain, for example by dropping the oldest bytes; a lateroutputsnapshot replaces whatever they kept. - In a diff,
changesis authoritative for affected paths and operations. Followchangeswhenpatchdisagrees with it, and handle diffs without apatch.
For v2 git_patch diffs, every path must be absolute and have no a/ or b/
prefix, including rename/copy header paths. Git quoting and /dev/null for
an added/deleted file's missing side still apply. Configure diff parsers not to
strip a leading path component, since that would drop part of an absolute
path, and take paths from changes or from the ---, +++, rename, and copy
lines rather than by splitting the diff --git header. The SDK carries patch
text; it does not rewrite paths or compute diffs from snippets.
Draft v2 type changes:
- v2 has its own
RequestId,Request, andResponsetypes. Request IDs are a string or an integer, nevernull; error responses may still usenullwhen the request ID could not be determined. AvailableCommandis atype-tagged union. Build commands withAvailableCommand::Command(Command::new(..))and read them throughname(),description(), andmeta(). Unknown command kinds parse asOther. Ignore them, and do not show them as slash commands. Schema 1.13 allowed showing them as plain commands. Schema 1.18 forbids it, because each command type defines how its entries are invoked.ToolCallLocation.lineis a 1-basedNonZeroU32. The builder still takes au32and treats0as no line.TerminalExitStatus.exit_codeis ani64holding the code the platform reports, so negative codes such as Windows NTSTATUS values survive.ResourceLink.sizeis au64byte count; a negative value parses as unknown.terminal_updatehas an optionalsizewithcolsandrows, patched like the other terminal fields. Clients should render the output at that size when it is present.RequestPermissionOutcome::Cancelledcarries aCancelledPermissionOutcomewith optional_meta;{"outcome": "cancelled"}still parses.AuthCapabilitiesis nowClientAuthCapabilities.DiffPatch,ToolCallPermissionSubject, andIcongain a_metafield (meta).- Unknown variants keep their optional fields verbatim in
fields.
v2::SessionBuilder::with_mcp_server, v2::ResumeSessionBuilder::with_mcp_server,
v2::ForkSessionBuilder::with_mcp_server, and Proxy.v2().with_mcp_server
declare an McpServer::Acp server. The agent must advertise
session.mcp.acp to accept it; otherwise a conforming agent rejects the setup
request with -32602. When the downstream agent only supports another MCP
transport, place a compatibility proxy such as McpOverAcpPolyfill in front of
it. The SDK does not check the agent's capability before sending the
declaration.
Client projections should also follow these rules:
- A
state_updatewith an unrecognized state never means idle. Show a generic busy state until a known state arrives. - An update or chunk for an unseen message ID creates the message. Its first update or chunk fixes its timeline position; later updates patch it in place, even after idle. Updates for different entities may interleave, and a new ID does not end any other entity.
- Show an unrecognized tool
kindasother. - A
tool_callpermission subject applies to the stored tool call exactly like atool_call_update, in the order received and before the prompt is shown. A first-seentoolCallIdcreates the tool call. - The Agent owns remembered
allow_alwaysandreject_alwaysdecisions and their scope. Clients do not infer that scope from the option kind.
Draft v2 type changes:
- JSON-RPC batching is removed from v2, and
v2::schema::JsonRpcBatchno longer exists. The SDK dropped batch support in both protocol versions: every connection answers a JSON array with a single-32600error withid: null(see JSON-RPC batches). Error.datais typed asErrorData:UnsupportedProtocolVersionErrorDatafor-32022,SessionNotFoundErrorDatafor-32002, or any other JSON.v2::Error::resource_not_found()no longer takes auri; usesession_not_found(session_id)for a missing session.v2::Error::unsupported_protocol_versionbuilds the typed data; convert either into the SDK'sErrorwith.into(). An error response'sidmay benull.UsageUpdate.costis aMaybeUndefined<Cost>patch field: omitted leaves the cost unchanged,nullclears it, and a value replaces it.Cost.currencyis any unit string, not only an ISO 4217 code.ElicitationContentValue::Otherholds any JSON value, such as the value of a custom or future property type. Validate accepted content against the requested schema yourself.ElicitationSchemarequirestype: "object"andpropertieson the wire.PromptRequest.promptmust not be empty, and select config options and option groups must not be empty.SessionInfo.additionalDirectoriestreatsnullas[], andAnnotations.lastModifiedaccepts any ISO 8601 string.- Subagent updates gain
cwdandadditional_directoriespatch fields (absolute paths; omitted leaves them unchanged,nullinherits the parent's). They neededunstable_subagentsuntil schema 1.18.
Connection and lifecycle rules:
- A failed
initializeleaves the connection uninitialized, and the client may sendinitializeagain.Agent.protocol_router()now keeps the connection open after rejecting a first message. Agents ignore notifications that arrive before initialization. - Each side picks its own request IDs, so the same ID may be in flight in both
directions.
$/cancel_requestnames a request its sender issued, and is ignored for unknown or already-answered requests. Unknown notifications, including$/-prefixed ones, are ignored. A connection's end fails its unanswered outgoing requests. session/closeis idempotent: closing an unknown or already-closed session succeeds.v2::ActiveSession::closetreats a-32002response as success, and Testy andsimple_agent_v2close idempotently.- Closing cancels the session's work like
session/cancel, cancels pending elicitations scoped to it, and answers held prompts with-32800. After the close response the agent sends no notifications or requests for that session, but it still answerssession/promptrequests that were pending. Deleting a session first cancels its work and frees its resources, as if it had been closed when it is active. cwdmust be absolute in the native syntax of the agent's machine, and agents reject a relativecwdinsession/new,session/resume, and thesession/listfilter with-32602. That filter matches only the exact directory. Agents that advertiseadditionalDirectoriesreject relative entries; agents that do not ignore the field. Testy andsimple_agent_v2follow these rules.- MCP server names must be unique within
mcpServers, and agents reject duplicates with-32602. The v2with_mcp_serverbuilders fail with-32602instead of declaring a server whose name is already in the request. Onsession/resumeandsession/fork,mcpServersis the complete set for the session; agents do not restore servers from earlier requests. A resume must use the session'scwd. session/new,session/resume, andsession/forkresponses carry the completeconfigOptionsand available commands when the session has any. Clients replace their lists with them, including lists from updates that arrived before the response.- An empty-string
nextCursorfromsession/listis a valid cursor; only an omitted ornullcursor ends the results.
Client projections should also follow these rules:
- A
plan_updatereplaces the whole plan with thatplanId, even when itstypechanged to one the client does not understand. - A
cancelledpermission outcome, or aselectedoutcome whoseoptionIdwas not offered, means not approved. Clients sendcancelledwhen the user dismisses the request or the client declines it by policy. completed,failed, andcancelledtool call statuses are final, and agents finalize unfinished tool calls when foreground work ends.- A prompt invokes a slash command only when its first text block starts with
/and the command name followed by whitespace or the end of the block. This applies tocommandentries only. - Show a placeholder for content you cannot render rather than dropping it.
- A terminal's
sizeis a rendering hint. _metakeystraceparent,tracestate, andbaggageuse the W3C formats; extensions use reverse-DNS prefixes such ascom.example/requestId.
Draft v2 type changes:
SessionUpdate::ReplayCompletemarks the end of the history replay that asession/resumerequest asked for.ReplayCompletehas only an optional_meta.- Subagents are part of v2 and no longer need
unstable_subagents:SessionUpdate::SubagentUpdate,SessionUpdate::SessionMessage,SessionUpdate::SessionMessageChunk,StateUpdate::Unknown,BackgroundWork::Subagent, andToolCallContent::Sessionare always available. These wire values used to parse asOtherwithout the feature and now parse as their typed variants, so add match arms where you relied on the fallback. v1 neededunstable_subagentsuntil schema 1.20. - The
fileplan type is removed (plan operations neededunstable_plan_operationsuntil schema 1.20):PlanUpdateContent::File,PlanFile, andPlanUpdateContent::fileno longer exist, and a receivedfileplan parses asPlanUpdateContent::Other.PlanMarkdowngains an optionalpath, the absolute path of the file that holds the plan. Build it withPlanUpdateContent::Markdown(PlanMarkdown::new(..).path(..)). - With
unstable_agent_skills,AvailableCommand::Skilladvertises an Agent Skill andContentBlock::Skill(SkillReference) invokes one in a prompt. The agent advertisesPromptCapabilities::skill, and the client advertisessession.commands.skill. - With
unstable_prompt_suggestions,SessionUpdate::PromptSuggestionsUpdatereplaces the session's list of prompts the agent predicts the user will send next. The client advertisessession.promptSuggestions. ClientCapabilities::session(ClientSessionCapabilities) exists with either of those two features. It only groups capabilities:{}advertises nothing.- With
unstable_speed_config_category,SessionConfigOptionCategory::Speedmarks a response speed option such as a fast mode toggle. - With
unstable_session_forkandunstable_session_options,ForkSessionRequest::config_optionscarries initial config option values. The entries apply on top of the configuration the fork would start with.
Agents must follow these replay rules:
- For a
session/resumerequest with areplayFromcursor, sendreplay_completeexactly once on the session, after the last replayed update and before current-state synchronization and the response. Send it for an empty replay too. - Do not send it when
replayFromis omitted ornull, forsession/new, when the cursor is rejected with-32602, or when the replay cannot finish. - From the time you receive the request until you send
replay_complete, every update you send for the session must be replayed history. Hold everything else until after it: chunks of a message that is still streaming, notices,available_commands_update,config_option_update, and the current session info and state snapshots. Permission requests and elicitations should wait too. - Each child session announced with
subagent_updateduring the resume gets its ownreplay_completeon its ownsessionId, after its last replayed update. The markers of different sessions have no required order. replay_completedoes not make the session active. A session that was not active before the request becomes active with the successful response, sosession/promptfails with-32002until then.- Report known gaps in the retained history after
replay_complete, for example with a notice.
Testy and simple_agent_v2 follow these rules.
Other agent rules:
auth/logoutis idempotent. Respond with the same empty result when there is no authenticated state to end.- Answer a repeated
tool_call/cancelorterminal/cancelwith-32602once the tool call is final or the terminal has exited. Thetool_call_updateorterminal_updatereports the outcome. - List the values of a
selectthought_leveloption from lowest to highest, and of aselectspeedoption from slowest to fastest. In abooleanspeedoption,trueis the faster setting. contentontool_call_updatereplaces the whole array, so send thesessionreferences again when you send new content. Add a reference only for a handle you can resolve to a known session.- Omit
senderSessionIdon a session-directed message when you have no evidence of who sent it. Do not assume the parent. - Report
unknownon an actual loss of observability, not after silence. Leave unfinished tool calls and terminals as last reported, and update capabilities explicitly when a control is no longer available. - Send
skillcommand entries only to a client that advertisessession.commands.skill, and advertisesession.prompt.skillwhen you do. Reject a prompt with askillblock that your capability does not allow, or whosenameyou cannot resolve, with-32602and without inserting it. - Send
prompt_suggestions_updateonly to a client that advertisessession.promptSuggestions. When you accept asession/prompt, replace a non-empty list or withdraw it with an empty one. Do not replay suggestions. - While applying initial
configOptions, do not sendconfig_option_updatefor them on the requesting connection, and send other updates they cause only after thesession/neworsession/forkresponse. A resumed session that is active on other connections does getconfig_option_updatethere. session/optionsmay start the agent's runtime to resolve the options. It still stores no session and writes no history.
Client projections should also follow these rules:
- Use
replay_completeto find the end of the replay you requested. Every update for the session after it is not history. Sendingsession/resumedoes not mark the start of the replay: when the session is already active on the connection, a live update the agent sent before it received the request can still arrive beforereplay_complete. A resume that ends in an error response or a closed connection withoutreplay_completedelivered a truncated replay. Do not treat those updates as a complete replay. A successful response withoutreplay_completeis a protocol violation, but the response still confirms that the requested replay finished. - Ignore command types you do not understand, and never show them as slash
commands. The
/namesyntax invokescommandentries only. Invoke a skill with askillcontent block, and only where the agent'ssession.prompt.skillcapability allows it: one leading block, or any number at any position withinline. - On
unknown, stop presenting a previousrunningorrequires_actionas confirmed live activity. It is notidle, not an outcome, and not a cancellation. A later known state replaces it. - Session-directed messages are agent content. Attribute them to the sending session and do not present them as user messages.
- Render a markdown plan from
content. Usepathonly to open the file, and do not read apathoutside the session's roots without user action. - Skip a whole prompt suggestion you cannot use rather than dropping one of its blocks. Never send a suggestion as a prompt without an explicit user action.
- Render
speedoptions near the model selector. - Do not call
session/optionson a timer.
Type changes:
PromptRequest::idempotency_key(idempotencyKey) identifies onesession/promptsubmission across attempts.IdempotencyKeyis an opaque, case-sensitive string of 1 to 255 characters.IdempotencyKey::newand deserialization reject any other value, so a typed handler never sees a malformed key: the SDK answers that request with-32602.unstable_protocol_v2also enablesIdempotencyKey::generate, which creates a key from a random UUID.StringPropertySchema::patternis removed from draft v2. A string property in an elicitation form takesminLength,maxLength,format, anddefault. Apatternon a received form is ignored and not kept. v1 keepspattern.Diff.changeshasminItems: 1in the JSON Schema. The SDK still parses an empty list.- With
unstable_session_cursors,SessionUpdate::CursorUpdateannounces a session cursor: one position in the session's timeline where the client can run a method.CursorUpdatecarries aCursorId, aCursorCapabilitiespatch, and aCursorAnchorthat names a message, tool call, or compaction and the side of it the cursor is on. Withunstable_session_forkas well,ForkSessionRequest::cursor_idforks the session at a cursor that advertisescapabilities.fork. Both exist in v1 and draft v2. v1 clients opt in withClientSessionCapabilities::cursors. Draft v2 needs no capability, and without the featurecursor_updateparses asSessionUpdate::Other. - The schema's aggregate enums (
ClientRequest,AgentResponse,AgentNotificationand the others, in both versions) gainfrom_method_and_params,from_method_and_result, andsupports_method, withschema::MethodDispatchErroras their error. They select the payload type from the JSON-RPC method. SDK connections already parse messages by method throughJsonRpcMessage::parse_message, so these are for code that parses messages without a connection. Do not deserialize an aggregate enum directly: its untaggedDeserializeselects a variant by payload shape.
Agents must follow these session/prompt retry rules:
- Support
idempotencyKeywhen you advertisecapabilities.session. There is no separate capability. - Look the key up in the session named by
sessionId. With nothing recorded, handle the prompt as usual and record the key in the same step as the inserted user message. - For a key recorded with an inserted message, insert nothing, start no second
round of work, and respond with that message's
messageId. - For a key whose prompt is not inserted yet, such as a held prompt, do not hold a second prompt. Answer the retry when you answer the first request, with the same outcome.
- An error response means the message was not inserted. Record nothing, so the client can send the same prompt with the same key again.
- If you honor
$/cancel_requestfor any pending request with a key, withdraw the prompt if it is not inserted yet and answer every pending request for that key with-32800. - A key is scoped to its session. The same key in another session, including
one that
session/forkcreated from this one, names an unrelated submission. - Never insert a second message for a recorded key. A request that reuses a
recorded key with a different prompt is answered as a retry, or rejected
with
-32602if you compare prompts. Comparing is optional. - Recognize a key for as long as you retain its message, across connections and restarts. For a message you do not retain, recognize the key while the session stays active on a connection.
- Do not echo keys in session updates,
session/listresults, or replayed history. A proxy forwardsidempotencyKeyunchanged.
Testy and simple_agent_v2 follow these rules. They do not compare prompts.
Other agent rules:
- Include the session's
configOptionsandavailableCommandsin thesession/newandsession/resumeresponses when the lists are nonempty. If a new session is not idle, send itsstate_updateafter thesession/newresponse. - If the agent cannot apply a supplied
systemPromptbecause the value is unsupported or exceeds its limits, answer-32602without creating or resuming the session. session/cancelandsession/closedrop the prompts you have received and not inserted. Answer each with-32800. Honoring$/cancel_requestfor a held prompt is optional: either answer-32800and do not insert the message, or insert it as usual. When the connection ends, you may insert or discard the prompts you were holding.- Cancel each permission request and elicitation you stop waiting for after
session/cancelwith$/cancel_request. This is now required. - Before you replay an entity from its beginning as chunks, send that entity's
update with the chunked field emptied:
content: []on the message update for message and session-directed message chunks,content: []ontool_call_updatefortool_call_content_chunk,output: { "data": "" }onterminal_updateforterminal_output_chunk, andsummary: []oncompaction_updateforcompaction_summary_chunk. An entity you replay without chunks needs no such update. - A resume without
replayFromsynchronizes the session's state, not its conversation. Send the currentstate_update, and before it a currentterminal_update,tool_call_update, orsubagent_updatefor each item in itsbackgroundlist. You are not required to resend foreground work in progress. - Do not move a tool call back to
pending, also not with"status": null.completed,failed, andcancelledare final. Report further work on a new tool call. - Include at least one entry in a diff's
changes. - Do not send
patternon string properties. Validate the submitted value yourself. - A child session you announced with
subagent_updateis not an unknown session.session/cancel,tool_call/cancel, andterminal/cancelcan name a child where its capabilities allow. Reject any other request that names a child with-32602. Do not return child sessions fromsession/list. Closing a session also stops its child sessions, at any depth. - The
capabilitiesandstatein a replayedsubagent_updateare historical. After a replay, send a currentsubagent_updatefor each child in thebackgroundlist after the parent'sreplay_complete. - A session's
costincludes the costs of its descendants. - Announce a session cursor only for a position where at least one method
works, and only after the entity it is anchored to. Never change a cursor's
anchor. Reject a
cursorIdyou did not issue for that session, no longer accept, or that does not support the method with-32602. Never act at another position and never fork the whole session instead. In v1, sendcursor_updateonly to a client that advertisessession.cursors, and send amessageIdon every message you anchor to.
Client projections should also follow these rules:
- To let the agent recognize a retry of a prompt whose response was lost, send
the prompt with
send_prompt_blocks_with_key. Create one key per submission when the user submits the input, and store it with the pending input. Use a new key for a new submission, and never send a key with a different prompt. On a new connection, callinitializeandsession/resumebefore you retry. Schema 1.20 changed what a client does next: see Schema 1.20. - Without a key, or with a key the agent has forgotten, a
session/promptsent again can insert a second user message.auth/login,auth/logout,session/resume,session/set_config_option,session/list,session/close,session/delete,tool_call/cancel, andterminal/cancelcan be sent again as they are. A secondsession/newcreates a second session. Do not sendsession/cancelagain later: a later copy can cancel newer foreground work or a newly held prompt. Retrysession/set_config_optionbefore you send further changes, because a retry sent after a later change to the same option overwrites it. Treat-32002fromsession/deleteas a successful delete. Atool_call/cancelorterminal/cancelretry gets-32602once the tool call is final or the terminal has exited. See Retrying requests. - When
$/cancel_requestarrives for asession/request_permissionorelicitation/createrequest you have not answered, dismiss the prompt and answer with thecancelledoutcome or thecancelaction. Observe it withResponder::cancellation. A responder dropped after the cancellation answers-32800instead. Also dismiss permission and elicitation prompts for a session you closed. - Correlate a submitted prompt with the agent's
user_messageby the response'smessageId, not by content. The agent owns the inserted message and may report different content. The client may display what it originally sent; replay carries the agent's content. - A resume without
replayFromdoes not resynchronize messages or foreground tool calls. Keep the session's entities if you missed no updates, and request replay otherwise, for example after a lost connection. - Tool call
content, terminaloutput, and compactionsummaryfollow the same order as message content: an update that carries the field replaces what the chunks accumulated, and later chunks append to it. - Show the status from the latest
tool_call_updateyou received. refusalonly reports that the agent refused to continue. Do not remove or mark the user's message because of it. The v1 stop reason still means that the prompt and everything after it are left out of the next prompt.- Do not evaluate
patternon string properties. - Register the child session that a
subagent_updateannounces, and attribute updates and requests with itssessionIdto it. Send nothing for a child except responses to its requests,session/cancelwhile the child advertisescapabilities.cancel, andtool_call/cancelorterminal/cancelfor an item that advertises its own. When you resume the parent, discard the child state and capabilities you held and wait for current ones: asubagent_updatebefore the parent'sreplay_completecarries historicalcapabilitiesandstate. A permission request or elicitation can name a child session, so do not sendsession/cancelfor the session a request names unless it is a session you created or a child that advertisescapabilities.cancel. - A session's
costalready includes the costs of its descendants. Do not add a child session's cost to an ancestor's. A child without a reported cost has an unknown cost, not zero. Context window usage belongs to each session and is not summed. See Subagents for the other client rules: the patch semantics ofsubagent_update, inherited working directories, read-only child configuration, session-directed messages, and what to show after the connection ends. - Build cursors from
cursor_updatebycursorId. Pass a cursor to a method only when the agent announced it on that session and it currently advertises that method's capability, and pass thecursorId, never the anchored entity's ID. Do not guess a place for a cursor whose anchor you do not hold or do not recognize. A full replay rebuilds the session's cursors, so discard the ones you held. A forked session does not inherit cursors.
ACP v2 moved from Draft to Preview with this release, and its schema is
2.0.0-beta.1. The SDK's v2 API stays behind unstable_protocol_v2.
Type changes:
- Plan operations are stable in both protocol versions, and the
unstable_plan_operationsfeature is removed. In v2,PlanUpdateContent::Markdown,PlanMarkdown, andSessionUpdate::PlanRemovedare always available and need no client capability. Amarkdownplan and aplan_removedupdate used to parse asOtherwithout the feature and now parse as their typed variants, so add match arms where you relied on the fallback. In v1,SessionUpdate::PlanUpdate,SessionUpdate::PlanRemoved, andClientCapabilities::planneed no feature. - Subagent sessions are stable in v1, and the
unstable_subagentsfeature is removed. v2 has included them since schema 1.18. - Maps on the wire keep their key order. In v2 they are
indexmap::IndexMaps instead ofBTreeMaps: thefieldsof everyOther*variant,ElicitationSchema::properties, andElicitationAcceptAction::content.SetProviderRequest::headers(withunstable_llm_providers) was aHashMapand is anIndexMapin both versions. In v1, thefieldsofOtherStateUpdate,OtherStopReason, andOtherCursorAnchorareIndexMaps too. The other v1 maps are unchanged. Name the type asagent_client_protocol::schema::IndexMap, which the SDK re-exports since schema 1.21. Where a function takes the map directly, such asOtherMcpServer::new, an array of pairs with.into()also works.apply_meta_updatekeeps the order of the remaining keys when it removes one. CompactionUpdate::statusis aMaybeUndefined<CompactionStatus>patch field: omitted leaves the status unchanged,nullresets it toin_progress, and a value replaces it.CompactionUpdate::newtakes only the ID. Set the status withCompactionUpdate::status. v1 still requiresstatus.UsageUpdate::metais aMaybeUndefined<Meta>metadata patch, like the_metaof the other upsert updates. Apply it withapply_meta_update.TerminalSize::colsandTerminalSize::rowsareNonZeroU16. A receivedsizewith a zero is parsed as no size, and the rest of theterminal_updatestill applies.- A
RequestPermissionRequestwith an emptyoptionslist fails typed parsing. The SDK answers that request with-32602and the handler does not run. CreateElicitationRequestrejects arequestIdsent together withsessionIdortoolCallId, and anullin the scope field that is not used.MultiSelectItemsis selected bytype: the titledanyOfform has notype, and atypeof"string"needsenum.SystemPrompt.contenthasminItems: 1in the JSON Schema. The SDK still parses an empty list.- The v2 JSON Schema validates the JSON-RPC envelope by method and direction
and rejects
"params": null. The SDK omitsparamswhen a message has none. The schema also carriesx-stateandx-scopehints, which say what a message changes and which session, tool call, or request it belongs to. They add no Rust types.
Agents must follow these rules:
- When
session/cancelstops active foreground work, send that work's pending updates and thenidlewithstopReason: "cancelled"before you start new foreground work in the session, including work you start yourself. Do not reject a prompt because cancellation is in progress. Insert and acknowledge it, or hold it with its response pending if its work would start right away. Listed background work is not covered, and its updates can follow the idle update. If the foreground work had already ended, its stop reason stands. - A
selectedpermission outcome is not approval by itself. MatchoptionIdagainst the options you offered in that request. Do not perform the operation when the option's kind isreject_onceorreject_always, when no option matches, or when you do not understand the kind or the outcome. Neither the option'snamenor the spelling of itsoptionIdoverrides a rejection. An error response is not approval, and neither is a connection that ends before the client answers. - Reject a stdio MCP server whose
commandis not an absolute path with-32602. When a server fails to launch or connect, continue without it and tell the user, for example with a notice. - Reject a
session/closeor asession/deletethat names a child session with-32602. A child's config options are read-only, because no child capability permitssession/set_config_option. - Give every config option a
configIdthat is unique inconfigOptions, and every group agroupIdthat is unique in its option. - Do not replay past
usage_update,session_info_update,config_option_update, oravailable_commands_updatenotifications. They describe earlier values. Synchronize the current ones instead. - During a replaying resume, hold live updates and requests for each child
you announce until that child's
replay_complete. - Report
unknownwhenever you can no longer observe a session's foreground work. This applies to every session, not only to child sessions. - A child session also reports
cancelledwhen its parent cancels its work. A response that arrives after work was cancelled must not permit a new task for the same child. - Return the sessions with the most recent activity first from
session/list. - Send
plan_removedwhen a plan is cancelled, for example when the user rejects it. A finished plan does not need to be removed. Thepathof a markdown plan must be absolute. - In an elicitation, omit the scope field you do not use instead of sending
null, and do not sendtoolCallIdwithrequestId. - Every authentication method carries a
type, amethodId, and aname. Theenvnames of a terminal method should be unique. Otherwise the last entry with a name wins.
Testy and simple_agent_v2 already follow the rules that apply to them. They
have no permission requests, MCP servers, config options, plans, or child
sessions.
Client projections should also follow these rules:
- A successful
session/resume, with or without replay, does not tell you whether an unanswered prompt was inserted or whether the agent kept itsidempotencyKey. If a duplicate would matter and you cannot rule one out, do not resend the prompt automatically. Tell the user that the prompt might already have run and let them decide. A retry sends the original prompt and key in a newsession/promptrequest. This replaces the earlier advice to resend a pending prompt as soon as the session is resumed. - Accept
itemsandmarkdownplans andplan_removed. Accepting a plan does not require showing it. Aplan_updatereplaces the whole plan with thatplanId. Ignore aplan_removedfor aplanIdyou do not know. - When the user picks an offered option, answer with
selectedand itsoptionId, also for a rejection. You can return the ID without interpreting the option's kind. - Keep accepting updates after
session/cancel, including updates for surviving background work after theidleupdate withstopReason: "cancelled". To stop a listed tool call or terminal, sendtool_call/cancelorterminal/cancelwhile the item advertisescapabilities.cancel.session/cancelon a child does not stop a terminal in the child'sbackgroundlist. - Adjacent text blocks form one continuous text. Join them without a separator before rendering, in messages and in tool call content.
- Tool call titles, session titles, and the names of commands and config
options are plain text. Do not render them as Markdown. A plan entry's
contentmay contain Markdown. As of schema 1.21.2, so may the descriptions listed under Schema 1.21. - Answer
elicitation/createwith-32602when you cannot render the form. - Follow a
nextCursorfromsession/listwith the samecwdyou sent before. - Call
auth/logoutonly when the agent advertised authentication methods. - After the agent selects a lower protocol version, you may send that
version, or a lower one you support, when you repeat
initializeor open a new connection.Client.protocol_connector()does this when it falls back.
Schema 1.21 changes Rust types, not the wire format:
-
agent_client_protocol::schema::IndexMapis the map type of the schema's ordered maps: thefieldsof everyOther*variant,ElicitationSchema::properties,ElicitationAcceptAction::content, andSetProviderRequest::headers. The schema crate re-exports it fromindexmap, and the SDK passes it on. If you addedindexmapto your dependencies for schema 1.20 only to name this type, you can remove it.use agent_client_protocol::schema::IndexMap; use agent_client_protocol::v1::schema::{OtherStateUpdate, StateUpdate}; use serde_json::json; let fields = IndexMap::from([("reason".to_string(), json!("rate_limited"))]); let state = StateUpdate::Other(OtherStateUpdate::new("_paused", fields));
-
As of 1.21.1, two preview v2 variants take boxed payloads. When constructing
v2::schema::SessionConfigOptionValue::Other(other), useSessionConfigOptionValue::from(other)instead. Forv2::schema::ToolCallContent::Diff(diff), useToolCallContent::from(diff). Both conversions accept the unboxed payload. When matching an owned variant,*otheror*diffgives you the owned payload again. The v1 types are unchanged.
Schema 1.21.2 changes one rule for clients. These description fields in
v2::schema may contain Markdown, like a text block:
- An authentication method:
AuthMethodAgent::descriptionandAuthMethodTerminal::description. - A command:
Command::description, andSkill::descriptionunderunstable_agent_skills. - A config option and each of its values:
SessionConfigOption::descriptionandSessionConfigSelectOption::description. - A permission request:
RequestPermissionRequest::description. - A subagent:
SubagentUpdate::description.
Schema 1.20 listed the descriptions of commands and config options as plain
text. A client that renders Markdown in text blocks can now render these
fields the same way. Names, titles, and every other description, such as
those in an elicitation form, are still plain text.
There are no new rules for agents. An agent can use Markdown in these fields.