agent-block-mcp 0.33.0

rmcp client wrapper for agent-block (MCP server lifecycle + custom handler)
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
//! Rich client tests: resources, prompts, progress, and concurrent access.
//!
//! Uses in-process duplex servers (same pattern as `concurrency_tests`).
//!
//! Moved verbatim out of `lib.rs` (`#[cfg(test)] mod rich_tests`). `super`
//! still resolves to the crate root, so `use super::*` is unchanged.

use super::*;
use rmcp::{
    model::{
        CompleteRequestParams, CompleteResult, CompletionInfo, GetPromptRequestParams,
        GetPromptResponse, GetPromptResult, ListPromptsResult, ListResourceTemplatesResult,
        ListResourcesResult, NumberOrString, PaginatedRequestParams, ProgressNotificationParam,
        ProgressToken, Prompt, PromptMessage, ReadResourceRequestParams, ReadResourceResponse,
        ReadResourceResult, Reference, ResourceContents, Role, ServerCapabilities, ServerInfo,
    },
    service::{MaybeSendFuture, RequestContext},
    ErrorData as McpError, RoleServer, ServerHandler, ServiceExt,
};
use std::sync::Arc;
use tokio::sync::RwLock;

// ── Test Servers ────────────────────────────────────────────────────

#[derive(Clone)]
struct ResourceTestServer;

impl ServerHandler for ResourceTestServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_resources().build())
    }

    fn list_resources(
        &self,
        _request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListResourcesResult, McpError>> + MaybeSendFuture + '_
    {
        let resources = vec![
            rmcp::model::Resource::new("file:///hello.txt", "hello.txt"),
            rmcp::model::Resource::new("file:///world.txt", "world.txt"),
        ];
        std::future::ready(Ok(ListResourcesResult::with_all_items(resources)))
    }

    fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<ReadResourceResponse, McpError>> + MaybeSendFuture + '_
    {
        let uri = request.uri.clone();
        let text = format!("content of {uri}");
        std::future::ready(Ok(ReadResourceResult::new(vec![ResourceContents::text(
            text, uri,
        )])
        .into()))
    }

    fn list_resource_templates(
        &self,
        _request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListResourceTemplatesResult, McpError>>
           + MaybeSendFuture
           + '_ {
        let templates = vec![
            rmcp::model::ResourceTemplate::new("file:///{name}.txt", "file-template"),
            rmcp::model::ResourceTemplate::new("db:///{table}/{id}", "db-template"),
        ];
        std::future::ready(Ok(ListResourceTemplatesResult::with_all_items(templates)))
    }
}

#[derive(Clone)]
struct PromptTestServer;

impl ServerHandler for PromptTestServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_prompts().build())
    }

    fn list_prompts(
        &self,
        _request: Option<PaginatedRequestParams>,
        _ctx: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListPromptsResult, McpError>> + MaybeSendFuture + '_
    {
        let prompts = vec![
            Prompt::new("greet", Some("Greeting prompt"), None),
            Prompt::new("farewell", Some("Farewell prompt"), None),
        ];
        std::future::ready(Ok(ListPromptsResult::with_all_items(prompts)))
    }

    fn get_prompt(
        &self,
        request: GetPromptRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<GetPromptResponse, McpError>> + MaybeSendFuture + '_
    {
        let name = request.name.clone();
        let message = PromptMessage::new_text(Role::User, format!("This is the '{name}' prompt."));
        std::future::ready(Ok(GetPromptResult::new(vec![message]).into()))
    }
}

// ── Helpers ─────────────────────────────────────────────────────────

async fn attach_resource_server(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = ResourceTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

async fn attach_prompt_server(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = PromptTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

#[derive(Clone)]
struct CompleteTestServer;

impl ServerHandler for CompleteTestServer {
    fn get_info(&self) -> ServerInfo {
        // Enable both prompts and resources so this server handles both ref kinds.
        ServerInfo::new(
            ServerCapabilities::builder()
                .enable_prompts()
                .enable_resources()
                .build(),
        )
    }

    async fn complete(
        &self,
        request: CompleteRequestParams,
        _ctx: RequestContext<RoleServer>,
    ) -> Result<CompleteResult, McpError> {
        let info = match &request.r#ref {
            Reference::Prompt(_) => CompletionInfo::with_pagination(
                vec!["alice".to_string(), "alpha".to_string()],
                Some(2),
                false,
            )
            .expect("valid completion info"),
            Reference::Resource(_) => {
                CompletionInfo::with_pagination(vec!["file:///a.txt".to_string()], Some(1), false)
                    .expect("valid completion info")
            }
            // Reference is #[non_exhaustive]; future variants complete to nothing.
            _ => CompletionInfo::with_pagination(vec![], Some(0), false)
                .expect("valid completion info"),
        };
        Ok(CompleteResult::new(info))
    }
}

async fn attach_complete_server(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = CompleteTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

// ── Tests: list_resources ───────────────────────────────────────────

#[tokio::test]
async fn list_resources_returns_all_resources() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;

    let result = mgr
        .list_resources("res")
        .await
        .expect("list_resources should succeed");

    let arr = result.as_array().expect("should be JSON array");
    assert_eq!(arr.len(), 2, "expected 2 resources: {result}");
}

#[tokio::test]
async fn list_resources_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .list_resources("ghost")
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

// ── Tests: list_resource_templates ─────────────────────────────────

#[tokio::test]
async fn list_resource_templates_returns_all_templates() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;

    let result = mgr
        .list_resource_templates("res")
        .await
        .expect("list_resource_templates should succeed");

    let arr = result.as_array().expect("should be JSON array");
    assert_eq!(arr.len(), 2, "expected 2 templates: {result}");

    let uri_template = arr[0]
        .get("uriTemplate")
        .and_then(|v| v.as_str())
        .expect("first template should have uriTemplate");
    assert!(
        uri_template.contains("{name}"),
        "uriTemplate should contain placeholder: {uri_template}"
    );
}

#[tokio::test]
async fn list_resource_templates_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .list_resource_templates("ghost")
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

// ── Tests: read_resource ────────────────────────────────────────────

#[tokio::test]
async fn read_resource_returns_contents() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;

    let result = mgr
        .read_resource("res", "file:///hello.txt")
        .await
        .expect("read_resource should succeed");

    let contents = result
        .get("contents")
        .and_then(|v| v.as_array())
        .expect("should have contents array");
    assert!(!contents.is_empty(), "contents must not be empty: {result}");

    let text = contents[0]
        .get("text")
        .and_then(|v| v.as_str())
        .expect("should have text field");
    assert!(
        text.contains("file:///hello.txt"),
        "text should contain uri: {text}"
    );
}

#[tokio::test]
async fn read_resource_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .read_resource("ghost", "file:///any.txt")
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

// ── Tests: list_prompts ─────────────────────────────────────────────

#[tokio::test]
async fn list_prompts_returns_all_prompts() {
    let mut mgr = McpManager::new();
    attach_prompt_server(&mut mgr, "prm").await;

    let result = mgr
        .list_prompts("prm")
        .await
        .expect("list_prompts should succeed");

    let arr = result.as_array().expect("should be JSON array");
    assert_eq!(arr.len(), 2, "expected 2 prompts: {result}");
}

#[tokio::test]
async fn list_prompts_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .list_prompts("ghost")
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

// ── Tests: get_prompt ───────────────────────────────────────────────

#[tokio::test]
async fn get_prompt_returns_messages() {
    let mut mgr = McpManager::new();
    attach_prompt_server(&mut mgr, "prm").await;

    let result = mgr
        .get_prompt("prm", "greet", serde_json::Value::Null)
        .await
        .expect("get_prompt should succeed");

    let messages = result
        .get("messages")
        .and_then(|v| v.as_array())
        .expect("should have messages array");
    assert!(!messages.is_empty(), "messages must not be empty: {result}");
}

#[tokio::test]
async fn get_prompt_rejects_non_object_args() {
    let mgr = McpManager::new();
    let err = mgr
        .get_prompt("any", "greet", serde_json::json!([1, 2]))
        .await
        .expect_err("array args must error");
    assert!(
        err.to_string().contains("args must be a JSON object"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn get_prompt_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .get_prompt("ghost", "greet", serde_json::Value::Null)
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

// ── Tests: complete ─────────────────────────────────────────────────

#[tokio::test]
async fn complete_prompt_ref_returns_values() {
    let mut mgr = McpManager::new();
    attach_complete_server(&mut mgr, "cmp").await;

    let ref_json = serde_json::json!({ "type": "ref/prompt", "name": "greet" });
    let result = mgr
        .complete("cmp", ref_json, "name", "al")
        .await
        .expect("complete with prompt ref should succeed");

    let completion = result
        .get("completion")
        .expect("result should have 'completion' key");
    let values = completion
        .get("values")
        .and_then(|v| v.as_array())
        .expect("completion should have 'values' array");
    assert!(
        !values.is_empty(),
        "values must not be empty for prompt ref: {result}"
    );
}

#[tokio::test]
async fn complete_resource_ref_returns_values() {
    let mut mgr = McpManager::new();
    attach_complete_server(&mut mgr, "cmp").await;

    let ref_json = serde_json::json!({ "type": "ref/resource", "uri": "file:///a.txt" });
    let result = mgr
        .complete("cmp", ref_json, "uri", "file:///")
        .await
        .expect("complete with resource ref should succeed");

    let completion = result
        .get("completion")
        .expect("result should have 'completion' key");
    let values = completion
        .get("values")
        .and_then(|v| v.as_array())
        .expect("completion should have 'values' array");
    assert!(
        !values.is_empty(),
        "values must not be empty for resource ref: {result}"
    );
}

#[tokio::test]
async fn complete_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let ref_json = serde_json::json!({ "type": "ref/prompt", "name": "greet" });
    let err = mgr
        .complete("ghost", ref_json, "name", "al")
        .await
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn complete_invalid_ref_kind_returns_error() {
    let mgr = McpManager::new();
    let ref_json = serde_json::json!({ "type": "ref/unknown", "name": "x" });
    let err = mgr
        .complete("any", ref_json, "name", "x")
        .await
        .expect_err("invalid ref kind must error");
    assert!(
        err.to_string().contains("invalid ref kind"),
        "unexpected error: {err}"
    );
}

// ── Tests: concurrent reads ─────────────────────────────────────────

/// Verify that list_resources and list_prompts can run concurrently under
/// RwLock::read — neither serializes behind the other.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn concurrent_list_resources_and_list_prompts() {
    let mgr = Arc::new(RwLock::new(McpManager::new()));

    {
        let mut w = mgr.write().await;
        attach_resource_server(&mut w, "res").await;
        attach_prompt_server(&mut w, "prm").await;
    }

    let mgr_a = Arc::clone(&mgr);
    let mgr_b = Arc::clone(&mgr);

    let (r1, r2) = tokio::join!(
        async move { mgr_a.read().await.list_resources("res").await },
        async move { mgr_b.read().await.list_prompts("prm").await },
    );

    r1.expect("list_resources should succeed concurrently");
    r2.expect("list_prompts should succeed concurrently");
}

// ── Tests: on_progress handler registry marker ─────────────────────

#[test]
fn mark_on_progress_sets_flag_accessible_by_handler() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("srv");
    assert!(
        !handler
            .registry
            .lock()
            .unwrap()
            .get("srv")
            .unwrap()
            .on_progress
    );
    handler.mark_on_progress("srv");
    assert!(
        handler
            .registry
            .lock()
            .unwrap()
            .get("srv")
            .unwrap()
            .on_progress
    );
}

// ── Tests: connect_http ─────────────────────────────────────────────

/// connect_http on an unreachable address fails with BlockError::Mcp or Timeout.
#[cfg(feature = "mcp-http")]
#[tokio::test]
async fn connect_http_unreachable_returns_error() {
    let mut mgr = McpManager::with_rpc_timeout(Duration::from_millis(100))
        .expect("non-zero timeout must be accepted");

    let err = mgr
        .connect_http(
            "test",
            "http://127.0.0.1:19999/mcp",
            serde_json::Value::Null,
        )
        .await
        .expect_err("unreachable URL must produce an error");

    let msg = err.to_string();
    assert!(
        msg.contains("http connect") || msg.contains("timed out"),
        "unexpected error: {msg}"
    );
}

// ── Tests: on_log and sampling marker flags ─────────────────────────

#[test]
fn mark_on_log_sets_flag_accessible_by_handler() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("log-srv");
    assert!(
        !handler
            .registry
            .lock()
            .unwrap()
            .get("log-srv")
            .unwrap()
            .on_log
    );
    handler.mark_on_log("log-srv");
    assert!(
        handler
            .registry
            .lock()
            .unwrap()
            .get("log-srv")
            .unwrap()
            .on_log
    );
}

#[test]
fn mark_sampling_sets_flag_accessible_by_handler() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("samp-srv");
    assert!(
        !handler
            .registry
            .lock()
            .unwrap()
            .get("samp-srv")
            .unwrap()
            .sampling
    );
    handler.mark_sampling("samp-srv");
    assert!(
        handler
            .registry
            .lock()
            .unwrap()
            .get("samp-srv")
            .unwrap()
            .sampling
    );
}

// ── Tests: send_cancelled ───────────────────────────────────────────

/// send_cancelled on an unknown server must not panic.
#[tokio::test]
async fn send_cancelled_unknown_server_is_no_op() {
    let mgr = McpManager::new();
    // Should not panic — logs a warn and returns.
    mgr.send_cancelled("ghost", Some(42));
}

/// send_cancelled on a live in-process server completes without error.
#[tokio::test]
async fn send_cancelled_live_server_does_not_panic() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;
    // Pass Some(0) as a concrete request_id (live server will ignore unknown IDs).
    mgr.send_cancelled("res", Some(0));
    // Give the spawned task a moment to complete.
    tokio::time::sleep(Duration::from_millis(50)).await;
}

// ── Tests: server_name set before clone in connect ──────────────────

/// Verifies the server_name + registry handshake in the connect flow.
///
/// `connect` sets `handler.server_name` before `clone()` then resets it
/// to `None` on the shared template. `ensure_server` ensures the registry
/// has an entry.  We test this without spawning a real transport by using
/// `ensure_server` + manual server_name mutation, which mirrors the
/// actual `connect` / `connect_http` code path.
#[test]
fn handler_server_name_reset_after_simulated_connect() {
    let mut mgr = McpManager::new();
    // Simulate what connect() does before cloning the handler.
    mgr.handler.ensure_server("srv-x");
    mgr.handler.server_name = Some("srv-x".to_string());
    let cloned = mgr.handler.clone();
    mgr.handler.server_name = None;

    // Template must be reset; clone must retain the name.
    assert!(
        mgr.handler.server_name.is_none(),
        "template server_name must be None after simulated connect"
    );
    assert_eq!(
        cloned.server_name.as_deref(),
        Some("srv-x"),
        "cloned handler must carry the server_name"
    );
    // Registry entry created by ensure_server.
    let guard = mgr.handler.registry.lock().unwrap();
    assert!(
        guard.contains_key("srv-x"),
        "registry must have entry after ensure_server"
    );
}

// ── Tests: progress dispatch (no-isle path) ─────────────────────────

/// Verifies the on_progress no-op path when handler_isle is None:
/// ensure_server + mark_on_progress sets the flag, and calling on_progress
/// with a real notification completes without panic when no isle is wired.
#[tokio::test]
async fn on_progress_no_op_when_no_isle() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("srv");
    handler.mark_on_progress("srv");

    // Simulate a progress notification arriving from rmcp task.
    let params =
        ProgressNotificationParam::new(ProgressToken(NumberOrString::String("tok-1".into())), 0.5)
            .with_total(1.0);

    // We can't construct a full NotificationContext without a live Peer.
    // The no-isle path exits immediately, so this is covered by the unit test
    // in handler::tests::dispatcher_no_op_when_no_handler.
    // This test validates the flag path end-to-end via the registry.
    let guard = handler.registry.lock().unwrap();
    assert!(
        guard.get("srv").unwrap().on_progress,
        "on_progress flag must be set after mark_on_progress"
    );
    drop(guard);

    // The handler's on_progress is async; with no isle it short-circuits.
    // We exercise it via a minimal timeout-wrapped call.
    let _ = params;
}

// ── Tests: server_info ──────────────────────────────────────────────

#[tokio::test]
async fn server_info_unknown_server_returns_error() {
    let mgr = McpManager::new();
    let err = mgr
        .server_info("ghost")
        .expect_err("unknown server must error");
    assert!(
        err.to_string().contains("no server named"),
        "unexpected error: {err}"
    );
}

#[tokio::test]
async fn server_info_returns_capabilities_for_resource_server() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;

    let info = mgr
        .server_info("res")
        .expect("server_info should succeed after handshake");

    let caps = info
        .get("capabilities")
        .expect("InitializeResult must have capabilities field");
    assert!(
        caps.get("resources").is_some(),
        "resource server must advertise resources capability: {caps}"
    );
}

#[tokio::test]
async fn server_info_returns_capabilities_for_prompt_server() {
    let mut mgr = McpManager::new();
    attach_prompt_server(&mut mgr, "prm").await;

    let info = mgr
        .server_info("prm")
        .expect("server_info should succeed after handshake");

    let caps = info
        .get("capabilities")
        .expect("InitializeResult must have capabilities field");
    assert!(
        caps.get("prompts").is_some(),
        "prompt server must advertise prompts capability: {caps}"
    );
}

// ── Tests: logging capability gate (case c) ─────────────────────────

/// A server that declares logging capability.
#[derive(Clone)]
struct LoggingCapableServer;

impl ServerHandler for LoggingCapableServer {
    // rmcp 3.x: `enable_logging()` is deprecated by SEP-2577. Kept on
    // the test surface until the migration to the post-2577 logging
    // API lands (tracked separately).
    #[allow(deprecated)]
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(
            ServerCapabilities::builder()
                .enable_tools()
                .enable_logging()
                .build(),
        )
    }
}

async fn attach_logging_server(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = LoggingCapableServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

/// Verifies that `server_info` for a server with logging capability
/// returns `capabilities.logging` as a non-null field.  This is the
/// Rust-side condition that the Lua `connect_mcp_servers` gate checks:
/// `caps.logging ~= nil`.
#[tokio::test]
async fn server_info_returns_logging_capability_when_declared() {
    let mut mgr = McpManager::new();
    attach_logging_server(&mut mgr, "log").await;

    let info = mgr
        .server_info("log")
        .expect("server_info should succeed after handshake");

    let caps = info
        .get("capabilities")
        .expect("InitializeResult must have capabilities field");
    assert!(
        caps.get("logging").is_some(),
        "logging-capable server must advertise logging capability: {caps}"
    );
}

/// Verifies that `server_info` for a server WITHOUT logging capability
/// returns no `capabilities.logging` field, confirming the gate condition
/// correctly evaluates to `caps.logging == nil` in Lua.
#[tokio::test]
async fn server_info_has_no_logging_capability_for_tool_only_server() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;

    let info = mgr
        .server_info("res")
        .expect("server_info should succeed after handshake");

    let caps = info
        .get("capabilities")
        .expect("InitializeResult must have capabilities field");
    assert!(
        caps.get("logging").is_none(),
        "resource-only server must not advertise logging capability: {caps}"
    );
}

// ── Tests: call_tool progress token auto-attach ─────────────────────

/// Integration test: verifies that `call_tool` (and list_resources, which
/// shares the same connection path) succeeds both when an `on_progress`
/// handler is registered for the server and when it is not.
#[tokio::test]
async fn call_tool_succeeds_with_and_without_progress_handler() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "srv").await;

    // Without on_progress handler — should succeed.
    mgr.list_resources("srv")
        .await
        .expect("list_resources without handler should succeed");

    // With on_progress handler — auto-attach path is exercised; should still succeed.
    mgr.handler.mark_on_progress("srv");
    mgr.list_resources("srv")
        .await
        .expect("list_resources with handler should succeed");
}

// ── Test Server: RootsTestServer ────────────────────────────────────

/// A test server that, when `call_tool` is invoked, issues a `roots/list`
/// request back to the client (server→client direction) and embeds the
/// result in the tool response. This exercises the Crux C2 duplex path:
/// the client's `ClientHandler::list_roots` override is triggered by the
/// server's outbound `peer.list_roots()` call.
#[derive(Clone)]
struct RootsTestServer;

impl ServerHandler for RootsTestServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
    }

    // rmcp 3.x: `peer.list_roots()` is deprecated by SEP-2577. Kept
    // on the test surface until the migration to the post-2577 roots
    // API lands (tracked separately).
    #[allow(deprecated)]
    async fn call_tool(
        &self,
        _params: rmcp::model::CallToolRequestParams,
        ctx: RequestContext<RoleServer>,
    ) -> Result<rmcp::model::CallToolResponse, McpError> {
        // Issue a server→client `roots/list` request.
        // This triggers `AgentBlockClientHandler::list_roots` on the
        // client side, which calls the registered Lua roots handler.
        let roots_result = ctx.peer.list_roots().await.map_err(|e| {
            McpError::internal_error(format!("server list_roots failed: {e}"), None)
        })?;
        // Return the count and first URI as text so the test can assert.
        let count = roots_result.roots.len();
        let first_uri = roots_result
            .roots
            .first()
            .map(|r| r.uri.as_str())
            .unwrap_or("(none)");
        Ok(
            rmcp::model::CallToolResult::success(vec![rmcp::model::ContentBlock::text(format!(
                "roots:{count}:{first_uri}"
            ))])
            .into(),
        )
    }
}

/// Attach a `RootsTestServer` to `mgr` under `name`, with a pre-configured
/// handler Isle that has a Lua roots handler installed for the server name.
///
/// Returns the `IsleDriver` so the caller can keep the driver alive.
async fn attach_roots_server_with_isle(
    mgr: &mut McpManager,
    name: &str,
) -> mlua_isle::AsyncIsleDriver {
    use mlua_isle::AsyncIsle;

    // Spawn the isle with a trivial init, then configure it via exec().
    let (isle, driver) = AsyncIsle::spawn(|_lua: &mlua::Lua| Ok(()))
        .await
        .expect("AsyncIsle::spawn should succeed");

    let name_owned = name.to_string();
    isle.exec(move |lua| {
        handler::install_mcp_dispatcher_on_handler_isle(lua)
            .map_err(|e| mlua_isle::IsleError::Lua(format!("setup dispatcher: {e}")))?;
        // Pre-install the Lua roots handler for `name_owned`.
        use mlua::prelude::*;
        let handlers: LuaTable = lua
            .globals()
            .get("__mcp_roots_handlers")
            .map_err(|e| mlua_isle::IsleError::Lua(format!("get handlers: {e}")))?;
        let cb: LuaFunction = lua
            .load(
                r#"
                return function(server_name)
                    return {
                        { uri = "file:///test", name = "TestRoot" },
                    }
                end
            "#,
            )
            .set_name("@test_roots_handler")
            .eval()
            .map_err(|e| mlua_isle::IsleError::Lua(format!("eval: {e}")))?;
        handlers
            .set(name_owned.as_str(), cb)
            .map_err(|e| mlua_isle::IsleError::Lua(format!("set handler: {e}")))?;
        Ok(String::new())
    })
    .await
    .expect("isle setup must succeed");

    let isle_arc = std::sync::Arc::new(isle);

    // Build a fresh handler, wire the isle and server_name BEFORE calling
    // serve() so the RunningService clone has them set.
    let mut handler = AgentBlockClientHandler::new();
    handler.handler_isle = Some(std::sync::Arc::clone(&isle_arc));
    handler.server_name = Some(name.to_string());
    handler.mark_roots(name);

    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = RootsTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);

    driver
}

/// Attach a plain `RootsTestServer` without any Lua handler wired.
/// Used for testing the no-handler error path.
async fn attach_roots_server_bare(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = RootsTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

// ── Tests: mark_roots flag ──────────────────────────────────────────

/// (T1) mark_roots sets the registry flag that list_roots checks.
#[test]
fn mark_roots_sets_flag_accessible_by_handler() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("roots-srv");
    assert!(
        !handler
            .registry
            .lock()
            .unwrap()
            .get("roots-srv")
            .unwrap()
            .roots
    );
    handler.mark_roots("roots-srv");
    assert!(
        handler
            .registry
            .lock()
            .unwrap()
            .get("roots-srv")
            .unwrap()
            .roots
    );
}

// ── Tests: notify_roots_list_changed ───────────────────────────────

/// (T2) notify_roots_list_changed on an unknown server must not panic.
#[tokio::test]
async fn notify_roots_list_changed_unknown_server_is_no_op() {
    let mgr = McpManager::new();
    // Should not panic — logs a warn and returns.
    mgr.notify_roots_list_changed("ghost");
}

/// (T1) notify_roots_list_changed on a live in-process server completes
/// without error. Mirrors `send_cancelled_live_server_does_not_panic`.
#[tokio::test]
async fn notify_roots_list_changed_live_server_does_not_panic() {
    let mut mgr = McpManager::new();
    attach_resource_server(&mut mgr, "res").await;
    mgr.notify_roots_list_changed("res");
    // Give the spawned task a moment to complete.
    tokio::time::sleep(Duration::from_millis(50)).await;
}

// ── Tests: live duplex roots round-trip (Crux C2) ───────────────────

/// (T1 / Crux C2) Live duplex test: the server issues `roots/list` to the
/// client while the client concurrently sends `notify_roots_list_changed`
/// back to the server. Both must complete successfully.
///
/// Flow:
///  (a) server→client: `call_tool` triggers `peer.list_roots()` which
///      dispatches to `AgentBlockClientHandler::list_roots` on the client.
///  (b) client→server: `notify_roots_list_changed` fires a
///      `notifications/roots/list_changed` notification concurrently.
///
/// This test verifies thread-safety of the ROOTS_HANDLERS registry under
/// real async dispatch (Crux C2: concurrent flight, not sequential stubs).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn live_duplex_roots_round_trip() {
    let mut mgr = McpManager::new();
    let _driver = attach_roots_server_with_isle(&mut mgr, "roots").await;

    // Wrap in Arc<RwLock<...>> for concurrent access.
    let mgr_arc = std::sync::Arc::new(tokio::sync::RwLock::new(mgr));

    // (a) Spawn call_tool — server will issue list_roots back to client.
    let mgr_a = std::sync::Arc::clone(&mgr_arc);
    let call_handle = tokio::spawn(async move {
        mgr_a
            .read()
            .await
            .call_tool("roots", "any_tool", serde_json::json!({}))
            .await
    });

    // (b) Concurrently send notify_roots_list_changed client→server.
    let mgr_b = std::sync::Arc::clone(&mgr_arc);
    let notify_handle = tokio::spawn(async move {
        // Small yield to let call_tool start, ensuring concurrent flight.
        tokio::time::sleep(Duration::from_millis(5)).await;
        mgr_b.read().await.notify_roots_list_changed("roots");
    });

    // Both must complete without panic.
    let tool_result = call_handle.await.expect("call_handle must not panic");
    notify_handle.await.expect("notify_handle must not panic");

    // The tool result contains the roots count embedded in the text.
    let result = tool_result.expect("call_tool must succeed");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("roots:1:file:///test"),
        "expected roots:1:file:///test in tool result: {result_json}"
    );
}

/// (T3) list_roots without a registered handler returns method_not_found error.
/// The server propagates the error back to the client as a McpError.
#[tokio::test]
async fn live_duplex_roots_no_handler_returns_error() {
    let mut mgr = McpManager::new();
    // Attach without wiring an isle or handler — call_tool triggers list_roots
    // which should return method_not_found on the client side.
    attach_roots_server_bare(&mut mgr, "roots-no-handler").await;

    let result = mgr
        .call_tool("roots-no-handler", "any_tool", serde_json::json!({}))
        .await;
    // The server propagates the list_roots method_not_found error as a
    // BlockError on the client side.
    assert!(
        result.is_err(),
        "call_tool must fail when no roots handler is registered: {result:?}"
    );
}

// ── Test Server: ElicitationTestServer ─────────────────────────────

/// A test server that, when `call_tool` is invoked, issues an
/// `elicitation/create` request back to the client (server→client direction)
/// using a Form variant and embeds the result in the tool response. This
/// exercises the `ClientHandler::create_elicitation` override.
#[derive(Clone)]
struct ElicitationTestServer;

impl ServerHandler for ElicitationTestServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
    }

    async fn call_tool(
        &self,
        _params: rmcp::model::CallToolRequestParams,
        ctx: RequestContext<RoleServer>,
    ) -> Result<rmcp::model::CallToolResponse, McpError> {
        use rmcp::model::{
            ElicitRequestParams, ElicitationSchema, PrimitiveSchemaDefinition, StringSchema,
        };
        use std::collections::BTreeMap;

        // Build a minimal Form variant with one string property.
        let mut props = BTreeMap::new();
        props.insert(
            "name".to_string(),
            PrimitiveSchemaDefinition::String(StringSchema::new()),
        );
        let schema = ElicitationSchema::new(props);
        let req = ElicitRequestParams::FormElicitationParams {
            meta: None,
            message: "What is your name?".to_string(),
            requested_schema: schema,
        };

        let result = ctx
            .peer
            .create_elicitation(req)
            .await
            .map_err(|e| McpError::internal_error(format!("create_elicitation: {e}"), None))?;

        // Encode action + content as text so tests can assert.
        let action_str = match result.action {
            rmcp::model::ElicitationAction::Accept => "accept",
            rmcp::model::ElicitationAction::Decline => "decline",
            rmcp::model::ElicitationAction::Cancel => "cancel",
            // ElicitationAction is #[non_exhaustive].
            _ => "unknown",
        };
        let content_str = result
            .content
            .map(|v| serde_json::to_string(&v).unwrap_or_default())
            .unwrap_or_default();
        Ok(
            rmcp::model::CallToolResult::success(vec![rmcp::model::ContentBlock::text(format!(
                "elicitation:{action_str}:{content_str}"
            ))])
            .into(),
        )
    }
}

/// A test server that issues a Url-variant `elicitation/create` request.
/// Used to verify that the client always returns Decline for Url variants
/// without dispatching to the Lua callback.
#[derive(Clone)]
struct ElicitationUrlTestServer;

impl ServerHandler for ElicitationUrlTestServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
    }

    async fn call_tool(
        &self,
        _params: rmcp::model::CallToolRequestParams,
        ctx: RequestContext<RoleServer>,
    ) -> Result<rmcp::model::CallToolResponse, McpError> {
        use rmcp::model::ElicitRequestParams;

        // Issue a Url variant — client must always Decline without Lua dispatch.
        let req = ElicitRequestParams::UrlElicitationParams {
            meta: None,
            message: "Please complete this form online".to_string(),
            url: "https://example.com/form".to_string(),
            elicitation_id: "test-elicitation-id-001".to_string(),
        };

        let result = ctx
            .peer
            .create_elicitation(req)
            .await
            .map_err(|e| McpError::internal_error(format!("create_elicitation: {e}"), None))?;

        let action_str = match result.action {
            rmcp::model::ElicitationAction::Accept => "accept",
            rmcp::model::ElicitationAction::Decline => "decline",
            rmcp::model::ElicitationAction::Cancel => "cancel",
            // ElicitationAction is #[non_exhaustive].
            _ => "unknown",
        };
        Ok(
            rmcp::model::CallToolResult::success(vec![rmcp::model::ContentBlock::text(format!(
                "url_elicitation:{action_str}"
            ))])
            .into(),
        )
    }
}

/// Attach an `ElicitationTestServer` (Form variant) with a pre-configured
/// handler Isle that has a Lua elicitation handler returning the given
/// `action` and (for accept) a content object `{name: "Alice"}`.
///
/// Returns the `IsleDriver` so the caller can keep the driver alive.
async fn attach_elicitation_server_with_isle(
    mgr: &mut McpManager,
    name: &str,
    action: &str,
) -> mlua_isle::AsyncIsleDriver {
    use mlua_isle::AsyncIsle;

    let (isle, driver) = AsyncIsle::spawn(|_lua: &mlua::Lua| Ok(()))
        .await
        .expect("AsyncIsle::spawn should succeed");

    let name_owned = name.to_string();
    let action_owned = action.to_string();
    isle.exec(move |lua| {
        handler::install_mcp_dispatcher_on_handler_isle(lua)
            .map_err(|e| mlua_isle::IsleError::Lua(format!("setup dispatcher: {e}")))?;
        // Pre-install the Lua elicitation handler for `name_owned`.
        use mlua::prelude::*;
        let handlers: LuaTable = lua
            .globals()
            .get("__mcp_elicitation_handlers")
            .map_err(|e| mlua_isle::IsleError::Lua(format!("get handlers: {e}")))?;

        // Build a handler that returns the requested action.
        let handler_src = match action_owned.as_str() {
            "accept" => {
                r#"
                return function(server_name, message, schema_json)
                    return { action = "accept", content = { name = "Alice" } }
                end
            "#
            }
            "decline" => {
                r#"
                return function(server_name, message, schema_json)
                    return { action = "decline" }
                end
            "#
            }
            "cancel" => {
                r#"
                return function(server_name, message, schema_json)
                    return { action = "cancel" }
                end
            "#
            }
            _ => {
                r#"
                return function(server_name, message, schema_json)
                    return { action = "decline" }
                end
            "#
            }
        };

        let cb: LuaFunction = lua
            .load(handler_src)
            .set_name("@test_elicitation_handler")
            .eval()
            .map_err(|e| mlua_isle::IsleError::Lua(format!("eval: {e}")))?;
        handlers
            .set(name_owned.as_str(), cb)
            .map_err(|e| mlua_isle::IsleError::Lua(format!("set handler: {e}")))?;
        Ok(String::new())
    })
    .await
    .expect("isle setup must succeed");

    let isle_arc = std::sync::Arc::new(isle);

    let mut handler = AgentBlockClientHandler::new();
    handler.handler_isle = Some(std::sync::Arc::clone(&isle_arc));
    handler.server_name = Some(name.to_string());
    handler.mark_elicitation(name);

    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = ElicitationTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);

    driver
}

/// Attach a plain `ElicitationTestServer` (Form variant) without any Lua
/// handler wired. Used for testing the no-handler Decline path.
/// Server name is wired so `create_elicitation` can check the registry and
/// return `Decline` (not `method_not_found`) when no handler is registered.
async fn attach_elicitation_server_bare(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = ElicitationTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let mut handler = AgentBlockClientHandler::new();
    handler.ensure_server(name);
    handler.server_name = Some(name.to_string());
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

/// Attach a `ElicitationUrlTestServer` (Url variant) without any Lua handler.
/// Used to verify Url-variant always returns Decline.
async fn attach_elicitation_url_server(mgr: &mut McpManager, name: &str) {
    let (server_side, client_side) = tokio::io::duplex(65536);
    tokio::spawn(async move {
        if let Ok(running) = ElicitationUrlTestServer.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler.serve(client_side).await.expect("handshake");
    mgr.servers.insert(name.to_string(), running);
}

// ── Tests: mark_elicitation flag ───────────────────────────────────

/// (T1) mark_elicitation sets the registry flag that create_elicitation checks.
#[test]
fn mark_elicitation_sets_flag_accessible_by_handler() {
    let handler = AgentBlockClientHandler::new();
    handler.ensure_server("elicit-srv");
    assert!(
        !handler
            .registry
            .lock()
            .unwrap()
            .get("elicit-srv")
            .unwrap()
            .elicitation
    );
    handler.mark_elicitation("elicit-srv");
    assert!(
        handler
            .registry
            .lock()
            .unwrap()
            .get("elicit-srv")
            .unwrap()
            .elicitation
    );
}

// ── Tests: live duplex elicitation round-trips ─────────────────────

/// (T1 / elicitation_accept) Form variant + handler accept → Accept result with content.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn elicitation_accept_returns_accept_with_content() {
    let mut mgr = McpManager::new();
    let _driver = attach_elicitation_server_with_isle(&mut mgr, "elicit", "accept").await;

    let result = mgr
        .call_tool("elicit", "any_tool", serde_json::json!({}))
        .await
        .expect("call_tool must succeed");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("elicitation:accept:"),
        "expected elicitation:accept: in result: {result_json}"
    );
    assert!(
        result_json.contains("Alice"),
        "expected Alice in content: {result_json}"
    );
}

/// (T2 / elicitation_decline) Form variant + handler decline → Decline result.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn elicitation_decline_returns_decline() {
    let mut mgr = McpManager::new();
    let _driver = attach_elicitation_server_with_isle(&mut mgr, "elicit", "decline").await;

    let result = mgr
        .call_tool("elicit", "any_tool", serde_json::json!({}))
        .await
        .expect("call_tool must succeed");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("elicitation:decline:"),
        "expected elicitation:decline: in result: {result_json}"
    );
}

/// (T3 / elicitation_cancel) Form variant + handler cancel → Cancel result.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn elicitation_cancel_returns_cancel() {
    let mut mgr = McpManager::new();
    let _driver = attach_elicitation_server_with_isle(&mut mgr, "elicit", "cancel").await;

    let result = mgr
        .call_tool("elicit", "any_tool", serde_json::json!({}))
        .await
        .expect("call_tool must succeed");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("elicitation:cancel:"),
        "expected elicitation:cancel: in result: {result_json}"
    );
}

/// (T4 / elicitation_url_decline) Url variant → always Decline, no Lua dispatch.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn elicitation_url_variant_always_declines() {
    let mut mgr = McpManager::new();
    attach_elicitation_url_server(&mut mgr, "elicit-url").await;

    let result = mgr
        .call_tool("elicit-url", "any_tool", serde_json::json!({}))
        .await
        .expect("call_tool must succeed");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("url_elicitation:decline"),
        "expected url_elicitation:decline in result: {result_json}"
    );
}

/// (T5 / elicitation_no_handler) Form variant + no handler → Decline (spec neutral).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn elicitation_no_handler_returns_decline() {
    let mut mgr = McpManager::new();
    // Attach without wiring an isle or elicitation handler.
    attach_elicitation_server_bare(&mut mgr, "elicit-bare").await;

    let result = mgr
        .call_tool("elicit-bare", "any_tool", serde_json::json!({}))
        .await
        .expect("call_tool must succeed — no handler → Decline, not error");
    let result_json = serde_json::to_string(&result).expect("serialize result");
    assert!(
        result_json.contains("elicitation:decline:"),
        "expected elicitation:decline: when no handler registered: {result_json}"
    );
}

// ── Test Servers: ping ─────────────────────────────────────────────

/// A server that sleeps `delay` before responding to every `ping`.
/// Used to drive the timeout path in `McpManager::ping`.
/// K-181: ServerHandler impl uses `async fn` directly.
#[derive(Clone)]
struct SlowPingServer {
    delay: Duration,
}

impl ServerHandler for SlowPingServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().build())
    }

    async fn ping(&self, _ctx: RequestContext<RoleServer>) -> Result<(), McpError> {
        tokio::time::sleep(self.delay).await;
        Ok(())
    }
}

/// Attach an in-process `SlowPingServer` to `mgr` under `name`.
async fn attach_slow_ping_server(mgr: &mut McpManager, name: &str, delay: Duration) {
    let (server_side, client_side) = tokio::io::duplex(8192);
    let server = SlowPingServer { delay };
    tokio::spawn(async move {
        if let Ok(running) = server.serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let handler = AgentBlockClientHandler::new();
    let running = handler
        .serve(client_side)
        .await
        .expect("client handshake should succeed over duplex");
    mgr.servers.insert(name.to_string(), running);
}

// ── Tests: ping ────────────────────────────────────────────────────

/// (P1) ping against a responsive server returns Ok(latency_ms).
/// latency_ms is a non-negative integer (monotonic Instant measurement).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ping_success_returns_latency_ms() {
    let mut mgr = McpManager::new();
    // Use zero delay so the test completes quickly.
    attach_slow_ping_server(&mut mgr, "pingsrv", Duration::from_millis(0)).await;

    let result = mgr.ping("pingsrv").await;
    let latency_ms = result.expect("ping should succeed against a live server");
    // latency_ms must be a non-negative integer; the type is u64.
    // We cannot assert an exact value, but it should be <= 5000ms on
    // any reasonable CI box.
    assert!(
        latency_ms <= 5000,
        "latency_ms={latency_ms} looks unreasonable (> 5 s)"
    );
}

/// (P2) ping against a server that delays beyond rpc_timeout returns
/// `BlockError::Timeout`, not `BlockError::Mcp`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ping_timeout_returns_block_error_timeout() {
    let mut mgr = McpManager::with_rpc_timeout(Duration::from_millis(1))
        .expect("with_rpc_timeout(1ms) should succeed");
    // Server sleeps 200ms, rpc_timeout is 1ms → guaranteed timeout.
    attach_slow_ping_server(&mut mgr, "slowping", Duration::from_millis(200)).await;

    let result = mgr.ping("slowping").await;
    let err = result.expect_err("ping should time out");
    assert!(
        matches!(err, BlockError::Timeout(_)),
        "expected BlockError::Timeout, got: {err:?}"
    );
    let msg = err.to_string();
    assert!(
        msg.contains("timed out"),
        "timeout message should contain 'timed out': {msg}"
    );
}