agent-client-protocol 3.1.0

Core protocol types and traits for the Agent Client Protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#![cfg(feature = "unstable_mcp_over_acp")]

use agent_client_protocol::{
    Agent, Channel, Client, ConnectionTo, Error, NullRun, RawJsonRpcMessage, RunWithConnectionTo,
    TransportFrame,
    mcp_server::{McpOutcome, McpRequest, McpRequestContext, McpServer, McpService},
};
use futures::{StreamExt, future::BoxFuture};
use serde_json::{Value, json};
use std::{
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
    },
    time::Duration,
};
use tokio::sync::oneshot;

fn original_error() -> Error {
    Error::internal_error().data("local runner failed")
}

struct FailingRunner(Option<oneshot::Receiver<()>>);

impl RunWithConnectionTo<Agent> for FailingRunner {
    async fn run_with_connection_to(self, _: ConnectionTo<Agent>) -> Result<(), Error> {
        if let Some(release) = self.0 {
            release.await.unwrap();
        }
        Err(original_error())
    }
}

struct PendingRunner {
    dropped: Arc<AtomicBool>,
    request: Option<oneshot::Receiver<()>>,
    ack: Option<oneshot::Sender<()>>,
    completed: Option<Arc<AtomicBool>>,
}

impl Drop for PendingRunner {
    fn drop(&mut self) {
        if let Some(completed) = &self.completed {
            assert!(
                completed.load(Ordering::Acquire),
                "actual sibling runner dropped before owned async cleanup"
            );
        }
        self.dropped.store(true, Ordering::Release);
    }
}

impl RunWithConnectionTo<Agent> for PendingRunner {
    async fn run_with_connection_to(mut self, _: ConnectionTo<Agent>) -> Result<(), Error> {
        if let Some(request) = self.request.take() {
            request.await.unwrap();
            self.ack.take().unwrap().send(()).unwrap();
        }
        std::future::pending().await
    }
}

struct LiveService(Mutex<Option<oneshot::Sender<McpRequestContext<Agent>>>>);

impl McpService<Agent> for LiveService {
    fn execute(
        &self,
        request: McpRequest,
        context: McpRequestContext<Agent>,
    ) -> BoxFuture<'static, Result<McpOutcome, Error>> {
        if request.method == "_test/subscribe" {
            self.0
                .lock()
                .unwrap()
                .take()
                .unwrap()
                .send(context.clone())
                .unwrap();
            Box::pin(async move {
                context.operation_cancellation().cancelled().await;
                Err(Error::request_cancelled())
            })
        } else {
            Box::pin(async { Ok(McpOutcome::Result(json!({"live":true}))) })
        }
    }
}

struct CleanupService {
    started: Mutex<Option<oneshot::Sender<()>>>,
    cleaning: Mutex<Option<oneshot::Sender<()>>>,
    release: Mutex<Option<oneshot::Receiver<()>>>,
    runner: Mutex<Option<oneshot::Sender<()>>>,
    ack: Mutex<Option<oneshot::Receiver<()>>>,
    completed: Arc<AtomicBool>,
}

impl McpService<Agent> for CleanupService {
    fn execute(
        &self,
        _: McpRequest,
        context: McpRequestContext<Agent>,
    ) -> BoxFuture<'static, Result<McpOutcome, Error>> {
        let started = self.started.lock().unwrap().take().unwrap();
        let cleaning = self.cleaning.lock().unwrap().take().unwrap();
        let release = self.release.lock().unwrap().take().unwrap();
        let runner = self.runner.lock().unwrap().take().unwrap();
        let ack = self.ack.lock().unwrap().take().unwrap();
        let completed = self.completed.clone();
        Box::pin(async move {
            started.send(()).unwrap();
            context.operation_cancellation().cancelled().await;
            assert!(
                context
                    .send_notification("notifications/progress", None)
                    .await
                    .is_err()
            );
            runner.send(()).unwrap();
            ack.await.unwrap();
            cleaning.send(()).unwrap();
            release.await.unwrap();
            completed.store(true, Ordering::Release);
            Err(Error::request_cancelled())
        })
    }
}

fn send(peer: &Channel, value: Value) {
    let message: RawJsonRpcMessage = serde_json::from_value(value).unwrap();
    peer.tx
        .unbounded_send(TransportFrame::Single(message))
        .unwrap();
}

async fn raw(peer: &mut Channel) -> Value {
    let TransportFrame::Single(message) = peer.rx.next().await.unwrap() else {
        panic!("expected single frame")
    };
    serde_json::to_value(message).unwrap()
}

fn request(peer: &Channel, server: &Value, id: &str, method: &str) {
    send(
        peer,
        json!({"jsonrpc":"2.0","id":id,"method":"mcp/message","params":{
            "serverId":server,"requestId":id,"method":method,"params":{"_meta":{
                "io.modelcontextprotocol/protocolVersion":"2026-07-28",
                "io.modelcontextprotocol/clientCapabilities":{}
            }}
        }}),
    );
}

async fn assert_live(
    peer: &mut Channel,
    server: &Value,
    context: &McpRequestContext<Agent>,
    id: &str,
) {
    assert!(!context.operation_cancellation().is_cancelled());
    context
        .send_notification("notifications/progress", None)
        .await
        .unwrap();
    loop {
        let message = raw(peer).await;
        if message["method"] == "mcp/message" {
            assert_eq!(message["params"]["requestId"], "subscription");
            break;
        }
        assert!(
            message.get("method").is_none(),
            "unexpected publication: {message}"
        );
    }
    request(peer, server, id, "tools/list");
    loop {
        let message = raw(peer).await;
        if message["id"] == id {
            assert_eq!(message["result"]["result"]["live"], true, "{message}");
            break;
        }
    }
}

async fn v1_scope_error(owned_cleanup: bool, foreground_success: bool) {
    tokio::time::timeout(Duration::from_secs(10), async {
        let (transport, mut peer) = Channel::duplex();
        let (live_tx, live_rx) = oneshot::channel();
        let (ready_tx, ready_rx) = oneshot::channel();
        let (fail_tx, fail_rx) = oneshot::channel();
        let (finish_op_tx, finish_op_rx) = oneshot::channel();
        let (op_finished_tx, op_finished_rx) = oneshot::channel();
        let (returned_tx, mut returned_rx) = oneshot::channel();
        let (finish_tx, finish_rx) = oneshot::channel();
        let (started_tx, started_rx) = oneshot::channel();
        let (cleaning_tx, cleaning_rx) = oneshot::channel();
        let (release_tx, release_rx) = oneshot::channel();
        let (runner_tx, runner_rx) = oneshot::channel();
        let (ack_tx, ack_rx) = oneshot::channel();
        let completed = Arc::new(AtomicBool::new(false));
        let dropped = Arc::new(AtomicBool::new(false));
        let runner_dropped = dropped.clone();
        let runner_completed = completed.clone();
        let service_completed = completed.clone();
        let client = tokio::spawn(Client.builder().connect_with(transport, async move |cx| {
            let _live = cx
                .build_session_cwd()?
                .with_mcp_server(McpServer::new_service(
                    "live",
                    LiveService(Mutex::new(Some(live_tx))),
                    NullRun,
                ))?
                .block_task()
                .start_session()
                .await?;
            // The unrelated subscription is already executing before the local
            // runner can fail, including the immediate-failure case.
            ready_rx.await.unwrap();
            let scoped = cx
                .build_session_cwd()?
                .with_mcp_server(McpServer::new_service(
                    "pending",
                    CleanupService {
                        started: Mutex::new(Some(started_tx)),
                        cleaning: Mutex::new(Some(cleaning_tx)),
                        release: Mutex::new(Some(release_rx)),
                        runner: Mutex::new(Some(runner_tx)),
                        ack: Mutex::new(Some(ack_rx)),
                        completed: service_completed,
                    },
                    PendingRunner {
                        dropped: runner_dropped,
                        request: owned_cleanup.then_some(runner_rx),
                        ack: owned_cleanup.then_some(ack_tx),
                        completed: owned_cleanup.then_some(runner_completed),
                    },
                ))?
                .with_mcp_server(McpServer::new_service(
                    "failing",
                    LiveService(Mutex::new(None)),
                    FailingRunner(owned_cleanup.then_some(fail_rx)),
                ))?;
            let error = scoped
                .block_task()
                .run_until(async |_session| {
                    if foreground_success {
                        finish_op_rx.await.unwrap();
                        op_finished_tx.send(()).unwrap();
                        Ok(())
                    } else {
                        std::future::pending::<Result<(), Error>>().await
                    }
                })
                .await
                .unwrap_err();
            assert_eq!(error, original_error());
            returned_tx.send(()).unwrap();
            finish_rx.await.unwrap();
            Ok(())
        }));
        let live_setup = raw(&mut peer).await;
        let live_server = live_setup["params"]["mcpServers"][0]["serverId"].clone();
        send(
            &peer,
            json!({"jsonrpc":"2.0","id":live_setup["id"],"result":{"sessionId":"live"}}),
        );
        request(&peer, &live_server, "subscription", "_test/subscribe");
        let live_context = live_rx.await.unwrap();
        ready_tx.send(()).unwrap();
        if owned_cleanup {
            let setup = raw(&mut peer).await;
            let server = setup["params"]["mcpServers"][0]["serverId"].clone();
            send(
                &peer,
                json!({"jsonrpc":"2.0","id":setup["id"],"result":{"sessionId":"scoped"}}),
            );
            request(&peer, &server, "owned", "tools/list");
            started_rx.await.unwrap();
            fail_tx.send(()).unwrap();
            cleaning_rx.await.unwrap();
            assert!(matches!(
                returned_rx.try_recv(),
                Err(oneshot::error::TryRecvError::Empty)
            ));
            assert!(!dropped.load(Ordering::Acquire));
            assert_live(&mut peer, &live_server, &live_context, "during").await;
            if foreground_success {
                finish_op_tx.send(()).unwrap();
                op_finished_rx.await.unwrap();
            }
            release_tx.send(()).unwrap();
        }
        returned_rx.await.unwrap();
        assert!(
            dropped.load(Ordering::Acquire),
            "sibling destructor must run before scope returns"
        );
        if owned_cleanup {
            assert!(completed.load(Ordering::Acquire));
        }
        assert_live(&mut peer, &live_server, &live_context, "after").await;
        finish_tx.send(()).unwrap();
        client.await.unwrap().unwrap();
    })
    .await
    .expect("v1 local runner error timed out");
}

#[tokio::test]
async fn immediate_v1_composed_runner_error_preserves_other_session() {
    v1_scope_error(false, false).await;
}

#[tokio::test]
async fn v1_composed_runner_error_drives_sibling_owned_cleanup_before_return() {
    v1_scope_error(true, false).await;
}

#[tokio::test]
async fn foreground_success_does_not_hide_observed_v1_runner_error() {
    v1_scope_error(true, true).await;
}

#[cfg(feature = "unstable_protocol_v2")]
#[tokio::test]
async fn immediate_v2_composed_startup_error_preserves_existing_mcp_admission() {
    use agent_client_protocol::schema::{ProtocolVersion, v2};

    tokio::time::timeout(Duration::from_secs(10), async {
        let (transport, mut peer) = Channel::duplex();
        let (returned_tx, returned_rx) = oneshot::channel();
        let (finish_tx, finish_rx) = oneshot::channel();
        let dropped = Arc::new(AtomicBool::new(false));
        let runner_dropped = dropped.clone();
        let client = tokio::spawn(Client.v2().connect_with(transport, async move |cx| {
            cx.send_request(v2::InitializeRequest::new(
                ProtocolVersion::V2, v2::Implementation::new("scope-test", "1"),
            )).block_task().await?;
            let _live = cx.build_session_cwd()?
                .with_mcp_server(McpServer::new_service(
                    "live", LiveService(Mutex::new(None)), NullRun,
                ))?.start_session().block_task().await?;
            let error = cx.build_session_cwd()?
                .with_mcp_server(McpServer::new_service(
                    "pending", LiveService(Mutex::new(None)),
                    PendingRunner {
                        dropped: runner_dropped, request: None, ack: None, completed: None,
                    },
                ))?
                .with_mcp_server(McpServer::new_service(
                    "failing", LiveService(Mutex::new(None)), FailingRunner(None),
                ))?
                .start_session().block_task().await.unwrap_err();
            assert_eq!(error, original_error());
            returned_tx.send(()).unwrap();
            finish_rx.await.unwrap();
            Ok(())
        }));
        let initialize = raw(&mut peer).await;
        let initialized = v2::InitializeResponse::new(
            ProtocolVersion::V2, v2::Implementation::new("scope-agent", "1"),
        ).capabilities(v2::AgentCapabilities::new().session(
            v2::SessionCapabilities::new().mcp(
                v2::McpCapabilities::new().acp(v2::McpAcpCapabilities::new()),
            ),
        ));
        send(&peer, json!({"jsonrpc":"2.0","id":initialize["id"],"result":initialized}));
        let live_setup = raw(&mut peer).await;
        let live_server = live_setup["params"]["mcpServers"][0]["serverId"].clone();
        send(&peer, json!({"jsonrpc":"2.0","id":live_setup["id"],"result":v2::NewSessionResponse::new("live")}));
        returned_rx.await.unwrap();
        assert!(dropped.load(Ordering::Acquire));
        // The very next frame must be the existing registration's response, not
        // a session/new request from the failed prepublication setup.
        request(&peer, &live_server, "after-startup-error", "tools/list");
        let response = raw(&mut peer).await;
        assert_eq!(response["id"], "after-startup-error", "{response}");
        assert_eq!(response["result"]["result"]["live"], true, "{response}");
        finish_tx.send(()).unwrap();
        client.await.unwrap().unwrap();
    }).await.expect("v2 composed startup error timed out");
}

#[cfg(feature = "unstable_protocol_v2")]
#[tokio::test]
async fn attached_v2_composed_runner_error_remains_connection_fatal() {
    use agent_client_protocol::schema::{ProtocolVersion, v2};

    tokio::time::timeout(Duration::from_secs(10), async {
        let (transport, mut peer) = Channel::duplex();
        let (fail_tx, fail_rx) = oneshot::channel();
        let (attached_tx, attached_rx) = oneshot::channel();
        let dropped = Arc::new(AtomicBool::new(false));
        let runner_dropped = dropped.clone();
        let client = tokio::spawn(Client.v2().connect_with(transport, async move |cx| {
            cx.send_request(v2::InitializeRequest::new(
                ProtocolVersion::V2, v2::Implementation::new("scope-test", "1"),
            )).block_task().await?;
            let _session = cx.build_session_cwd()?
                .with_mcp_server(McpServer::new_service(
                    "pending", LiveService(Mutex::new(None)),
                    PendingRunner {
                        dropped: runner_dropped, request: None, ack: None, completed: None,
                    },
                ))?
                .with_mcp_server(McpServer::new_service(
                    "failing", LiveService(Mutex::new(None)), FailingRunner(Some(fail_rx)),
                ))?
                .start_session().block_task().await?;
            // Neither unrelated infinite user work nor the foreground may be joined.
            cx.spawn(std::future::pending::<Result<(), Error>>())?;
            attached_tx.send(()).unwrap();
            std::future::pending::<Result<(), Error>>().await
        }));
        let initialize = raw(&mut peer).await;
        let initialized = v2::InitializeResponse::new(
            ProtocolVersion::V2, v2::Implementation::new("scope-agent", "1"),
        ).capabilities(v2::AgentCapabilities::new().session(
            v2::SessionCapabilities::new().mcp(
                v2::McpCapabilities::new().acp(v2::McpAcpCapabilities::new()),
            ),
        ));
        send(&peer, json!({"jsonrpc":"2.0","id":initialize["id"],"result":initialized}));
        let setup = raw(&mut peer).await;
        send(&peer, json!({"jsonrpc":"2.0","id":setup["id"],"result":v2::NewSessionResponse::new("attached")}));
        attached_rx.await.unwrap();
        fail_tx.send(()).unwrap();
        let error = client.await.unwrap().unwrap_err();
        assert_eq!(error.code, original_error().code);
        assert_eq!(error.data.unwrap()["data"], "local runner failed");
        assert!(dropped.load(Ordering::Acquire));
    }).await.expect("attached v2 runner failure must stop the connection");
}