agent-client-protocol-polyfill 2.1.0

Polyfill proxies for Agent Client Protocol backward compatibility
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use agent_client_protocol::{
    Error, JsonRpcMessage, JsonRpcResponse, UntypedMessage,
    schema::{
        InitializeProxyRequest, METHOD_INITIALIZE_PROXY, ProtocolVersion,
        v1::{
            ConnectMcpRequest, ConnectMcpResponse, DisconnectMcpRequest, DisconnectMcpResponse,
            LoadSessionRequest, McpServer, MessageMcpNotification, MessageMcpRequest,
            NewSessionRequest, ResumeSessionRequest,
        },
    },
};
use serde_json::{Map, Value};

#[cfg(feature = "unstable_protocol_v2")]
use agent_client_protocol::schema::v2;

#[cfg(feature = "unstable_session_fork")]
use agent_client_protocol::schema::v1::ForkSessionRequest;

/// ACP schema selected by the conductor's proxy initialization request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PolyfillProtocol {
    V1,
    #[cfg(feature = "unstable_protocol_v2")]
    V2,
}

impl PolyfillProtocol {
    pub(crate) fn from_initialize_request(request: &UntypedMessage) -> Result<Self, Error> {
        if request.method() != METHOD_INITIALIZE_PROXY {
            return Err(Error::invalid_request()
                .data(format!("expected `{METHOD_INITIALIZE_PROXY}` request")));
        }

        let requested = request
            .params()
            .get("protocolVersion")
            .cloned()
            .ok_or_else(invalid_initialize_protocol_version)
            .and_then(|version| {
                serde_json::from_value::<ProtocolVersion>(version)
                    .map_err(|_| invalid_initialize_protocol_version())
            })?;

        let protocol = if requested == ProtocolVersion::V1 {
            Self::V1
        } else {
            #[cfg(feature = "unstable_protocol_v2")]
            {
                if requested == ProtocolVersion::V2 {
                    Self::V2
                } else {
                    return Err(unsupported_protocol_version(requested));
                }
            }

            #[cfg(not(feature = "unstable_protocol_v2"))]
            {
                return Err(unsupported_protocol_version(requested));
            }
        };

        protocol.validate_initialize_request(request)?;
        Ok(protocol)
    }

    fn validate_initialize_request(self, request: &UntypedMessage) -> Result<(), Error> {
        match self {
            Self::V1 => {
                InitializeProxyRequest::parse_message(request.method(), request.params())?;
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                v2::InitializeProxyRequest::parse_message(request.method(), request.params())?;
            }
        }
        Ok(())
    }

    pub(crate) fn transform_initialize_response(
        self,
        response: &mut Value,
    ) -> Result<DownstreamMcpMode, Error> {
        let mode = match self {
            Self::V1 => {
                let response = agent_client_protocol::schema::v1::InitializeResponse::from_value(
                    "initialize",
                    response.clone(),
                )?;
                DownstreamMcpMode::from_capabilities(
                    response.agent_capabilities.mcp_capabilities.http,
                    response.agent_capabilities.mcp_capabilities.acp,
                )
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                let response = v2::InitializeResponse::from_value("initialize", response.clone())?;
                let mcp = response
                    .capabilities
                    .session
                    .as_ref()
                    .and_then(|session| session.mcp.as_ref());
                DownstreamMcpMode::from_capabilities(
                    mcp.is_some_and(|mcp| mcp.http.is_some()),
                    mcp.is_some_and(|mcp| mcp.acp.is_some()),
                )
            }
        };

        if mode == DownstreamMcpMode::HttpAdapter {
            self.advertise_native_mcp(response)?;
        }
        Ok(mode)
    }

    fn advertise_native_mcp(self, response: &mut Value) -> Result<(), Error> {
        let response = response
            .as_object_mut()
            .ok_or_else(|| invalid_initialize_response("result must be an object"))?;
        match self {
            Self::V1 => {
                let mcp = response
                    .get_mut("agentCapabilities")
                    .and_then(Value::as_object_mut)
                    .and_then(|capabilities| capabilities.get_mut("mcpCapabilities"))
                    .and_then(Value::as_object_mut)
                    .ok_or_else(|| {
                        invalid_initialize_response(
                            "HTTP MCP support did not have an object capability container",
                        )
                    })?;
                mcp.insert("acp".into(), Value::Bool(true));
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                let mcp = response
                    .get_mut("capabilities")
                    .and_then(Value::as_object_mut)
                    .and_then(|capabilities| capabilities.get_mut("session"))
                    .and_then(Value::as_object_mut)
                    .and_then(|session| session.get_mut("mcp"))
                    .and_then(Value::as_object_mut)
                    .ok_or_else(|| {
                        invalid_initialize_response(
                            "HTTP MCP support did not have an object capability container",
                        )
                    })?;
                mcp.insert("acp".into(), Value::Object(Map::new()));
            }
        }
        Ok(())
    }

    pub(crate) fn is_session_setup_method(self, method: &str) -> bool {
        match self {
            Self::V1 => {
                matches!(method, "session/new" | "session/load" | "session/resume")
                    || cfg!(feature = "unstable_session_fork") && method == "session/fork"
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                matches!(method, "session/new" | "session/resume")
                    || cfg!(feature = "unstable_session_fork") && method == "session/fork"
            }
        }
    }

    pub(crate) fn validate_session_setup_request(
        self,
        request: &UntypedMessage,
    ) -> Result<(), Error> {
        match self {
            Self::V1 => match request.method() {
                "session/new" => {
                    NewSessionRequest::parse_message(request.method(), request.params())?;
                }
                "session/load" => {
                    LoadSessionRequest::parse_message(request.method(), request.params())?;
                }
                "session/resume" => {
                    ResumeSessionRequest::parse_message(request.method(), request.params())?;
                }
                #[cfg(feature = "unstable_session_fork")]
                "session/fork" => {
                    ForkSessionRequest::parse_message(request.method(), request.params())?;
                }
                method => return Err(unexpected_session_setup_method(method)),
            },
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => match request.method() {
                "session/new" => {
                    v2::NewSessionRequest::parse_message(request.method(), request.params())?;
                }
                "session/resume" => {
                    v2::ResumeSessionRequest::parse_message(request.method(), request.params())?;
                }
                #[cfg(feature = "unstable_session_fork")]
                "session/fork" => {
                    v2::ForkSessionRequest::parse_message(request.method(), request.params())?;
                }
                method => return Err(unexpected_session_setup_method(method)),
            },
        }
        Ok(())
    }

    pub(crate) fn native_server(self, value: Value) -> Option<NativeServer> {
        let raw = value.as_object()?.clone();
        match self {
            Self::V1 => {
                let McpServer::Acp(server) = serde_json::from_value(value).ok()? else {
                    return None;
                };
                Some(NativeServer {
                    raw,
                    name: server.name,
                    server_id: server.server_id.to_string(),
                })
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                let v2::McpServer::Acp(server) = serde_json::from_value(value).ok()? else {
                    return None;
                };
                Some(NativeServer {
                    raw,
                    name: server.name,
                    server_id: server.server_id.to_string(),
                })
            }
        }
    }

    pub(crate) fn connect_request(self, server_id: String) -> Result<UntypedMessage, Error> {
        match self {
            Self::V1 => ConnectMcpRequest::new(server_id).to_untyped_message(),
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => v2::ConnectMcpRequest::new(server_id).to_untyped_message(),
        }
    }

    pub(crate) fn connect_response_id(self, response: Value) -> Result<String, Error> {
        match self {
            Self::V1 => ConnectMcpResponse::from_value("mcp/connect", response)
                .map(|response| response.connection_id.to_string()),
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => v2::ConnectMcpResponse::from_value("mcp/connect", response)
                .map(|response| response.connection_id.to_string()),
        }
    }

    pub(crate) fn message_request(
        self,
        connection_id: String,
        message: UntypedMessage,
    ) -> Result<UntypedMessage, Error> {
        let (method, params) = message.into_parts();
        let params = into_mcp_params(params)?;
        match self {
            Self::V1 => MessageMcpRequest::new(connection_id, method)
                .params(params)
                .to_untyped_message(),
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => v2::MessageMcpRequest::new(connection_id, method)
                .params(params)
                .to_untyped_message(),
        }
    }

    pub(crate) fn message_notification(
        self,
        connection_id: String,
        message: UntypedMessage,
    ) -> Result<UntypedMessage, Error> {
        let (method, params) = message.into_parts();
        let params = into_mcp_params(params)?;
        match self {
            Self::V1 => MessageMcpNotification::new(connection_id, method)
                .params(params)
                .to_untyped_message(),
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => v2::MessageMcpNotification::new(connection_id, method)
                .params(params)
                .to_untyped_message(),
        }
    }

    pub(crate) fn parse_message_request(
        self,
        request: UntypedMessage,
    ) -> Result<NativeMcpMessage, Error> {
        match self {
            Self::V1 => {
                let parsed = MessageMcpRequest::parse_message(request.method(), request.params())?;
                Ok(NativeMcpMessage {
                    raw: request,
                    connection_id: parsed.connection_id.to_string(),
                    method: parsed.method,
                    params: parsed.params,
                })
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                let parsed =
                    v2::MessageMcpRequest::parse_message(request.method(), request.params())?;
                Ok(NativeMcpMessage {
                    raw: request,
                    connection_id: parsed.connection_id.to_string(),
                    method: parsed.method,
                    params: parsed.params,
                })
            }
        }
    }

    pub(crate) fn parse_message_notification(
        self,
        notification: UntypedMessage,
    ) -> Result<NativeMcpMessage, Error> {
        match self {
            Self::V1 => {
                let parsed = MessageMcpNotification::parse_message(
                    notification.method(),
                    notification.params(),
                )?;
                Ok(NativeMcpMessage {
                    raw: notification,
                    connection_id: parsed.connection_id.to_string(),
                    method: parsed.method,
                    params: parsed.params,
                })
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                let parsed = v2::MessageMcpNotification::parse_message(
                    notification.method(),
                    notification.params(),
                )?;
                Ok(NativeMcpMessage {
                    raw: notification,
                    connection_id: parsed.connection_id.to_string(),
                    method: parsed.method,
                    params: parsed.params,
                })
            }
        }
    }

    pub(crate) fn disconnect_request(self, connection_id: String) -> Result<UntypedMessage, Error> {
        match self {
            Self::V1 => DisconnectMcpRequest::new(connection_id).to_untyped_message(),
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => v2::DisconnectMcpRequest::new(connection_id).to_untyped_message(),
        }
    }

    pub(crate) fn validate_disconnect_response(self, response: Value) -> Result<(), Error> {
        match self {
            Self::V1 => {
                DisconnectMcpResponse::from_value("mcp/disconnect", response)?;
            }
            #[cfg(feature = "unstable_protocol_v2")]
            Self::V2 => {
                v2::DisconnectMcpResponse::from_value("mcp/disconnect", response)?;
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum DownstreamMcpMode {
    #[default]
    Unknown,
    Native,
    HttpAdapter,
    Unavailable,
}

impl DownstreamMcpMode {
    pub(crate) fn from_capabilities(http: bool, acp: bool) -> Self {
        if acp {
            Self::Native
        } else if http {
            Self::HttpAdapter
        } else {
            Self::Unavailable
        }
    }
}

#[derive(Debug)]
pub(crate) struct NativeServer {
    raw: Map<String, Value>,
    pub(crate) name: String,
    pub(crate) server_id: String,
}

impl NativeServer {
    pub(crate) fn http_declaration(
        mut self,
        protocol: PolyfillProtocol,
        url: String,
    ) -> Result<Value, Error> {
        self.raw.remove("serverId");
        self.raw.insert("type".into(), Value::String("http".into()));
        self.raw.insert("name".into(), Value::String(self.name));
        self.raw.insert("url".into(), Value::String(url));
        // V1 requires the field and v2 accepts it. Keeping the explicit empty
        // list gives both versions one stable raw compatibility shape.
        self.raw.insert("headers".into(), Value::Array(Vec::new()));
        let declaration = Value::Object(self.raw);

        match protocol {
            PolyfillProtocol::V1 => {
                serde_json::from_value::<McpServer>(declaration.clone())
                    .map_err(Error::into_internal_error)?;
            }
            #[cfg(feature = "unstable_protocol_v2")]
            PolyfillProtocol::V2 => {
                serde_json::from_value::<v2::McpServer>(declaration.clone())
                    .map_err(Error::into_internal_error)?;
            }
        }
        Ok(declaration)
    }
}

#[derive(Debug)]
pub(crate) struct NativeMcpMessage {
    pub(crate) raw: UntypedMessage,
    pub(crate) connection_id: String,
    pub(crate) method: String,
    pub(crate) params: Option<Map<String, Value>>,
}

pub(crate) fn native_params_into_value(params: Option<Map<String, Value>>) -> Value {
    params.map_or(Value::Null, Value::Object)
}

fn into_mcp_params(params: Value) -> Result<Option<Map<String, Value>>, Error> {
    match params {
        Value::Null => Ok(None),
        Value::Object(params) => Ok(Some(params)),
        params => Err(Error::invalid_params().data(serde_json::json!({
            "reason": "MCP message params must be an object or null",
            "params": params,
        }))),
    }
}

fn invalid_initialize_protocol_version() -> Error {
    Error::invalid_params().data("initialize.protocolVersion must be a valid ACP protocol version")
}

fn unsupported_protocol_version(version: ProtocolVersion) -> Error {
    Error::invalid_request().data(format!(
        "MCP-over-ACP polyfill does not support ACP protocol version {version}"
    ))
}

fn unexpected_session_setup_method(method: &str) -> Error {
    Error::invalid_request().data(format!(
        "`{method}` is not a session setup method for the selected ACP version"
    ))
}

fn invalid_initialize_response(reason: &'static str) -> Error {
    Error::invalid_params().data(format!("invalid initialize response: {reason}"))
}

#[cfg(test)]
mod tests {
    use agent_client_protocol::{
        JsonRpcMessage,
        schema::{ProtocolVersion, v1},
    };

    #[cfg(feature = "unstable_protocol_v2")]
    use agent_client_protocol::{ErrorCode, JsonRpcResponse};

    use super::PolyfillProtocol;

    #[test]
    fn http_declaration_preserves_extension_fields() {
        let declaration = serde_json::json!({
            "type": "acp",
            "name": "native",
            "serverId": "native-id",
            "_meta": {
                "source": "test"
            },
            "futureField": {
                "preserve": true
            }
        });
        let native = PolyfillProtocol::V1
            .native_server(declaration)
            .expect("the declaration should be recognized as native MCP");

        let transformed = native
            .http_declaration(PolyfillProtocol::V1, "http://127.0.0.1:4321".to_string())
            .expect("the transformed declaration should be valid v1 MCP");

        assert_eq!(
            transformed,
            serde_json::json!({
                "type": "http",
                "name": "native",
                "url": "http://127.0.0.1:4321",
                "headers": [],
                "_meta": {
                    "source": "test"
                },
                "futureField": {
                    "preserve": true
                }
            })
        );
    }

    #[test]
    fn native_message_keeps_the_original_wrapper() {
        let request = agent_client_protocol::UntypedMessage {
            method: "mcp/message".to_string(),
            params: serde_json::json!({
                "connectionId": "connection",
                "method": "tools/list",
                "params": {
                    "cursor": "next"
                },
                "_meta": {
                    "trace": "preserve"
                },
                "futureField": true
            }),
        };

        let parsed = PolyfillProtocol::V1
            .parse_message_request(request.clone())
            .expect("the native wrapper should parse");

        assert_eq!(parsed.raw, request);
        assert_eq!(parsed.connection_id, "connection");
        assert_eq!(parsed.method, "tools/list");
        assert_eq!(
            parsed.params,
            Some(serde_json::Map::from_iter([(
                "cursor".to_string(),
                serde_json::json!("next")
            )]))
        );
    }

    #[test]
    fn v1_session_setup_methods_match_the_stable_schema() {
        assert!(PolyfillProtocol::V1.is_session_setup_method("session/new"));
        assert!(PolyfillProtocol::V1.is_session_setup_method("session/load"));
        assert!(PolyfillProtocol::V1.is_session_setup_method("session/resume"));
        assert_eq!(
            PolyfillProtocol::V1.is_session_setup_method("session/fork"),
            cfg!(feature = "unstable_session_fork")
        );
        assert!(!PolyfillProtocol::V1.is_session_setup_method("session/prompt"));
    }

    #[test]
    fn session_setup_validation_allows_extensions_but_rejects_invalid_fields() {
        let mut request = v1::NewSessionRequest::new(std::path::PathBuf::from("/tmp"))
            .to_untyped_message()
            .expect("the session request should serialize");
        request.params["futureField"] = serde_json::json!({
            "preserve": true
        });
        PolyfillProtocol::V1
            .validate_session_setup_request(&request)
            .expect("extension fields should remain forward-compatible");

        request.params["cwd"] = serde_json::json!(42);
        let error = PolyfillProtocol::V1
            .validate_session_setup_request(&request)
            .expect_err("invalid selected-schema fields must be rejected");
        assert_eq!(error.code, agent_client_protocol::ErrorCode::InvalidParams);
    }

    #[cfg(feature = "unstable_protocol_v2")]
    #[test]
    fn v2_session_setup_methods_exclude_v1_load() {
        assert!(PolyfillProtocol::V2.is_session_setup_method("session/new"));
        assert!(!PolyfillProtocol::V2.is_session_setup_method("session/load"));
        assert!(PolyfillProtocol::V2.is_session_setup_method("session/resume"));
        assert_eq!(
            PolyfillProtocol::V2.is_session_setup_method("session/fork"),
            cfg!(feature = "unstable_session_fork")
        );
        assert!(!PolyfillProtocol::V2.is_session_setup_method("session/prompt"));
    }

    #[cfg(feature = "unstable_protocol_v2")]
    #[test]
    fn future_protocol_version_is_not_assumed_to_be_v2() {
        let initialize = agent_client_protocol::schema::v2::InitializeRequest::new(
            ProtocolVersion::V2,
            agent_client_protocol::schema::v2::Implementation::new("test", "1.0.0"),
        );
        let mut request =
            agent_client_protocol::schema::v2::InitializeProxyRequest::new(initialize)
                .to_untyped_message()
                .expect("the initialize request should serialize");
        request.params["protocolVersion"] = serde_json::json!(3);

        let error = PolyfillProtocol::from_initialize_request(&request)
            .expect_err("an unselected future schema must not be interpreted as v2");

        assert_eq!(error.code, ErrorCode::InvalidRequest);
        assert_eq!(
            error.data,
            Some(serde_json::json!(
                "MCP-over-ACP polyfill does not support ACP protocol version 3"
            ))
        );
    }

    #[cfg(feature = "unstable_protocol_v2")]
    #[test]
    fn v2_initialize_adaptation_preserves_the_raw_response() {
        use agent_client_protocol::schema::v2;

        let response = v2::InitializeResponse::new(
            ProtocolVersion::V2,
            v2::Implementation::new("test", "1.0.0"),
        )
        .capabilities(
            v2::AgentCapabilities::new().session(
                v2::SessionCapabilities::new()
                    .mcp(v2::McpCapabilities::new().http(v2::McpHttpCapabilities::new())),
            ),
        );
        let mut response = serde_json::to_value(response).expect("the response should serialize");
        response["futureField"] = serde_json::json!({
            "preserve": true
        });

        let mode = PolyfillProtocol::V2
            .transform_initialize_response(&mut response)
            .expect("the v2 HTTP capability should be adaptable");

        assert_eq!(mode, super::DownstreamMcpMode::HttpAdapter);
        assert_eq!(
            response["capabilities"]["session"]["mcp"]["acp"],
            serde_json::json!({})
        );
        assert_eq!(
            response["futureField"],
            serde_json::json!({
                "preserve": true
            })
        );

        v2::InitializeResponse::from_value("initialize", response)
            .expect("the adapted response should remain valid v2");
    }

    #[cfg(feature = "unstable_protocol_v2")]
    #[test]
    fn v2_disconnect_uses_and_validates_the_selected_schema() {
        use agent_client_protocol::schema::v2;

        let request = PolyfillProtocol::V2
            .disconnect_request("connection".to_string())
            .expect("the v2 disconnect request should serialize");
        let parsed = v2::DisconnectMcpRequest::parse_message(request.method(), request.params())
            .expect("the disconnect request should be valid v2");
        assert_eq!(parsed.connection_id.to_string(), "connection");

        let response = serde_json::to_value(v2::DisconnectMcpResponse::new())
            .expect("the v2 disconnect response should serialize");
        PolyfillProtocol::V2
            .validate_disconnect_response(response)
            .expect("the v2 disconnect response should validate");
    }

    #[test]
    fn v1_initialize_request_selects_v1() {
        let request = agent_client_protocol::schema::InitializeProxyRequest {
            initialize: v1::InitializeRequest::new(ProtocolVersion::V1),
        }
        .to_untyped_message()
        .expect("the initialize request should serialize");

        assert_eq!(
            PolyfillProtocol::from_initialize_request(&request)
                .expect("the request should select v1"),
            PolyfillProtocol::V1
        );
    }
}