pmcp 2.17.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
//! MCP client implementation.

use crate::error::{Error, Result};
use crate::shared::{
    EnhancedMiddlewareChain, MiddlewareContext, Protocol, ProtocolOptions, Transport,
};
use crate::types::tasks::{
    resolve_poll_interval, CancelTaskRequest, CancelTaskResult, CreateTaskResult,
    GetTaskPayloadRequest, GetTaskRequest, GetTaskResult, ListTasksRequest, ListTasksResult, Task,
    TaskMetadata, TaskPollDecision, TaskStatus, MIN_POLL_MS,
};
use crate::types::{
    CallToolRequest, CallToolResult, CancelledNotification, ClientCapabilities, ClientNotification,
    ClientRequest, CompleteRequest, CompleteResult, CreateMessageParams, CreateMessageResult,
    GetPromptRequest, GetPromptResult, Implementation, InitializeRequest, InitializeResult,
    ListPromptsRequest, ListPromptsResult, ListResourceTemplatesRequest,
    ListResourceTemplatesResult, ListResourcesRequest, ListResourcesResult, ListToolsRequest,
    ListToolsResult, LoggingLevel, Notification, ProgressNotification, PromptInfo,
    ReadResourceRequest, ReadResourceResult, Request, RequestId, ResourceInfo, ResourceTemplate,
    ServerCapabilities, SubscribeRequest, ToolInfo, UnsubscribeRequest,
};
use std::collections::HashMap;
use std::sync::Arc;
use uuid::Uuid;

#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::{mpsc, oneshot, RwLock};

#[cfg(target_arch = "wasm32")]
use futures::SinkExt;
#[cfg(target_arch = "wasm32")]
use futures_channel::{mpsc, oneshot};
#[cfg(target_arch = "wasm32")]
use futures_locks::RwLock;

#[cfg(all(not(target_arch = "wasm32"), feature = "http-client"))]
pub mod auth;
pub mod host;
pub mod http_logging_middleware;
pub mod http_middleware;
#[cfg(all(not(target_arch = "wasm32"), feature = "oauth"))]
pub mod oauth;
pub mod oauth_middleware;
mod options;
pub mod transport;

pub use options::ClientOptions;

pub use host::{
    ApprovalDecision, ClientHostRegistry, HostElicitationHandler, HostSamplingHandler,
    PreflightApproval, RootsProvider, SamplingResultReview,
};

/// Response from a task-augmented `tools/call`.
///
/// When calling [`Client::call_tool_with_task`], the server may return either
/// an async task (poll with `tasks/get`) or a synchronous result.
#[derive(Debug, Clone)]
pub enum ToolCallResponse {
    /// The server returned a synchronous result (no task created).
    Result(CallToolResult),
    /// The server created an async task. Poll with [`Client::tasks_get`]
    /// until the task reaches a terminal status, then call
    /// [`Client::tasks_result`] to get the final `CallToolResult`.
    Task(Task),
}

/// Options controlling [`Client::wait_for_task`] polling.
///
/// A caller who holds a [`TaskMetadata`] (e.g. from
/// [`CallToolResult::related_task`](crate::types::CallToolResult::related_task))
/// composes options directly via [`WaitForTaskOptions::from_metadata`] /
/// [`From<TaskMetadata>`] — no hand-copying of poll fields.
#[derive(Debug, Clone, Default)]
pub struct WaitForTaskOptions {
    /// Override polling interval, in **milliseconds**. When `None`, the
    /// task-reported `pollInterval` (then a built-in default) is used. The
    /// effective interval is clamped to a small floor so a zero value cannot
    /// hot-spin the poll loop.
    pub poll_interval: Option<u64>,
    /// Maximum total time to poll before returning a timeout error, in
    /// **seconds**. When `None`, polling continues until the task is terminal
    /// (or enters `input_required`, which surfaces an error immediately — see
    /// [`Client::wait_for_task`]).
    pub max_poll_duration_secs: Option<u64>,
}

impl WaitForTaskOptions {
    /// Build options from a [`TaskMetadata`], copying its poll fields verbatim.
    pub fn from_metadata(meta: &TaskMetadata) -> Self {
        Self {
            poll_interval: meta.poll_interval,
            max_poll_duration_secs: meta.max_poll_duration_secs,
        }
    }

    /// Fill any unset fields from `meta`; existing `self` values take precedence.
    #[must_use]
    pub fn or_from_metadata(mut self, meta: &TaskMetadata) -> Self {
        self.poll_interval = self.poll_interval.or(meta.poll_interval);
        self.max_poll_duration_secs = self.max_poll_duration_secs.or(meta.max_poll_duration_secs);
        self
    }
}

impl From<TaskMetadata> for WaitForTaskOptions {
    fn from(meta: TaskMetadata) -> Self {
        Self::from_metadata(&meta)
    }
}

/// MCP client for connecting to servers.
pub struct Client<T: Transport> {
    transport: Arc<RwLock<T>>,
    protocol: Arc<RwLock<Protocol>>,
    middleware_chain: Arc<RwLock<EnhancedMiddlewareChain>>,
    capabilities: Option<ClientCapabilities>,
    server_capabilities: Option<ServerCapabilities>,
    server_version: Option<Implementation>,
    instructions: Option<String>,
    initialized: bool,
    info: Implementation,
    notification_tx: Option<mpsc::Sender<Notification>>,
    active_requests: Arc<RwLock<HashMap<RequestId, oneshot::Sender<()>>>>,
    options: ClientOptions,
    /// Registered host handlers answering inbound server -> client requests
    /// (sampling / elicitation / roots). Immutable after construction.
    host_registry: crate::client::host::ClientHostRegistry,
}

impl<T: Transport> std::fmt::Debug for Client<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Client")
            .field("transport", &"<Arc<RwLock<Transport>>>")
            .field("protocol", &"<Arc<RwLock<Protocol>>>")
            .field("capabilities", &self.capabilities)
            .field("server_capabilities", &self.server_capabilities)
            .field("initialized", &self.initialized)
            .field("info", &self.info)
            .field("host_registry", &self.host_registry)
            .finish()
    }
}

impl<T: Transport> Client<T> {
    /// Create a new client with the given transport.
    ///
    /// Uses default client information with the name "pmcp-client" and the
    /// current crate version.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{Client, StdioTransport};
    ///
    /// let transport = StdioTransport::new();
    /// let client = Client::new(transport);
    /// ```
    pub fn new(transport: T) -> Self {
        Self::with_info(
            transport,
            Implementation::new("pmcp-client", env!("CARGO_PKG_VERSION")),
        )
    }

    /// Create a new client with custom info.
    ///
    /// Allows specifying custom client name and version information that will
    /// be sent to the server during initialization.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{Client, StdioTransport, Implementation};
    ///
    /// let transport = StdioTransport::new();
    /// let client_info = Implementation::new("my-custom-client", "2.1.0");
    /// let client = Client::with_info(transport, client_info);
    /// ```
    pub fn with_info(transport: T, client_info: Implementation) -> Self {
        Self {
            transport: Arc::new(RwLock::new(transport)),
            protocol: Arc::new(RwLock::new(Protocol::new(ProtocolOptions::default()))),
            middleware_chain: Arc::new(RwLock::new(EnhancedMiddlewareChain::new())),
            capabilities: None,
            server_capabilities: None,
            server_version: None,
            instructions: None,
            initialized: false,
            info: client_info,
            notification_tx: None,
            active_requests: Arc::new(RwLock::new(HashMap::new())),
            options: ClientOptions::default(),
            host_registry: crate::client::host::ClientHostRegistry::default(),
        }
    }

    /// Create a new client with custom protocol options.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{Client, StdioTransport, Implementation};
    /// use pmcp::shared::ProtocolOptions;
    ///
    /// // Custom options for high-throughput scenarios
    /// let options = ProtocolOptions {
    ///     enforce_strict_capabilities: false,
    ///     debounced_notification_methods: vec![
    ///         "notifications/progress".to_string(),
    ///         "notifications/message".to_string(),
    ///     ],
    /// };
    ///
    /// let transport = StdioTransport::new();
    /// let client_info = Implementation::new("high-throughput-client", "1.0.0");
    ///
    /// let client = Client::with_options(transport, client_info, options);
    /// ```
    pub fn with_options(
        transport: T,
        client_info: Implementation,
        options: ProtocolOptions,
    ) -> Self {
        Self {
            transport: Arc::new(RwLock::new(transport)),
            protocol: Arc::new(RwLock::new(Protocol::new(options))),
            middleware_chain: Arc::new(RwLock::new(EnhancedMiddlewareChain::new())),
            capabilities: None,
            server_capabilities: None,
            server_version: None,
            instructions: None,
            initialized: false,
            info: client_info,
            notification_tx: None,
            active_requests: Arc::new(RwLock::new(HashMap::new())),
            options: ClientOptions::default(),
            host_registry: crate::client::host::ClientHostRegistry::default(),
        }
    }

    /// Construct a client with caller-supplied [`ClientOptions`].
    ///
    /// Mirrors [`Self::new`] but wires in a [`ClientOptions`] value so that
    /// [`Self::list_all_tools`] / [`Self::list_all_prompts`] / etc. honour a
    /// custom `max_iterations` cap.
    ///
    /// ## `ClientBuilder` parity
    ///
    /// [`ClientBuilder`] does not currently expose a `.client_options()` setter.
    /// If you need a custom [`ClientOptions`], construct the client via
    /// [`Self::with_client_options`] directly.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(transport: T) -> pmcp::Result<()> {
    /// use pmcp::{Client, ClientOptions};
    ///
    /// let opts = ClientOptions::default().with_max_iterations(50);
    /// let _client = Client::with_client_options(transport, opts);
    /// # Ok(()) }
    /// ```
    pub fn with_client_options(transport: T, options: ClientOptions) -> Self {
        Self {
            transport: Arc::new(RwLock::new(transport)),
            protocol: Arc::new(RwLock::new(Protocol::new(ProtocolOptions::default()))),
            middleware_chain: Arc::new(RwLock::new(EnhancedMiddlewareChain::new())),
            capabilities: None,
            server_capabilities: None,
            server_version: None,
            instructions: None,
            initialized: false,
            info: Implementation::default(),
            notification_tx: None,
            active_requests: Arc::new(RwLock::new(HashMap::new())),
            options,
            host_registry: crate::client::host::ClientHostRegistry::default(),
        }
    }

    /// Initialize the connection with the server.
    ///
    /// Performs the MCP initialization handshake, negotiating capabilities and
    /// receiving server information. This must be called before using other
    /// client methods.
    ///
    /// # Host capabilities are registry-derived (`sampling` / `elicitation` / `roots`)
    ///
    /// The three host-side capability fields — `sampling`, `elicitation`, and
    /// `roots` — are **derived from the handlers registered on
    /// [`ClientBuilder`]**, not from the value passed here. If no matching host
    /// handler is registered, the corresponding field is forced to `None` on the
    /// wire even when the caller set it (the anti-capability-lie rule: a client
    /// must not advertise a host capability it cannot service). Register handlers
    /// via [`ClientBuilder::on_sampling`], [`ClientBuilder::on_elicitation`], and
    /// [`ClientBuilder::on_roots`] to advertise these capabilities. When a
    /// handler *is* registered, any caller-configured detail for that field
    /// (e.g. `roots.list_changed`) is preserved. All other capability fields
    /// (`tasks`, `experimental`, ...) pass through unchanged.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    ///
    /// let capabilities = ClientCapabilities::default();
    /// let server_info = client.initialize(capabilities).await?;
    ///
    /// println!("Server: {} v{}",
    ///          server_info.server_info.name,
    ///          server_info.server_info.version);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is already initialized
    /// - The server rejects the initialization
    /// - Communication with the server fails
    pub async fn initialize(
        &mut self,
        mut capabilities: ClientCapabilities,
    ) -> Result<InitializeResult> {
        if self.initialized {
            return Err(Error::InvalidState("Client already initialized".into()));
        }

        // HOST-05: make the three host capability fields reflect the registry
        // (registry-authoritative anti-capability-lie) before advertising them.
        self.derive_host_capabilities(&mut capabilities);

        self.capabilities = Some(capabilities.clone());

        // Send initialize request
        let request = Request::Client(Box::new(ClientRequest::Initialize(InitializeRequest {
            protocol_version: crate::types::LATEST_PROTOCOL_VERSION.to_string(),
            capabilities,
            client_info: self.info.clone(),
        })));

        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        // Parse initialize result
        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                let init_result = serde_json::from_value::<InitializeResult>(result)
                    .map_err(|e| Error::parse(format!("Invalid initialize result: {e}")))?;

                // Validate protocol version
                if !crate::types::SUPPORTED_PROTOCOL_VERSIONS
                    .contains(&init_result.protocol_version.as_str())
                {
                    return Err(Error::protocol_msg(format!(
                        "Server protocol version {} not supported",
                        init_result.protocol_version
                    )));
                }

                self.server_capabilities = Some(init_result.capabilities.clone());
                self.server_version = Some(init_result.server_info.clone());
                self.instructions.clone_from(&init_result.instructions);
                self.initialized = true;

                // Send initialized notification
                self.send_notification(Notification::Client(ClientNotification::Initialized))
                    .await?;

                Ok(init_result)
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Apply the HOST-05 registry-authoritative rule to the three host
    /// capability fields (`sampling`/`elicitation`/`roots`), leaving every other
    /// field (`tasks`, `experimental`, ...) untouched.
    ///
    /// Per field:
    /// - **Handler absent** => force `None` (locked anti-capability-lie: a
    ///   caller-set value with no registered handler is discarded, closing the
    ///   spoofing hole where a client advertises a host capability it cannot
    ///   actually service).
    /// - **Handler present, caller left `None`** => insert `Some(default())`.
    /// - **Handler present, caller configured detail** => preserve the caller's
    ///   value unchanged (keeps configured sampling tool support / roots
    ///   `list_changed` / elicitation modes).
    ///
    /// There is deliberately no independent public setter for these three
    /// fields — advertisement is derived, never independently assertable.
    fn derive_host_capabilities(&self, capabilities: &mut ClientCapabilities) {
        // Apply the HOST-05 rule to one capability field:
        // - handler absent (`registered == false`) => force `None`,
        // - handler present, caller left `None` => insert `Some(default())`,
        // - handler present, caller configured detail => leave untouched.
        fn sync_cap<C: Default>(slot: &mut Option<C>, registered: bool) {
            if !registered {
                *slot = None;
            } else if slot.is_none() {
                *slot = Some(C::default());
            }
        }

        sync_cap(
            &mut capabilities.sampling,
            self.host_registry.sampling.is_some(),
        );
        sync_cap(
            &mut capabilities.elicitation,
            self.host_registry.elicitation.is_some(),
        );
        sync_cap(&mut capabilities.roots, self.host_registry.roots.is_some());
    }

    /// Get server capabilities after initialization.
    pub fn get_server_capabilities(&self) -> Option<&ServerCapabilities> {
        self.server_capabilities.as_ref()
    }

    /// Get server version information after initialization.
    pub fn get_server_version(&self) -> Option<&Implementation> {
        self.server_version.as_ref()
    }

    /// Get server instructions after initialization.
    pub fn get_instructions(&self) -> Option<&str> {
        self.instructions.as_deref()
    }

    /// Send a ping to the server.
    pub async fn ping(&self) -> Result<()> {
        self.ensure_initialized()?;
        let request = Request::Client(Box::new(ClientRequest::Ping));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(_) => Ok(()),
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Set the logging level on the server.
    pub async fn set_logging_level(&self, level: LoggingLevel) -> Result<()> {
        self.ensure_initialized()?;
        self.assert_capability("logging", "logging/setLevel")?;

        let request = Request::Client(Box::new(ClientRequest::SetLoggingLevel { level }));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(_) => Ok(()),
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// List available tools.
    ///
    /// Retrieves information about all tools available on the server, including
    /// their names, descriptions, and input schemas.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // List all tools
    /// let tools = client.list_tools(None).await?;
    /// for tool in tools.tools {
    ///     println!("Tool: {} - {}",
    ///              tool.name,
    ///              tool.description.unwrap_or_else(|| "No description".to_string()));
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Arguments
    ///
    /// * `cursor` - Optional pagination cursor for retrieving additional results
    pub async fn list_tools(&self, cursor: Option<String>) -> Result<ListToolsResult> {
        self.ensure_initialized()?;
        self.assert_capability("tools", "tools/list")?;

        let request = Request::Client(Box::new(ClientRequest::ListTools(ListToolsRequest {
            cursor,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Call a tool.
    ///
    /// Invokes a server-provided tool with the specified name and arguments.
    /// The server must have declared the tool via the tools capability during initialization.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the tool to call
    /// * `arguments` - JSON value containing the tool's arguments
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    /// use serde_json::json;
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Call a simple tool with no arguments
    /// let result = client.call_tool(
    ///     "list_files".to_string(),
    ///     json!({})
    /// ).await?;
    ///
    /// // Call a tool with specific arguments
    /// let search_result = client.call_tool(
    ///     "search".to_string(),
    ///     json!({
    ///         "query": "rust programming",
    ///         "limit": 10
    ///     })
    /// ).await?;
    ///
    /// // Tools can return structured data
    /// if let Some(content) = result.content.first() {
    ///     match content {
    ///         pmcp::Content::Text { text } => {
    ///             println!("Tool result: {}", text);
    ///         }
    ///         _ => println!("Non-text tool result"),
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support tools
    /// - The tool name doesn't exist
    /// - The arguments are invalid for the tool
    /// - Network or protocol errors occur
    pub async fn call_tool(
        &self,
        name: String,
        arguments: serde_json::Value,
    ) -> Result<CallToolResult> {
        self.ensure_initialized()?;
        self.assert_capability("tools", "tools/call")?;

        let request = Request::Client(Box::new(ClientRequest::CallTool(CallToolRequest {
            name,
            arguments,
            _meta: None,
            task: None,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    // =========================================================================
    // MCP Tasks (2025-11-25)
    // =========================================================================

    /// Call a tool with task augmentation.
    ///
    /// Sends a `tools/call` request with the `task` field set, signaling to the
    /// server that this client supports async task polling. The server may return
    /// either a `CreateTaskResult` (async task created) or a `CallToolResult`
    /// (sync result) depending on the tool's `taskSupport` declaration.
    ///
    /// Use [`call_tool`](Self::call_tool) instead if you don't need task support.
    ///
    /// # Returns
    ///
    /// - `Ok(ToolCallResponse::Task(task))` if the server created an async task.
    ///   Poll with [`tasks_get`](Self::tasks_get) until the task reaches a
    ///   terminal status.
    /// - `Ok(ToolCallResponse::Result(result))` if the server returned the
    ///   result synchronously.
    pub async fn call_tool_with_task(
        &self,
        name: String,
        arguments: serde_json::Value,
    ) -> Result<ToolCallResponse> {
        self.ensure_initialized()?;
        self.assert_capability("tools", "tools/call")?;

        let request = Request::Client(Box::new(ClientRequest::CallTool(CallToolRequest {
            name,
            arguments,
            _meta: None,
            task: Some(serde_json::json!({})),
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                // Try CreateTaskResult first (more specific), fall back to CallToolResult.
                // This avoids brittle key-name duck-typing.
                if let Ok(task_result) = serde_json::from_value::<CreateTaskResult>(result.clone())
                {
                    Ok(ToolCallResponse::Task(task_result.task))
                } else {
                    let tool_result: CallToolResult =
                        serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))?;
                    Ok(ToolCallResponse::Result(tool_result))
                }
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Call a tool with task augmentation AND custom request `_meta`.
    ///
    /// Identical to [`call_tool_with_task`](Self::call_tool_with_task) except the
    /// request carries the supplied [`RequestMeta`](crate::types::protocol::RequestMeta)
    /// as `_meta`, so namespaced/guard state (attached via
    /// [`RequestMeta::with_meta`](crate::types::protocol::RequestMeta::with_meta))
    /// travels alongside the task augmentation in a single `tools/call`.
    ///
    /// Passing an empty `RequestMeta` behaves like `call_tool_with_task`.
    ///
    /// # Returns
    ///
    /// - `Ok(ToolCallResponse::Task(task))` if the server created an async task.
    /// - `Ok(ToolCallResponse::Result(result))` if the server returned the
    ///   result synchronously.
    ///
    /// # Errors
    ///
    /// Returns an error if the client is not initialized, the server does not
    /// support tools, or a network/protocol error occurs.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use pmcp::{Client, types::protocol::RequestMeta};
    /// # async fn run(client: &Client<pmcp::StdioTransport>) -> pmcp::Result<()> {
    /// let meta = RequestMeta::new()
    ///     .with_meta("io.modelcontextprotocol/related-task", serde_json::json!({"taskId": "t-1"}));
    /// let resp = client
    ///     .call_tool_with_task_and_meta("member".to_string(), serde_json::json!({}), meta)
    ///     .await?;
    /// # let _ = resp;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn call_tool_with_task_and_meta(
        &self,
        name: String,
        arguments: serde_json::Value,
        meta: crate::types::protocol::RequestMeta,
    ) -> Result<ToolCallResponse> {
        self.ensure_initialized()?;
        self.assert_capability("tools", "tools/call")?;

        let request = Request::Client(Box::new(ClientRequest::CallTool(CallToolRequest {
            name,
            arguments,
            _meta: Some(meta),
            task: Some(serde_json::json!({})),
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                // Try CreateTaskResult first (more specific), fall back to CallToolResult.
                if let Ok(task_result) = serde_json::from_value::<CreateTaskResult>(result.clone())
                {
                    Ok(ToolCallResponse::Task(task_result.task))
                } else {
                    let tool_result: CallToolResult =
                        serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))?;
                    Ok(ToolCallResponse::Result(tool_result))
                }
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Call a tool (non-task) with custom request `_meta`.
    ///
    /// Identical to [`call_tool`](Self::call_tool) except the request carries the
    /// supplied [`RequestMeta`](crate::types::protocol::RequestMeta) as `_meta`.
    /// Namespaced/guard state travels via
    /// [`RequestMeta::with_meta`](crate::types::protocol::RequestMeta::with_meta)
    /// and is visible to the server tool handler through `extra.request_meta`.
    ///
    /// Passing an empty `RequestMeta` behaves like `call_tool`.
    ///
    /// # Errors
    ///
    /// Returns an error if the client is not initialized, the server does not
    /// support tools, the tool name does not exist, or a network/protocol error
    /// occurs.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use pmcp::{Client, types::protocol::RequestMeta};
    /// # async fn run(client: &Client<pmcp::StdioTransport>) -> pmcp::Result<()> {
    /// let meta = RequestMeta::new().with_meta("x-pmcp-team-depth", serde_json::json!(1));
    /// let result = client
    ///     .call_tool_with_meta("echo".to_string(), serde_json::json!({}), meta)
    ///     .await?;
    /// # let _ = result;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn call_tool_with_meta(
        &self,
        name: String,
        arguments: serde_json::Value,
        meta: crate::types::protocol::RequestMeta,
    ) -> Result<CallToolResult> {
        self.ensure_initialized()?;
        self.assert_capability("tools", "tools/call")?;

        let request = Request::Client(Box::new(ClientRequest::CallTool(CallToolRequest {
            name,
            arguments,
            _meta: Some(meta),
            task: None,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Get the current status of a task.
    ///
    /// Polls the server for the task's current state. Call this repeatedly
    /// (respecting `task.poll_interval`) until the task reaches a terminal
    /// status (`Completed`, `Failed`, or `Cancelled`).
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The server doesn't support tasks
    /// - The task ID doesn't exist or belongs to another owner
    pub async fn tasks_get(&self, task_id: &str) -> Result<Task> {
        self.ensure_initialized()?;
        self.assert_capability("tasks", "tasks/get")?;

        let request = Request::Client(Box::new(ClientRequest::TasksGet(GetTaskRequest {
            task_id: task_id.to_string(),
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        let task_result: GetTaskResult = self.parse_task_payload(response, "tasks/get").await?;
        Ok(task_result.task)
    }

    /// Get the final result of a completed task.
    ///
    /// For a task-augmented `tools/call`, this returns the `CallToolResult`
    /// that the tool would have returned synchronously. Only valid when
    /// the task status is `Completed`.
    pub async fn tasks_result(&self, task_id: &str) -> Result<CallToolResult> {
        self.ensure_initialized()?;
        self.assert_capability("tasks", "tasks/result")?;

        let request = Request::Client(Box::new(ClientRequest::TasksResult(
            GetTaskPayloadRequest {
                task_id: task_id.to_string(),
            },
        )));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        self.parse_task_payload::<CallToolResult>(response, "tasks/result")
            .await
    }

    /// Poll a task to terminal status, then return its `tasks/result`.
    ///
    /// Drives `tasks/get` in a loop until [`TaskStatus::is_terminal`], honoring
    /// the polling interval (caller override, else the task-reported
    /// `pollInterval`, else a built-in default) and an optional overall timeout.
    /// On terminal status it fetches and returns the persisted
    /// [`CallToolResult`] via [`Client::tasks_result`].
    ///
    /// # Wasm safety
    ///
    /// The delay between polls uses [`crate::runtime::sleep`] (not
    /// `tokio::time::sleep` directly) and the timeout is measured with
    /// [`web_time::Instant`] (not `std::time::Instant`, which panics on
    /// `wasm32`), so this compiles and runs in the browser.
    ///
    /// # Hot-loop protection
    ///
    /// The effective interval is clamped to a small floor (50 ms), so a zero or
    /// absent `pollInterval` cannot turn the loop into a busy spin.
    ///
    /// # Errors
    ///
    /// - Propagates `tasks/get` / `tasks/result` transport and protocol errors.
    /// - Returns [`Error::Timeout`] when `opts.max_poll_duration_secs` elapses
    ///   before the task reaches a terminal status. Each sleep is clamped to
    ///   the remaining budget, so a large (possibly server-reported) poll
    ///   interval cannot overshoot the caller's budget by more than roughly
    ///   the 50 ms clamp floor.
    /// - Returns [`Error::Validation`] when the task enters
    ///   [`TaskStatus::InputRequired`]: that state is NOT terminal and needs
    ///   client-side action (elicitation) this poller cannot provide, so
    ///   polling on would hang forever under the default (unbounded) options.
    ///   Handle the required input, then resume polling.
    ///
    /// # Durable and replay consumers
    ///
    /// Do **not** wrap `wait_for_task` inside a durable / replay workflow step.
    /// It sleeps, loops, and owns the whole polling lifecycle, which is
    /// non-deterministic under replay (each re-execution would re-sleep and
    /// re-poll). A durable consumer should instead call
    /// [`Task::poll_decision`](crate::types::tasks::Task::poll_decision) plus
    /// [`resolve_poll_interval`] once per tick inside its own memoized step and
    /// persist the decision
    /// between ticks — those are pure, replay-deterministic functions of the
    /// polled task, unlike this blocking poller (D-11 / D-16).
    ///
    /// See the pmcp-book "Durable and replay consumers" section
    /// (heading `## Durable and replay consumers` in
    /// `pmcp-book/src/ch12-7-tasks.md`) for the full per-poll pattern:
    /// <https://paiml.github.io/rust-mcp-sdk/ch12-7-tasks.html#durable-and-replay-consumers>.
    /// (This is a deliberate plain-text/URL reference, not a rustdoc intra-doc
    /// link, so it never fails `cargo doc` even before that page ships.)
    ///
    /// # Example
    ///
    /// ```ignore
    /// use pmcp::client::WaitForTaskOptions;
    ///
    /// // `result` came from a task-augmented tools/call.
    /// if let Some(meta) = result.related_task() {
    ///     let final_result = client
    ///         .wait_for_related_task(&meta, WaitForTaskOptions::default())
    ///         .await?;
    /// }
    /// ```
    pub async fn wait_for_task(
        &self,
        task_id: &str,
        opts: WaitForTaskOptions,
    ) -> Result<CallToolResult> {
        self.ensure_initialized()?;
        self.assert_capability("tasks", "tasks/get")?;

        // Wasm-safe monotonic clock: IS std::time::Instant on native, browser-safe on wasm.
        let start = web_time::Instant::now();
        loop {
            let task = self.tasks_get(task_id).await?;

            // Single source of truth for the stop / ask / sleep decision: the
            // `poll_decision()` classifier in src/types/tasks.rs (D-13). No
            // parallel terminal-status or input-required comparison lives here,
            // so the poller and the classifier cannot drift. This matches the
            // `#[non_exhaustive]` `TaskPollDecision` exhaustively (no `_` arm)
            // because it is in-crate — a future variant becomes a compile error
            // here, forcing an explicit decision.
            match task.poll_decision() {
                // Terminal — stop polling and fetch the persisted result below.
                TaskPollDecision::Terminal { .. } => break,
                // `input_required` is NOT terminal, and the task cannot progress
                // without client-side action this poller does not perform —
                // surface it (returning BEFORE any tasks/result fetch) instead
                // of spinning until a (possibly absent) timeout (CR-01).
                TaskPollDecision::InputRequired => {
                    return Err(Error::validation(format!(
                        "task {task_id} is input_required; wait_for_task cannot provide \
                         input — handle the elicitation, then resume polling"
                    )));
                },
                // Still running — resolve the next sleep through the shared
                // resolver (D-02: caller override, else the server-reported
                // pollInterval hint, else the default, floored to MIN_POLL_MS).
                TaskPollDecision::InProgress { poll_hint } => {
                    let mut interval = resolve_poll_interval(opts.poll_interval, poll_hint);

                    // Enforce the overall polling budget (millisecond precision)
                    // and clamp the next sleep to the REMAINING budget — the
                    // interval may be server-chosen (task-reported pollInterval),
                    // and an unclamped sleep would overshoot a caller-specified
                    // budget by up to one arbitrary server interval. This clamp
                    // is loop state (not task state), so it stays INLINE here
                    // rather than moving into the classifier or resolver (WR-01 /
                    // D-09).
                    if let Some(max_secs) = opts.max_poll_duration_secs {
                        let budget_ms = max_secs.saturating_mul(1000);
                        let elapsed_ms =
                            u64::try_from(start.elapsed().as_millis()).unwrap_or(u64::MAX);
                        let remaining_ms = budget_ms.saturating_sub(elapsed_ms);
                        if remaining_ms == 0 {
                            return Err(Error::timeout(budget_ms));
                        }
                        interval = interval.min(remaining_ms.max(MIN_POLL_MS));
                    }
                    crate::runtime::sleep(std::time::Duration::from_millis(interval)).await;
                },
            }
        }

        self.tasks_result(task_id).await
    }

    /// Poll a task referenced by [`TaskMetadata`] to terminal, then return its
    /// `tasks/result` — the zero-glue counterpart of [`Client::wait_for_task`].
    ///
    /// Any fields left unset in `opts` are filled from `meta`
    /// ([`WaitForTaskOptions::or_from_metadata`]) so a caller who holds a
    /// [`CallToolResult::related_task`](crate::types::CallToolResult::related_task)
    /// result composes without hand-copying poll fields.
    ///
    /// # Errors
    ///
    /// Same as [`Client::wait_for_task`].
    pub async fn wait_for_related_task(
        &self,
        meta: &TaskMetadata,
        opts: WaitForTaskOptions,
    ) -> Result<CallToolResult> {
        self.wait_for_task(&meta.task_id, opts.or_from_metadata(meta))
            .await
    }

    /// List tasks owned by the current client.
    pub async fn tasks_list(&self, cursor: Option<String>) -> Result<ListTasksResult> {
        self.ensure_initialized()?;
        self.assert_capability("tasks", "tasks/list")?;

        let request = Request::Client(Box::new(ClientRequest::TasksList(ListTasksRequest {
            cursor,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        self.parse_task_payload::<ListTasksResult>(response, "tasks/list")
            .await
    }

    /// Cancel a running task.
    pub async fn tasks_cancel(&self, task_id: &str) -> Result<Task> {
        self.ensure_initialized()?;
        self.assert_capability("tasks", "tasks/cancel")?;

        let request = Request::Client(Box::new(ClientRequest::TasksCancel(CancelTaskRequest {
            task_id: task_id.to_string(),
            result: None,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        let cancel_result: CancelTaskResult =
            self.parse_task_payload(response, "tasks/cancel").await?;
        Ok(cancel_result.task)
    }

    /// Deserialize a `tasks/*` response payload into `T`, emitting a structured
    /// WARN (method + transport identity + serde error) on a deserialize failure
    /// before surfacing it. Centralizes the four task endpoints' identical
    /// result-vs-error handling.
    ///
    /// Lock-on-error: the transport identity is read only on the cold failure
    /// path (D-LOCK-ON-ERROR — no cached field on `Client`).
    async fn parse_task_payload<D: serde::de::DeserializeOwned>(
        &self,
        response: crate::types::JSONRPCResponse,
        method: &'static str,
    ) -> Result<D> {
        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                match serde_json::from_value::<D>(result) {
                    Ok(value) => Ok(value),
                    Err(e) => {
                        let transport = self.transport.read().await.transport_type();
                        Self::log_task_deserialize_error(
                            method,
                            std::any::type_name::<D>(),
                            transport,
                            &e,
                        );
                        Err(Error::parse(e.to_string()))
                    },
                }
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Call a tool and automatically poll until the task completes.
    ///
    /// This is a high-level convenience method that encapsulates the full
    /// task lifecycle:
    #[cfg(not(target_arch = "wasm32"))]
    /// 1. Calls the tool with task augmentation
    /// 2. If the server returns a task, polls `tasks/get` until terminal status
    /// 3. Returns the final `CallToolResult`
    ///
    /// If the server returns a sync result (no task), returns it immediately.
    ///
    /// # Arguments
    ///
    /// * `name` - Tool name
    /// * `arguments` - Tool arguments
    /// * `max_polls` - Maximum number of poll attempts before giving up (0 = unlimited)
    pub async fn call_tool_and_poll(
        &self,
        name: String,
        arguments: serde_json::Value,
        max_polls: usize,
    ) -> Result<CallToolResult> {
        /// Default polling interval when the server doesn't specify one.
        const DEFAULT_POLL_INTERVAL_MS: u64 = 5000;

        let response = self.call_tool_with_task(name, arguments).await?;

        match response {
            ToolCallResponse::Result(result) => Ok(result),
            ToolCallResponse::Task(initial_task) => {
                let task_id = initial_task.task_id.clone();
                let mut poll_ms = initial_task
                    .poll_interval
                    .unwrap_or(DEFAULT_POLL_INTERVAL_MS);
                let mut polls = 0;

                loop {
                    polls += 1;

                    let task = self.tasks_get(&task_id).await?;

                    if task.status == TaskStatus::InputRequired {
                        return Err(Error::internal(format!(
                            "Task {} requires input — handle interactively via tasks_get/tasks_cancel",
                            task_id
                        )));
                    }

                    if task.status.is_terminal() {
                        if task.status == TaskStatus::Completed {
                            // Try to get the full result via tasks/result
                            match self.tasks_result(&task_id).await {
                                Ok(result) => return Ok(result),
                                // Only fall back for method-not-found (-32601); propagate real errors
                                Err(Error::Protocol { code, .. })
                                    if code == crate::error::ErrorCode::METHOD_NOT_FOUND =>
                                {
                                    let text = task
                                        .status_message
                                        .unwrap_or_else(|| "Task completed".to_string());
                                    return Ok(CallToolResult::new(vec![
                                        crate::types::Content::text(text),
                                    ]));
                                },
                                Err(e) => return Err(e),
                            }
                        } else {
                            // Failed or Cancelled
                            let text = task
                                .status_message
                                .unwrap_or_else(|| format!("Task {}", task.status));
                            return Ok(CallToolResult::error(vec![crate::types::Content::text(
                                text,
                            )]));
                        }
                    }

                    if max_polls > 0 && polls >= max_polls {
                        return Err(Error::internal(format!(
                            "Task {} did not complete after {} polls",
                            task_id, max_polls
                        )));
                    }

                    // Honor updated poll_interval from server (e.g., exponential backoff)
                    if let Some(interval) = task.poll_interval {
                        poll_ms = interval;
                    }

                    tokio::time::sleep(std::time::Duration::from_millis(poll_ms)).await;
                }
            },
        }
    }

    // =========================================================================
    // Prompts
    // =========================================================================

    /// List available prompts.
    ///
    /// Retrieves information about all prompts available on the server, including
    /// their names, descriptions, and required arguments.
    ///
    /// # Arguments
    ///
    /// * `cursor` - Optional cursor for pagination of large prompt lists
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // List all prompts
    /// let prompts = client.list_prompts(None).await?;
    /// for prompt in prompts.prompts {
    ///     println!("Prompt: {} - {}",
    ///              prompt.name,
    ///              prompt.description.unwrap_or_else(|| "No description".to_string()));
    ///     
    ///     // Show required arguments
    ///     if let Some(args) = prompt.arguments {
    ///         for arg in args {
    ///             println!("  - {}: {} (required: {})",
    ///                      arg.name,
    ///                      arg.description.unwrap_or_else(|| "No description".to_string()),
    ///                      arg.required);
    ///         }
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support prompts
    /// - Network or protocol errors occur
    pub async fn list_prompts(&self, cursor: Option<String>) -> Result<ListPromptsResult> {
        self.ensure_initialized()?;
        self.assert_capability("prompts", "prompts/list")?;

        let request = Request::Client(Box::new(ClientRequest::ListPrompts(ListPromptsRequest {
            cursor,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Get a prompt.
    ///
    /// Retrieves a specific prompt from the server with the provided arguments.
    /// The prompt is processed by the server and returned with filled-in content.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the prompt to retrieve
    /// * `arguments` - Key-value pairs for prompt arguments
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    /// use std::collections::HashMap;
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Get a prompt with arguments
    /// let mut args = HashMap::new();
    /// args.insert("language".to_string(), "Rust".to_string());
    /// args.insert("topic".to_string(), "async programming".to_string());
    ///
    /// let prompt_result = client.get_prompt(
    ///     "code_review".to_string(),
    ///     args
    /// ).await?;
    ///
    /// println!("Prompt description: {}",
    ///          prompt_result.description.unwrap_or_else(|| "No description".to_string()));
    ///
    /// // Process the prompt messages
    /// for message in prompt_result.messages {
    ///     println!("Role: {}", message.role);
    ///     match &message.content {
    ///         pmcp::Content::Text { text } => {
    ///             println!("Content: {}", text);
    ///         }
    ///         _ => println!("Non-text content"),
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support prompts
    /// - The prompt name doesn't exist
    /// - Required arguments are missing
    /// - Network or protocol errors occur
    pub async fn get_prompt(
        &self,
        name: String,
        arguments: HashMap<String, String>,
    ) -> Result<GetPromptResult> {
        self.ensure_initialized()?;
        self.assert_capability("prompts", "prompts/get")?;

        let request = Request::Client(Box::new(ClientRequest::GetPrompt(GetPromptRequest {
            name,
            arguments,
            _meta: None,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    // === Typed call helpers ===

    /// Call a tool with typed, serializable arguments.
    ///
    /// Serializes `args` via `serde_json::to_value` and delegates to
    /// [`Self::call_tool`]. Serialization failures are mapped to
    /// [`Error::validation`] with the underlying serde error message.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct Search { query: String, limit: u32 }
    ///
    /// let _ = client.call_tool_typed(
    ///     "search",
    ///     &Search { query: "rust mcp".into(), limit: 10 },
    /// ).await?;
    /// # Ok(()) }
    /// ```
    pub async fn call_tool_typed<A: serde::Serialize + ?Sized + Sync>(
        &self,
        name: impl Into<String> + Send,
        args: &A,
    ) -> Result<CallToolResult> {
        let value = serde_json::to_value(args)
            .map_err(|e| Error::validation(format!("call_tool_typed arguments: {e}")))?;
        self.call_tool(name.into(), value).await
    }

    /// Typed sibling of [`Self::call_tool_with_task`].
    ///
    /// Delegates to the two-argument [`Self::call_tool_with_task`]; there is no
    /// `TaskMetadata` parameter on the live client API, so none is exposed here.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// use serde::Serialize;
    /// #[derive(Serialize)]
    /// struct Args { file: String }
    /// let _ = client.call_tool_typed_with_task("scan", &Args { file: "a.rs".into() }).await?;
    /// # Ok(()) }
    /// ```
    pub async fn call_tool_typed_with_task<A: serde::Serialize + ?Sized + Sync>(
        &self,
        name: impl Into<String> + Send,
        args: &A,
    ) -> Result<ToolCallResponse> {
        let value = serde_json::to_value(args)
            .map_err(|e| Error::validation(format!("call_tool_typed_with_task arguments: {e}")))?;
        self.call_tool_with_task(name.into(), value).await
    }

    /// Typed sibling of [`Self::call_tool_and_poll`].
    ///
    /// Delegates to the three-argument [`Self::call_tool_and_poll`]
    /// (`name, arguments, max_polls: usize`). There is no `poll_interval` or
    /// `TaskMetadata` parameter on the live client API — the server-supplied
    /// `poll_interval` is honoured internally by `call_tool_and_poll`.
    ///
    /// `max_polls = 0` means unlimited polls, matching the sibling's semantics.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// use serde::Serialize;
    /// #[derive(Serialize)]
    /// struct Args { job: String }
    /// let _ = client.call_tool_typed_and_poll(
    ///     "build",
    ///     &Args { job: "nightly".into() },
    ///     30, // max_polls
    /// ).await?;
    /// # Ok(()) }
    /// ```
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn call_tool_typed_and_poll<A: serde::Serialize + ?Sized + Sync>(
        &self,
        name: impl Into<String> + Send,
        args: &A,
        max_polls: usize,
    ) -> Result<CallToolResult> {
        let value = serde_json::to_value(args)
            .map_err(|e| Error::validation(format!("call_tool_typed_and_poll arguments: {e}")))?;
        self.call_tool_and_poll(name.into(), value, max_polls).await
    }

    /// Get a prompt with typed, serializable arguments.
    ///
    /// Serializes `args` to a JSON object, then coerces each leaf to a `String`
    /// for the wire-level `HashMap<String, String>` arguments:
    /// - `null` entries are omitted
    /// - `string` entries pass through unchanged (no JSON-quoting)
    /// - `number` and `bool` entries use `Display` (e.g. `42`, `true`)
    /// - `array` and `object` entries are re-serialized via
    ///   [`serde_json::to_string`]
    ///
    /// Non-object top-level serializations are rejected with
    /// [`Error::validation`].
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct SummaryArgs { topic: String, length: u32 }
    ///
    /// let _ = client.get_prompt_typed(
    ///     "summarize",
    ///     &SummaryArgs { topic: "rust async".into(), length: 200 },
    /// ).await?;
    /// # Ok(()) }
    /// ```
    pub async fn get_prompt_typed<A: serde::Serialize + ?Sized + Sync>(
        &self,
        name: impl Into<String> + Send,
        args: &A,
    ) -> Result<GetPromptResult> {
        let value = serde_json::to_value(args)
            .map_err(|e| Error::validation(format!("get_prompt_typed arguments: {e}")))?;
        let serde_json::Value::Object(obj) = value else {
            return Err(Error::validation(
                "prompts/get arguments must serialize to a JSON object",
            ));
        };
        let mut out: HashMap<String, String> = HashMap::with_capacity(obj.len());
        for (k, v) in obj {
            match v {
                serde_json::Value::Null => {},
                serde_json::Value::String(s) => {
                    out.insert(k, s);
                },
                serde_json::Value::Bool(_) | serde_json::Value::Number(_) => {
                    out.insert(k, v.to_string());
                },
                serde_json::Value::Array(_) | serde_json::Value::Object(_) => {
                    let nested = serde_json::to_string(&v).map_err(|e| {
                        Error::validation(format!("get_prompt_typed nested arg {k}: {e}"))
                    })?;
                    out.insert(k, nested);
                },
            }
        }
        self.get_prompt(name.into(), out).await
    }

    /// List available resources.
    ///
    /// Retrieves information about all resources available on the server, including
    /// their names, descriptions, URIs, and MIME types.
    ///
    /// # Arguments
    ///
    /// * `cursor` - Optional cursor for pagination of large resource lists
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // List all resources
    /// let resources = client.list_resources(None).await?;
    /// for resource in resources.resources {
    ///     println!("Resource: {} ({})", resource.name, resource.uri);
    ///     if let Some(description) = resource.description {
    ///         println!("  Description: {}", description);
    ///     }
    ///     if let Some(mime_type) = resource.mime_type {
    ///         println!("  MIME Type: {}", mime_type);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support resources
    /// - Network or protocol errors occur
    pub async fn list_resources(&self, cursor: Option<String>) -> Result<ListResourcesResult> {
        self.ensure_initialized()?;
        self.assert_capability("resources", "resources/list")?;

        let request = Request::Client(Box::new(ClientRequest::ListResources(
            ListResourcesRequest { cursor },
        )));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// List resource templates.
    ///
    /// Retrieves information about all resource templates available on the server.
    /// Resource templates define patterns for dynamically generated resources.
    ///
    /// # Arguments
    ///
    /// * `cursor` - Optional cursor for pagination of large template lists
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // List all resource templates
    /// let templates = client.list_resource_templates(None).await?;
    /// for template in templates.resource_templates {
    ///     println!("Template: {} ({})", template.name, template.uri_template);
    ///     if let Some(description) = template.description {
    ///         println!("  Description: {}", description);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support resource templates
    /// - Network or protocol errors occur
    pub async fn list_resource_templates(
        &self,
        cursor: Option<String>,
    ) -> Result<ListResourceTemplatesResult> {
        self.ensure_initialized()?;
        self.assert_capability("resources", "resources/templates/list")?;

        let request = Request::Client(Box::new(ClientRequest::ListResourceTemplates(
            ListResourceTemplatesRequest { cursor },
        )));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    // === Auto-paginating list helpers ===

    /// List all tools across all pages, auto-paginating on `next_cursor`.
    ///
    /// Loops calling [`Self::list_tools`], terminating when the server returns
    /// `next_cursor: None`. Safety cap: if the loop runs more than
    /// `self.options.max_iterations` iterations (default `100`), returns
    /// [`Error::Validation`] instead of continuing or silently truncating.
    ///
    /// Empty-string cursors (`Some("")`) do NOT terminate the loop — only
    /// `None` does. This matches the MCP spec, which treats the cursor as an
    /// opaque server token and does not ascribe meaning to the empty string.
    ///
    /// # Memory
    ///
    /// This helper accumulates **all pages** in memory before returning. For
    /// very large servers, prefer the paginated single-page
    /// [`Self::list_tools`] and stream the output yourself — this helper is a
    /// convenience API and will amplify memory usage proportional to the
    /// total tool count.
    ///
    /// # Errors
    ///
    /// - Any error surfaced by [`Self::list_tools`] propagates unchanged.
    /// - Cap exceeded → `Error::Validation("list_all_tools exceeded max_iterations cap of N pages")`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// let tools = client.list_all_tools().await?;
    /// println!("discovered {} tools", tools.len());
    /// # Ok(()) }
    /// ```
    pub async fn list_all_tools(&self) -> Result<Vec<ToolInfo>> {
        let cap = self.options.max_iterations;
        let mut out: Vec<ToolInfo> = Vec::new();
        let mut cursor: Option<String> = None;
        for _ in 0..cap {
            let page = self.list_tools(cursor).await?;
            out.extend(page.tools);
            match page.next_cursor {
                None => return Ok(out),
                Some(next) => cursor = Some(next),
            }
        }
        Err(Error::validation(format!(
            "list_all_tools exceeded max_iterations cap of {cap} pages"
        )))
    }

    /// List all prompts across all pages, auto-paginating on `next_cursor`.
    ///
    /// Semantics identical to [`Self::list_all_tools`]: bounded by
    /// `self.options.max_iterations`, terminates only on `next_cursor: None`,
    /// returns [`Error::Validation`] on cap exceeded.
    ///
    /// # Memory
    ///
    /// Accumulates all pages in memory; prefer [`Self::list_prompts`] for
    /// very large servers.
    ///
    /// # Errors
    ///
    /// - Any error surfaced by [`Self::list_prompts`] propagates unchanged.
    /// - Cap exceeded → `Error::Validation("list_all_prompts exceeded max_iterations cap of N pages")`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// let prompts = client.list_all_prompts().await?;
    /// println!("discovered {} prompts", prompts.len());
    /// # Ok(()) }
    /// ```
    pub async fn list_all_prompts(&self) -> Result<Vec<PromptInfo>> {
        let cap = self.options.max_iterations;
        let mut out: Vec<PromptInfo> = Vec::new();
        let mut cursor: Option<String> = None;
        for _ in 0..cap {
            let page = self.list_prompts(cursor).await?;
            out.extend(page.prompts);
            match page.next_cursor {
                None => return Ok(out),
                Some(next) => cursor = Some(next),
            }
        }
        Err(Error::validation(format!(
            "list_all_prompts exceeded max_iterations cap of {cap} pages"
        )))
    }

    /// List all resources across all pages, auto-paginating on `next_cursor`.
    ///
    /// Semantics identical to [`Self::list_all_tools`]: bounded by
    /// `self.options.max_iterations`, terminates only on `next_cursor: None`,
    /// returns [`Error::Validation`] on cap exceeded.
    ///
    /// # Memory
    ///
    /// Accumulates all pages in memory; prefer [`Self::list_resources`] for
    /// very large servers.
    ///
    /// # Errors
    ///
    /// - Any error surfaced by [`Self::list_resources`] propagates unchanged.
    /// - Cap exceeded → `Error::Validation("list_all_resources exceeded max_iterations cap of N pages")`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// let resources = client.list_all_resources().await?;
    /// println!("discovered {} resources", resources.len());
    /// # Ok(()) }
    /// ```
    pub async fn list_all_resources(&self) -> Result<Vec<ResourceInfo>> {
        let cap = self.options.max_iterations;
        let mut out: Vec<ResourceInfo> = Vec::new();
        let mut cursor: Option<String> = None;
        for _ in 0..cap {
            let page = self.list_resources(cursor).await?;
            out.extend(page.resources);
            match page.next_cursor {
                None => return Ok(out),
                Some(next) => cursor = Some(next),
            }
        }
        Err(Error::validation(format!(
            "list_all_resources exceeded max_iterations cap of {cap} pages"
        )))
    }

    /// List all resource templates across all pages, auto-paginating on
    /// `next_cursor`.
    ///
    /// Uses the distinct `resources/templates/list` capability path (all
    /// other `list_all_*` helpers hit their own methods). Semantics otherwise
    /// identical to [`Self::list_all_tools`]: bounded by
    /// `self.options.max_iterations`, terminates only on `next_cursor: None`,
    /// returns [`Error::Validation`] on cap exceeded.
    ///
    /// # Memory
    ///
    /// Accumulates all pages in memory; prefer
    /// [`Self::list_resource_templates`] for very large servers.
    ///
    /// # Errors
    ///
    /// - Any error surfaced by [`Self::list_resource_templates`] propagates unchanged.
    /// - Cap exceeded → `Error::Validation("list_all_resource_templates exceeded max_iterations cap of N pages")`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # async fn ex<T: pmcp::shared::Transport + Send + Sync + 'static>(mut client: pmcp::Client<T>) -> pmcp::Result<()> {
    /// let templates = client.list_all_resource_templates().await?;
    /// println!("discovered {} templates", templates.len());
    /// # Ok(()) }
    /// ```
    pub async fn list_all_resource_templates(&self) -> Result<Vec<ResourceTemplate>> {
        let cap = self.options.max_iterations;
        let mut out: Vec<ResourceTemplate> = Vec::new();
        let mut cursor: Option<String> = None;
        for _ in 0..cap {
            let page = self.list_resource_templates(cursor).await?;
            out.extend(page.resource_templates);
            match page.next_cursor {
                None => return Ok(out),
                Some(next) => cursor = Some(next),
            }
        }
        Err(Error::validation(format!(
            "list_all_resource_templates exceeded max_iterations cap of {cap} pages"
        )))
    }

    /// Read a resource.
    ///
    /// Retrieves the content of a specific resource from the server by its URI.
    /// Resources can contain text, binary data, or structured content.
    ///
    /// # Arguments
    ///
    /// * `uri` - The URI of the resource to read
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Read a text resource
    /// let resource = client.read_resource("file://readme.txt".to_string()).await?;
    /// for content in resource.contents {
    ///     match content {
    ///         pmcp::Content::Text { text } => {
    ///             println!("Text content: {}", text);
    ///         }
    ///         pmcp::Content::Resource { uri, .. } => {
    ///             println!("Resource reference: {}", uri);
    ///         }
    ///         _ => println!("Other content type"),
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support resources
    /// - The resource URI doesn't exist
    /// - Access to the resource is denied
    /// - Network or protocol errors occur
    pub async fn read_resource(&self, uri: String) -> Result<ReadResourceResult> {
        self.ensure_initialized()?;
        self.assert_capability("resources", "resources/read")?;

        let request = Request::Client(Box::new(ClientRequest::ReadResource(ReadResourceRequest {
            uri,
            _meta: None,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Subscribe to resource updates.
    ///
    /// Subscribes to receive notifications when a resource changes.
    /// The server will send notifications when the subscribed resource is modified.
    ///
    /// # Arguments
    ///
    /// * `uri` - The URI of the resource to subscribe to
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Subscribe to a configuration file
    /// client.subscribe_resource("file://config/settings.json".to_string()).await?;
    ///
    /// // Now the client will receive notifications when settings.json changes
    /// // Handle notifications in your event loop
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support resource subscriptions
    /// - The resource URI doesn't exist
    /// - Network or protocol errors occur
    pub async fn subscribe_resource(&self, uri: String) -> Result<()> {
        self.ensure_initialized()?;
        self.assert_capability("resources", "resources/subscribe")?;

        // Check if server supports subscriptions
        if let Some(resources) = &self
            .server_capabilities
            .as_ref()
            .and_then(|c| c.resources.as_ref())
        {
            if !resources.subscribe.unwrap_or(false) {
                return Err(Error::capability(
                    "Server does not support resource subscriptions",
                ));
            }
        }

        let request = Request::Client(Box::new(ClientRequest::Subscribe(SubscribeRequest { uri })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(_) => Ok(()),
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Unsubscribe from resource updates.
    ///
    /// Unsubscribes from notifications for a previously subscribed resource.
    /// After unsubscribing, the client will no longer receive change notifications.
    ///
    /// # Arguments
    ///
    /// * `uri` - The URI of the resource to unsubscribe from
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Subscribe to a resource
    /// client.subscribe_resource("file://config/settings.json".to_string()).await?;
    ///
    /// // Later, unsubscribe when no longer needed
    /// client.unsubscribe_resource("file://config/settings.json".to_string()).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support resource subscriptions
    /// - The resource URI was not previously subscribed to
    /// - Network or protocol errors occur
    pub async fn unsubscribe_resource(&self, uri: String) -> Result<()> {
        self.ensure_initialized()?;
        self.assert_capability("resources", "resources/unsubscribe")?;

        let request = Request::Client(Box::new(ClientRequest::Unsubscribe(UnsubscribeRequest {
            uri,
        })));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(_) => Ok(()),
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Request completion from the server.
    ///
    /// Requests auto-completion suggestions from the server for a given context.
    /// This is useful for implementing IDE-like features with contextual suggestions.
    ///
    /// # Arguments
    ///
    /// * `params` - The completion request parameters
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities, CompleteRequest};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Request completion for partial text
    /// let completion_request = CompleteRequest {
    ///     r#ref: pmcp::CompletionReference::Resource {
    ///         uri: "file://code.rs".to_string(),
    ///     },
    ///     argument: pmcp::CompletionArgument {
    ///         name: "function_name".to_string(),
    ///         value: "calc_".to_string(),
    ///     },
    /// };
    ///
    /// let completions = client.complete(completion_request).await?;
    /// for completion in completions.completion.values {
    ///     println!("Suggestion: {}", completion);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support completions
    /// - The completion context is invalid
    /// - Network or protocol errors occur
    pub async fn complete(&self, params: CompleteRequest) -> Result<CompleteResult> {
        self.ensure_initialized()?;
        self.assert_capability("completions", "completion/complete")?;

        let request = Request::Client(Box::new(ClientRequest::Complete(params)));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Create a message using sampling (for LLM providers).
    ///
    /// Requests the server to generate a message using its language model capabilities.
    /// This is typically used by servers that provide LLM functionality.
    ///
    /// # The "LLM-server pattern" (INVERSE of spec host sampling)
    ///
    /// This method is the **LLM-server pattern**: the *client* asks a *server*
    /// whose [`pmcp::SamplingHandler`](crate::SamplingHandler) runs the LLM. It
    /// is the **inverse** of MCP spec host sampling, where a server requests
    /// sampling and the client answers via a
    /// [`pmcp::client::host::HostSamplingHandler`](crate::client::host::HostSamplingHandler).
    /// Both directions are supported and neither is deprecated — pick the one
    /// that matches who owns the model. This path is unchanged by the client
    /// host surface.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities, CreateMessageParams, SamplingMessage};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let mut capabilities = ClientCapabilities::default();
    /// capabilities.sampling = Some(Default::default());
    ///
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(capabilities).await?;
    ///
    /// // Create a message with the LLM
    /// let msg = SamplingMessage::new(
    ///     pmcp::types::Role::User,
    ///     pmcp::types::SamplingMessageContent::Text {
    ///         text: "Explain how to implement a binary search tree".to_string(),
    ///         meta: None,
    ///     },
    /// );
    /// let prefs = pmcp::types::ModelPreferences::new()
    ///     .with_hints(vec![pmcp::types::ModelHint::new("gpt-4")])
    ///     .with_cost_priority(0.5)
    ///     .with_speed_priority(0.3)
    ///     .with_intelligence_priority(0.2);
    /// let mut request = CreateMessageParams::new(vec![msg])
    ///     .with_model_preferences(prefs)
    ///     .with_system_prompt("You are a helpful programming assistant")
    ///     .with_temperature(0.7)
    ///     .with_max_tokens(1000);
    /// request.include_context = pmcp::types::IncludeContext::ThisServer;
    ///
    /// let result = client.create_message(request).await?;
    /// println!("Model: {}", result.model);
    /// println!("Response: {:?}", result.content);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The server doesn't support sampling
    /// - The request parameters are invalid
    /// - Network or protocol errors occur
    pub async fn create_message(&self, params: CreateMessageParams) -> Result<CreateMessageResult> {
        self.ensure_initialized()?;
        self.assert_capability("sampling", "sampling/createMessage")?;

        let request = Request::Client(Box::new(ClientRequest::CreateMessage(Box::new(params))));
        let request_id = RequestId::String(Uuid::new_v4().to_string());
        let response = self.send_request(request_id, request).await?;

        match response.payload {
            crate::types::jsonrpc::ResponsePayload::Result(result) => {
                serde_json::from_value(result).map_err(|e| Error::parse(e.to_string()))
            },
            crate::types::jsonrpc::ResponsePayload::Error(error) => {
                Err(Error::from_jsonrpc_error(error))
            },
        }
    }

    /// Send roots list changed notification.
    ///
    /// Notifies the server that the client's root list has changed.
    /// This is typically sent when the workspace or project roots are modified.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{ClientBuilder, StdioTransport, ClientCapabilities};
    /// use pmcp::types::roots::{ListRootsResult, Root};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// // Roots advertisement is registry-derived (HOST-05): the client must
    /// // register a roots provider for the `roots` capability to reach the
    /// // wire. Build via `ClientBuilder` and register one with `on_roots`.
    /// let transport = StdioTransport::new();
    /// let mut client = ClientBuilder::new(transport)
    ///     .on_roots(|| async {
    ///         Ok(ListRootsResult {
    ///             roots: vec![Root {
    ///                 uri: "file:///workspace".to_string(),
    ///                 name: Some("workspace".to_string()),
    ///             }],
    ///         })
    ///     })
    ///     .build();
    ///
    /// // With a provider registered, a caller-set `list_changed` is preserved,
    /// // so the client advertises that it emits roots-list-changed notices.
    /// let mut capabilities = ClientCapabilities::default();
    /// capabilities.roots = Some(pmcp::RootsCapabilities { list_changed: true });
    /// client.initialize(capabilities).await?;
    ///
    /// // Notify server when project roots change
    /// client.send_roots_list_changed().await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - The client doesn't support roots list changed notifications
    /// - Network or protocol errors occur
    pub async fn send_roots_list_changed(&self) -> Result<()> {
        self.ensure_initialized()?;
        if let Some(roots) = &self.capabilities.as_ref().and_then(|c| c.roots.as_ref()) {
            if roots.list_changed {
                // OK, we support it
            } else {
                return Err(Error::capability(
                    "Client does not support roots list changed notifications",
                ));
            }
        }

        self.send_notification(Notification::Client(ClientNotification::RootsListChanged))
            .await
    }

    /// Authenticate with the server.
    ///
    /// Performs authentication using the provided authentication information.
    /// This should be called after initialization if the server requires authentication.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, AuthInfo, AuthScheme};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    ///
    /// // Initialize first
    /// client.initialize(pmcp::ClientCapabilities::default()).await?;
    ///
    /// // Authenticate with bearer token
    /// let auth = AuthInfo {
    ///     scheme: AuthScheme::Bearer,
    ///     token: Some("your-api-token".to_string()),
    ///     oauth: None,
    ///     params: Default::default(),
    /// };
    ///
    /// client.authenticate(&auth)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The client is not initialized
    /// - Authentication fails
    /// - The server doesn't support authentication
    pub fn authenticate(&self, auth_info: &crate::types::AuthInfo) -> Result<()> {
        self.ensure_initialized()?;

        // In a real implementation, this would send an authentication request
        // For now, we'll just validate that we can authenticate
        match auth_info.scheme {
            crate::types::AuthScheme::None => Ok(()),
            crate::types::AuthScheme::Bearer => {
                if auth_info.token.is_none() {
                    return Err(Error::validation("Bearer token required"));
                }
                Ok(())
            },
            crate::types::AuthScheme::OAuth2 => {
                if auth_info.oauth.is_none() {
                    return Err(Error::validation("OAuth information required"));
                }
                Ok(())
            },
            crate::types::AuthScheme::Custom(_) => {
                // Custom auth schemes would be handled here
                Ok(())
            },
        }
    }

    /// Cancel a request.
    ///
    /// Sends a cancellation notification for an active request.
    /// This allows graceful termination of long-running operations.
    ///
    /// # Arguments
    ///
    /// * `request_id` - The ID of the request to cancel
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities, RequestId};
    /// use serde_json::json;
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Start a long-running operation
    /// let request_id = RequestId::String("long-operation-123".to_string());
    ///
    /// // Later, cancel the request if needed
    /// client.cancel_request(&request_id).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Network or protocol errors occur while sending the cancellation
    pub async fn cancel_request(&self, request_id: &RequestId) -> Result<()> {
        // Send cancellation notification
        self.send_notification(Notification::Cancelled(
            CancelledNotification::new(request_id.clone())
                .with_reason("User requested cancellation"),
        ))
        .await?;

        // Cancel any local tracking
        let sender = self.active_requests.write().await.remove(request_id);
        if let Some(sender) = sender {
            let _ = sender.send(());
        }

        Ok(())
    }

    /// Send a progress notification.
    ///
    /// Sends a progress update for a long-running operation.
    /// This allows the server or client to track operation progress.
    ///
    /// # Arguments
    ///
    /// * `progress` - The progress notification to send
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{Client, StdioTransport, ClientCapabilities, ProgressNotification, RequestId};
    ///
    /// # async fn example() -> pmcp::Result<()> {
    /// let transport = StdioTransport::new();
    /// let mut client = Client::new(transport);
    /// client.initialize(ClientCapabilities::default()).await?;
    ///
    /// // Send progress update for a file processing operation
    /// let progress = ProgressNotification::new(
    ///     pmcp::ProgressToken::String("file-processing".to_string()),
    ///     75.0,
    ///     Some("Processing files...".to_string()),
    /// );
    ///
    /// client.send_progress(progress).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - Network or protocol errors occur while sending the notification
    pub async fn send_progress(&self, progress: ProgressNotification) -> Result<()> {
        self.send_notification(Notification::Progress(progress))
            .await
    }

    /// Emit a `WARN` when a `tasks/*` response fails to deserialize.
    ///
    /// This is the single shared observability helper for all four task
    /// deserialize sites (`tasks/get`, `tasks/result`, `tasks/list`,
    /// `tasks/cancel`). It logs the originating `method`, the available
    /// transport identity, the deserialize `target` type, and the serde
    /// `error` — then the caller still returns `Err` (control flow is
    /// unchanged; this only adds observability, closing TASKDX-03).
    ///
    /// `transport` is [`Transport::transport_type`] (e.g. `"stdio"`,
    /// `"http"`) — the only server identity available here, because the
    /// `Transport` trait exposes no per-instance URL. TASKDX-03 logs this
    /// identity, not a genuine endpoint URL.
    fn log_task_deserialize_error(
        method: &'static str,
        target_type: &'static str,
        transport: &'static str,
        error: &serde_json::Error,
    ) {
        tracing::warn!(
            method = method,
            transport = transport,
            target = target_type,
            error = %error,
            "task response failed to deserialize",
        );
    }

    /// Check if client is initialized.
    fn ensure_initialized(&self) -> Result<()> {
        if self.initialized {
            Ok(())
        } else {
            Err(Error::InvalidState("Client not initialized".into()))
        }
    }

    /// Assert that the server has a specific capability.
    fn assert_capability(&self, capability: &str, method: &str) -> Result<()> {
        let has_capability = match capability {
            "tools" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.tools.is_some()),
            "prompts" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.prompts.is_some()),
            "resources" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.resources.is_some()),
            "logging" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.logging.is_some()),
            "completions" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.completions.is_some()),
            "tasks" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.tasks.is_some()),
            // The LLM-server pattern: `create_message` asks a server whose
            // `SamplingHandler` runs the LLM. A pmcp `Server` built with
            // `.sampling(handler)` advertises this by setting
            // `ServerCapabilities.sampling = Some(..)` (see
            // `src/server/mod.rs` `ServerBuilder::sampling`), so the check
            // mirrors that field. Without this arm every `create_message`
            // call fell through to `_ => false` and unconditionally errored.
            "sampling" => self
                .server_capabilities
                .as_ref()
                .is_some_and(|c| c.sampling.is_some()),
            _ => {
                // A capability string reached here without a matching arm. This
                // is a programming error (a new capability was wired without
                // updating this match), not a server-side condition. Make it
                // loud in tests/debug builds while preserving the conservative
                // "not supported" behavior in release.
                tracing::error!(
                    "unknown capability string {capability:?} (required for {method}) — add an arm to assert_capability"
                );
                debug_assert!(
                    false,
                    "unknown capability string {capability:?} (required for {method}) — add an arm to assert_capability"
                );
                false
            },
        };

        if has_capability {
            Ok(())
        } else {
            Err(Error::capability(format!(
                "Server does not support {} (required for {})",
                capability, method
            )))
        }
    }

    /// Send a request and wait for response.
    async fn send_request(
        &self,
        request_id: RequestId,
        request: Request,
    ) -> Result<crate::types::JSONRPCResponse> {
        use crate::shared::protocol_helpers::create_request;

        // Track request for cancellation
        let (cancel_tx, _cancel_rx) = oneshot::channel();
        self.active_requests
            .write()
            .await
            .insert(request_id.clone(), cancel_tx);

        // Create middleware context
        let context = MiddlewareContext::with_request_id(request_id.to_string());

        // Everything after the `active_requests` registration runs inside this
        // inner future so that EVERY error exit funnels through the single
        // cleanup point below (WR-04). On any `Err` — middleware, outbound
        // `send`, inbound `receive`, response middleware, or the host-dispatch
        // reply `send` — the pending entry (and its oneshot cancel sender) is
        // removed before the error propagates, so a `Client` that outlives a
        // failed request never leaks the id or collides with stale state on a
        // later reused id. The happy path still removes the entry inline when
        // the matching response arrives.
        let result = async {
            // Convert to JSONRPC request
            let mut jsonrpc_request = create_request(request_id.clone(), request.clone());

            // Process request through middleware chain (read-only access)
            self.middleware_chain
                .read()
                .await
                .process_request_with_context(&mut jsonrpc_request, &context)
                .await?;

            // Send request through transport
            let message = crate::types::TransportMessage::Request {
                id: request_id.clone(),
                request,
            };

            self.transport.write().await.send(message).await?;

            // Wait for response, dispatching any unsolicited notifications along the way
            loop {
                let response_message = self.transport.write().await.receive().await?;

                match response_message {
                    crate::types::TransportMessage::Response(mut response) => {
                        // Remove from active requests (happy path)
                        self.active_requests.write().await.remove(&request_id);

                        // Process response through middleware chain (read-only access)
                        self.middleware_chain
                            .read()
                            .await
                            .process_response_with_context(&mut response, &context)
                            .await?;
                        return Ok(response);
                    },
                    crate::types::TransportMessage::Notification(notification) => {
                        // Unsolicited notification (e.g., progress, resource changes, SSE events)
                        // Convert to JSONRPC notification for middleware processing
                        use crate::shared::protocol_helpers::create_notification;
                        let mut jsonrpc_notification = create_notification(notification.clone());

                        // Process through protocol middleware chain
                        let notif_context = MiddlewareContext::default();

                        if let Err(e) = self
                            .middleware_chain
                            .write()
                            .await
                            .process_notification_with_context(
                                &mut jsonrpc_notification,
                                &notif_context,
                            )
                            .await
                        {
                            // Log error but don't terminate dispatcher - continue processing
                            tracing::warn!(
                                "Notification middleware processing failed for {}: {}",
                                jsonrpc_notification.method,
                                e
                            );
                        }

                        // Forward to notification handler if registered
                        if let Some(tx) = &self.notification_tx {
                            // Clone the sender because send() requires &mut self
                            #[allow(unused_mut)]
                            let mut tx_clone = tx.clone();
                            if let Err(e) = tx_clone.send(notification).await {
                                tracing::debug!("Notification channel closed: {}", e);
                            }
                        }

                        // Continue loop to wait for the actual response
                    },
                    crate::types::TransportMessage::Request { id, request } => {
                        // Any inbound request at a client is server -> client by
                        // definition (the MCP host direction). Answer it from the
                        // registered host handlers, then keep waiting for the
                        // original response (request_id stays pending). A failed
                        // reply `send` propagates via `?` and is cleaned up at
                        // the single exit point below.
                        let response = self.dispatch_host_request(id, request).await;
                        self.transport
                            .write()
                            .await
                            .send(crate::types::TransportMessage::Response(response))
                            .await?;
                    },
                }
            }
        }
        .await;

        // Single WR-04 exit-cleanup invariant: remove the pending entry on any
        // error path (the happy path already removed it above).
        if result.is_err() {
            self.active_requests.write().await.remove(&request_id);
        }
        result
    }

    /// Answer an inbound server -> client request from the host registry.
    ///
    /// Returns a [`JSONRPCResponse`](crate::types::JSONRPCResponse) that the
    /// caller sends back over the transport. A known request kind with no
    /// registered handler yields `-32601` (method-not-found); a
    /// handler/provider failure yields a sanitized `-32603` (the raw error is
    /// logged locally, never forwarded to the remote server). The connection is
    /// never dropped.
    async fn dispatch_host_request(
        &self,
        id: RequestId,
        request: Request,
    ) -> crate::types::JSONRPCResponse {
        use crate::client::host::{classify_host_request, HostRequestKind};
        match classify_host_request(&request) {
            HostRequestKind::Sampling => self.dispatch_host_sampling(id, request).await,
            HostRequestKind::Elicitation => self.dispatch_host_elicitation(id, request).await,
            HostRequestKind::Roots => self.dispatch_host_roots(id).await,
            // Spec MUST: answer inbound `ping` with an empty-object success
            // result so keepalive pings from servers/proxies do not fail (and
            // do not tear down the connection).
            HostRequestKind::Ping => {
                crate::types::JSONRPCResponse::success(id, serde_json::json!({}))
            },
            HostRequestKind::Unhandled => Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            ),
        }
    }

    /// Route a classified sampling request through the two-stage host approval
    /// model and the registered sampling handler.
    ///
    /// # Policy-denial taxonomy
    ///
    /// Sampling has two host-side access-control stages, both applied ONLY to
    /// the sampling path (never elicitation/roots):
    ///
    /// 1. **Preflight** ([`ClientBuilder::on_sampling_approval`]): an optional
    ///    gate (default-allow) that, when registered, runs BEFORE the handler. A
    ///    [`ApprovalDecision::Deny`] here prevents the LLM call entirely — no
    ///    tokens are billed — genuinely mitigating coerced / denial-of-wallet
    ///    sampling. When no preflight callback is registered, the handler runs
    ///    (default allow).
    /// 2. **Result review** ([`ClientBuilder::on_sampling_result_review`]): an
    ///    optional post-generation stage that sees the produced completion and
    ///    can deny after the fact. Its default (no callback) is pass-through.
    ///
    /// A denial from either stage returns a sanitized `-32603` response with the
    /// GENERIC message `"request denied by host policy"`. The callback's
    /// `Deny(reason)` is logged locally via `tracing::warn!` and is NEVER
    /// forwarded to the remote server (avoids leaking local host policy). The
    /// connection is kept alive — a denial is a normal JSON-RPC error response,
    /// not a transport failure.
    async fn dispatch_host_sampling(
        &self,
        id: RequestId,
        request: Request,
    ) -> crate::types::JSONRPCResponse {
        // At least one sampling handler (legacy or WithTools) must be registered.
        if self.host_registry.sampling.is_none() && self.host_registry.sampling_with_tools.is_none()
        {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        }
        let Some(params) = Self::extract_sampling_params(request) else {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        };

        // (1) PREFLIGHT approval gate — runs BEFORE any handler so a denial
        // prevents the LLM call entirely (no tokens billed). It operates on the
        // request params, so it is IDENTICAL for the legacy and WithTools paths
        // (the wallet gate is never weakened by the WithTools surface).
        if let Some(approval) = &self.host_registry.approval {
            if let ApprovalDecision::Deny(reason) = approval(params.clone()).await {
                tracing::warn!(%reason, "sampling denied by host preflight");
                return Self::host_error(
                    id,
                    crate::error::ErrorCode::INTERNAL_ERROR.as_i32(),
                    "request denied by host policy",
                );
            }
        }

        // Prefer the tool-aware handler; otherwise the legacy single-content one.
        // A client that registered only a legacy handler keeps its EXACT current
        // wire behavior (serializes a `CreateMessageResult`).
        if self.host_registry.sampling_with_tools.is_some() {
            self.answer_sampling_with_tools(id, params).await
        } else {
            self.answer_sampling_legacy(id, params).await
        }
    }

    /// Legacy single-content sampling answer path (unchanged behavior).
    async fn answer_sampling_legacy(
        &self,
        id: RequestId,
        params: CreateMessageParams,
    ) -> crate::types::JSONRPCResponse {
        let handler = self
            .host_registry
            .sampling
            .as_ref()
            .expect("caller checked a legacy handler is present");

        // Capture an owned clone of the params for result review ONLY when a
        // review callback is registered; otherwise the handler consumes
        // `params` below with zero extra clones.
        let review_params = self
            .host_registry
            .result_review
            .is_some()
            .then(|| params.clone());

        let result = match handler.handle_create_message(params).await {
            Ok(result) => result,
            Err(e) => return Self::host_handler_error(id, "sampling/createMessage", &e),
        };

        // Optional post-generation review (default pass-through). `review_params`
        // is `Some` exactly when `result_review` is `Some`, so the pair matches.
        if let (Some(review), Some(params)) = (&self.host_registry.result_review, review_params) {
            if let ApprovalDecision::Deny(reason) = review(params, result.clone()).await {
                tracing::warn!(%reason, "sampling denied by host result review");
                return Self::host_error(
                    id,
                    crate::error::ErrorCode::INTERNAL_ERROR.as_i32(),
                    "request denied by host policy",
                );
            }
        }

        Self::host_ok(id, &result)
    }

    /// Tool-aware (`WithTools`) sampling answer path.
    ///
    /// The preflight gate has already run in `dispatch_host_sampling`. The
    /// optional result-review gate is NOT weakened: when a reviewer is
    /// registered, the `WithTools` completion is projected to a single-content
    /// [`CreateMessageResult`] (tool blocks rendered as a text summary) so the
    /// reviewer still sees the completion and can deny it. The returned wire
    /// value remains the full `CreateMessageResultWithTools`.
    async fn answer_sampling_with_tools(
        &self,
        id: RequestId,
        params: CreateMessageParams,
    ) -> crate::types::JSONRPCResponse {
        let handler = self
            .host_registry
            .sampling_with_tools
            .as_ref()
            .expect("caller checked a WithTools handler is present");

        let review_params = self
            .host_registry
            .result_review
            .is_some()
            .then(|| params.clone());

        let result = match handler.handle_create_message_with_tools(params).await {
            Ok(result) => result,
            Err(e) => return Self::host_handler_error(id, "sampling/createMessage", &e),
        };

        if let (Some(review), Some(params)) = (&self.host_registry.result_review, review_params) {
            let projected = Self::project_with_tools_for_review(&result);
            if let ApprovalDecision::Deny(reason) = review(params, projected).await {
                tracing::warn!(%reason, "sampling denied by host result review");
                return Self::host_error(
                    id,
                    crate::error::ErrorCode::INTERNAL_ERROR.as_i32(),
                    "request denied by host policy",
                );
            }
        }

        Self::host_ok(id, &result)
    }

    /// Project a [`CreateMessageResultWithTools`] into a single-content
    /// [`CreateMessageResult`] for the (optional) result-review gate. Tool
    /// blocks have no single-`Content` counterpart, so they are rendered as a
    /// short text marker; the reviewer still sees enough to allow or deny.
    fn project_with_tools_for_review(
        result: &crate::types::sampling::CreateMessageResultWithTools,
    ) -> crate::types::sampling::CreateMessageResult {
        use crate::types::sampling::SamplingMessageContent as Smc;
        let text = result
            .content
            .iter()
            .map(|c| match c {
                Smc::Text { text, .. } => text.clone(),
                Smc::Image { .. } => "[image]".to_string(),
                Smc::Audio { .. } => "[audio]".to_string(),
                Smc::ToolUse { name, id, .. } => format!("[tool_use {name} {id}]"),
                Smc::ToolResult { tool_use_id, .. } => format!("[tool_result {tool_use_id}]"),
            })
            .collect::<Vec<_>>()
            .join(" ");
        let mut cmr = crate::types::sampling::CreateMessageResult::new(
            crate::types::Content::text(text),
            &result.model,
        );
        if let Some(reason) = &result.stop_reason {
            cmr = cmr.with_stop_reason(reason.clone());
        }
        cmr
    }

    /// Route a classified elicitation request to the registered host handler.
    async fn dispatch_host_elicitation(
        &self,
        id: RequestId,
        request: Request,
    ) -> crate::types::JSONRPCResponse {
        let Some(handler) = &self.host_registry.elicitation else {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        };
        // Extract the single elicitation parse variant inline (server-side
        // `elicitation/create`); anything else is not routable here.
        let Request::Server(server) = request else {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        };
        let crate::types::ServerRequest::ElicitationCreate(params) = *server else {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        };
        match handler.handle_elicitation(*params).await {
            Ok(result) => Self::host_ok(id, &result),
            Err(e) => Self::host_handler_error(id, "elicitation/create", &e),
        }
    }

    /// Answer a classified `roots/list` request from the registered provider.
    async fn dispatch_host_roots(&self, id: RequestId) -> crate::types::JSONRPCResponse {
        let Some(provider) = &self.host_registry.roots else {
            return Self::host_error(
                id,
                crate::error::ErrorCode::METHOD_NOT_FOUND.as_i32(),
                "Method not found",
            );
        };
        match provider().await {
            Ok(result) => Self::host_ok(id, &result),
            Err(e) => Self::host_handler_error(id, "roots/list", &e),
        }
    }

    /// Extract [`CreateMessageParams`] from either inbound sampling parse
    /// variant (client-alias or server), handling the parse ambiguity.
    fn extract_sampling_params(request: Request) -> Option<CreateMessageParams> {
        match request {
            Request::Client(client) => match *client {
                ClientRequest::CreateMessage(params) => Some(*params),
                _ => None,
            },
            Request::Server(server) => match *server {
                crate::types::ServerRequest::CreateMessage(params) => Some(*params),
                _ => None,
            },
        }
    }

    /// Build a successful host response, serializing the handler result.
    fn host_ok<S: serde::Serialize>(id: RequestId, value: &S) -> crate::types::JSONRPCResponse {
        match serde_json::to_value(value) {
            Ok(v) => crate::types::JSONRPCResponse::success(id, v),
            Err(e) => {
                tracing::error!("failed to serialize host response: {e}");
                Self::host_error(
                    id,
                    crate::error::ErrorCode::INTERNAL_ERROR.as_i32(),
                    "Internal error handling host request",
                )
            },
        }
    }

    /// Build a JSON-RPC error response that keeps the connection alive.
    ///
    /// All host error responses are sanitized: only the generic `message`
    /// passed here crosses the wire. Raw handler errors and policy-denial
    /// reasons are logged locally by the caller (never forwarded to the remote
    /// server), so local host policy is not leaked. Callers pass the
    /// appropriate [`ErrorCode`](crate::error::ErrorCode) constant:
    /// - `METHOD_NOT_FOUND` for a known request kind with no registered handler,
    /// - `INTERNAL_ERROR` for a sanitized policy denial (a preflight or
    ///   result-review callback returning [`ApprovalDecision::Deny`]) or a
    ///   handler/provider/serialization failure.
    fn host_error(id: RequestId, code: i32, message: &str) -> crate::types::JSONRPCResponse {
        crate::types::JSONRPCResponse::error(
            id,
            crate::types::jsonrpc::JSONRPCError::new(code, message),
        )
    }

    /// Log a handler/provider failure locally and return a sanitized
    /// `INTERNAL_ERROR`.
    fn host_handler_error(
        id: RequestId,
        method: &str,
        err: &Error,
    ) -> crate::types::JSONRPCResponse {
        tracing::error!("host handler for {method} failed: {err}");
        Self::host_error(
            id,
            crate::error::ErrorCode::INTERNAL_ERROR.as_i32(),
            "Internal error handling host request",
        )
    }

    /// Send a notification.
    async fn send_notification(&self, notification: Notification) -> Result<()> {
        let message = crate::types::TransportMessage::Notification(notification);
        self.transport.write().await.send(message).await
    }
}

/// Builder for creating clients with custom configuration.
///
/// # Examples
///
/// ```rust
/// use pmcp::{ClientBuilder, StdioTransport};
///
/// # async fn example() -> Result<(), pmcp::Error> {
/// // Basic client builder
/// let transport = StdioTransport::new();
/// let client = ClientBuilder::new(transport)
///     .enforce_strict_capabilities(true)
///     .build();
///
/// // Client with debounced notifications
/// let transport2 = StdioTransport::new();
/// let debounced_client = ClientBuilder::new(transport2)
///     .debounced_notifications(vec![
///         "notifications/progress".to_string(),
///         "notifications/log".to_string(),
///     ])
///     .enforce_strict_capabilities(false)
///     .build();
///
/// // Chain multiple configurations
/// let transport3 = StdioTransport::new();
/// let configured_client = ClientBuilder::new(transport3)
///     .enforce_strict_capabilities(true)
///     .debounced_notifications(vec!["notifications/resources/changed".to_string()])
///     .build();
/// # Ok(())
/// # }
/// ```
pub struct ClientBuilder<T: Transport> {
    transport: T,
    options: ProtocolOptions,
    middleware_chain: EnhancedMiddlewareChain,
    host_registry: crate::client::host::ClientHostRegistry,
}

impl<T: Transport> std::fmt::Debug for ClientBuilder<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientBuilder")
            .field("transport", &"<Transport>")
            .field("options", &self.options)
            .finish()
    }
}

impl<T: Transport> ClientBuilder<T> {
    /// Create a new client builder.
    pub fn new(transport: T) -> Self {
        Self {
            transport,
            options: ProtocolOptions::default(),
            middleware_chain: EnhancedMiddlewareChain::new(),
            host_registry: crate::client::host::ClientHostRegistry::default(),
        }
    }

    /// Set whether to enforce strict capabilities.
    pub fn enforce_strict_capabilities(mut self, enforce: bool) -> Self {
        self.options.enforce_strict_capabilities = enforce;
        self
    }

    /// Set debounced notification methods.
    pub fn debounced_notifications(mut self, methods: Vec<String>) -> Self {
        self.options.debounced_notification_methods = methods;
        self
    }

    /// Add middleware to the client.
    ///
    /// Middleware are executed in priority order (Critical → High → Normal → Low → Lowest).
    /// Multiple middleware with the same priority are executed in the order they were added.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{ClientBuilder, StdioTransport};
    /// use pmcp::shared::MetricsMiddleware;
    /// use std::sync::Arc;
    ///
    /// # async fn example() -> Result<(), pmcp::Error> {
    /// let transport = StdioTransport::new();
    /// let client = ClientBuilder::new(transport)
    ///     .with_middleware(Arc::new(MetricsMiddleware::new("my-service".to_string())))
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_middleware(
        mut self,
        middleware: Arc<dyn crate::shared::AdvancedMiddleware>,
    ) -> Self {
        self.middleware_chain.add(middleware);
        self
    }

    /// Add protocol-level middleware to the client.
    ///
    /// This is an alias for `with_middleware()` that provides explicit naming to distinguish
    /// protocol middleware (operates on JSON-RPC messages) from HTTP middleware
    /// (operates on HTTP requests/responses via `StreamableHttpTransportConfigBuilder`).
    ///
    /// Middleware are executed in priority order (Critical → High → Normal → Low → Lowest).
    /// Multiple middleware with the same priority are executed in the order they were added.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{ClientBuilder, StdioTransport};
    /// use pmcp::shared::MetricsMiddleware;
    /// use std::sync::Arc;
    ///
    /// # async fn example() -> Result<(), pmcp::Error> {
    /// let transport = StdioTransport::new();
    /// let client = ClientBuilder::new(transport)
    ///     .with_protocol_middleware(Arc::new(MetricsMiddleware::new("my-service".to_string())))
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_protocol_middleware(
        self,
        middleware: Arc<dyn crate::shared::AdvancedMiddleware>,
    ) -> Self {
        self.with_middleware(middleware)
    }

    /// Set the entire middleware chain.
    ///
    /// This replaces any previously configured middleware.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use pmcp::{ClientBuilder, StdioTransport};
    /// use pmcp::shared::EnhancedMiddlewareChain;
    ///
    /// # async fn example() -> Result<(), pmcp::Error> {
    /// let mut chain = EnhancedMiddlewareChain::new();
    /// // Add middleware to chain...
    ///
    /// let transport = StdioTransport::new();
    /// let client = ClientBuilder::new(transport)
    ///     .middleware_chain(chain)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn middleware_chain(mut self, chain: EnhancedMiddlewareChain) -> Self {
        self.middleware_chain = chain;
        self
    }

    /// Register a host sampling handler answering inbound
    /// `sampling/createMessage` requests (the MCP host direction).
    ///
    /// This is the INVERSE of [`Client::create_message`] (the LLM-server
    /// pattern). See [`crate::client::host`] for the full disambiguation.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use pmcp::{ClientBuilder, StdioTransport};
    /// use pmcp::client::host::HostSamplingHandler;
    /// use pmcp::types::sampling::{CreateMessageParams, CreateMessageResult};
    /// use pmcp::types::Content;
    /// use async_trait::async_trait;
    ///
    /// struct MyHost;
    /// #[async_trait]
    /// impl HostSamplingHandler for MyHost {
    ///     async fn handle_create_message(
    ///         &self,
    ///         _params: CreateMessageParams,
    ///     ) -> pmcp::Result<CreateMessageResult> {
    ///         Ok(CreateMessageResult::new(Content::text("hi"), "my-model"))
    ///     }
    /// }
    ///
    /// let client = ClientBuilder::new(StdioTransport::new())
    ///     .on_sampling(MyHost)
    ///     .build();
    /// ```
    pub fn on_sampling(mut self, handler: impl host::HostSamplingHandler + 'static) -> Self {
        self.host_registry.sampling = Some(Arc::new(handler));
        self
    }

    /// Register a **tool-aware** host sampling handler answering inbound
    /// `sampling/createMessage` requests with a
    /// [`CreateMessageResultWithTools`](crate::types::sampling::CreateMessageResultWithTools).
    ///
    /// This is the `WithTools` counterpart of [`ClientBuilder::on_sampling`]: the
    /// handler can return `tool_use` / `tool_result` content blocks that the
    /// legacy single-`Content` result cannot express (MCP 2025-11-25). When both
    /// a legacy and a `WithTools` handler are registered, the `WithTools` handler is
    /// preferred. The preflight approval gate
    /// ([`ClientBuilder::on_sampling_approval`]) still runs unchanged; the
    /// optional result-review gate sees a single-content projection of the
    /// completion (tool blocks rendered as a text marker) so it is never
    /// silently bypassed.
    pub fn on_sampling_with_tools(
        mut self,
        handler: impl host::HostSamplingHandlerWithTools + 'static,
    ) -> Self {
        self.host_registry.sampling_with_tools = Some(Arc::new(handler));
        self
    }

    /// Register a host elicitation handler answering inbound
    /// `elicitation/create` requests.
    pub fn on_elicitation(mut self, handler: impl host::HostElicitationHandler + 'static) -> Self {
        self.host_registry.elicitation = Some(Arc::new(handler));
        self
    }

    /// Register a roots provider answering inbound `roots/list` requests.
    ///
    /// The provider is generic over any closure returning a future that yields
    /// `Result<ListRootsResult>`, so callers never construct the
    /// [`RootsProvider`] alias by hand.
    pub fn on_roots<F, Fut>(mut self, provider: F) -> Self
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<crate::types::roots::ListRootsResult>>
            + Send
            + 'static,
    {
        self.host_registry.roots = Some(Arc::new(move || Box::pin(provider())));
        self
    }

    /// Register an optional pre-handler sampling approval gate.
    ///
    /// The callback is generic over any closure taking owned
    /// [`CreateMessageParams`] and returning a future that yields an
    /// [`ApprovalDecision`]. It is invoked by `dispatch_host_sampling` BEFORE
    /// the sampling handler runs as of this phase, so an
    /// [`ApprovalDecision::Deny`] prevents the LLM
    /// call entirely. The gate is optional and default-allow: when none is
    /// registered, inbound sampling reaches the handler unchallenged.
    pub fn on_sampling_approval<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(CreateMessageParams) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = host::ApprovalDecision> + Send + 'static,
    {
        self.host_registry.approval = Some(Arc::new(move |params| Box::pin(callback(params))));
        self
    }

    /// Register an optional post-handler sampling result review.
    ///
    /// The callback receives the owned request params and the produced
    /// [`CreateMessageResult`] and returns a future yielding an
    /// [`ApprovalDecision`]. It is invoked by `dispatch_host_sampling` AFTER the
    /// sampling handler runs as of this phase, so an
    /// [`ApprovalDecision::Deny`] suppresses the
    /// completion. It is optional and default pass-through: when none is
    /// registered the completion is returned as-is.
    pub fn on_sampling_result_review<F, Fut>(mut self, callback: F) -> Self
    where
        F: Fn(CreateMessageParams, CreateMessageResult) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = host::ApprovalDecision> + Send + 'static,
    {
        self.host_registry.result_review = Some(Arc::new(move |params, result| {
            Box::pin(callback(params, result))
        }));
        self
    }

    /// Build the client.
    pub fn build(self) -> Client<T> {
        let mut client = Client::with_options(
            self.transport,
            Implementation::new("pmcp-client", env!("CARGO_PKG_VERSION")),
            self.options,
        );
        // Replace the default middleware chain with the configured one
        client.middleware_chain = Arc::new(RwLock::new(self.middleware_chain));
        // Thread the configured host registry onto the client.
        client.host_registry = self.host_registry;
        client
    }
}

impl<T: Transport> Clone for Client<T> {
    fn clone(&self) -> Self {
        Self {
            transport: self.transport.clone(),
            protocol: self.protocol.clone(),
            middleware_chain: self.middleware_chain.clone(),
            capabilities: self.capabilities.clone(),
            server_capabilities: self.server_capabilities.clone(),
            server_version: self.server_version.clone(),
            instructions: self.instructions.clone(),
            initialized: self.initialized,
            info: self.info.clone(),
            notification_tx: self.notification_tx.clone(),
            active_requests: self.active_requests.clone(),
            options: self.options.clone(),
            host_registry: self.host_registry.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shared::Transport;
    use crate::types::{
        jsonrpc::{JSONRPCError, ResponsePayload},
        JSONRPCResponse, ProgressNotification, ProgressToken, TransportMessage,
    };
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::{Arc, Mutex};

    /// Mock transport for testing
    #[derive(Debug)]
    struct MockTransport {
        responses: Arc<Mutex<Vec<TransportMessage>>>,
        sent_messages: Arc<Mutex<Vec<TransportMessage>>>,
    }

    impl MockTransport {
        fn new() -> Self {
            Self {
                responses: Arc::new(Mutex::new(Vec::new())),
                sent_messages: Arc::new(Mutex::new(Vec::new())),
            }
        }

        fn with_responses(responses: Vec<TransportMessage>) -> Self {
            Self {
                responses: Arc::new(Mutex::new(responses)),
                sent_messages: Arc::new(Mutex::new(Vec::new())),
            }
        }

        #[allow(dead_code)]
        fn add_response(&self, response: TransportMessage) {
            self.responses.lock().unwrap().push(response);
        }
    }

    #[async_trait]
    impl Transport for MockTransport {
        async fn send(&mut self, message: TransportMessage) -> Result<()> {
            self.sent_messages.lock().unwrap().push(message);
            Ok(())
        }

        async fn receive(&mut self) -> Result<TransportMessage> {
            self.responses
                .lock()
                .unwrap()
                .pop()
                .ok_or_else(|| Error::protocol_msg("No more responses"))
        }

        async fn close(&mut self) -> Result<()> {
            Ok(())
        }
    }

    #[test]
    fn test_client_creation() {
        let transport = MockTransport::new();
        let client = Client::new(transport);
        assert!(!client.initialized);
        assert_eq!(client.info.name, "pmcp-client");
    }

    #[test]
    fn test_client_with_info() {
        let transport = MockTransport::new();
        let info = Implementation::new("test-client", "1.0.0");
        let client = Client::with_info(transport, info);
        assert_eq!(client.info.name, "test-client");
        assert_eq!(client.info.version, "1.0.0");
    }

    // === ClientOptions wiring tests ===

    #[test]
    fn test_client_new_uses_default_options() {
        let transport = MockTransport::new();
        let client = Client::new(transport);
        assert_eq!(client.options.max_iterations, 100);
    }

    #[test]
    fn test_client_with_client_options_threads_value() {
        let transport = MockTransport::new();
        let opts = ClientOptions {
            max_iterations: 7,
            ..Default::default()
        };
        let client = Client::with_client_options(transport, opts);
        assert_eq!(client.options.max_iterations, 7);
    }

    #[test]
    fn test_client_with_options_preserves_default_client_options() {
        let transport = MockTransport::new();
        let client = Client::with_options(
            transport,
            Implementation::default(),
            ProtocolOptions::default(),
        );
        assert_eq!(client.options.max_iterations, 100);
    }

    // === Client host dispatch unit tests (HOST-01/HOST-05) ===

    struct MockHostSampling;

    #[async_trait]
    impl host::HostSamplingHandler for MockHostSampling {
        async fn handle_create_message(
            &self,
            _params: CreateMessageParams,
        ) -> Result<CreateMessageResult> {
            Ok(CreateMessageResult::new(
                crate::types::Content::text("mock host completion"),
                "mock-host-model",
            ))
        }
    }

    struct FailingHostSampling;

    #[async_trait]
    impl host::HostSamplingHandler for FailingHostSampling {
        async fn handle_create_message(
            &self,
            _params: CreateMessageParams,
        ) -> Result<CreateMessageResult> {
            Err(Error::protocol_msg(
                "secret path /etc/passwd leaked in error",
            ))
        }
    }

    fn sampling_client_alias_request() -> Request {
        // Inbound sampling parses as the CLIENT variant (parse ambiguity).
        Request::Client(Box::new(ClientRequest::CreateMessage(Box::new(
            CreateMessageParams::new(Vec::new()),
        ))))
    }

    #[tokio::test]
    async fn test_dispatch_sampling_alias_reaches_handler() {
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(MockHostSampling)
            .build();
        let id = RequestId::from(1i64);
        let response = client
            .dispatch_host_request(id, sampling_client_alias_request())
            .await;
        assert!(
            response.is_success(),
            "inbound sampling (client-alias parse) must reach the host handler, got: {response:?}"
        );
    }

    #[tokio::test]
    async fn test_dispatch_known_unhandled_returns_method_not_found() {
        // No handlers registered; a KNOWN roots/list request must yield -32601.
        let client = Client::new(MockTransport::new());
        let id = RequestId::from(2i64);
        let request = Request::Server(Box::new(crate::types::ServerRequest::ListRoots));
        let response = client.dispatch_host_request(id, request).await;
        match response.payload {
            ResponsePayload::Error(e) => assert_eq!(e.code, -32601),
            ResponsePayload::Result(r) => panic!("expected -32601 error, got result: {r:?}"),
        }
    }

    #[tokio::test]
    async fn test_dispatch_handler_error_is_sanitized_32603() {
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(FailingHostSampling)
            .build();
        let id = RequestId::from(3i64);
        let response = client
            .dispatch_host_request(id, sampling_client_alias_request())
            .await;
        match response.payload {
            ResponsePayload::Error(e) => {
                assert_eq!(e.code, -32603);
                // Sanitized: the raw handler error text must NOT cross the wire.
                assert!(
                    !e.message.contains("/etc/passwd"),
                    "handler error text must be sanitized, got: {}",
                    e.message
                );
            },
            ResponsePayload::Result(r) => panic!("expected -32603 error, got result: {r:?}"),
        }
    }

    #[tokio::test]
    async fn test_dispatch_roots_provider_answers() {
        let client = ClientBuilder::new(MockTransport::new())
            .on_roots(|| async { Ok(crate::types::roots::ListRootsResult { roots: Vec::new() }) })
            .build();
        let id = RequestId::from(4i64);
        let request = Request::Server(Box::new(crate::types::ServerRequest::ListRoots));
        let response = client.dispatch_host_request(id, request).await;
        assert!(
            response.is_success(),
            "roots provider must answer roots/list"
        );
    }

    // === Sampling approval (preflight + result-review) unit tests (HOST-04) ===

    /// Host sampling handler that flips an `AtomicBool` when invoked, so tests
    /// can prove whether the LLM call happened.
    struct TrackingHostSampling {
        invoked: Arc<std::sync::atomic::AtomicBool>,
    }

    #[async_trait]
    impl host::HostSamplingHandler for TrackingHostSampling {
        async fn handle_create_message(
            &self,
            _params: CreateMessageParams,
        ) -> Result<CreateMessageResult> {
            self.invoked
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(CreateMessageResult::new(
                crate::types::Content::text("tracked completion"),
                "tracked-model",
            ))
        }
    }

    fn assert_policy_denied(response: &JSONRPCResponse) {
        match &response.payload {
            ResponsePayload::Error(e) => {
                assert_eq!(e.code, -32603, "policy denial must be -32603");
                assert_eq!(
                    e.message, "request denied by host policy",
                    "policy denial message must be the generic sanitized string"
                );
            },
            ResponsePayload::Result(r) => panic!("expected -32603 denial, got result: {r:?}"),
        }
    }

    #[tokio::test]
    async fn test_sampling_no_preflight_runs_handler() {
        // (a) No preflight callback => handler runs, completion returned.
        let invoked = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(TrackingHostSampling {
                invoked: invoked.clone(),
            })
            .build();
        let response = client
            .dispatch_host_request(RequestId::from(10i64), sampling_client_alias_request())
            .await;
        assert!(response.is_success(), "default (no preflight) must allow");
        assert!(
            invoked.load(std::sync::atomic::Ordering::SeqCst),
            "handler must run when no preflight is registered"
        );
    }

    #[tokio::test]
    async fn test_sampling_preflight_deny_skips_handler() {
        // (b) Preflight Deny => handler is NOT called (denial-of-wallet fix) and
        // the raw deny reason must NOT cross the wire.
        let invoked = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(TrackingHostSampling {
                invoked: invoked.clone(),
            })
            .on_sampling_approval(|_params| async {
                host::ApprovalDecision::Deny("local-secret-reason".to_string())
            })
            .build();
        let response = client
            .dispatch_host_request(RequestId::from(11i64), sampling_client_alias_request())
            .await;
        assert_policy_denied(&response);
        assert!(
            !invoked.load(std::sync::atomic::Ordering::SeqCst),
            "handler must NOT run when preflight denies (no LLM call, no tokens)"
        );
        // The raw deny reason must never be forwarded.
        if let ResponsePayload::Error(e) = &response.payload {
            assert!(
                !e.message.contains("local-secret-reason"),
                "deny reason must be logged locally, not forwarded: {}",
                e.message
            );
        }
    }

    #[tokio::test]
    async fn test_sampling_preflight_allow_runs_handler() {
        // (c) Preflight Allow => completion returned.
        let invoked = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(TrackingHostSampling {
                invoked: invoked.clone(),
            })
            .on_sampling_approval(|_params| async { host::ApprovalDecision::Allow })
            .build();
        let response = client
            .dispatch_host_request(RequestId::from(12i64), sampling_client_alias_request())
            .await;
        assert!(
            response.is_success(),
            "preflight Allow must return completion"
        );
        assert!(
            invoked.load(std::sync::atomic::Ordering::SeqCst),
            "handler must run after preflight Allow"
        );
    }

    #[tokio::test]
    async fn test_sampling_result_review_deny_after_handler() {
        // (d) result_review Deny after an allowed preflight => -32603, but the
        // handler WAS called (generation happened, then was suppressed).
        let invoked = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(TrackingHostSampling {
                invoked: invoked.clone(),
            })
            .on_sampling_result_review(|_params, _result| async {
                host::ApprovalDecision::Deny("post-gen-reason".to_string())
            })
            .build();
        let response = client
            .dispatch_host_request(RequestId::from(13i64), sampling_client_alias_request())
            .await;
        assert_policy_denied(&response);
        assert!(
            invoked.load(std::sync::atomic::Ordering::SeqCst),
            "handler runs before result review can deny"
        );
    }

    #[tokio::test]
    async fn test_sampling_result_review_absent_is_passthrough() {
        // (e) result_review absent => pass-through (Allow), completion returned.
        let invoked = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(TrackingHostSampling {
                invoked: invoked.clone(),
            })
            .on_sampling_approval(|_params| async { host::ApprovalDecision::Allow })
            .build();
        let response = client
            .dispatch_host_request(RequestId::from(14i64), sampling_client_alias_request())
            .await;
        assert!(
            response.is_success(),
            "absent result_review must pass through"
        );
    }

    // === Capability derivation unit tests (HOST-05) ===

    struct MockHostElicit;

    #[async_trait]
    impl host::HostElicitationHandler for MockHostElicit {
        async fn handle_elicitation(
            &self,
            _params: crate::types::elicitation::ElicitRequestParams,
        ) -> Result<crate::types::elicitation::ElicitResult> {
            Ok(crate::types::elicitation::ElicitResult {
                action: crate::types::elicitation::ElicitAction::Accept,
                content: None,
            })
        }
    }

    #[test]
    fn test_capability_sampling_registered_is_present() {
        // (a) handler registered + default caps => sampling present.
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(MockHostSampling)
            .build();
        let mut caps = ClientCapabilities::default();
        client.derive_host_capabilities(&mut caps);
        assert!(caps.sampling.is_some(), "registered sampling => present");
    }

    #[test]
    fn test_capability_sampling_unregistered_default_is_absent() {
        // (b) no handler + default caps => sampling absent.
        let client = Client::new(MockTransport::new());
        let mut caps = ClientCapabilities::default();
        client.derive_host_capabilities(&mut caps);
        assert!(caps.sampling.is_none(), "unregistered sampling => absent");
    }

    #[test]
    fn test_capability_sampling_anti_lie_discards_caller_value() {
        // (c) ANTI-LIE: no handler + caller-set sampling => forced None.
        let client = Client::new(MockTransport::new());
        let mut caps = ClientCapabilities {
            sampling: Some(crate::types::capabilities::SamplingCapabilities::default()),
            ..Default::default()
        };
        client.derive_host_capabilities(&mut caps);
        assert!(
            caps.sampling.is_none(),
            "caller-set sampling with no handler must be discarded (anti-capability-lie)"
        );
    }

    #[test]
    fn test_capability_sampling_preserves_caller_detail() {
        // (d) PRESERVATION: handler present + caller-configured sub-field =>
        // that exact detail is preserved (not reset to default()).
        let client = ClientBuilder::new(MockTransport::new())
            .on_sampling(MockHostSampling)
            .build();
        let mut caps = ClientCapabilities {
            sampling: Some(crate::types::capabilities::SamplingCapabilities {
                models: Some(vec!["gpt-4o".to_string()]),
                ..Default::default()
            }),
            ..Default::default()
        };
        client.derive_host_capabilities(&mut caps);
        let sampling = caps.sampling.expect("handler present => sampling kept");
        assert_eq!(
            sampling.models,
            Some(vec!["gpt-4o".to_string()]),
            "caller-configured models must be preserved, not reset to default"
        );
    }

    #[test]
    fn test_capability_elicitation_and_roots_parallel() {
        // (e) elicitation + roots follow the same rule.
        // Registered => present.
        let client = ClientBuilder::new(MockTransport::new())
            .on_elicitation(MockHostElicit)
            .on_roots(|| async { Ok(crate::types::roots::ListRootsResult { roots: Vec::new() }) })
            .build();
        let mut caps = ClientCapabilities::default();
        client.derive_host_capabilities(&mut caps);
        assert!(
            caps.elicitation.is_some(),
            "registered elicitation => present"
        );
        assert!(caps.roots.is_some(), "registered roots => present");

        // Unregistered + caller-set => discarded (anti-lie) for both.
        let bare = Client::new(MockTransport::new());
        let mut caps2 = ClientCapabilities {
            elicitation: Some(crate::types::capabilities::ElicitationCapabilities::default()),
            roots: Some(crate::types::capabilities::RootsCapabilities::default()),
            ..Default::default()
        };
        bare.derive_host_capabilities(&mut caps2);
        assert!(caps2.elicitation.is_none(), "elicitation anti-lie");
        assert!(caps2.roots.is_none(), "roots anti-lie");
    }

    #[test]
    fn test_capability_derivation_leaves_tasks_and_experimental_untouched() {
        // (f) tasks / experimental are never modified by host derivation.
        let client = Client::new(MockTransport::new());
        let mut experimental = HashMap::new();
        experimental.insert("custom".to_string(), serde_json::json!(true));
        let mut caps = ClientCapabilities {
            tasks: Some(crate::types::capabilities::ClientTasksCapability::default()),
            experimental: Some(experimental),
            ..Default::default()
        };
        client.derive_host_capabilities(&mut caps);
        assert!(caps.tasks.is_some(), "tasks must be preserved");
        assert_eq!(
            caps.experimental.and_then(|e| e.get("custom").cloned()),
            Some(serde_json::json!(true)),
            "experimental must be preserved"
        );
    }

    // === Typed-helper unit tests ===

    #[tokio::test]
    async fn test_call_tool_typed_serialize_error_maps_to_validation() {
        use serde::Serialize;
        // A type whose Serialize impl always errors.
        struct Bad;
        impl Serialize for Bad {
            fn serialize<S: serde::Serializer>(
                &self,
                _: S,
            ) -> std::result::Result<S::Ok, S::Error> {
                Err(serde::ser::Error::custom("nope"))
            }
        }
        let transport = MockTransport::new();
        let client = Client::new(transport);
        let err = client.call_tool_typed("any", &Bad).await.unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("call_tool_typed arguments"), "got: {msg}");
        assert!(msg.contains("nope"), "serde error must surface: {msg}");
        assert!(matches!(err, Error::Validation(_)));
    }

    #[tokio::test]
    async fn test_get_prompt_typed_non_object_rejected() {
        let transport = MockTransport::new();
        let client = Client::new(transport);
        // Vec<i32> serializes to Value::Array, which is non-object.
        let err = client
            .get_prompt_typed("p", &vec![1, 2, 3])
            .await
            .unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("must serialize to a JSON object"),
            "got: {msg}"
        );
        assert!(matches!(err, Error::Validation(_)));
    }

    #[tokio::test]
    async fn test_get_prompt_typed_string_values_not_quoted() {
        use serde::Serialize;
        #[derive(Serialize)]
        struct Args {
            topic: String,
            length: u32,
            verbose: bool,
            ignored: Option<String>,
        }
        // Unit-test the coercion directly by building the intermediate HashMap
        // in-situ. Wire round-trip is covered in tests/list_all_pagination.rs;
        // here we only care that the leaf-coercion rules are honoured.
        let args = Args {
            topic: "rust".into(),
            length: 200,
            verbose: true,
            ignored: None,
        };
        let value = serde_json::to_value(&args).unwrap();
        let obj = value.as_object().unwrap().clone();
        assert_eq!(
            obj.get("topic").unwrap(),
            &serde_json::Value::String("rust".into())
        );
        assert_eq!(obj.get("length").unwrap().to_string(), "200");
        assert_eq!(obj.get("verbose").unwrap().to_string(), "true");
        assert!(matches!(
            obj.get("ignored").unwrap(),
            serde_json::Value::Null
        ));
    }

    #[test]
    fn test_client_builder() {
        let transport = MockTransport::new();
        let client = ClientBuilder::new(transport)
            .enforce_strict_capabilities(true)
            .debounced_notifications(vec!["test".to_string()])
            .build();
        assert!(
            tokio::runtime::Runtime::new()
                .unwrap()
                .block_on(client.protocol.read())
                .options()
                .enforce_strict_capabilities
        );
    }

    #[tokio::test]
    async fn test_client_initialization() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {}
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let transport = MockTransport::with_responses(vec![init_response]);
        let mut client = Client::new(transport);

        let caps = ClientCapabilities::minimal();

        let result = client.initialize(caps).await;
        assert!(result.is_ok());
        assert!(client.initialized);
        assert_eq!(client.server_version.as_ref().unwrap().name, "test-server");
    }

    #[tokio::test]
    async fn test_ping() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let ping_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({})),
        });

        let transport = MockTransport::with_responses(vec![ping_response, init_response]);
        let mut client = Client::new(transport);

        // Initialize first
        let _ = client.initialize(ClientCapabilities::default()).await;

        // Ping
        let result = client.ping().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_list_tools() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {}
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let tools_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({
                "tools": [{
                    "name": "test-tool",
                    "description": "Test tool",
                    "inputSchema": {}
                }]
            })),
        });

        let transport = MockTransport::with_responses(vec![tools_response, init_response]);
        let mut client = Client::new(transport);

        // Initialize with tools capability
        let _ = client.initialize(ClientCapabilities::minimal()).await;

        // List tools
        let result = client.list_tools(None).await;
        assert!(result.is_ok());
        let tools = result.unwrap();
        assert_eq!(tools.tools.len(), 1);
        assert_eq!(tools.tools[0].name, "test-tool");
    }

    #[tokio::test]
    async fn test_error_response() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {}
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let error_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Error(JSONRPCError {
                code: -32601,
                message: "Method not found".to_string(),
                data: None,
            }),
        });

        let transport = MockTransport::with_responses(vec![error_response, init_response]);
        let mut client = Client::new(transport);

        // Initialize
        let _ = client.initialize(ClientCapabilities::default()).await;

        // Try to list tools - should get error
        let result = client.list_tools(None).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Method not found"));
    }

    #[tokio::test]
    async fn test_uninitialized_error() {
        let transport = MockTransport::new();
        let client = Client::new(transport);

        // Try to call method without initialization
        let result = client.ping().await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not initialized"));
    }

    #[tokio::test]
    async fn test_capability_enforcement() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    // No tools capability
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let transport = MockTransport::with_responses(vec![init_response]);
        let mut client = Client::new(transport);

        // Initialize without tools capability
        let _ = client.initialize(ClientCapabilities::default()).await;

        // Try to list tools - should fail
        let result = client.list_tools(None).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not supported"));
    }

    #[test]
    fn test_assert_capability_sampling_present_when_server_advertises() {
        // Regression for CR-01: a server advertising `sampling` must satisfy
        // the capability assertion `create_message` performs.
        let mut client = Client::new(MockTransport::new());
        client.server_capabilities = Some(ServerCapabilities {
            sampling: Some(crate::types::SamplingCapabilities::default()),
            ..Default::default()
        });
        assert!(
            client
                .assert_capability("sampling", "sampling/createMessage")
                .is_ok(),
            "sampling capability must be recognized when the server advertises it"
        );
    }

    #[test]
    fn test_assert_capability_sampling_absent_errors() {
        // Negative half of CR-01: no `sampling` advertised => capability error.
        let mut client = Client::new(MockTransport::new());
        client.server_capabilities = Some(ServerCapabilities::default());
        let err = client
            .assert_capability("sampling", "sampling/createMessage")
            .expect_err("missing sampling capability must error");
        assert!(
            err.to_string().contains("does not support sampling"),
            "unexpected error message: {err}"
        );
    }

    /// Transport whose first `send` (the outgoing request) succeeds and whose
    /// second `send` (the host response) fails, returning a single inbound
    /// request from `receive` in between. Drives the WR-04 leak path.
    #[derive(Debug)]
    struct FailSecondSend {
        sends: Arc<Mutex<usize>>,
        inbound: Arc<Mutex<Option<TransportMessage>>>,
    }

    #[async_trait]
    impl Transport for FailSecondSend {
        async fn send(&mut self, _message: TransportMessage) -> Result<()> {
            let mut n = self.sends.lock().unwrap();
            *n += 1;
            if *n >= 2 {
                Err(Error::internal("host response send failed"))
            } else {
                Ok(())
            }
        }

        async fn receive(&mut self) -> Result<TransportMessage> {
            let msg = self.inbound.lock().unwrap().take();
            if let Some(msg) = msg {
                Ok(msg)
            } else {
                Err(Error::internal("no more messages"))
            }
        }

        async fn close(&mut self) -> Result<()> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn test_host_response_send_failure_cleans_active_requests() {
        // WR-04: when sending the host response fails, the in-flight request's
        // entry (and its oneshot cancel sender) must be removed from
        // active_requests before the error propagates, matching the Response
        // arm's cleanup.
        let inbound = TransportMessage::Request {
            id: RequestId::from("inbound-1".to_string()),
            request: Request::Client(Box::new(ClientRequest::Ping)),
        };
        let client = Client::new(FailSecondSend {
            sends: Arc::new(Mutex::new(0)),
            inbound: Arc::new(Mutex::new(Some(inbound))),
        });

        let req_id = RequestId::from("outgoing-1".to_string());
        let request = Request::Client(Box::new(ClientRequest::Ping));
        let result = client.send_request(req_id.clone(), request).await;

        assert!(
            result.is_err(),
            "host-response send failure must propagate as an error"
        );
        assert!(
            !client.active_requests.read().await.contains_key(&req_id),
            "pending entry must be removed when the host response send fails"
        );
    }

    #[tokio::test]
    async fn test_receive_failure_cleans_active_requests() {
        // WR-04: a failure in the inbound `receive` (before any response
        // arrives) must also funnel through the single exit-cleanup point, so
        // the pending entry does not leak. `FailSecondSend` with no inbound
        // message sends the outgoing request successfully (send #1) and then
        // errors on `receive` ("no more messages").
        let client = Client::new(FailSecondSend {
            sends: Arc::new(Mutex::new(0)),
            inbound: Arc::new(Mutex::new(None)),
        });

        let req_id = RequestId::from("outgoing-recv".to_string());
        let request = Request::Client(Box::new(ClientRequest::Ping));
        let result = client.send_request(req_id.clone(), request).await;

        assert!(
            result.is_err(),
            "transport receive failure must propagate as an error"
        );
        assert!(
            !client.active_requests.read().await.contains_key(&req_id),
            "pending entry must be removed when the transport receive fails"
        );
    }

    #[tokio::test]
    async fn test_send_progress() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let transport = MockTransport::with_responses(vec![init_response]);
        let mut client = Client::new(transport);

        // Initialize
        let _ = client.initialize(ClientCapabilities::default()).await;

        // Send progress
        let progress = ProgressNotification::new(
            ProgressToken::String("test".to_string()),
            50.0,
            Some("Halfway done".to_string()),
        );

        let result = client.send_progress(progress).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_complete() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "completions": {}
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let complete_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({
                "completion": {
                    "values": ["test1", "test2"]
                }
            })),
        });

        let transport = MockTransport::with_responses(vec![complete_response, init_response]);
        let mut client = Client::new(transport);

        // Initialize
        let _ = client.initialize(ClientCapabilities::default()).await;

        // Complete
        let result = client
            .complete(CompleteRequest {
                r#ref: crate::types::CompletionReference::Resource {
                    uri: "test://test".to_string(),
                },
                argument: crate::types::CompletionArgument {
                    name: "test".to_string(),
                    value: "t".to_string(),
                },
            })
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_read_resource() {
        let init_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "resources": {}
                },
                "serverInfo": {
                    "name": "test-server",
                    "version": "1.0.0"
                }
            })),
        });

        let read_response = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({
                "contents": [{
                    "type": "text",
                    "text": "Hello, world!"
                }]
            })),
        });

        let transport = MockTransport::with_responses(vec![read_response, init_response]);
        let mut client = Client::new(transport);

        // Initialize
        let _ = client.initialize(ClientCapabilities::minimal()).await;

        // Read resource
        let result = client.read_resource("test://test".to_string()).await;
        if let Err(e) = &result {
            tracing::error!("Read resource error: {:?}", e);
        }
        assert!(result.is_ok());
        let contents = result.unwrap();
        assert_eq!(contents.contents.len(), 1);
    }

    // === list_all_* in-module tests ===

    fn list_all_init_response() -> TransportMessage {
        TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": {
                    "tools": {},
                    "prompts": {},
                    "resources": {},
                },
                "serverInfo": { "name": "test-server", "version": "1.0.0" }
            })),
        })
    }

    fn page_response<V: Into<serde_json::Value>>(
        id: i64,
        items_field: &str,
        items: V,
        next_cursor: Option<&str>,
    ) -> TransportMessage {
        let mut payload = serde_json::Map::new();
        payload.insert(items_field.to_string(), items.into());
        if let Some(c) = next_cursor {
            payload.insert("nextCursor".to_string(), json!(c));
        }
        TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(id),
            payload: ResponsePayload::Result(serde_json::Value::Object(payload)),
        })
    }

    #[tokio::test]
    async fn test_list_all_tools_single_page() {
        let page = page_response(
            2,
            "tools",
            json!([{"name": "only", "description": "t", "inputSchema": {}}]),
            None,
        );
        // MockTransport pops from tail; push reversed + init last.
        let transport = MockTransport::with_responses(vec![page, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_tools().await.expect("ok");
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].name, "only");
    }

    #[tokio::test]
    async fn test_list_all_tools_three_pages_in_order() {
        let p1 = page_response(
            2,
            "tools",
            json!([{"name": "a", "description": "t", "inputSchema": {}}]),
            Some("p2"),
        );
        let p2 = page_response(
            3,
            "tools",
            json!([{"name": "b", "description": "t", "inputSchema": {}}]),
            Some("p3"),
        );
        let p3 = page_response(
            4,
            "tools",
            json!([{"name": "c", "description": "t", "inputSchema": {}}]),
            None,
        );
        // Reverse-push: pages last-to-first, init last.
        let transport = MockTransport::with_responses(vec![p3, p2, p1, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_tools().await.expect("ok");
        let names: Vec<_> = all.into_iter().map(|t| t.name).collect();
        assert_eq!(names, vec!["a", "b", "c"]);
    }

    #[tokio::test]
    async fn test_list_all_tools_cap_enforced() {
        // max_iterations=3, server emits 4 pages all with Some(_).
        let p1 = page_response(
            2,
            "tools",
            json!([{"name": "a", "description": "t", "inputSchema": {}}]),
            Some("p2"),
        );
        let p2 = page_response(
            3,
            "tools",
            json!([{"name": "b", "description": "t", "inputSchema": {}}]),
            Some("p3"),
        );
        let p3 = page_response(
            4,
            "tools",
            json!([{"name": "c", "description": "t", "inputSchema": {}}]),
            Some("p4"),
        );
        let p4 = page_response(
            5,
            "tools",
            json!([{"name": "d", "description": "t", "inputSchema": {}}]),
            Some("p5"),
        );
        let transport =
            MockTransport::with_responses(vec![p4, p3, p2, p1, list_all_init_response()]);
        let opts = ClientOptions {
            max_iterations: 3,
            ..Default::default()
        };
        let mut client = Client::with_client_options(transport, opts);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let err = client.list_all_tools().await.unwrap_err();
        let msg = format!("{err}");
        assert!(matches!(err, Error::Validation(_)), "got: {msg}");
        assert!(msg.contains("list_all_tools"), "method name missing: {msg}");
        assert!(msg.contains('3'), "cap value missing: {msg}");
    }

    #[tokio::test]
    async fn test_list_all_tools_empty_string_cursor_continues() {
        // First page has next_cursor: Some("") — MUST continue the loop.
        let p1 = page_response(
            2,
            "tools",
            json!([{"name": "a", "description": "t", "inputSchema": {}}]),
            Some(""),
        );
        let p2 = page_response(
            3,
            "tools",
            json!([{"name": "b", "description": "t", "inputSchema": {}}]),
            None,
        );
        let transport = MockTransport::with_responses(vec![p2, p1, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_tools().await.expect("ok");
        let names: Vec<_> = all.into_iter().map(|t| t.name).collect();
        assert_eq!(
            names,
            vec!["a", "b"],
            "Some(\"\") must continue the loop (Pitfall 2)"
        );
    }

    #[tokio::test]
    async fn test_list_all_tools_max_iterations_zero_errors_immediately() {
        // max_iterations=0: loop body must not execute; no tools/list sent.
        // Only the init response is pre-loaded — if the loop ever called
        // list_tools, receive() would then fail with "No more responses"
        // (a Protocol error), not a Validation error.
        let transport = MockTransport::with_responses(vec![list_all_init_response()]);
        let sent_ref = Arc::clone(&transport.sent_messages);
        let opts = ClientOptions {
            max_iterations: 0,
            ..Default::default()
        };
        let mut client = Client::with_client_options(transport, opts);
        let _ = client.initialize(ClientCapabilities::minimal()).await;

        // Snapshot sent count BEFORE the call — initialize() sent its init
        // request. We assert no ADDITIONAL tools/list request is sent.
        let sent_before = sent_ref.lock().unwrap().len();

        let err = client.list_all_tools().await.unwrap_err();
        let msg = format!("{err}");
        assert!(matches!(err, Error::Validation(_)), "got: {msg}");
        assert!(msg.contains('0'), "cap value missing: {msg}");
        assert!(msg.contains("list_all_tools"), "method name missing: {msg}");

        let sent_after = sent_ref.lock().unwrap().clone();
        assert_eq!(
            sent_after.len(),
            sent_before,
            "transport must not receive any tools/list request when max_iterations=0; sent: {sent_after:?}"
        );
    }

    #[tokio::test]
    async fn test_list_all_prompts_three_pages_in_order() {
        let p1 = page_response(2, "prompts", json!([{"name": "p1"}]), Some("p2"));
        let p2 = page_response(3, "prompts", json!([{"name": "p2"}]), Some("p3"));
        let p3 = page_response(3, "prompts", json!([{"name": "p3"}]), None);
        let transport = MockTransport::with_responses(vec![p3, p2, p1, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_prompts().await.expect("ok");
        let names: Vec<_> = all.into_iter().map(|p| p.name).collect();
        assert_eq!(names, vec!["p1", "p2", "p3"]);
    }

    #[tokio::test]
    async fn test_list_all_resources_three_pages_in_order() {
        let p1 = page_response(
            2,
            "resources",
            json!([{"uri": "file://a", "name": "a"}]),
            Some("p2"),
        );
        let p2 = page_response(
            3,
            "resources",
            json!([{"uri": "file://b", "name": "b"}]),
            Some("p3"),
        );
        let p3 = page_response(
            4,
            "resources",
            json!([{"uri": "file://c", "name": "c"}]),
            None,
        );
        let transport = MockTransport::with_responses(vec![p3, p2, p1, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_resources().await.expect("ok");
        let names: Vec<_> = all.into_iter().map(|r| r.name).collect();
        assert_eq!(names, vec!["a", "b", "c"]);
    }

    #[tokio::test]
    async fn test_list_all_resource_templates_two_pages_in_order() {
        let p1 = page_response(
            2,
            "resourceTemplates",
            json!([{"uriTemplate": "file://{a}", "name": "ta"}]),
            Some("p2"),
        );
        let p2 = page_response(
            3,
            "resourceTemplates",
            json!([{"uriTemplate": "file://{b}", "name": "tb"}]),
            None,
        );
        let transport = MockTransport::with_responses(vec![p2, p1, list_all_init_response()]);
        let mut client = Client::new(transport);
        let _ = client.initialize(ClientCapabilities::minimal()).await;
        let all = client.list_all_resource_templates().await.expect("ok");
        let names: Vec<_> = all.into_iter().map(|t| t.name).collect();
        assert_eq!(names, vec!["ta", "tb"]);
    }

    // === TASKDX-03: WARN on task deserialize failure ===

    /// A single captured tracing event's structured fields.
    #[derive(Debug, Clone, Default)]
    struct CapturedEvent {
        level: String,
        fields: std::collections::HashMap<String, String>,
        message: String,
    }

    /// Minimal in-test recording subscriber that captures events' structured
    /// fields into a shared `Vec`, with NO dependency on `tracing-subscriber`.
    ///
    /// Installed via `tracing::subscriber::with_default` (scoped, never a global
    /// `init()`), so it cannot leak across tests run under `--test-threads=1`.
    #[derive(Clone, Default)]
    struct RecordingSubscriber {
        events: Arc<Mutex<Vec<CapturedEvent>>>,
    }

    struct FieldCollector<'a>(&'a mut CapturedEvent);

    impl tracing::field::Visit for FieldCollector<'_> {
        fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
            let rendered = format!("{value:?}");
            if field.name() == "message" {
                // Debug-rendered messages are wrapped in quotes; strip them.
                self.0.message = rendered.trim_matches('"').to_string();
            } else {
                self.0.fields.insert(
                    field.name().to_string(),
                    rendered.trim_matches('"').to_string(),
                );
            }
        }

        fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
            if field.name() == "message" {
                self.0.message = value.to_string();
            } else {
                self.0
                    .fields
                    .insert(field.name().to_string(), value.to_string());
            }
        }
    }

    impl tracing::Subscriber for RecordingSubscriber {
        fn enabled(&self, _metadata: &tracing::Metadata<'_>) -> bool {
            true
        }

        fn new_span(&self, _span: &tracing::span::Attributes<'_>) -> tracing::span::Id {
            tracing::span::Id::from_u64(1)
        }

        fn record(&self, _span: &tracing::span::Id, _values: &tracing::span::Record<'_>) {}

        fn record_follows_from(&self, _span: &tracing::span::Id, _follows: &tracing::span::Id) {}

        fn event(&self, event: &tracing::Event<'_>) {
            let mut captured = CapturedEvent {
                level: event.metadata().level().to_string(),
                ..Default::default()
            };
            event.record(&mut FieldCollector(&mut captured));
            self.events.lock().unwrap().push(captured);
        }

        fn enter(&self, _span: &tracing::span::Id) {}
        fn exit(&self, _span: &tracing::span::Id) {}
    }

    /// Build an init response advertising the `tasks` capability.
    fn tasks_init_response() -> TransportMessage {
        TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(1i64),
            payload: ResponsePayload::Result(json!({
                "protocolVersion": "2025-06-18",
                "capabilities": { "tasks": {} },
                "serverInfo": { "name": "test-server", "version": "1.0.0" }
            })),
        })
    }

    #[test]
    fn test_tasks_get_malformed_response_emits_warn_and_errs() {
        // A flat Task missing the required `task` wrapper — the deliberately
        // wrong shape for GetTaskResult (incident bug #3).
        let malformed = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({
                "taskId": "abc",
                "status": "completed"
            })),
        });
        let transport = MockTransport::with_responses(vec![malformed, tasks_init_response()]);
        let mut client = Client::new(transport);
        let _ = futures::executor::block_on(client.initialize(ClientCapabilities::default()));

        let recorder = RecordingSubscriber::default();
        let sink = recorder.events.clone();
        let result = tracing::subscriber::with_default(recorder, || {
            futures::executor::block_on(client.tasks_get("abc"))
        });

        // Control flow unchanged: still returns Err (a parse error).
        assert!(result.is_err(), "malformed tasks/get must still return Err");

        // Structural WARN assertion (not a substring of the message text).
        let events = sink.lock().unwrap();
        let warn = events
            .iter()
            .find(|e| e.fields.get("method").map(String::as_str) == Some("tasks/get"))
            .expect("a WARN naming method=tasks/get must be captured");
        assert_eq!(warn.level, "WARN", "must be a WARN level event");
        assert!(
            warn.fields.contains_key("error"),
            "WARN must carry the serde error field, got: {:?}",
            warn.fields
        );
        assert!(
            warn.fields.contains_key("transport"),
            "WARN must carry the transport identity field, got: {:?}",
            warn.fields
        );
    }

    #[test]
    fn test_tasks_result_malformed_response_emits_warn_and_errs() {
        // CallToolResult requires `content`; a bare bool is the wrong shape.
        let malformed = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!(true)),
        });
        let transport = MockTransport::with_responses(vec![malformed, tasks_init_response()]);
        let mut client = Client::new(transport);
        let _ = futures::executor::block_on(client.initialize(ClientCapabilities::default()));

        let recorder = RecordingSubscriber::default();
        let sink = recorder.events.clone();
        let result = tracing::subscriber::with_default(recorder, || {
            futures::executor::block_on(client.tasks_result("abc"))
        });

        assert!(
            result.is_err(),
            "malformed tasks/result must still return Err"
        );

        let events = sink.lock().unwrap();
        let warn = events
            .iter()
            .find(|e| e.fields.get("method").map(String::as_str) == Some("tasks/result"))
            .expect("a WARN naming method=tasks/result must be captured");
        assert_eq!(warn.level, "WARN");
        assert!(warn.fields.contains_key("error"), "got: {:?}", warn.fields);
        assert!(
            warn.fields.contains_key("transport"),
            "got: {:?}",
            warn.fields
        );
    }

    #[test]
    fn test_tasks_get_well_formed_response_emits_no_warn() {
        let well_formed = TransportMessage::Response(JSONRPCResponse {
            jsonrpc: "2.0".to_string(),
            id: RequestId::from(2i64),
            payload: ResponsePayload::Result(json!({
                "task": {
                    "taskId": "abc",
                    "status": "completed",
                    "createdAt": "2026-06-21T00:00:00Z",
                    "lastUpdatedAt": "2026-06-21T00:00:00Z"
                }
            })),
        });
        let transport = MockTransport::with_responses(vec![well_formed, tasks_init_response()]);
        let mut client = Client::new(transport);
        let _ = futures::executor::block_on(client.initialize(ClientCapabilities::default()));

        let recorder = RecordingSubscriber::default();
        let sink = recorder.events.clone();
        let result = tracing::subscriber::with_default(recorder, || {
            futures::executor::block_on(client.tasks_get("abc"))
        });

        assert!(
            result.is_ok(),
            "well-formed tasks/get must succeed: {result:?}"
        );
        let events = sink.lock().unwrap();
        assert!(
            !events
                .iter()
                .any(|e| e.fields.get("method").map(String::as_str) == Some("tasks/get")),
            "no task-deserialize WARN must fire on a well-formed response"
        );
    }
}