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
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
//! MCP Client — manages MCP server child processes via rmcp.
//!
//! Uses `rmcp` (1.4.x) `RunningService<RoleClient, AgentBlockClientHandler>` internally.
//! `AgentBlockClientHandler` provides custom notification handling via Lua callbacks
//! (wired in Subtask 2/3). For Subtask 1, all notification methods are default no-ops.
//!
//! All rmcp round-trips are wrapped in a per-call timeout so a hung child
//! cannot block a Lua coroutine indefinitely.
//!
//! # Concurrency contract
//!
//! `list_tools` and `call_tool` take `&self`, so the manager can be held
//! under `tokio::sync::RwLock` and multiple RPCs — including against the
//! same server — can proceed in parallel via read guards. Request/response
//! multiplexing on a single server is handled by rmcp's `Peer`, which
//! pairs each outbound request with a `oneshot` receiver keyed by request
//! ID. `connect` and `disconnect` are mutating (`&mut self`) and must take
//! the write guard.
//!
//! This contract is covered by in-process unit tests in `#[cfg(test)]` at
//! the bottom of this file. If rmcp alters its `Peer` concurrency model,
//! or if this module is refactored to re-serialize RPCs, those tests fail.
//!
//! # Usage from Lua
//!
//! ```lua
//! mcp.connect("outline", "outline-mcp", {})
//! local tools = mcp.list_tools("outline")
//! local result = mcp.call("outline", "shelf", {})
//! mcp.disconnect("outline")
//! ```
pub mod handler;
pub(crate) mod http;
pub mod lua_json;
use std::collections::HashMap;
use std::process::Stdio;
use std::sync::Arc;
use std::time::{Duration, Instant};
use mlua_isle::AsyncIsle;
use rmcp::{
model::{
ArgumentInfo, CallToolRequestParams, CancelledNotification, CancelledNotificationParam,
ClientRequest, CompleteRequestParams, GetPromptRequestParams, NumberOrString, PingRequest,
ReadResourceRequestParams, Reference, RootsListChangedNotification, ServerResult,
SubscribeRequestParams, UnsubscribeRequestParams,
},
service::{RoleClient, RunningService},
transport::TokioChildProcess,
ServiceExt,
};
use tokio::process::Command;
use tokio::time::timeout;
use tracing::warn;
use agent_block_types::error::{BlockError, BlockResult};
pub use handler::AgentBlockClientHandler;
/// Default RPC round-trip timeout when no explicit value is provided.
pub const DEFAULT_RPC_TIMEOUT: Duration = Duration::from_secs(30);
pub struct McpManager {
/// Server connections keyed by name. `pub(crate)` so integration tests
/// can insert in-process test servers directly (same as `concurrency_tests`
/// in this module).
pub servers: HashMap<String, RunningService<RoleClient, AgentBlockClientHandler>>,
rpc_timeout: Duration,
/// Shared handler instance — all connections share the same registry Arc.
pub handler: AgentBlockClientHandler,
}
impl McpManager {
pub fn new() -> Self {
Self {
servers: HashMap::new(),
rpc_timeout: DEFAULT_RPC_TIMEOUT,
handler: AgentBlockClientHandler::new(),
}
}
/// Construct a manager with a caller-specified RPC timeout.
/// Applies to `connect`, `list_tools`, and `call_tool` alike.
///
/// `rpc_timeout` must be non-zero. `Duration::ZERO` would cause every
/// `tokio::time::timeout` to fire immediately, silently turning every
/// MCP round-trip into a timeout error — for an autonomous agent that
/// is a "everything looks broken" failure mode. We reject it at
/// construction time so the misconfiguration surfaces loudly at
/// startup instead of being swallowed at the first RPC.
pub fn with_rpc_timeout(rpc_timeout: Duration) -> BlockResult<Self> {
if rpc_timeout.is_zero() {
return Err(BlockError::Mcp(
"rpc_timeout must be > 0 (got Duration::ZERO); \
every MCP RPC would time out immediately"
.to_string(),
));
}
Ok(Self {
servers: HashMap::new(),
rpc_timeout,
handler: AgentBlockClientHandler::new(),
})
}
/// Spawn the MCP server process and complete the MCP initialize handshake.
///
/// `trace_context`: if `true`, `__ab_obs` observability context will be
/// injected into `call_tool` arguments for this server. Defaults to `false`
/// (opt-in) so that third-party / untrusted stdio servers do not receive agent
/// identity metadata unless explicitly enabled.
pub async fn connect(
&mut self,
name: &str,
command: &str,
args: &[String],
trace_context: bool,
) -> BlockResult<()> {
let mut cmd = Command::new(command);
cmd.args(args).stderr(Stdio::inherit());
let transport = TokioChildProcess::new(cmd).map_err(|e| {
warn!(server = %name, command = %command, error = %e, "mcp spawn failed");
BlockError::Mcp(format!("spawn '{command}': {e}"))
})?;
let rpc_timeout = self.rpc_timeout;
// Ensure the handler registry has an entry for this server name
// so callbacks can be registered immediately after connect returns.
self.handler.ensure_server(name);
self.handler.set_trace_context(name, trace_context);
// Set server_name before clone so create_message can identify the
// connection without needing the RequestContext to carry server identity.
// The mutate-template → clone → reset dance is required because
// AgentBlockClientHandler is shared across all connections via Arc<Mutex>
// for the registry, but create_message needs per-connection server identity
// that is NOT shared. Cloning after setting server_name gives each
// RunningService its own immutable copy of the name while the registry Arc
// continues to be shared. Both connect() and connect_http() use this pattern.
self.handler.server_name = Some(name.to_string());
let handler = self.handler.clone();
// Reset server_name on the shared template so the next connect call
// starts fresh.
self.handler.server_name = None;
let running = timeout(rpc_timeout, handler.serve(transport))
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp initialize timed out");
BlockError::Timeout(format!(
"initialize '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp initialize failed");
BlockError::Mcp(format!("initialize '{name}': {e}"))
})?;
self.servers.insert(name.to_string(), running);
Ok(())
}
/// Call `tools/list` and return the tools as a JSON array.
///
/// Immutable receiver so concurrent readers can share an `RwLock<McpManager>`.
pub async fn list_tools(&self, name: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp list_tools on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let tools = timeout(rpc_timeout, srv.list_all_tools())
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp list_tools timed out");
BlockError::Timeout(format!(
"list_tools '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp list_tools failed");
BlockError::Mcp(format!("list_tools '{name}': {e}"))
})?;
serde_json::to_value(&tools)
.map_err(|e| BlockError::Mcp(format!("serialize list_tools result: {e}")))
}
/// Call `tools/call` with the given tool name and arguments.
///
/// Returns the full rmcp `CallToolResult` serialized to JSON
/// (`{"content": [...], "isError": bool, ...}`) on success, including
/// the `isError` flag — tool-execution errors are passed through to
/// the caller, following the MCP spec's intent that the LLM sees them
/// and self-corrects. Only protocol / transport / timeout failures
/// surface as `Err(BlockError::*)`.
///
/// `arguments` must be a JSON `Object` or `Null`. `Null` is treated as
/// "no arguments"; any other shape (array, scalar) returns an error
/// rather than silently dropping the payload.
/// Immutable receiver so concurrent readers can share an `RwLock<McpManager>`.
pub async fn call_tool(
&self,
name: &str,
tool_name: &str,
arguments: serde_json::Value,
) -> BlockResult<serde_json::Value> {
// Validate argument shape early so the error does not depend on
// whether the server is registered or reachable. MCP spec requires
// `arguments` to be an object (or absent); an array/scalar would
// serialize into `CallToolRequestParams` as-is and the server
// would reject it with an opaque protocol error.
let mut params = CallToolRequestParams::new(tool_name.to_string());
match arguments {
serde_json::Value::Object(obj) => {
params = params.with_arguments(obj);
}
serde_json::Value::Null => {}
other => {
let kind = match other {
serde_json::Value::Array(_) => "array",
serde_json::Value::String(_) => "string",
serde_json::Value::Number(_) => "number",
serde_json::Value::Bool(_) => "bool",
_ => "unknown",
};
return Err(BlockError::Mcp(format!(
"call_tool '{tool_name}' on '{name}': arguments must be a JSON object \
(got {kind})"
)));
}
}
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, tool = %tool_name, "mcp call_tool on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let result = timeout(rpc_timeout, srv.call_tool(params))
.await
.map_err(|_| {
warn!(server = %name, tool = %tool_name, timeout = ?rpc_timeout, "mcp call_tool timed out");
// Fire-and-forget cancellation notification so the server can
// clean up the timed-out request. request_id 0 is a sentinel
// (we do not have the rmcp-internal ID at this call site).
// Pass None: we do not have the rmcp-internal request ID at
// this call site, and sending ID=0 risks matching a real
// in-flight request on a server that allocates from zero.
self.send_cancelled(name, None);
BlockError::Timeout(format!(
"call_tool '{tool_name}' on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, tool = %tool_name, error = %e, "mcp call_tool failed");
BlockError::Mcp(format!("call_tool '{tool_name}' on '{name}': {e}"))
})?;
serde_json::to_value(&result)
.map_err(|e| BlockError::Mcp(format!("serialize call_tool result: {e}")))
}
/// Cancel the named server and remove it from the manager.
///
/// The server is removed from the internal map **before** the cancel
/// round-trip begins, so a slow or failed cancel never leaves a
/// zombie entry behind. If graceful cancel exceeds `rpc_timeout`,
/// the service handle is dropped at the end of the match arm —
/// rmcp's `Drop` impl cancels the peer's cancellation token, which
/// terminates the internal task and closes the transport — and
/// `BlockError::Timeout` is returned.
///
/// The same `rpc_timeout` is reused here so callers have a single
/// knob governing every MCP round-trip (see `with_rpc_timeout`).
///
/// Callers may re-`connect` the same name safely after any outcome.
pub async fn disconnect(&mut self, name: &str) -> BlockResult<()> {
let Some(running) = self.servers.remove(name) else {
return Ok(());
};
let cancel_timeout = self.rpc_timeout;
match timeout(cancel_timeout, running.cancel()).await {
Ok(Ok(_)) => Ok(()),
Ok(Err(e)) => {
warn!(server = %name, error = %e, "mcp cancel failed");
Err(BlockError::Mcp(format!("cancel '{name}': {e}")))
}
Err(_) => {
warn!(server = %name, timeout = ?cancel_timeout, "mcp cancel timed out");
Err(BlockError::Timeout(format!(
"cancel '{name}' timed out after {cancel_timeout:?}"
)))
}
}
}
/// Cancel all managed servers.
///
/// Every server is disconnected regardless of individual failures.
/// The first error encountered is returned so shutdown can signal
/// a problem; **subsequent** errors are logged at `warn` level so
/// they are not silently discarded.
pub async fn disconnect_all(&mut self) -> BlockResult<()> {
let mut first_err: Option<BlockError> = None;
let names: Vec<String> = self.servers.keys().cloned().collect();
for name in names {
if let Err(e) = self.disconnect(&name).await {
if first_err.is_none() {
first_err = Some(e);
} else {
warn!(server = %name, error = %e, "disconnect failed during disconnect_all");
}
}
}
match first_err {
Some(e) => Err(e),
None => Ok(()),
}
}
/// Wire the handler Isle into this manager's `AgentBlockClientHandler`.
///
/// Must be called after both the `McpManager` and the `AsyncIsle` are
/// constructed. The handler Isle is used to dispatch Lua notification
/// callbacks (`on_progress` etc.) from the rmcp task thread.
///
/// Idempotent: a second call replaces the previous Isle reference.
pub fn set_handler_isle(&mut self, isle: Arc<AsyncIsle>) {
self.handler.handler_isle = Some(isle);
}
/// Wire the main Isle into the shared `AgentBlockClientHandler`.
///
/// Must be called after construction and before `connect` / `connect_http`
/// so that progress/log notification dispatchers can call user Lua callbacks
/// stored in the main Isle's globals (upvalue-safe path).
///
/// Also starts the bounded notification dispatch task (M-3: capacity-128 channel
/// that prevents unbounded memory growth from chatty notification sources).
///
/// Idempotent: a second call replaces the previous Isle reference and restarts
/// the dispatch task on the new channel.
pub fn set_main_isle(&mut self, isle: Arc<AsyncIsle>) {
self.handler.main_isle = Some(isle);
self.handler.start_dispatch_task();
}
/// Connect to an MCP server via Streamable HTTP transport.
///
/// `opts` may contain:
/// - `auth_header` (string): bearer-token authentication header value.
/// - `trace_context` (bool): if `true`, inject `__ab_obs` observability
/// context into `call_tool` arguments. Default: `false` (opt-in).
///
/// The handler Isle must be wired via `set_handler_isle` before calling
/// this method if `on_progress` callbacks are needed.
pub async fn connect_http(
&mut self,
name: &str,
url: &str,
opts: serde_json::Value,
) -> BlockResult<()> {
let trace_context = opts
.get("trace_context")
.and_then(|v| v.as_bool())
.unwrap_or(false);
self.handler.ensure_server(name);
self.handler.set_trace_context(name, trace_context);
// Same mutate-template → clone → reset dance as connect(); see the comment
// there for the rationale (per-connection server_name, shared registry Arc).
self.handler.server_name = Some(name.to_string());
let handler = self.handler.clone();
self.handler.server_name = None;
let running =
http::connect_http_transport(name, url, &opts, handler, self.rpc_timeout).await?;
self.servers.insert(name.to_string(), running);
Ok(())
}
/// Call `resources/list` and return resources as a JSON array.
///
/// Immutable receiver — usable under `RwLock::read` alongside concurrent RPCs.
pub async fn list_resources(&self, name: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp list_resources on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let resources = timeout(rpc_timeout, srv.list_all_resources())
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp list_resources timed out");
BlockError::Timeout(format!(
"list_resources '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp list_resources failed");
BlockError::Mcp(format!("list_resources '{name}': {e}"))
})?;
serde_json::to_value(&resources)
.map_err(|e| BlockError::Mcp(format!("serialize list_resources result: {e}")))
}
/// Call `resources/templates/list` and return resource templates as a JSON array.
///
/// Immutable receiver — usable under `RwLock::read` alongside concurrent RPCs.
pub async fn list_resource_templates(&self, name: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp list_resource_templates on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let templates = timeout(rpc_timeout, srv.list_all_resource_templates())
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp list_resource_templates timed out");
BlockError::Timeout(format!(
"list_resource_templates '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp list_resource_templates failed");
BlockError::Mcp(format!("list_resource_templates '{name}': {e}"))
})?;
serde_json::to_value(&templates)
.map_err(|e| BlockError::Mcp(format!("serialize list_resource_templates result: {e}")))
}
/// Send a `ping` keepalive to the named server and return the round-trip
/// latency in milliseconds.
///
/// Uses `send_request(ClientRequest::PingRequest(...))` — rmcp 1.4.0 has
/// no dedicated `Peer::ping()` method. Latency is measured with
/// `Instant::now()` immediately before the send and `elapsed()` immediately
/// after the `EmptyResult` is received (crux must_not_simplify).
///
/// Immutable receiver — usable under `RwLock::read` alongside concurrent RPCs.
pub async fn ping(&self, name: &str) -> BlockResult<u64> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp ping on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
// Clone Peer out of RunningService before awaiting to avoid holding
// the lock across the await point (K-4 / await-holding-lock).
let peer = srv.peer().clone();
let ping_req = ClientRequest::PingRequest(PingRequest::default());
// Measure latency from immediately before send to immediately after
// EmptyResult receipt (crux: must_not_simplify).
let started = Instant::now();
let response = timeout(rpc_timeout, peer.send_request(ping_req))
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp ping timed out");
BlockError::Timeout(format!("ping '{name}' timed out after {rpc_timeout:?}"))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp ping failed");
BlockError::Mcp(format!("ping '{name}': {e}"))
})?;
match response {
ServerResult::EmptyResult(_) => {
let latency_ms = started.elapsed().as_millis() as u64;
Ok(latency_ms)
}
other => {
warn!(server = %name, "mcp ping: unexpected response");
Err(BlockError::Mcp(format!(
"ping '{name}': unexpected response: {other:?}"
)))
}
}
}
/// Call `resources/read` and return the resource contents as JSON.
///
/// Immutable receiver — usable under `RwLock::read`.
pub async fn read_resource(&self, name: &str, uri: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, uri = %uri, "mcp read_resource on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let params = ReadResourceRequestParams::new(uri);
let result = timeout(rpc_timeout, srv.read_resource(params))
.await
.map_err(|_| {
warn!(server = %name, uri = %uri, timeout = ?rpc_timeout, "mcp read_resource timed out");
BlockError::Timeout(format!(
"read_resource '{uri}' on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, uri = %uri, error = %e, "mcp read_resource failed");
BlockError::Mcp(format!("read_resource '{uri}' on '{name}': {e}"))
})?;
serde_json::to_value(&result)
.map_err(|e| BlockError::Mcp(format!("serialize read_resource result: {e}")))
}
/// Call `resources/subscribe` to subscribe to updates for the given URI.
///
/// Immutable receiver — usable under `RwLock::read`.
pub async fn subscribe_resource(&self, name: &str, uri: &str) -> BlockResult<()> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, uri = %uri, "mcp subscribe_resource on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let params = SubscribeRequestParams::new(uri);
timeout(rpc_timeout, srv.subscribe(params))
.await
.map_err(|_| {
warn!(server = %name, uri = %uri, timeout = ?rpc_timeout, "mcp subscribe_resource timed out");
BlockError::Timeout(format!(
"subscribe_resource '{uri}' on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, uri = %uri, error = %e, "mcp subscribe_resource failed");
BlockError::Mcp(format!("subscribe_resource '{uri}' on '{name}': {e}"))
})
}
/// Call `resources/unsubscribe` to stop receiving updates for the given URI.
///
/// Immutable receiver — usable under `RwLock::read`.
pub async fn unsubscribe_resource(&self, name: &str, uri: &str) -> BlockResult<()> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, uri = %uri, "mcp unsubscribe_resource on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let params = UnsubscribeRequestParams::new(uri);
timeout(rpc_timeout, srv.unsubscribe(params))
.await
.map_err(|_| {
warn!(server = %name, uri = %uri, timeout = ?rpc_timeout, "mcp unsubscribe_resource timed out");
BlockError::Timeout(format!(
"unsubscribe_resource '{uri}' on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, uri = %uri, error = %e, "mcp unsubscribe_resource failed");
BlockError::Mcp(format!("unsubscribe_resource '{uri}' on '{name}': {e}"))
})
}
/// Call `prompts/list` and return prompts as a JSON array.
///
/// Immutable receiver — usable under `RwLock::read`.
pub async fn list_prompts(&self, name: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp list_prompts on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let prompts = timeout(rpc_timeout, srv.list_all_prompts())
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp list_prompts timed out");
BlockError::Timeout(format!(
"list_prompts '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp list_prompts failed");
BlockError::Mcp(format!("list_prompts '{name}': {e}"))
})?;
serde_json::to_value(&prompts)
.map_err(|e| BlockError::Mcp(format!("serialize list_prompts result: {e}")))
}
/// Call `prompts/get` with the given prompt name and optional arguments.
///
/// `args` must be a JSON Object or Null. Immutable receiver.
pub async fn get_prompt(
&self,
name: &str,
prompt_name: &str,
args: serde_json::Value,
) -> BlockResult<serde_json::Value> {
let mut params = GetPromptRequestParams::new(prompt_name.to_string());
match args {
serde_json::Value::Object(obj) => {
params = params.with_arguments(obj);
}
serde_json::Value::Null => {}
other => {
let kind = match other {
serde_json::Value::Array(_) => "array",
serde_json::Value::String(_) => "string",
serde_json::Value::Number(_) => "number",
serde_json::Value::Bool(_) => "bool",
_ => "unknown",
};
return Err(BlockError::Mcp(format!(
"get_prompt '{prompt_name}' on '{name}': args must be a JSON object \
(got {kind})"
)));
}
}
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, prompt = %prompt_name, "mcp get_prompt on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let result = timeout(rpc_timeout, srv.get_prompt(params))
.await
.map_err(|_| {
warn!(server = %name, prompt = %prompt_name, timeout = ?rpc_timeout, "mcp get_prompt timed out");
BlockError::Timeout(format!(
"get_prompt '{prompt_name}' on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, prompt = %prompt_name, error = %e, "mcp get_prompt failed");
BlockError::Mcp(format!("get_prompt '{prompt_name}' on '{name}': {e}"))
})?;
serde_json::to_value(&result)
.map_err(|e| BlockError::Mcp(format!("serialize get_prompt result: {e}")))
}
/// Call `completion/complete` with the given reference and argument.
///
/// `ref_json` must be a JSON Object with a `type` field of either
/// `"ref/prompt"` (with a `name` field) or `"ref/resource"` (with a `uri`
/// field). Any other `type` value is rejected with `BlockError::Mcp`.
///
/// `CompletionContext` is not exposed (scope-out per issue.md:51); it is
/// always sent as `None`. Immutable receiver — usable under `RwLock::read`.
pub async fn complete(
&self,
name: &str,
ref_json: serde_json::Value,
arg_name: &str,
arg_value: &str,
) -> BlockResult<serde_json::Value> {
// Build the Reference by dispatching on the `type` field at runtime.
// This is the crux: both prompt-ref and resource-ref paths must be
// preserved; collapsing or hardcoding one variant is forbidden.
let reference = match ref_json.get("type").and_then(|v| v.as_str()) {
Some("ref/prompt") => {
let prompt_name = ref_json.get("name").and_then(|v| v.as_str()).unwrap_or("");
Reference::for_prompt(prompt_name)
}
Some("ref/resource") => {
let uri = ref_json.get("uri").and_then(|v| v.as_str()).unwrap_or("");
Reference::for_resource(uri)
}
Some(kind) => {
warn!(server = %name, kind = ?kind, "mcp complete: invalid ref kind");
return Err(BlockError::Mcp(format!(
"complete on '{name}': invalid ref kind '{kind}', \
expected 'ref/prompt' or 'ref/resource'"
)));
}
None => {
warn!(server = %name, "mcp complete: ref missing 'type' field");
return Err(BlockError::Mcp(format!(
"complete on '{name}': ref object has no 'type' field"
)));
}
};
let params = CompleteRequestParams::new(
reference,
ArgumentInfo {
name: arg_name.to_string(),
value: arg_value.to_string(),
},
);
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp complete on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let rpc_timeout = self.rpc_timeout;
let result = timeout(rpc_timeout, srv.complete(params))
.await
.map_err(|_| {
warn!(server = %name, timeout = ?rpc_timeout, "mcp complete timed out");
BlockError::Timeout(format!(
"complete on '{name}' timed out after {rpc_timeout:?}"
))
})?
.map_err(|e| {
warn!(server = %name, error = %e, "mcp complete failed");
BlockError::Mcp(format!("complete on '{name}': {e}"))
})?;
serde_json::to_value(&result)
.map_err(|e| BlockError::Mcp(format!("serialize complete result: {e}")))
}
/// Return the server's `InitializeResult` serialized as JSON.
///
/// `peer_info()` is sync (no I/O). It returns `Some` after a successful
/// MCP handshake and `None` before initialization completes.
///
/// Immutable receiver — usable under `RwLock::read`.
pub fn server_info(&self, name: &str) -> BlockResult<serde_json::Value> {
let srv = self.servers.get(name).ok_or_else(|| {
warn!(server = %name, "mcp server_info on unknown server");
BlockError::Mcp(format!("no server named '{name}'"))
})?;
let info = srv.peer_info().ok_or_else(|| {
warn!(server = %name, "mcp server_info: server not yet initialized");
BlockError::Mcp(format!("server '{name}' not yet initialized"))
})?;
serde_json::to_value(info)
.map_err(|e| BlockError::Mcp(format!("serialize server_info '{name}': {e}")))
}
/// Send a `notifications/cancelled` to the named server.
///
/// This is a best-effort fire-and-forget: the notification is spawned in a
/// separate task so the caller is not blocked waiting for transport ack.
/// Errors from the peer send are logged at `warn` level and discarded —
/// the MCP spec does not require the server to ack cancellations (fire-and-forget
/// by design; warn-level logging is intentional).
///
/// `request_id` is `Some(id)` when the caller has captured the rmcp-internal
/// request ID, or `None` when the ID is not available (e.g. a timeout fired
/// before the ID was obtained). When `None` the notification is **skipped
/// entirely** to avoid accidentally matching request ID 0 on a server that
/// allocates IDs starting from zero.
pub fn send_cancelled(&self, name: &str, request_id: Option<i64>) {
// Skip silently when no ID is available; sending a bogus sentinel value
// risks matching a real in-flight request (rmcp allocates from 0).
let id = match request_id {
Some(id) => id,
None => return,
};
let Some(srv) = self.servers.get(name) else {
warn!(server = %name, "send_cancelled: unknown server, ignoring");
return;
};
// Clone the Peer out of the RunningService before spawning so we do
// not hold any lock across the await (await-holding-lock prevention).
let peer = srv.peer().clone();
let name_owned = name.to_string();
tokio::spawn(async move {
// CancelledNotification is non-exhaustive; use ::new() which sets
// method = CancelledNotificationMethod::default() and extensions = Default.
let notification = CancelledNotification::new(CancelledNotificationParam {
request_id: NumberOrString::Number(id),
reason: Some("cancelled".to_owned()),
});
if let Err(e) = peer.send_notification(notification.into()).await {
warn!(
server = %name_owned,
request_id = %id,
error = %e,
"send_cancelled: peer send_notification failed"
);
}
});
}
/// Notify the named server that the client's roots list has changed.
///
/// Sends a `notifications/roots/list_changed` notification to the server as a
/// fire-and-forget operation. The server may respond by issuing a new
/// `roots/list` request.
///
/// # Arguments
/// - `name` — the name of the server connection to notify.
///
/// # Errors
/// None propagated. Unknown server is logged at warn level and silently
/// ignored. Send failures inside the spawned task are also logged at warn
/// level and discarded.
pub fn notify_roots_list_changed(&self, name: &str) {
let Some(srv) = self.servers.get(name) else {
warn!(server = %name, "notify_roots_list_changed: unknown server, ignoring");
return;
};
// Clone the Peer out of the RunningService before spawning so we do
// not hold any lock across the await (await-holding-lock prevention).
let peer = srv.peer().clone();
let name_owned = name.to_string();
tokio::spawn(async move {
// RootsListChangedNotification has no params; Default::default() is
// sufficient (method = RootsListChangedNotificationMethod::default(),
// extensions = Default).
let notification = RootsListChangedNotification::default();
if let Err(e) = peer.send_notification(notification.into()).await {
warn!(
server = %name_owned,
error = %e,
"notify_roots_list_changed: peer send_notification failed"
);
}
});
}
}
impl Default for McpManager {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn new_manager_is_empty() {
let mgr = McpManager::new();
assert!(mgr.servers.is_empty());
}
#[tokio::test]
async fn with_rpc_timeout_rejects_zero() {
// A ZERO timeout would make every `tokio::time::timeout` fire
// immediately, silently turning every RPC into a timeout error.
// For an autonomous agent that is a catastrophic failure mode —
// the misconfiguration must surface at construction, not be
// swallowed at the first MCP call.
let err = match McpManager::with_rpc_timeout(Duration::ZERO) {
Ok(_) => panic!("Duration::ZERO must be rejected"),
Err(e) => e,
};
assert!(
err.to_string().contains("rpc_timeout must be > 0"),
"unexpected error: {err}",
);
}
#[tokio::test]
async fn with_rpc_timeout_accepts_positive() {
let mgr = match McpManager::with_rpc_timeout(Duration::from_millis(1)) {
Ok(m) => m,
Err(e) => panic!("positive timeout must be accepted: {e}"),
};
assert!(mgr.servers.is_empty());
}
#[tokio::test]
async fn disconnect_nonexistent_is_ok() {
let mut mgr = McpManager::new();
assert!(mgr.disconnect("ghost").await.is_ok());
}
#[tokio::test]
async fn call_unknown_server_returns_error() {
// `let mgr =` (not `let mut`) also asserts at compile time that
// `call_tool` takes `&self`. Reverting to `&mut self` would break
// this call site.
let mgr = McpManager::new();
let res = mgr.call_tool("none", "dummy", serde_json::json!({})).await;
assert!(res.is_err());
}
#[tokio::test]
async fn list_tools_takes_shared_receiver() {
// Mirror guard for `list_tools(&self)`.
let mgr = McpManager::new();
let res = mgr.list_tools("none").await;
assert!(res.is_err());
}
#[tokio::test]
async fn disconnect_all_empties_map() {
let mut mgr = McpManager::new();
mgr.disconnect_all()
.await
.expect("disconnect_all on empty manager should succeed");
assert!(mgr.servers.is_empty());
}
#[tokio::test]
async fn call_tool_rejects_non_object_arguments() {
// Argument validation runs before the server lookup, so an
// array/scalar is rejected even without a live server.
let mgr = McpManager::new();
for bad in [
serde_json::json!([1, 2, 3]),
serde_json::json!("string"),
serde_json::json!(42),
serde_json::json!(true),
] {
let res = mgr.call_tool("anything", "dummy", bad.clone()).await;
let err = res.expect_err("non-object args must error");
let msg = err.to_string();
assert!(
msg.contains("arguments must be a JSON object"),
"unexpected error for {bad}: {msg}",
);
}
}
#[tokio::test]
async fn call_tool_accepts_null_arguments_as_absent() {
// Null is the documented "no arguments" form. It must pass the
// validation gate (and fail at the server-lookup step instead).
let mgr = McpManager::new();
let res = mgr
.call_tool("ghost", "dummy", serde_json::Value::Null)
.await;
let err = res.expect_err("expected no-server error, not arg-shape error");
assert!(
err.to_string().contains("no server named"),
"Null args should reach the lookup step: {err}",
);
}
}
/// Concurrency contract tests.
///
/// These tests nail down the **intended** concurrency model of `McpManager`
/// regardless of what rmcp does internally:
///
/// 1. `list_tools` / `call_tool` are `&self` ⇒ usable under `RwLock::read`.
/// 2. Two concurrent RPCs against the **same** server must overlap in
/// wall time (they do not serialize at the `McpManager` layer).
/// 3. The lock primitive is `RwLock`, not `Mutex` — concurrent reads
/// coexist and a write blocks while any read is held.
///
/// If rmcp changes its `Peer` concurrency contract, or if this module is
/// refactored back to `Mutex` / `&mut self`, these tests break loudly.
#[cfg(test)]
mod concurrency_tests {
use super::*;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::RwLock;
use rmcp::{
model::{CallToolRequestParams, CallToolResult, Content, ServerCapabilities, ServerInfo},
service::{MaybeSendFuture, RequestContext},
ErrorData as McpError, RoleServer, ServerHandler, ServiceExt,
};
/// A server that sleeps `delay` before every `tools/call`.
/// Used to observe whether two concurrent `call_tool` invocations
/// overlap (≈ `delay`) or serialize (≈ `2 × delay`).
#[derive(Clone)]
struct SlowToolServer {
delay: Duration,
}
impl ServerHandler for SlowToolServer {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
}
fn call_tool(
&self,
_params: CallToolRequestParams,
_ctx: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<CallToolResult, McpError>> + MaybeSendFuture + '_
{
let delay = self.delay;
async move {
tokio::time::sleep(delay).await;
Ok(CallToolResult::success(vec![Content::text("ok")]))
}
}
}
/// Spawn an in-process `SlowToolServer` wired to the given `McpManager`
/// via a `tokio::io::duplex` pair. Bypasses `TokioChildProcess` so the
/// test does not depend on an external binary.
async fn attach_slow_server(mgr: &mut McpManager, name: &str, delay: Duration) {
let (server_side, client_side) = tokio::io::duplex(8192);
let server = SlowToolServer { 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);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn concurrent_call_tool_same_server_does_not_serialize() {
let delay = Duration::from_millis(300);
let mgr = Arc::new(RwLock::new(McpManager::new()));
attach_slow_server(&mut *mgr.write().await, "slow", delay).await;
let start = Instant::now();
let a = {
let mgr = Arc::clone(&mgr);
async move {
mgr.read()
.await
.call_tool("slow", "slow_tool", serde_json::json!({}))
.await
}
};
let b = {
let mgr = Arc::clone(&mgr);
async move {
mgr.read()
.await
.call_tool("slow", "slow_tool", serde_json::json!({}))
.await
}
};
let (r1, r2) = tokio::join!(a, b);
let elapsed = start.elapsed();
r1.expect("first call succeeds");
r2.expect("second call succeeds");
// Serialized path would take ≥ 2×delay = 600ms. Parallel path
// should land near `delay` (300ms). Fail with generous margin if
// serialization is observed.
let serialized_budget = delay * 2 - Duration::from_millis(80);
assert!(
elapsed < serialized_budget,
"concurrent call_tool appears serialized: elapsed={:?}, serialized_budget={:?}",
elapsed,
serialized_budget,
);
}
#[tokio::test]
async fn two_reads_coexist_on_rwlock() {
// Structural check: confirms `RwLock` (not `Mutex`) is the primitive.
// A revert to `tokio::sync::Mutex` would drop `try_read` and break
// this test at compile time.
let mgr = Arc::new(RwLock::new(McpManager::new()));
let _g1 = mgr.read().await;
assert!(
mgr.try_read().is_ok(),
"RwLock rejected a concurrent second read guard",
);
}
#[tokio::test]
async fn write_blocks_while_read_held() {
let mgr = Arc::new(RwLock::new(McpManager::new()));
let _g1 = mgr.read().await;
assert!(
mgr.try_write().is_err(),
"write lock acquired while a read guard was held",
);
}
/// A server that always returns `CallToolResult::error`, i.e.
/// `isError = true`. Used to lock down pass-through semantics.
#[derive(Clone)]
struct IsErrorServer;
impl ServerHandler for IsErrorServer {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
}
async fn call_tool(
&self,
_params: CallToolRequestParams,
_ctx: RequestContext<RoleServer>,
) -> Result<CallToolResult, McpError> {
Ok(CallToolResult::error(vec![Content::text("tool blew up")]))
}
}
async fn attach_is_error_server(mgr: &mut McpManager, name: &str) {
let (server_side, client_side) = tokio::io::duplex(8192);
tokio::spawn(async move {
if let Ok(running) = IsErrorServer.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);
}
#[tokio::test]
async fn is_error_is_passed_through_in_ok_branch() {
// MCP spec: tool-execution errors come back as a successful RPC
// with `isError=true`. `call_tool` must return `Ok(..)` and
// preserve `isError` in the serialized JSON so the Lua bridge
// (and ultimately the LLM) sees it.
let mut mgr = McpManager::new();
attach_is_error_server(&mut mgr, "boom").await;
let val = mgr
.call_tool("boom", "explode", serde_json::json!({}))
.await
.expect("RPC succeeds even when isError=true");
assert_eq!(
val.get("isError").and_then(|v| v.as_bool()),
Some(true),
"isError must be preserved in Ok branch: {val}",
);
let content = val.get("content").and_then(|v| v.as_array()).cloned();
assert!(
content.as_ref().map(|c| !c.is_empty()).unwrap_or(false),
"content blocks must be forwarded alongside isError: {val:?}",
);
}
}
/// Rich client tests: resources, prompts, progress, and concurrent access.
///
/// Uses in-process duplex servers (same pattern as `concurrency_tests`).
#[cfg(test)]
mod rich_tests {
use super::*;
use rmcp::{
model::{
CompleteRequestParams, CompleteResult, CompletionInfo, GetPromptRequestParams,
GetPromptResult, ListPromptsResult, ListResourceTemplatesResult, ListResourcesResult,
NumberOrString, PaginatedRequestParams, ProgressNotificationParam, ProgressToken,
Prompt, PromptMessage, PromptMessageRole, RawResource, RawResourceTemplate,
ReadResourceRequestParams, ReadResourceResult, Reference, ResourceContents,
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(
RawResource::new("file:///hello.txt", "hello.txt"),
None,
),
rmcp::model::Resource::new(
RawResource::new("file:///world.txt", "world.txt"),
None,
),
];
std::future::ready(Ok(ListResourcesResult::with_all_items(resources)))
}
fn read_resource(
&self,
request: ReadResourceRequestParams,
_ctx: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ReadResourceResult, McpError>> + MaybeSendFuture + '_
{
let uri = request.uri.clone();
let text = format!("content of {uri}");
std::future::ready(Ok(ReadResourceResult::new(vec![ResourceContents::text(
text, uri,
)])))
}
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(
RawResourceTemplate::new("file:///{name}.txt", "file-template"),
None,
),
rmcp::model::ResourceTemplate::new(
RawResourceTemplate::new("db:///{table}/{id}", "db-template"),
None,
),
];
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<GetPromptResult, McpError>> + MaybeSendFuture + '_
{
let name = request.name.clone();
let message = PromptMessage::new_text(
PromptMessageRole::User,
format!("This is the '{name}' prompt."),
);
std::future::ready(Ok(GetPromptResult::new(vec![message])))
}
}
// ── 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"),
};
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.
#[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 {
progress_token: ProgressToken(NumberOrString::String("tok-1".into())),
progress: 0.5,
total: Some(1.0),
message: None,
};
// 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 v1.4: `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 v1.4: `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::CallToolResult, 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::Content::text(format!("roots:{count}:{first_uri}")),
]))
}
}
/// 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::CallToolResult, McpError> {
use rmcp::model::{
CreateElicitationRequestParams, ElicitationSchema, PrimitiveSchema, StringSchema,
};
use std::collections::BTreeMap;
// Build a minimal Form variant with one string property.
let mut props = BTreeMap::new();
props.insert(
"name".to_string(),
PrimitiveSchema::String(StringSchema::new()),
);
let schema = ElicitationSchema::new(props);
let req = CreateElicitationRequestParams::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",
};
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::Content::text(format!("elicitation:{action_str}:{content_str}")),
]))
}
}
/// 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::CallToolResult, McpError> {
use rmcp::model::CreateElicitationRequestParams;
// Issue a Url variant — client must always Decline without Lua dispatch.
let req = CreateElicitationRequestParams::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",
};
Ok(rmcp::model::CallToolResult::success(vec![
rmcp::model::Content::text(format!("url_elicitation:{action_str}")),
]))
}
}
/// 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}"
);
}
}