fastmcp-cli 0.7.0

CLI tooling for FastMCP - run, inspect, and install MCP servers
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
//! E2E tests for `fastmcp test`.

#![cfg(unix)]

use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::os::unix::process::CommandExt as _;
use std::process::{Child, ExitStatus, Stdio};
use std::process::{Command, Output};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, mpsc};
use std::time::{Duration, Instant};

const CLI_DEADLINE: Duration = Duration::from_secs(120);
const CAPTURE_DRAIN_DEADLINE: Duration = Duration::from_secs(1);
const MAX_CAPTURE_BYTES: usize = 8 * 1024 * 1024;
const PROCESS_CLEANUP_DEADLINE: Duration = Duration::from_secs(2);
const PROCESS_POLL_INTERVAL: Duration = Duration::from_millis(25);
const PROCESS_TERM_GRACE: Duration = Duration::from_millis(500);
const FORBIDDEN_CONTACT_WORK_CAP: usize = 16;
const FORBIDDEN_CONTACT_POLL_INTERVAL: Duration = Duration::from_millis(10);
const FORBIDDEN_CONTACT_FINAL_QUIET_INTERVAL: Duration = Duration::from_millis(20);
const FORBIDDEN_CONTACT_FINAL_DRAIN_DEADLINE: Duration = Duration::from_millis(250);
const FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE: Duration = Duration::from_millis(500);
const LOOPBACK_FIXTURE_DEADLINE: Duration = Duration::from_secs(2);
const LOOPBACK_FIXTURE_ACK_DEADLINE: Duration = Duration::from_millis(2500);
const LOOPBACK_FIXTURE_POLL_INTERVAL: Duration = Duration::from_millis(10);
const STATIC_MCP_SERVER_FIXTURE: &str = concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/tests/fixtures/static_mcp_server.sh"
);
const INSPECT_PROTOCOL_WIRE_PREFIX: &str = "FASTMCP_E2E_WIRE ";

const MODERN_INSPECT_FIXTURE: &str = r#"
emit_wire() {
    printf 'FASTMCP_E2E_WIRE %s\n' "$1" >&2
}

is_exact_modern_discovery() {
    printf '%s\n' "$1" | grep -Eq '^\{"jsonrpc":"2\.0","method":"server/discover","params":\{"_meta":\{"io\.modelcontextprotocol/clientCapabilities":\{\},"io\.modelcontextprotocol/clientInfo":\{"name":"[^"]*","version":"[^"]*"\},"io\.modelcontextprotocol/protocolVersion":"2026-07-28"\}\},"id":1\}$' || return 1
}

while IFS= read -r request; do
    emit_wire "$request"
    is_exact_modern_discovery "$request" || exit 1
    printf '%s\n' '{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{"completions":{},"extensions":{"io.example/inspect":{"enabled":true}}},"_meta":{"io.modelcontextprotocol/serverInfo":{"name":"modern-inspect-server","version":"1.0.0"}},"ttlMs":0,"cacheScope":"private"}}'
done
"#;

const LEGACY_FALLBACK_INSPECT_FIXTURE: &str = r#"
emit_wire() {
    printf 'FASTMCP_E2E_WIRE %s\n' "$1" >&2
}

require_no_final_metadata() {
    case "$1" in
        *'io.modelcontextprotocol/protocolVersion'*|*'io.modelcontextprotocol/clientCapabilities'*|*'io.modelcontextprotocol/clientInfo'*|*'io.modelcontextprotocol/serverInfo'*|*'io.modelcontextprotocol/subscriptionId'*)
            return 1
            ;;
    esac
}

is_exact_modern_discovery() {
    printf '%s\n' "$1" | grep -Eq '^\{"jsonrpc":"2\.0","method":"server/discover","params":\{"_meta":\{"io\.modelcontextprotocol/clientCapabilities":\{\},"io\.modelcontextprotocol/clientInfo":\{"name":"[^"]*","version":"[^"]*"\},"io\.modelcontextprotocol/protocolVersion":"2026-07-28"\}\},"id":1\}$' || return 1
}

is_exact_legacy_initialize() {
    printf '%s\n' "$1" | grep -Eq '^\{"jsonrpc":"2\.0","method":"initialize","params":\{"protocolVersion":"2024-11-05","capabilities":\{\},"clientInfo":\{"name":"[^"]*","version":"[^"]*"\}\},"id":[0-9]+\}$' || return 1
    require_no_final_metadata "$1"
}

is_exact_legacy_initialized_notification() {
    [ "$1" = '{"jsonrpc":"2.0","method":"notifications/initialized"}' ] || return 1
    require_no_final_metadata "$1"
}

is_exact_legacy_operating_request() {
    printf '%s\n' "$1" | grep -Eq '^\{"jsonrpc":"2\.0","method":"(tools/list|resources/list|resources/templates/list|prompts/list)","params":\{\},"id":[0-9]+\}$' || return 1
    require_no_final_metadata "$1"
}

respond() {
    printf '{"jsonrpc":"2.0","id":%s,"result":%s}\n' "$request_id" "$1"
}

respond_method_not_found() {
    printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32601,"message":"Method not found"}}\n' "$request_id"
}

require_request_id() {
    request_id=$(printf '%s\n' "$1" | sed -n 's/.*"id":[[:space:]]*\([0-9][0-9]*\).*/\1/p')
    case "$request_id" in
        '' | *[!0-9]*) return 1 ;;
    esac
}

state=awaiting_initialize
while IFS= read -r request; do
    emit_wire "$request"

    case "$state" in
        awaiting_initialize)
            require_request_id "$request" || exit 1
            case "$request" in
                *'"method":"server/discover"'*)
                    is_exact_modern_discovery "$request" || exit 1
                    respond_method_not_found
                    exit 0
                    ;;
                *'"method":"initialize"'*)
                    is_exact_legacy_initialize "$request" || exit 1
                    respond '{"protocolVersion":"2024-11-05","capabilities":{"tools":{},"resources":{},"prompts":{}},"serverInfo":{"name":"legacy-inspect-server","version":"1.0.0"}}'
                    state=awaiting_initialized_notification
                    ;;
                *) exit 1 ;;
            esac
            ;;
        awaiting_initialized_notification)
            is_exact_legacy_initialized_notification "$request" || exit 1
            state=operating
            ;;
        operating)
            is_exact_legacy_operating_request "$request" || exit 1
            case "$request" in
                *'"method":"tools/list"'*)
                    respond '{"tools":[]}'
                    ;;
                *'"method":"resources/list"'*)
                    respond '{"resources":[]}'
                    ;;
                *'"method":"resources/templates/list"'*)
                    respond '{"resourceTemplates":[]}'
                    ;;
                *'"method":"prompts/list"'*)
                    respond '{"prompts":[]}'
                    ;;
                *) exit 1 ;;
            esac
            ;;
        *) exit 1 ;;
    esac
done
"#;

const LEGACY_PLANTED_INITIALIZE_REQUEST: &str = r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"}},"id":1}"#;
const LEGACY_PLANTED_REPEATED_INITIALIZE_REQUEST: &str = r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"}},"id":2}"#;
const LEGACY_PLANTED_INITIALIZED_NOTIFICATION: &str =
    r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#;
const LEGACY_PLANTED_TOOLS_LIST_REQUEST: &str =
    r#"{"jsonrpc":"2.0","method":"tools/list","params":{},"id":2}"#;

const UNAUTHORIZED_DISCOVERY_REJECTION_INSPECT_FIXTURE: &str = r#"
emit_wire() {
    printf 'FASTMCP_E2E_WIRE %s\n' "$1" >&2
}

is_exact_modern_discovery() {
    printf '%s\n' "$1" | grep -Eq '^\{"jsonrpc":"2\.0","method":"server/discover","params":\{"_meta":\{"io\.modelcontextprotocol/clientCapabilities":\{\},"io\.modelcontextprotocol/clientInfo":\{"name":"[^"]*","version":"[^"]*"\},"io\.modelcontextprotocol/protocolVersion":"2026-07-28"\}\},"id":1\}$' || return 1
}

while IFS= read -r request; do
    emit_wire "$request"
    is_exact_modern_discovery "$request" || exit 1
    printf '%s\n' '{"jsonrpc":"2.0","id":1,"error":{"code":-32022,"message":"final discovery rejected"}}'
    exit 0
done
"#;

fn fastmcp_bin() -> String {
    env!("CARGO_BIN_EXE_fastmcp").to_string()
}

fn stdout_str(output: &Output) -> String {
    std::str::from_utf8(&output.stdout)
        .expect("fastmcp test stdout must be valid UTF-8")
        .to_owned()
}

fn stderr_str(output: &Output) -> String {
    std::str::from_utf8(&output.stderr)
        .expect("fastmcp test stderr must be valid UTF-8")
        .to_owned()
}

fn observed_protocol_wire(output: &Output) -> Vec<serde_json::Value> {
    stderr_str(output)
        .lines()
        .filter_map(|line| line.strip_prefix(INSPECT_PROTOCOL_WIRE_PREFIX))
        .map(|request| {
            serde_json::from_str(request)
                .unwrap_or_else(|error| panic!("fixture must record valid JSON-RPC wire: {error}"))
        })
        .collect()
}

fn request_method(request: &serde_json::Value) -> &str {
    request
        .get("method")
        .and_then(serde_json::Value::as_str)
        .expect("recorded wire message must have a method")
}

fn assert_no_final_metadata(request: &serde_json::Value) {
    let encoded = serde_json::to_string(request).expect("recorded wire must serialize");
    for final_metadata_key in [
        "io.modelcontextprotocol/protocolVersion",
        "io.modelcontextprotocol/clientCapabilities",
        "io.modelcontextprotocol/clientInfo",
        "io.modelcontextprotocol/serverInfo",
        "io.modelcontextprotocol/subscriptionId",
    ] {
        assert!(
            !encoded.contains(final_metadata_key),
            "legacy wire must not carry {final_metadata_key}"
        );
    }
}

fn assert_exact_modern_discovery_request(request: &serde_json::Value) {
    assert_eq!(
        request
            .as_object()
            .expect("modern discovery must be a JSON object")
            .len(),
        4,
        "modern discovery must contain only JSON-RPC envelope fields"
    );
    assert_eq!(request["jsonrpc"], "2.0");
    assert_eq!(request_method(request), "server/discover");
    assert_eq!(request["id"], 1);
    let params = request["params"]
        .as_object()
        .expect("modern discovery must carry object params");
    assert_eq!(
        params.len(),
        1,
        "modern discovery params must contain only _meta"
    );
    let metadata = params["_meta"]
        .as_object()
        .expect("modern discovery must carry request metadata");
    assert_eq!(
        metadata.len(),
        3,
        "modern discovery metadata must contain only final protocol fields"
    );
    assert_eq!(
        metadata["io.modelcontextprotocol/protocolVersion"],
        "2026-07-28"
    );
    assert!(
        metadata
            .get("io.modelcontextprotocol/clientCapabilities")
            .and_then(serde_json::Value::as_object)
            .is_some_and(|capabilities| capabilities.is_empty()),
        "modern discovery must carry exactly the empty final client capabilities object"
    );
    let client_info = metadata
        .get("io.modelcontextprotocol/clientInfo")
        .and_then(serde_json::Value::as_object)
        .expect("modern discovery must carry final client info object");
    assert_eq!(
        client_info.len(),
        2,
        "modern discovery client info must contain only name and version"
    );
    assert!(
        client_info
            .get("name")
            .and_then(serde_json::Value::as_str)
            .is_some(),
        "modern discovery client info name must be a string"
    );
    assert!(
        client_info
            .get("version")
            .and_then(serde_json::Value::as_str)
            .is_some(),
        "modern discovery client info version must be a string"
    );
}

fn assert_exact_legacy_initialize_request(request: &serde_json::Value) {
    assert_eq!(
        request
            .as_object()
            .expect("legacy initialize must be a JSON object")
            .len(),
        4,
        "legacy initialize must contain only JSON-RPC envelope fields"
    );
    assert_eq!(request["jsonrpc"], "2.0");
    assert_eq!(request_method(request), "initialize");
    assert!(
        request.get("id").is_some_and(serde_json::Value::is_number),
        "legacy initialize must carry a numeric JSON-RPC request ID"
    );
    let params = request["params"]
        .as_object()
        .expect("legacy initialize must carry params");
    assert_eq!(
        params.len(),
        3,
        "legacy initialize must contain exactly its three legacy fields"
    );
    assert_eq!(
        params
            .get("protocolVersion")
            .and_then(serde_json::Value::as_str),
        Some("2024-11-05"),
        "legacy initialize must carry the exact protocol version string"
    );
    assert!(
        params
            .get("capabilities")
            .and_then(serde_json::Value::as_object)
            .is_some_and(|capabilities| capabilities.is_empty()),
        "legacy initialize must carry exactly the empty legacy capabilities object"
    );
    let client_info = params
        .get("clientInfo")
        .and_then(serde_json::Value::as_object)
        .expect("legacy initialize clientInfo must be an object");
    assert_eq!(
        client_info.len(),
        2,
        "legacy clientInfo must contain only name and version"
    );
    assert!(
        client_info
            .get("name")
            .and_then(serde_json::Value::as_str)
            .is_some(),
        "legacy clientInfo.name must be a string"
    );
    assert!(
        client_info
            .get("version")
            .and_then(serde_json::Value::as_str)
            .is_some(),
        "legacy clientInfo.version must be a string"
    );
    assert!(
        !params.contains_key("_meta"),
        "legacy initialize must not carry final metadata"
    );
    assert_no_final_metadata(request);
}

fn assert_exact_legacy_initialized_notification_request(request: &serde_json::Value) {
    assert_eq!(
        request
            .as_object()
            .expect("legacy initialized notification must be a JSON object")
            .len(),
        2,
        "legacy initialized notification must contain only JSON-RPC envelope fields"
    );
    assert_eq!(request["jsonrpc"], "2.0");
    assert_eq!(request_method(request), "notifications/initialized");
    assert!(
        request.get("id").is_none(),
        "legacy initialized notification must not have an ID"
    );
    assert!(
        request.get("params").is_none(),
        "legacy initialized notification must not have params"
    );
    assert_no_final_metadata(request);
}

fn assert_exact_legacy_operating_request(request: &serde_json::Value) {
    assert_eq!(
        request
            .as_object()
            .expect("legacy operating request must be a JSON object")
            .len(),
        4,
        "legacy operating request must contain only JSON-RPC envelope fields"
    );
    assert_eq!(request["jsonrpc"], "2.0");
    assert!(
        matches!(
            request_method(request),
            "tools/list" | "resources/list" | "resources/templates/list" | "prompts/list"
        ),
        "legacy inspect must issue only operating list requests after initialization"
    );
    assert!(
        request.get("id").is_some_and(serde_json::Value::is_number),
        "legacy operating request must carry a numeric JSON-RPC request ID"
    );
    assert!(
        request
            .get("params")
            .and_then(serde_json::Value::as_object)
            .is_some_and(|params| params.is_empty()),
        "legacy operating request must carry exactly an empty params object"
    );
    assert_no_final_metadata(request);
}

fn assert_no_legacy_initialize(wire: &[serde_json::Value]) {
    assert!(
        wire.iter()
            .all(|request| request_method(request) != "initialize"),
        "a failed modern negotiation must not send a legacy initialize request"
    );
}

fn assert_modern_negotiation_wire(wire: &[serde_json::Value]) {
    assert_eq!(
        wire.len(),
        1,
        "modern inspect must send exactly one discovery request"
    );
    assert_exact_modern_discovery_request(&wire[0]);
}

fn assert_legacy_lifecycle_wire(wire: &[serde_json::Value]) {
    assert_eq!(
        wire.iter().map(request_method).collect::<Vec<_>>(),
        vec![
            "initialize",
            "notifications/initialized",
            "tools/list",
            "resources/list",
            "resources/templates/list",
            "prompts/list",
        ],
        "legacy inspect must run initialize, notification, then its operating requests exactly once"
    );
    assert_exact_legacy_initialize_request(&wire[0]);
    assert_exact_legacy_initialized_notification_request(&wire[1]);
    for request in &wire[2..] {
        assert_exact_legacy_operating_request(request);
    }
}

fn assert_legacy_negotiation_wire(wire: &[serde_json::Value]) {
    assert_legacy_lifecycle_wire(wire);
}

fn assert_auto_legacy_fallback_wire(wire: &[serde_json::Value]) {
    assert_eq!(
        request_method(
            wire.first()
                .expect("Auto fallback must record a modern discovery request"),
        ),
        "server/discover"
    );
    assert_exact_modern_discovery_request(&wire[0]);
    assert_legacy_lifecycle_wire(&wire[1..]);
}

#[derive(Debug)]
struct DeadlineExceeded {
    timeout: Duration,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
    cleanup_error: Option<String>,
}

struct ProcessGroupGuard {
    child: Option<Child>,
    process_group_id: u32,
    owns_process_group: bool,
    armed: bool,
}

impl ProcessGroupGuard {
    fn spawn(command: &mut Command) -> Self {
        command.process_group(0);
        let child = command.spawn().expect("spawn command");
        let process_group_id = child.id();
        Self {
            child: Some(child),
            process_group_id,
            owns_process_group: true,
            armed: true,
        }
    }

    fn child_mut(&mut self) -> &mut Child {
        self.child.as_mut().expect("process guard is disarmed")
    }

    fn wait_until(&mut self, timeout: Duration) -> Result<ExitStatus, DeadlineExceeded> {
        let started = Instant::now();
        loop {
            match self.child_is_zombie("failed to inspect child process") {
                Ok(true) => {
                    return Ok(self
                        .kill_and_reap()
                        .unwrap_or_else(|error| {
                            panic!("failed to clean up command descendants after exit: {error}")
                        })
                        .expect("observed zombie child must yield an exit status"));
                }
                Ok(false) => {}
                Err(error) => {
                    self.owns_process_group = false;
                    self.armed = false;
                    panic!("{error}; guard disarmed without signaling");
                }
            }

            if started.elapsed() >= timeout {
                return Err(DeadlineExceeded {
                    timeout,
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                    cleanup_error: self.kill_and_reap().err(),
                });
            }
            std::thread::sleep(PROCESS_POLL_INTERVAL);
        }
    }

    fn signal_group(&self, signal: &str) -> Result<(), String> {
        let target = format!("-{}", self.process_group_id);
        let status = Command::new("/bin/kill")
            .args([signal, "--", &target])
            .status()
            .map_err(|error| format!("failed to execute /bin/kill: {error}"))?;
        if status.success() || !process_group_has_live_member(self.process_group_id)? {
            return Ok(());
        }
        Err(format!(
            "/bin/kill {signal} -- {target} exited with {status}"
        ))
    }

    fn child_is_zombie(&self, context: &str) -> Result<bool, String> {
        process_is_zombie(self.child.as_ref().expect("process guard is disarmed").id())
            .map_err(|error| format!("{context}: {error}"))
    }

    fn wait_for_cleanup_state(&mut self, deadline: Instant) -> Result<(bool, bool), String> {
        loop {
            let child_exited = self.child_is_zombie("failed to inspect exact child")?;
            let group_live =
                self.owns_process_group && process_group_has_live_member(self.process_group_id)?;
            if (child_exited && !group_live) || Instant::now() >= deadline {
                return Ok((child_exited, group_live));
            }
            std::thread::sleep(PROCESS_POLL_INTERVAL);
        }
    }

    fn kill_and_reap(&mut self) -> Result<Option<ExitStatus>, String> {
        if !self.armed {
            return Ok(None);
        }

        let deadline = Instant::now()
            .checked_add(PROCESS_CLEANUP_DEADLINE)
            .unwrap_or_else(Instant::now);
        let mut errors = Vec::new();
        let mut direct_kill_error = None;
        let mut child_exited = match self.child_is_zombie("failed to inspect exact child") {
            Ok(exited) => exited,
            Err(error) => {
                self.owns_process_group = false;
                self.armed = false;
                return Err(format!("{error}; guard disarmed without signaling"));
            }
        };
        // Do not reap the process-group leader before group cleanup. Its live
        // or zombie PID pins the PGID until all TERM/KILL decisions are over.
        let mut group_live = match process_group_has_live_member(self.process_group_id) {
            Ok(live) => live,
            Err(error) => {
                self.owns_process_group = false;
                self.armed = false;
                return Err(format!(
                    "failed to inspect owned process group; guard disarmed without signaling: {error}"
                ));
            }
        };

        if group_live {
            if let Err(error) = self.signal_group("-TERM") {
                self.owns_process_group = false;
                self.armed = false;
                return Err(format!("{error}; guard disarmed without further signaling"));
            } else {
                let term_deadline = deadline.min(
                    Instant::now()
                        .checked_add(PROCESS_TERM_GRACE)
                        .unwrap_or(deadline),
                );
                match self.wait_for_cleanup_state(term_deadline) {
                    Ok((exited, live)) => {
                        child_exited = exited;
                        group_live = live;
                    }
                    Err(error) => {
                        self.owns_process_group = false;
                        self.armed = false;
                        return Err(format!("{error}; guard disarmed without further signaling"));
                    }
                }
                if group_live {
                    if let Err(error) = self.signal_group("-KILL") {
                        self.owns_process_group = false;
                        self.armed = false;
                        return Err(format!("{error}; guard disarmed without further signaling"));
                    }
                }
            }
        }

        if !child_exited {
            match self.child_is_zombie("failed to inspect exact child before direct kill") {
                Ok(true) => {}
                Ok(false) => {
                    direct_kill_error = self.child_mut().kill().err();
                }
                Err(error) => {
                    self.owns_process_group = false;
                    self.armed = false;
                    return Err(format!("{error}; guard disarmed without direct signaling"));
                }
            }
        }

        match self.wait_for_cleanup_state(deadline) {
            Ok((exited, live)) => {
                child_exited = exited;
                group_live = live;
            }
            Err(error) => {
                self.owns_process_group = false;
                self.armed = false;
                return Err(format!("{error}; guard disarmed without further signaling"));
            }
        }
        if !child_exited {
            if let Some(error) = direct_kill_error {
                errors.push(format!("failed to kill exact child: {error}"));
            }
            errors.push(format!(
                "exact child did not exit within {PROCESS_CLEANUP_DEADLINE:?}"
            ));
        }
        if group_live {
            errors.push(format!(
                "owned process group {} still has live members after {PROCESS_CLEANUP_DEADLINE:?}",
                self.process_group_id
            ));
        }
        let exit_status = match self.child_mut().try_wait() {
            Ok(status) => status,
            Err(error) => {
                errors.push(format!(
                    "failed to reap exact child after all signaling completed: {error}"
                ));
                None
            }
        };
        self.child = None;
        self.owns_process_group = false;
        self.armed = false;

        if errors.is_empty() {
            Ok(exit_status)
        } else {
            Err(errors.join("; "))
        }
    }
}

#[cfg(target_os = "linux")]
fn proc_process_disappeared(error: &std::io::Error) -> bool {
    error.kind() == std::io::ErrorKind::NotFound
        || error.raw_os_error() == Some(rustix::io::Errno::SRCH.raw_os_error())
}

#[cfg(target_os = "linux")]
fn process_group_has_live_member(process_group_id: u32) -> Result<bool, String> {
    let processes = std::fs::read_dir("/proc")
        .map_err(|error| format!("failed to enumerate /proc: {error}"))?;
    for entry in processes {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => {
                return Err(format!("failed to enumerate /proc entry: {error}"));
            }
        };
        let Ok(pid) = entry.file_name().to_string_lossy().parse::<u32>() else {
            continue;
        };
        let stat = match std::fs::read_to_string(format!("/proc/{pid}/stat")) {
            Ok(stat) => stat,
            Err(error) if proc_process_disappeared(&error) => continue,
            Err(error) => {
                return Err(format!("failed to read process {pid} state: {error}"));
            }
        };
        let (state, group) = linux_process_state_and_group(&stat)
            .ok_or_else(|| format!("malformed /proc/{pid}/stat"))?;
        if group == process_group_id && !matches!(state, 'Z' | 'X' | 'x') {
            return Ok(true);
        }
    }
    Ok(false)
}

#[cfg(all(unix, not(target_os = "linux")))]
fn process_group_has_live_member(process_group_id: u32) -> Result<bool, String> {
    let output = Command::new("/bin/ps")
        .args(["-ax", "-o", "pgid=", "-o", "stat="])
        .output()
        .map_err(|error| format!("failed to inspect process groups with /bin/ps: {error}"))?;
    if !output.status.success() {
        return Err(format!(
            "/bin/ps process-group inspection exited with {}",
            output.status
        ));
    }
    let stdout = std::str::from_utf8(&output.stdout)
        .map_err(|error| format!("/bin/ps emitted non-UTF-8 process-group output: {error}"))?;
    let mut group_live = false;
    for (line_index, line) in stdout.lines().enumerate() {
        if line.trim().is_empty() {
            continue;
        }
        let mut fields = line.split_ascii_whitespace();
        let group = fields
            .next()
            .ok_or_else(|| format!("/bin/ps line {} is missing a PGID", line_index + 1))?
            .parse::<u32>()
            .map_err(|error| {
                format!(
                    "/bin/ps line {} has an invalid PGID: {error}",
                    line_index + 1
                )
            })?;
        let state = fields
            .next()
            .and_then(|value| value.chars().next())
            .ok_or_else(|| format!("/bin/ps line {} is missing a state", line_index + 1))?;
        if fields.next().is_some() {
            return Err(format!(
                "/bin/ps line {} has unexpected extra fields",
                line_index + 1
            ));
        }
        if group == process_group_id && !matches!(state, 'Z' | 'X') {
            group_live = true;
        }
    }
    Ok(group_live)
}

#[cfg(target_os = "linux")]
fn process_is_zombie(pid: u32) -> Result<bool, String> {
    let stat = std::fs::read_to_string(format!("/proc/{pid}/stat"))
        .map_err(|error| format!("failed to read process {pid} state: {error}"))?;
    let (state, _) = linux_process_state_and_group(&stat)
        .ok_or_else(|| format!("malformed /proc/{pid}/stat"))?;
    Ok(matches!(state, 'Z' | 'X' | 'x'))
}

#[cfg(target_os = "linux")]
fn linux_process_state_and_group(stat: &str) -> Option<(char, u32)> {
    let (_, fields) = stat.rsplit_once(')')?;
    let mut fields = fields.split_ascii_whitespace();
    let state = fields.next()?.chars().next()?;
    let _parent_pid = fields.next()?;
    let process_group_id = fields.next()?.parse().ok()?;
    Some((state, process_group_id))
}

#[cfg(all(unix, not(target_os = "linux")))]
fn process_is_zombie(pid: u32) -> Result<bool, String> {
    let output = Command::new("/bin/ps")
        .args(["-o", "stat=", "-p", &pid.to_string()])
        .output()
        .map_err(|error| format!("failed to inspect process {pid} with /bin/ps: {error}"))?;
    if !output.status.success() {
        return Err(format!(
            "/bin/ps inspection for process {pid} exited with {}",
            output.status
        ));
    }
    let stdout = std::str::from_utf8(&output.stdout)
        .map_err(|_| format!("/bin/ps returned non-UTF-8 state for process {pid}"))?;
    let mut fields = stdout.split_ascii_whitespace();
    let state = fields
        .next()
        .and_then(|value| value.chars().next())
        .ok_or_else(|| format!("process {pid} disappeared before it was reaped"))?;
    if fields.next().is_some() {
        return Err(format!(
            "/bin/ps returned multiple state fields for process {pid}"
        ));
    }
    Ok(matches!(state, 'Z' | 'X'))
}

impl Drop for ProcessGroupGuard {
    fn drop(&mut self) {
        if let Err(error) = self.kill_and_reap() {
            eprintln!("fastmcp test-command harness cleanup failed: {error}");
        }
    }
}

struct PipeCapture {
    completion: mpsc::Receiver<CaptureOutcome>,
    retained: Arc<Mutex<Vec<u8>>>,
}

struct CaptureOutcome {
    truncated: bool,
    read_error: Option<String>,
}

fn capture_pipe<R>(mut pipe: R) -> PipeCapture
where
    R: Read + Send + 'static,
{
    let (completion, completed) = mpsc::sync_channel(1);
    let retained = Arc::new(Mutex::new(Vec::new()));
    let thread_retained = Arc::clone(&retained);
    drop(std::thread::spawn(move || {
        let mut buffer = [0_u8; 8 * 1024];
        let mut truncated = false;
        let read_error = loop {
            match pipe.read(&mut buffer) {
                Ok(0) => break None,
                Ok(read) => {
                    let mut bytes = thread_retained
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner);
                    let retained_bytes = (MAX_CAPTURE_BYTES - bytes.len()).min(read);
                    bytes.extend_from_slice(&buffer[..retained_bytes]);
                    truncated |= retained_bytes < read;
                }
                Err(error) => break Some(error.to_string()),
            }
        };
        let _ = completion.send(CaptureOutcome {
            truncated,
            read_error,
        });
    }));
    PipeCapture {
        completion: completed,
        retained,
    }
}

fn finish_capture(capture: PipeCapture, stream: &str, wait: Duration) -> (Vec<u8>, Option<String>) {
    let completion = capture.completion.recv_timeout(wait);
    let error = match completion {
        Ok(CaptureOutcome {
            truncated,
            read_error,
        }) => match (read_error, truncated) {
            (Some(error), _) => Some(format!("failed to capture {stream}: {error}")),
            (None, true) => Some(format!(
                "{stream} exceeded the {MAX_CAPTURE_BYTES}-byte capture limit"
            )),
            (None, false) => None,
        },
        Err(mpsc::RecvTimeoutError::Timeout) => {
            // A detached descendant can retain this pipe. Safe Rust has no
            // portable blocked-read cancellation; preserve partial evidence.
            Some(format!("{stream} did not close within {wait:?}"))
        }
        Err(mpsc::RecvTimeoutError::Disconnected) => {
            Some(format!("{stream} capture thread disconnected"))
        }
    };
    let bytes = capture
        .retained
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .clone();
    (bytes, error)
}

fn run_with_deadline(mut command: Command, timeout: Duration) -> Result<Output, DeadlineExceeded> {
    command.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut process = ProcessGroupGuard::spawn(&mut command);
    let stdout = capture_pipe(
        process
            .child_mut()
            .stdout
            .take()
            .expect("child stdout must be piped"),
    );
    let stderr = capture_pipe(
        process
            .child_mut()
            .stderr
            .take()
            .expect("child stderr must be piped"),
    );

    let deadline = Instant::now()
        .checked_add(timeout)
        .unwrap_or_else(Instant::now);
    let (stdout, stdout_error) = finish_capture(stdout, "stdout", remaining(deadline));
    if let Some(error) = stdout_error {
        let cleanup_error = process.kill_and_reap().err();
        let (stderr, stderr_error) = finish_capture(stderr, "stderr", CAPTURE_DRAIN_DEADLINE);
        return Err(DeadlineExceeded {
            timeout,
            stdout,
            stderr,
            cleanup_error: Some(
                [Some(error), stderr_error, cleanup_error]
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>()
                    .join("; "),
            ),
        });
    }

    let (stderr, stderr_error) = finish_capture(stderr, "stderr", remaining(deadline));
    if let Some(error) = stderr_error {
        let cleanup_error = process.kill_and_reap().err();
        return Err(DeadlineExceeded {
            timeout,
            stdout,
            stderr,
            cleanup_error: Some(
                [Some(error), cleanup_error]
                    .into_iter()
                    .flatten()
                    .collect::<Vec<_>>()
                    .join("; "),
            ),
        });
    }

    match process.wait_until(remaining(deadline)) {
        Ok(status) => Ok(Output {
            status,
            stdout,
            stderr,
        }),
        Err(mut expired) => {
            expired.timeout = timeout;
            expired.stdout = stdout;
            expired.stderr = stderr;
            Err(expired)
        }
    }
}

fn remaining(deadline: Instant) -> Duration {
    deadline.saturating_duration_since(Instant::now())
}

fn run_cli(args: &[&str]) -> Output {
    let mut command = Command::new(fastmcp_bin());
    command.args(args).env("FASTMCP_CHECK_FOR_UPDATES", "0");
    run_with_deadline(command, CLI_DEADLINE).unwrap_or_else(|expired| {
        panic!(
            "fastmcp exceeded the {:?} harness deadline; cleanup error: {:?}; captured stdout={} bytes, stderr={} bytes (content redacted)",
            expired.timeout,
            expired.cleanup_error,
            expired.stdout.len(),
            expired.stderr.len()
        )
    })
}

fn fixture_wait(deadline: Instant, context: &str) -> Duration {
    let wait = deadline.saturating_duration_since(Instant::now());
    assert!(
        !wait.is_zero(),
        "loopback fixture deadline elapsed while {context}"
    );
    wait.min(LOOPBACK_FIXTURE_POLL_INTERVAL)
}

fn accept_h1_fixture_connection(
    listener: &TcpListener,
    deadline: Instant,
    context: &str,
) -> TcpStream {
    listener
        .set_nonblocking(true)
        .expect("make loopback fixture listener nonblocking");
    loop {
        match listener.accept() {
            Ok((stream, _)) => return stream,
            Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                std::thread::park_timeout(fixture_wait(deadline, context));
            }
            Err(error) => panic!("{context}: accept loopback HTTP connection: {error}"),
        }
    }
}

fn read_h1_fixture_chunk(
    stream: &mut TcpStream,
    buffer: &mut [u8],
    deadline: Instant,
    context: &str,
) -> usize {
    loop {
        stream
            .set_read_timeout(Some(fixture_wait(deadline, context)))
            .expect("set loopback fixture read timeout");
        match stream.read(buffer) {
            Ok(read) => return read,
            Err(error)
                if matches!(
                    error.kind(),
                    std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock
                ) => {}
            Err(error) => panic!("{context}: read loopback HTTP request: {error}"),
        }
    }
}

fn configure_h1_fixture_write(stream: &TcpStream, deadline: Instant, context: &str) {
    stream
        .set_write_timeout(Some(fixture_wait(deadline, context)))
        .expect("set loopback fixture write timeout");
}

fn read_h1_json_request(stream: &mut TcpStream, deadline: Instant) -> (String, serde_json::Value) {
    let mut wire = Vec::new();
    let mut buffer = [0_u8; 4_096];
    let head_end = loop {
        let read = read_h1_fixture_chunk(stream, &mut buffer, deadline, "read HTTP request head");
        assert!(read > 0, "HTTP client closed before a complete request");
        wire.extend_from_slice(&buffer[..read]);
        if let Some(position) = wire.windows(4).position(|window| window == b"\r\n\r\n") {
            break position + 4;
        }
    };
    let head = std::str::from_utf8(&wire[..head_end])
        .expect("HTTP fixture request head is UTF-8")
        .to_owned();
    let content_length = head
        .lines()
        .find_map(|line| {
            let (name, value) = line.split_once(':')?;
            name.eq_ignore_ascii_case("content-length").then(|| {
                value
                    .trim()
                    .parse::<usize>()
                    .expect("HTTP fixture content length is numeric")
            })
        })
        .expect("HTTP fixture request has a content length");
    while wire.len() < head_end + content_length {
        let read = read_h1_fixture_chunk(stream, &mut buffer, deadline, "read HTTP request body");
        assert!(
            read > 0,
            "HTTP client closed before its complete request body"
        );
        wire.extend_from_slice(&buffer[..read]);
    }
    let body = serde_json::from_slice(&wire[head_end..head_end + content_length])
        .expect("HTTP fixture request body is JSON-RPC");
    (head, body)
}

fn write_h1_json_response(stream: &mut TcpStream, deadline: Instant, body: &str) {
    write_h1_json_response_with_status(stream, deadline, 200, body);
}

fn write_h1_json_response_with_status(
    stream: &mut TcpStream,
    deadline: Instant,
    status: u16,
    body: &str,
) {
    let reason = match status {
        200 => "OK",
        404 => "Not Found",
        _ => "Test Response",
    };
    configure_h1_fixture_write(stream, deadline, "write HTTP JSON response");
    write!(
        stream,
        "HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
        body.len()
    )
    .expect("write HTTP fixture response head");
    stream
        .write_all(body.as_bytes())
        .expect("write HTTP fixture response body");
    stream.flush().expect("flush HTTP fixture response");
}

#[cfg(feature = "legacy-2024-11-05")]
fn read_h1_request_head(stream: &mut TcpStream, deadline: Instant) -> String {
    let mut wire = Vec::new();
    let mut buffer = [0_u8; 4_096];
    while !wire.windows(4).any(|window| window == b"\r\n\r\n") {
        let read = read_h1_fixture_chunk(stream, &mut buffer, deadline, "read HTTP request head");
        assert!(read > 0, "HTTP client closed before its request head");
        wire.extend_from_slice(&buffer[..read]);
    }
    std::str::from_utf8(&wire)
        .expect("HTTP fixture request head is UTF-8")
        .to_owned()
}

#[cfg(feature = "legacy-2024-11-05")]
fn write_h1_empty_response(stream: &mut TcpStream, deadline: Instant, status: u16) {
    let reason = match status {
        202 => "Accepted",
        404 => "Not Found",
        500 => "Internal Server Error",
        _ => "Test Response",
    };
    configure_h1_fixture_write(stream, deadline, "write empty HTTP response");
    write!(
        stream,
        "HTTP/1.1 {status} {reason}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
    )
    .expect("write empty HTTP fixture response head");
    stream.flush().expect("flush empty HTTP fixture response");
}

#[cfg(feature = "legacy-2024-11-05")]
fn begin_h1_chunked_sse(stream: &mut TcpStream, deadline: Instant) {
    configure_h1_fixture_write(stream, deadline, "write chunked SSE response");
    stream
        .write_all(
            b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\nConnection: keep-alive\r\n\r\n",
        )
        .expect("write chunked legacy SSE response head");
    stream
        .flush()
        .expect("flush chunked legacy SSE response head");
}

#[cfg(feature = "legacy-2024-11-05")]
fn write_h1_chunked_sse_event(stream: &mut TcpStream, deadline: Instant, event: &str) {
    configure_h1_fixture_write(stream, deadline, "write chunked SSE event");
    write!(stream, "{:X}\r\n{event}\r\n", event.len()).expect("write chunked legacy SSE event");
    stream.flush().expect("flush chunked legacy SSE event");
}

#[cfg(feature = "legacy-2024-11-05")]
fn serve_legacy_http_inspect_bundle(
    listener: TcpListener,
    deadline: Instant,
    modern_probe_status: Option<u16>,
    advertised_message_target: String,
) {
    if let Some(status) = modern_probe_status {
        let mut modern =
            accept_h1_fixture_connection(&listener, deadline, "accept modern HTTP probe");
        let (head, request) = read_h1_json_request(&mut modern, deadline);
        assert!(
            head.starts_with("POST /mcp HTTP/1.1\r\n"),
            "Auto must contact the configured modern POST endpoint first"
        );
        assert_eq!(request["id"], 1);
        assert_eq!(request["method"], "server/discover");
        write_h1_empty_response(&mut modern, deadline, status);
    }

    let mut sse = accept_h1_fixture_connection(&listener, deadline, "accept legacy SSE GET");
    let sse_head = read_h1_request_head(&mut sse, deadline);
    assert!(
        sse_head.starts_with("GET /legacy-sse HTTP/1.1\r\n"),
        "the first legacy contact must use the configured SSE endpoint"
    );
    assert!(
        !sse_head.contains("MCP-Protocol-Version:"),
        "the exact legacy SSE GET must not carry final headers"
    );
    begin_h1_chunked_sse(&mut sse, deadline);
    write_h1_chunked_sse_event(
        &mut sse,
        deadline,
        &format!("event: endpoint\ndata: {advertised_message_target}\n\n"),
    );

    for (expected_method, response) in [
        (
            "initialize",
            Some(
                r#"{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{}},"serverInfo":{"name":"legacy-h1-inspect","version":"1.0.0"}}}"#,
            ),
        ),
        ("notifications/initialized", None),
        (
            "tools/list",
            Some(
                r#"{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"legacy-h1-tool","description":"legacy H1 catalog","inputSchema":{"type":"object"}}]}}"#,
            ),
        ),
    ] {
        let mut message = accept_h1_fixture_connection(
            &listener,
            deadline,
            "accept configured legacy message POST",
        );
        let (head, request) = read_h1_json_request(&mut message, deadline);
        assert!(
            head.starts_with("POST /legacy-message HTTP/1.1\r\n"),
            "legacy request must retain the configured message POST endpoint"
        );
        assert_eq!(request["method"], expected_method);
        assert!(
            request
                .get("params")
                .and_then(serde_json::Value::as_object)
                .is_none_or(|params| !params.contains_key("_meta")),
            "legacy wire must not carry final metadata"
        );
        write_h1_empty_response(&mut message, deadline, 202);
        if let Some(response) = response {
            write_h1_chunked_sse_event(
                &mut sse,
                deadline,
                &format!("event: message\ndata: {response}\n\n"),
            );
        }
    }
}

struct LoopbackFixture {
    completion: mpsc::Receiver<Result<(), String>>,
    worker: Option<std::thread::JoinHandle<()>>,
}

impl Drop for LoopbackFixture {
    fn drop(&mut self) {
        let Some(worker) = self.worker.take() else {
            return;
        };
        // Fixture I/O has a finite deadline. Even an assertion failure in the
        // owning test must retain and join the worker rather than detach it.
        let _ = self.completion.recv_timeout(LOOPBACK_FIXTURE_ACK_DEADLINE);
        let _ = worker.join();
    }
}

fn spawn_loopback_fixture<F>(context: &'static str, fixture: F) -> LoopbackFixture
where
    F: FnOnce(Instant) + Send + 'static,
{
    spawn_loopback_fixture_with_deadline(context, LOOPBACK_FIXTURE_DEADLINE, fixture)
}

fn spawn_loopback_fixture_with_deadline<F>(
    context: &'static str,
    timeout: Duration,
    fixture: F,
) -> LoopbackFixture
where
    F: FnOnce(Instant) + Send + 'static,
{
    let (completed, completion) = mpsc::channel();
    let worker = std::thread::spawn(move || {
        let deadline = Instant::now() + timeout;
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| fixture(deadline)))
            .map_err(|_| format!("{context} panicked"));
        // This acknowledgement is the worker's final action. Once the
        // bounded receipt arrives, joining cannot wait on fixture work.
        let _ = completed.send(result);
    });
    LoopbackFixture {
        completion,
        worker: Some(worker),
    }
}

fn wait_for_loopback_fixture(mut fixture: LoopbackFixture, context: &str) {
    let completion = fixture
        .completion
        .recv_timeout(LOOPBACK_FIXTURE_ACK_DEADLINE)
        .unwrap_or_else(|error| panic!("{context} did not acknowledge completion: {error}"));
    fixture
        .worker
        .take()
        .expect("loopback fixture worker must be retained until joined")
        .join()
        .unwrap_or_else(|_| panic!("{context} worker must join after completion acknowledgement"));
    completion.unwrap_or_else(|error| panic!("{context} failed: {error}"));
}

#[test]
fn loopback_fixture_timeout_acknowledges_before_settled_join() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback timeout fixture");
    let started = Instant::now();
    let mut fixture = spawn_loopback_fixture_with_deadline(
        "loopback timeout fixture",
        Duration::from_millis(30),
        move |deadline| {
            let _ =
                accept_h1_fixture_connection(&listener, deadline, "accept absent loopback client");
        },
    );

    let completion = fixture
        .completion
        .recv_timeout(Duration::from_millis(250))
        .expect("timed-out loopback fixture must acknowledge completion");
    fixture
        .worker
        .take()
        .expect("timed-out loopback fixture worker must be retained until joined")
        .join()
        .expect("timed-out loopback fixture must join after acknowledgement");
    assert!(
        completion.is_err(),
        "absent loopback client must fail the fixture"
    );
    assert!(
        started.elapsed() < Duration::from_millis(250),
        "absent loopback client must fail within the fixture deadline"
    );
}

#[test]
fn loopback_fixture_drop_joins_an_abandoned_bounded_worker() {
    let completed = Arc::new(AtomicBool::new(false));
    let worker_completed = Arc::clone(&completed);
    let fixture = spawn_loopback_fixture_with_deadline(
        "abandoned loopback fixture",
        Duration::from_millis(250),
        move |_| {
            std::thread::park_timeout(Duration::from_millis(40));
            worker_completed.store(true, Ordering::SeqCst);
        },
    );

    drop(fixture);
    assert!(
        completed.load(Ordering::SeqCst),
        "dropping a fixture must join its bounded worker instead of detaching it"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
fn inspect_http_bundle(
    policy: &str,
    modern_url: &str,
    legacy_sse_url: &str,
    legacy_message_url: &str,
) -> Output {
    run_cli(&[
        "inspect",
        "--http-url",
        modern_url,
        "--legacy-sse-url",
        legacy_sse_url,
        "--legacy-message-url",
        legacy_message_url,
        "--protocol-policy",
        policy,
        "--format",
        "json",
    ])
}

struct ForbiddenHttpContactObserver {
    shutdown: Option<mpsc::Sender<()>>,
    completion: Option<mpsc::Receiver<Result<(), String>>>,
    observer: Option<std::thread::JoinHandle<()>>,
}

impl Drop for ForbiddenHttpContactObserver {
    fn drop(&mut self) {
        if let Some(shutdown) = self.shutdown.take() {
            let _ = shutdown.send(());
        }
        if let Some(completion) = self.completion.take() {
            let _ = completion.recv_timeout(FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE);
        }
        if let Some(observer) = self.observer.take() {
            let _ = observer.join();
        }
    }
}

struct ForbiddenContactFinalDrainControl {
    started: mpsc::Sender<()>,
    permit: mpsc::Receiver<()>,
}

fn observer_shutdown_requested(shutdown_requested: &mpsc::Receiver<()>) -> bool {
    matches!(
        shutdown_requested.try_recv(),
        Ok(()) | Err(mpsc::TryRecvError::Disconnected)
    )
}

fn drain_forbidden_http_contacts_after_shutdown(
    listener: &TcpListener,
    contacts: &AtomicUsize,
    context: &str,
    shutdown_requested: &mpsc::Receiver<()>,
) -> Result<(), String> {
    let final_deadline = Instant::now() + FORBIDDEN_CONTACT_FINAL_DRAIN_DEADLINE;
    let mut quiet_deadline = Instant::now() + FORBIDDEN_CONTACT_FINAL_QUIET_INTERVAL;

    loop {
        for _ in 0..FORBIDDEN_CONTACT_WORK_CAP {
            match listener.accept() {
                Ok((_stream, _)) => {
                    contacts.fetch_add(1, Ordering::SeqCst);
                    quiet_deadline = Instant::now() + FORBIDDEN_CONTACT_FINAL_QUIET_INTERVAL;
                }
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
                Err(error) => {
                    return Err(format!(
                        "observe forbidden {context} contact during shutdown: {error}"
                    ));
                }
            }
        }

        let now = Instant::now();
        if now >= quiet_deadline || now >= final_deadline {
            return Ok(());
        }
        let wait = quiet_deadline
            .min(final_deadline)
            .saturating_duration_since(now)
            .min(FORBIDDEN_CONTACT_POLL_INTERVAL);
        match shutdown_requested.recv_timeout(wait) {
            Ok(())
            | Err(mpsc::RecvTimeoutError::Disconnected | mpsc::RecvTimeoutError::Timeout) => {}
        }
    }
}

fn observe_forbidden_http_contacts(
    listener: TcpListener,
    contacts: Arc<AtomicUsize>,
    context: &'static str,
    shutdown_requested: &mpsc::Receiver<()>,
    final_drain_control: Option<ForbiddenContactFinalDrainControl>,
) -> Result<(), String> {
    'observe: loop {
        if observer_shutdown_requested(shutdown_requested) {
            break;
        }

        for _ in 0..FORBIDDEN_CONTACT_WORK_CAP {
            if observer_shutdown_requested(shutdown_requested) {
                break 'observe;
            }
            match listener.accept() {
                Ok((_stream, _)) => {
                    contacts.fetch_add(1, Ordering::SeqCst);
                }
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
                Err(error) => return Err(format!("observe forbidden {context} contact: {error}")),
            }
        }

        match shutdown_requested.recv_timeout(FORBIDDEN_CONTACT_POLL_INTERVAL) {
            Ok(()) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
            Err(mpsc::RecvTimeoutError::Timeout) => {}
        }
    }

    if let Some(control) = final_drain_control {
        control
            .started
            .send(())
            .map_err(|_| format!("forbidden {context} final drain control was dropped"))?;
        control
            .permit
            .recv_timeout(FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
            .map_err(|error| {
                format!("forbidden {context} final drain was not permitted: {error}")
            })?;
    }
    drain_forbidden_http_contacts_after_shutdown(&listener, &contacts, context, shutdown_requested)
}

fn spawn_forbidden_http_contact_observer(
    listener: TcpListener,
    contacts: Arc<AtomicUsize>,
    context: &'static str,
) -> ForbiddenHttpContactObserver {
    spawn_forbidden_http_contact_observer_with_final_drain_control(
        listener, contacts, context, None,
    )
}

fn spawn_forbidden_http_contact_observer_with_final_drain_control(
    listener: TcpListener,
    contacts: Arc<AtomicUsize>,
    context: &'static str,
    final_drain_control: Option<ForbiddenContactFinalDrainControl>,
) -> ForbiddenHttpContactObserver {
    listener
        .set_nonblocking(true)
        .expect("make forbidden HTTP observer nonblocking");
    let (shutdown, shutdown_requested) = mpsc::channel();
    let (completed, completion) = mpsc::channel();
    let observer = std::thread::spawn(move || {
        let result = observe_forbidden_http_contacts(
            listener,
            contacts,
            context,
            &shutdown_requested,
            final_drain_control,
        );
        // The final drain has completed before this final worker action.
        let _ = completed.send(result);
    });
    ForbiddenHttpContactObserver {
        shutdown: Some(shutdown),
        completion: Some(completion),
        observer: Some(observer),
    }
}

fn stop_forbidden_http_contact_observer(mut observer: ForbiddenHttpContactObserver, context: &str) {
    let shutdown_result = observer
        .shutdown
        .take()
        .expect("forbidden HTTP observer shutdown sender must be retained")
        .send(());
    let completion = observer
        .completion
        .take()
        .expect("forbidden HTTP observer completion receiver must be retained")
        .recv_timeout(FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
        .unwrap_or_else(|error| {
            panic!("forbidden {context} observer did not acknowledge shutdown: {error}")
        });
    observer
        .observer
        .take()
        .expect("forbidden HTTP observer worker must be retained until joined")
        .join()
        .unwrap_or_else(|_| panic!("forbidden {context} observer must complete"));
    shutdown_result.unwrap_or_else(|_| panic!("signal forbidden {context} observer shutdown"));
    completion.unwrap_or_else(|error| panic!("forbidden {context} observer failed: {error}"));
}

#[test]
fn forbidden_contact_observer_caps_continuous_contacts_and_drains_shutdown_queue() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind observer helper fixture");
    let address = listener
        .local_addr()
        .expect("read observer helper fixture address");
    let contacts = Arc::new(AtomicUsize::new(0));
    let (final_drain_started, final_drain_started_at) = mpsc::channel();
    let (permit_final_drain, final_drain_permit) = mpsc::channel();
    let mut observer = spawn_forbidden_http_contact_observer_with_final_drain_control(
        listener,
        Arc::clone(&contacts),
        "observer helper fixture",
        Some(ForbiddenContactFinalDrainControl {
            started: final_drain_started,
            permit: final_drain_permit,
        }),
    );

    let continuous_contact_count = FORBIDDEN_CONTACT_WORK_CAP * 2;
    let continuous_contacts = (0..continuous_contact_count)
        .map(|_| {
            TcpStream::connect_timeout(&address, FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
                .expect("queue continuous observer contact")
        })
        .collect::<Vec<_>>();
    observer
        .shutdown
        .as_ref()
        .expect("observer helper shutdown sender must be retained")
        .send(())
        .expect("signal observer helper shutdown");
    final_drain_started_at
        .recv_timeout(FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
        .expect("observer helper must acknowledge final-drain start");
    let shutdown_continuous_contact_count = FORBIDDEN_CONTACT_WORK_CAP * 2;
    let shutdown_continuous_contacts = (0..shutdown_continuous_contact_count)
        .map(|_| {
            TcpStream::connect_timeout(&address, FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
                .expect("queue continuous observer contact during shutdown")
        })
        .collect::<Vec<_>>();
    let queued_at_shutdown =
        TcpStream::connect_timeout(&address, FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
            .expect("queue observer contact after shutdown acknowledgement");
    permit_final_drain
        .send(())
        .expect("permit observer helper final drain");

    let completion = observer
        .completion
        .take()
        .expect("observer helper completion receiver must be retained")
        .recv_timeout(FORBIDDEN_CONTACT_SHUTDOWN_ACK_DEADLINE)
        .expect("observer helper must acknowledge shutdown completion");
    observer
        .observer
        .take()
        .expect("observer helper worker must be retained until joined")
        .join()
        .expect("observer helper must join after completion acknowledgement");
    completion.expect("observer helper final drain must succeed");
    assert_eq!(
        contacts.load(Ordering::SeqCst),
        continuous_contact_count + shutdown_continuous_contact_count + 1,
        "the capped observer must count continuous contacts and the contact queued at shutdown"
    );
    drop((
        continuous_contacts,
        shutdown_continuous_contacts,
        queued_at_shutdown,
    ));
}

fn inspect_protocol_fixture(policy: &str, format: &str, fixture: &str) -> Output {
    run_cli(&[
        "inspect",
        "--protocol-policy",
        policy,
        "--format",
        format,
        "/bin/sh",
        "--",
        "-c",
        fixture,
    ])
}

fn run_legacy_fixture_with_requests(requests: &[&str]) -> Output {
    let input = format!("{}\n", requests.join("\n"));
    let mut command = Command::new("/bin/sh");
    command
        .arg("-c")
        .arg("printf '%s' \"$1\" | /bin/sh -c \"$2\"")
        .arg("legacy-lifecycle-fixture")
        .arg(&input)
        .arg(LEGACY_FALLBACK_INSPECT_FIXTURE);
    run_with_deadline(command, Duration::from_secs(10))
        .expect("legacy lifecycle fixture must exit after its input closes")
}

fn run_modern_fixture_with_request(request: &str) -> Output {
    let mut command = Command::new("/bin/sh");
    command
        .arg("-c")
        .arg("printf '%s\n' \"$1\" | /bin/sh -c \"$2\"")
        .arg("modern-discovery-fixture")
        .arg(request)
        .arg(MODERN_INSPECT_FIXTURE);
    run_with_deadline(command, Duration::from_secs(10))
        .expect("modern discovery fixture must exit after its input closes")
}

#[cfg(unix)]
#[test]
fn e2e_test_json_report_against_static_protocol_fixture() {
    // Use a deterministic stdio peer as the subprocess being tested.
    // This exercises:
    // - stdio subprocess spawning
    // - initialization
    // - ping
    // - tools/resources/prompts listing
    let output = run_cli(&[
        "test",
        "--json",
        "--idle-timeout",
        "30",
        "--absolute-timeout",
        "120",
        "/bin/sh",
        "--",
        STATIC_MCP_SERVER_FIXTURE,
    ]);

    assert!(output.status.success());

    let out = stdout_str(&output);
    let json: serde_json::Value = serde_json::from_str(&out).expect("parse test json");
    assert_eq!(json.get("success").and_then(|v| v.as_bool()), Some(true));
    let tests = json
        .get("tests")
        .and_then(|v| v.as_array())
        .expect("test results array");
    for (name, expected_details) in [
        ("initialize", "protocol 2024-11-05"),
        ("ping", "server responded"),
        ("list_tools", "4 tools"),
        ("list_resources", "2 resources"),
        ("list_prompts", "2 prompts"),
    ] {
        let result = tests
            .iter()
            .find(|test| test.get("name").and_then(|value| value.as_str()) == Some(name))
            .unwrap_or_else(|| panic!("test report must include {name}"));
        assert_eq!(
            result.get("success").and_then(serde_json::Value::as_bool),
            Some(true),
            "{name} must succeed"
        );
        assert_ne!(
            result.get("skipped").and_then(serde_json::Value::as_bool),
            Some(true),
            "{name} must exercise the peer instead of being skipped"
        );
        assert_eq!(
            result.get("details").and_then(serde_json::Value::as_str),
            Some(expected_details),
            "{name} must report the expected bounded result"
        );
    }
    assert!(json.get("total_duration_ms").is_some());
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_protocol_policy_reports_selected_era_and_exact_version() {
    for (
        case_name,
        policy,
        fixture,
        expected_server_name,
        expected_version,
        expected_era,
        assert_wire,
    ) in [
        (
            "modern-only",
            "modern-only",
            MODERN_INSPECT_FIXTURE,
            "modern-inspect-server",
            "2026-07-28",
            "modern-2026",
            assert_modern_negotiation_wire as fn(&[serde_json::Value]),
        ),
        (
            "auto-modern",
            "auto",
            MODERN_INSPECT_FIXTURE,
            "modern-inspect-server",
            "2026-07-28",
            "modern-2026",
            assert_modern_negotiation_wire,
        ),
        (
            "legacy-only",
            "legacy-only",
            LEGACY_FALLBACK_INSPECT_FIXTURE,
            "legacy-inspect-server",
            "2024-11-05",
            "legacy-2024",
            assert_legacy_negotiation_wire,
        ),
        (
            "auto-legacy-fallback",
            "auto",
            LEGACY_FALLBACK_INSPECT_FIXTURE,
            "legacy-inspect-server",
            "2024-11-05",
            "legacy-2024",
            assert_auto_legacy_fallback_wire,
        ),
    ] {
        let text_output = inspect_protocol_fixture(policy, "text", fixture);
        assert!(
            text_output.status.success(),
            "{policy} inspect text should succeed, stderr: {}",
            stderr_str(&text_output)
        );
        let expected_text =
            format!("Protocol: policy={policy} version={expected_version} era={expected_era}");
        let text = stdout_str(&text_output);
        let expected_server_text = format!("Server: {expected_server_name} v1.0.0");
        assert!(
            text.lines().any(|line| line == expected_server_text),
            "{case_name} inspect must render the server identity admitted from its selected-era response"
        );
        assert_eq!(
            text.lines().find(|line| line.starts_with("Protocol: ")),
            Some(expected_text.as_str()),
            "{case_name} text inspect must emit the selected protocol triad exactly"
        );
        if expected_era == "modern-2026" {
            let capabilities = text
                .lines()
                .find(|line| line.starts_with("Capabilities (final discovery): "))
                .expect(
                    "modern inspect text must identify and retain final discovery capabilities",
                );
            assert!(capabilities.contains("\"completions\":{}"));
            assert!(capabilities.contains("\"io.example/inspect\":{\"enabled\":true}"));
        } else {
            assert!(
                text.lines().any(|line| line
                    == "Capabilities: tools=true resources=true prompts=true logging=false"),
                "exact legacy inspect must retain its legacy capability rendering"
            );
        }
        assert_wire(&observed_protocol_wire(&text_output));

        let json_output = inspect_protocol_fixture(policy, "json", fixture);
        assert!(
            json_output.status.success(),
            "{policy} inspect JSON should succeed, stderr: {}",
            stderr_str(&json_output)
        );
        let json: serde_json::Value =
            serde_json::from_str(&stdout_str(&json_output)).expect("inspect output should be JSON");
        assert_eq!(json["server"]["name"], expected_server_name);
        assert_eq!(json["server"]["version"], "1.0.0");
        assert_eq!(json["protocol"]["policy"], policy);
        assert_eq!(json["protocol"]["version"], expected_version);
        assert_eq!(json["protocol"]["era"], expected_era);
        if expected_era == "modern-2026" {
            assert_eq!(json["capabilities"]["completions"], serde_json::json!({}));
            assert_eq!(
                json["capabilities"]["extensions"]["io.example/inspect"]["enabled"], true,
                "modern inspect JSON must retain final extension settings"
            );
        } else {
            assert!(
                json["capabilities"].get("completions").is_none()
                    && json["capabilities"].get("extensions").is_none(),
                "exact legacy inspect must not render final-only discovery members"
            );
        }
        assert_wire(&observed_protocol_wire(&json_output));
    }
}

#[test]
fn e2e_cli_inspect_modern_discovery_planted_negative_rejects_invalid_completions_shape() {
    let fixture = MODERN_INSPECT_FIXTURE.replace("\"completions\":{}", "\"completions\":true");
    let output = inspect_protocol_fixture("modern-only", "json", &fixture);

    assert!(
        !output.status.success(),
        "changing only completions from an object to a boolean must reject final discovery"
    );
    let wire = observed_protocol_wire(&output);
    assert_modern_negotiation_wire(&wire);
    assert_no_legacy_initialize(&wire);
}

#[test]
fn e2e_cli_inspect_modern_discovery_planted_negative_rejects_invalid_extensions_shape() {
    let fixture = MODERN_INSPECT_FIXTURE.replace(
        r#""extensions":{"io.example/inspect":{"enabled":true}}"#,
        r#""extensions":{"io.example/inspect":true}"#,
    );
    assert_ne!(fixture, MODERN_INSPECT_FIXTURE);
    let output = inspect_protocol_fixture("modern-only", "json", &fixture);

    assert!(
        !output.status.success(),
        "changing only an extension setting from an object to a boolean must reject final discovery"
    );
    let wire = observed_protocol_wire(&output);
    assert_modern_negotiation_wire(&wire);
    assert_no_legacy_initialize(&wire);
}

#[test]
fn e2e_cli_inspect_modern_discovery_planted_negative_rejects_malformed_server_info_metadata() {
    let fixture =
        MODERN_INSPECT_FIXTURE.replace(r#""name":"modern-inspect-server""#, r#""name":false"#);
    assert_ne!(fixture, MODERN_INSPECT_FIXTURE);
    let output = inspect_protocol_fixture("modern-only", "json", &fixture);

    assert!(
        !output.status.success(),
        "changing only _meta serverInfo.name from a string to a boolean must reject final discovery"
    );
    let wire = observed_protocol_wire(&output);
    assert_modern_negotiation_wire(&wire);
    assert_no_legacy_initialize(&wire);
}

#[test]
fn e2e_cli_inspect_http_bundle_modern_only_uses_live_modern_h1_and_negotiated_status_renderer() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind modern HTTP inspect fixture");
    let address = listener
        .local_addr()
        .expect("read modern HTTP inspect fixture address");
    let url = format!("http://{address}/mcp");
    let fixture = spawn_loopback_fixture("modern HTTP inspect fixture", move |deadline| {
        let mut stream = accept_h1_fixture_connection(
            &listener,
            deadline,
            "accept modern HTTP inspect connection",
        );
        let (head, request) = read_h1_json_request(&mut stream, deadline);
        assert!(
            head.starts_with("POST /mcp HTTP/1.1\r\n"),
            "inspect must use the configured modern H1 POST route"
        );
        assert_eq!(request["id"], 1);
        assert_eq!(request["method"], "server/discover");
        assert_eq!(
            request["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"],
            "2026-07-28"
        );
        write_h1_json_response(
            &mut stream,
            deadline,
            r#"{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{"tools":{}},"_meta":{"io.modelcontextprotocol/serverInfo":{"name":"modern-h1-inspect","version":"1.0.0"}},"ttlMs":0,"cacheScope":"private"}}"#,
        );

        let mut stream = accept_h1_fixture_connection(
            &listener,
            deadline,
            "accept modern HTTP tools-list connection",
        );
        let (head, request) = read_h1_json_request(&mut stream, deadline);
        assert!(
            head.starts_with("POST /mcp HTTP/1.1\r\n"),
            "inspect tools/list must retain the configured modern H1 POST route"
        );
        assert_eq!(request["id"], 2);
        assert_eq!(request["method"], "tools/list");
        assert_eq!(
            request["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"],
            "2026-07-28"
        );
        write_h1_json_response(
            &mut stream,
            deadline,
            r#"{"jsonrpc":"2.0","id":2,"result":{"resultType":"complete","tools":[{"name":"h1-tool","description":"modern H1 catalog","inputSchema":{"type":"object"}}],"ttlMs":0,"cacheScope":"private"}}"#,
        );
    });

    let output = run_cli(&[
        "inspect",
        "--http-url",
        &url,
        "--protocol-policy",
        "modern-only",
        "--format",
        "json",
    ]);
    assert!(
        output.status.success(),
        "modern HTTP inspect should succeed, stderr: {}",
        stderr_str(&output)
    );
    wait_for_loopback_fixture(fixture, "modern HTTP inspect fixture");

    let rendered: serde_json::Value =
        serde_json::from_str(&stdout_str(&output)).expect("inspect output is diagnostic JSON");
    assert_eq!(rendered["server"]["name"], "modern-h1-inspect");
    assert_eq!(rendered["protocol"]["policy"], "modern-only");
    assert_eq!(rendered["protocol"]["version"], "2026-07-28");
    assert_eq!(rendered["protocol"]["era"], "modern-2026");
    assert_eq!(rendered["tools"][0]["name"], "h1-tool");
}

#[cfg(not(feature = "legacy-2024-11-05"))]
#[test]
fn e2e_cli_no_default_auto_and_legacy_only_refuse_before_http_contact() {
    let listener =
        TcpListener::bind("127.0.0.1:0").expect("bind no-default feature-unavailable observer");
    let address = listener
        .local_addr()
        .expect("read no-default feature-unavailable observer address");
    let url = format!("http://{address}/mcp");
    let contacts = Arc::new(AtomicUsize::new(0));
    let observer = spawn_forbidden_http_contact_observer(
        listener,
        Arc::clone(&contacts),
        "no-default feature-unavailable HTTP",
    );

    for policy in ["auto", "legacy-only"] {
        let output = run_cli(&[
            "inspect",
            "--http-url",
            &url,
            "--protocol-policy",
            policy,
            "--format",
            "json",
        ]);
        assert!(
            !output.status.success(),
            "a no-default-features CLI must refuse {policy} before HTTP contact"
        );
        let stderr = stderr_str(&output);
        assert!(
            stderr.contains("FeatureUnavailable") && stderr.contains("legacy-2024-11-05"),
            "the no-default-features refusal for {policy} must name the compiled-out feature: {stderr}"
        );
    }

    stop_forbidden_http_contact_observer(observer, "no-default feature-unavailable HTTP");
    assert_eq!(
        contacts.load(Ordering::SeqCst),
        0,
        "Auto and LegacyOnly must fail before contacting the configured HTTP endpoint"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_auto_keeps_modern_after_discovery_when_application_post_fails() {
    let modern_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind Auto modern application fixture");
    let modern_address = modern_listener
        .local_addr()
        .expect("read Auto modern application fixture address");
    let legacy_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind forbidden Auto legacy observer");
    let legacy_address = legacy_listener
        .local_addr()
        .expect("read forbidden Auto legacy observer address");
    let modern_url = format!("http://{modern_address}/mcp");
    let legacy_sse_url = format!("http://{legacy_address}/legacy-sse");
    let legacy_message_url = format!("http://{legacy_address}/legacy-message");
    let legacy_contacts = Arc::new(AtomicUsize::new(0));
    let legacy_observer = spawn_forbidden_http_contact_observer(
        legacy_listener,
        Arc::clone(&legacy_contacts),
        "Auto legacy",
    );
    let modern_fixture = spawn_loopback_fixture(
        "Auto modern application fixture",
        move |deadline| {
            let mut discovery = accept_h1_fixture_connection(
                &modern_listener,
                deadline,
                "accept Auto modern discovery POST",
            );
            let (head, request) = read_h1_json_request(&mut discovery, deadline);
            assert!(head.starts_with("POST /mcp HTTP/1.1\r\n"));
            assert_eq!(request["id"], 1);
            assert_eq!(request["method"], "server/discover");
            assert_eq!(
                request["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"],
                "2026-07-28"
            );
            write_h1_json_response(
                &mut discovery,
                deadline,
                r#"{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{"tools":{}},"_meta":{"io.modelcontextprotocol/serverInfo":{"name":"auto-modern-h1-inspect","version":"1.0.0"}},"ttlMs":0,"cacheScope":"private"}}"#,
            );

            let mut application = accept_h1_fixture_connection(
                &modern_listener,
                deadline,
                "accept Auto modern tools-list POST",
            );
            let (head, request) = read_h1_json_request(&mut application, deadline);
            assert!(head.starts_with("POST /mcp HTTP/1.1\r\n"));
            assert_eq!(request["id"], 2);
            assert_eq!(request["method"], "tools/list");
            assert_eq!(
                request["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"],
                "2026-07-28"
            );
            write_h1_empty_response(&mut application, deadline, 500);
        },
    );

    let output = inspect_http_bundle("auto", &modern_url, &legacy_sse_url, &legacy_message_url);
    assert!(
        !output.status.success(),
        "an application POST failure after modern discovery must fail instead of downgrading"
    );
    wait_for_loopback_fixture(modern_fixture, "Auto modern application fixture");
    stop_forbidden_http_contact_observer(legacy_observer, "Auto legacy");
    assert_eq!(
        legacy_contacts.load(Ordering::SeqCst),
        0,
        "a modern-selected Auto connection must not contact either configured legacy endpoint after an application failure"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_auto_uses_authorized_legacy_fallback() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind Auto legacy HTTP fixture");
    let address = listener
        .local_addr()
        .expect("read Auto legacy HTTP fixture address");
    let modern_url = format!("http://{address}/mcp");
    let legacy_sse_url = format!("http://{address}/legacy-sse");
    let legacy_message_url = format!("http://{address}/legacy-message");
    let fixture_message_url = legacy_message_url.clone();
    let fixture = spawn_loopback_fixture("Auto legacy HTTP fixture", move |deadline| {
        serve_legacy_http_inspect_bundle(listener, deadline, Some(404), fixture_message_url);
    });

    let output = inspect_http_bundle("auto", &modern_url, &legacy_sse_url, &legacy_message_url);
    assert!(
        output.status.success(),
        "Auto must use the configured legacy bundle after its authorized modern refusal: {}",
        stderr_str(&output)
    );
    wait_for_loopback_fixture(fixture, "Auto legacy HTTP fixture");
    let rendered: serde_json::Value =
        serde_json::from_str(&stdout_str(&output)).expect("Auto inspect output is diagnostic JSON");
    assert_eq!(rendered["server"]["name"], "legacy-h1-inspect");
    assert_eq!(rendered["protocol"]["policy"], "auto");
    assert_eq!(rendered["protocol"]["version"], "2024-11-05");
    assert_eq!(rendered["protocol"]["era"], "legacy-2024");
    assert_eq!(rendered["tools"][0]["name"], "legacy-h1-tool");
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_auto_rejects_recognized_discovery_error_without_legacy_contact() {
    let modern_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind recognized-refusal modern fixture");
    let modern_address = modern_listener
        .local_addr()
        .expect("read recognized-refusal modern fixture address");
    let legacy_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind recognized-refusal legacy observer");
    let legacy_address = legacy_listener
        .local_addr()
        .expect("read recognized-refusal legacy observer address");
    let modern_url = format!("http://{modern_address}/mcp");
    let legacy_sse_url = format!("http://{legacy_address}/legacy-sse");
    let legacy_message_url = format!("http://{legacy_address}/legacy-message");
    let legacy_contacts = Arc::new(AtomicUsize::new(0));
    let legacy_observer = spawn_forbidden_http_contact_observer(
        legacy_listener,
        Arc::clone(&legacy_contacts),
        "recognized-refusal legacy",
    );
    let modern_fixture =
        spawn_loopback_fixture("recognized-refusal modern fixture", move |deadline| {
            let mut modern = accept_h1_fixture_connection(
                &modern_listener,
                deadline,
                "accept recognized-refusal modern discovery POST",
            );
            let (head, request) = read_h1_json_request(&mut modern, deadline);
            assert!(head.starts_with("POST /mcp HTTP/1.1\r\n"));
            assert_eq!(request["id"], 1);
            assert_eq!(request["method"], "server/discover");
            write_h1_json_response_with_status(
                &mut modern,
                deadline,
                404,
                r#"{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"Method not found"}}"#,
            );
        });

    let output = inspect_http_bundle("auto", &modern_url, &legacy_sse_url, &legacy_message_url);
    assert!(
        !output.status.success(),
        "a recognized discovery error at the same 404 as the fallback-positive case must not downgrade"
    );
    wait_for_loopback_fixture(modern_fixture, "recognized-refusal modern fixture");
    stop_forbidden_http_contact_observer(legacy_observer, "recognized-refusal legacy");
    assert_eq!(
        legacy_contacts.load(Ordering::SeqCst),
        0,
        "a recognized modern discovery error must not contact either configured legacy endpoint"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_legacy_only_skips_modern_contact() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind LegacyOnly HTTP fixture");
    let address = listener
        .local_addr()
        .expect("read LegacyOnly HTTP fixture address");
    let modern_url = format!("http://{address}/mcp");
    let legacy_sse_url = format!("http://{address}/legacy-sse");
    let legacy_message_url = format!("http://{address}/legacy-message");
    let fixture_message_url = legacy_message_url.clone();
    let fixture = spawn_loopback_fixture("LegacyOnly HTTP fixture", move |deadline| {
        serve_legacy_http_inspect_bundle(listener, deadline, None, fixture_message_url);
    });

    let output = inspect_http_bundle(
        "legacy-only",
        &modern_url,
        &legacy_sse_url,
        &legacy_message_url,
    );
    assert!(
        output.status.success(),
        "LegacyOnly must use the configured legacy bundle directly: {}",
        stderr_str(&output)
    );
    wait_for_loopback_fixture(fixture, "LegacyOnly HTTP fixture");
    let rendered: serde_json::Value = serde_json::from_str(&stdout_str(&output))
        .expect("LegacyOnly inspect output is diagnostic JSON");
    assert_eq!(rendered["protocol"]["policy"], "legacy-only");
    assert_eq!(rendered["protocol"]["version"], "2024-11-05");
    assert_eq!(rendered["protocol"]["era"], "legacy-2024");
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_rejects_incomplete_auto_without_contact() {
    let listener = TcpListener::bind("127.0.0.1:0").expect("bind no-probe HTTP fixture");
    let address = listener
        .local_addr()
        .expect("read no-probe HTTP fixture address");
    let url = format!("http://{address}/mcp");
    let legacy_sse_url = format!("http://{address}/legacy-sse");
    let probes = Arc::new(AtomicUsize::new(0));
    let observer = spawn_forbidden_http_contact_observer(
        listener,
        Arc::clone(&probes),
        "HTTP policy rejection",
    );

    let output = run_cli(&[
        "inspect",
        "--http-url",
        &url,
        "--legacy-sse-url",
        &legacy_sse_url,
        "--protocol-policy",
        "auto",
        "--format",
        "json",
    ]);
    assert!(
        !output.status.success(),
        "an incomplete Auto bundle must not infer its missing legacy message endpoint"
    );
    assert!(
        stderr_str(&output).contains("requires a configured legacy message POST target"),
        "the incomplete-bundle rejection must be explicit"
    );
    stop_forbidden_http_contact_observer(observer, "HTTP policy rejection");
    assert_eq!(
        probes.load(Ordering::SeqCst),
        0,
        "incomplete-bundle rejection must occur before any HTTP side effect"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_rejects_mismatched_legacy_message_endpoint() {
    let sse_listener = TcpListener::bind("127.0.0.1:0").expect("bind mismatch SSE fixture");
    let sse_address = sse_listener
        .local_addr()
        .expect("read mismatch SSE fixture address");
    let configured_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind configured message observer");
    let configured_address = configured_listener
        .local_addr()
        .expect("read configured message observer address");
    let advertised_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind advertised message observer");
    let advertised_address = advertised_listener
        .local_addr()
        .expect("read advertised message observer address");
    let configured_contacts = Arc::new(AtomicUsize::new(0));
    let advertised_contacts = Arc::new(AtomicUsize::new(0));
    let configured_observer = spawn_forbidden_http_contact_observer(
        configured_listener,
        Arc::clone(&configured_contacts),
        "configured message",
    );
    let advertised_observer = spawn_forbidden_http_contact_observer(
        advertised_listener,
        Arc::clone(&advertised_contacts),
        "advertised message",
    );
    let fixture = spawn_loopback_fixture("mismatch legacy SSE fixture", move |deadline| {
        let mut sse =
            accept_h1_fixture_connection(&sse_listener, deadline, "accept mismatch legacy SSE GET");
        let head = read_h1_request_head(&mut sse, deadline);
        assert!(head.starts_with("GET /legacy-sse HTTP/1.1\r\n"));
        begin_h1_chunked_sse(&mut sse, deadline);
        write_h1_chunked_sse_event(
            &mut sse,
            deadline,
            &format!("event: endpoint\ndata: http://{advertised_address}/legacy-message\n\n"),
        );
    });
    let modern_url = format!("http://{sse_address}/mcp");
    let legacy_sse_url = format!("http://{sse_address}/legacy-sse");
    let configured_message_url = format!("http://{configured_address}/legacy-message");

    let output = inspect_http_bundle(
        "legacy-only",
        &modern_url,
        &legacy_sse_url,
        &configured_message_url,
    );
    assert!(
        !output.status.success(),
        "changing only the SSE-advertised message target must reject the bundle"
    );
    wait_for_loopback_fixture(fixture, "mismatch legacy SSE fixture");
    stop_forbidden_http_contact_observer(configured_observer, "configured message");
    stop_forbidden_http_contact_observer(advertised_observer, "advertised message");
    assert_eq!(
        configured_contacts.load(Ordering::SeqCst),
        0,
        "a mismatched legacy endpoint must not contact the configured message route"
    );
    assert_eq!(
        advertised_contacts.load(Ordering::SeqCst),
        0,
        "a mismatched legacy endpoint must not contact the advertised foreign route"
    );
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_http_bundle_auto_rejects_unauthorized_fallback_without_legacy_contact() {
    let modern_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind unauthorized modern fixture");
    let modern_address = modern_listener
        .local_addr()
        .expect("read unauthorized modern fixture address");
    let legacy_listener =
        TcpListener::bind("127.0.0.1:0").expect("bind unauthorized legacy observer");
    let legacy_address = legacy_listener
        .local_addr()
        .expect("read unauthorized legacy observer address");
    let modern_url = format!("http://{modern_address}/mcp");
    let legacy_sse_url = format!("http://{legacy_address}/legacy-sse");
    let legacy_message_url = format!("http://{legacy_address}/legacy-message");
    let legacy_contacts = Arc::new(AtomicUsize::new(0));
    let modern_fixture = spawn_loopback_fixture("unauthorized modern fixture", move |deadline| {
        let mut modern = accept_h1_fixture_connection(
            &modern_listener,
            deadline,
            "accept unauthorized modern probe",
        );
        let (head, request) = read_h1_json_request(&mut modern, deadline);
        assert!(head.starts_with("POST /mcp HTTP/1.1\r\n"));
        assert_eq!(request["method"], "server/discover");
        write_h1_empty_response(&mut modern, deadline, 500);
    });
    let legacy_observer = spawn_forbidden_http_contact_observer(
        legacy_listener,
        Arc::clone(&legacy_contacts),
        "unauthorized legacy",
    );

    let output = inspect_http_bundle("auto", &modern_url, &legacy_sse_url, &legacy_message_url);
    assert!(
        !output.status.success(),
        "changing only the authorized 404 refusal to an ordinary 500 must not downgrade"
    );
    wait_for_loopback_fixture(modern_fixture, "unauthorized modern fixture");
    stop_forbidden_http_contact_observer(legacy_observer, "unauthorized legacy");
    assert_eq!(
        legacy_contacts.load(Ordering::SeqCst),
        0,
        "an unauthorized modern failure must not contact a legacy endpoint"
    );
}

#[test]
fn e2e_legacy_lifecycle_fixture_planted_negative_rejects_list_before_initialized() {
    let output = run_legacy_fixture_with_requests(&[
        LEGACY_PLANTED_INITIALIZE_REQUEST,
        LEGACY_PLANTED_TOOLS_LIST_REQUEST,
    ]);
    assert!(
        !output.status.success(),
        "legacy fixture must reject an operating list request before notifications/initialized"
    );
    let wire = observed_protocol_wire(&output);
    assert_eq!(
        wire.iter().map(request_method).collect::<Vec<_>>(),
        vec!["initialize", "tools/list"],
        "planted pre-initialization list must be the rejected observed wire"
    );
    assert_exact_legacy_initialize_request(&wire[0]);
    assert_exact_legacy_operating_request(&wire[1]);
}

#[test]
fn e2e_legacy_lifecycle_fixture_planted_negative_rejects_repeated_initialize() {
    let output = run_legacy_fixture_with_requests(&[
        LEGACY_PLANTED_INITIALIZE_REQUEST,
        LEGACY_PLANTED_REPEATED_INITIALIZE_REQUEST,
    ]);
    assert!(
        !output.status.success(),
        "legacy fixture must reject a repeated initialize before notifications/initialized"
    );
    let wire = observed_protocol_wire(&output);
    assert_eq!(
        wire.iter().map(request_method).collect::<Vec<_>>(),
        vec!["initialize", "initialize"],
        "planted repeated initialization must be the rejected observed wire"
    );
    assert_exact_legacy_initialize_request(&wire[0]);
    assert_exact_legacy_initialize_request(&wire[1]);
}

#[test]
fn e2e_legacy_initialized_fixture_planted_negatives_reject_noncanonical_envelopes() {
    for (case_name, malformed_notification) in [
        (
            "extra envelope field",
            r#"{"jsonrpc":"2.0","method":"notifications/initialized","extra":true}"#,
        ),
        (
            "unexpected params object",
            r#"{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}"#,
        ),
    ] {
        let output = run_legacy_fixture_with_requests(&[
            LEGACY_PLANTED_INITIALIZE_REQUEST,
            malformed_notification,
        ]);
        assert!(
            !output.status.success(),
            "legacy fixture must reject initialized notification with {case_name}"
        );
        let wire = observed_protocol_wire(&output);
        assert_eq!(
            wire.len(),
            2,
            "rejected initialized notification must follow exactly one initialize"
        );
        assert_exact_legacy_initialize_request(&wire[0]);
        let expected: serde_json::Value = serde_json::from_str(malformed_notification)
            .expect("planted notification must be JSON");
        assert_eq!(
            wire[1], expected,
            "fixture must reject the observed initialized notification with {case_name}"
        );
    }
}

#[test]
fn e2e_legacy_operating_fixture_planted_negatives_reject_noncanonical_envelopes() {
    for (case_name, malformed_operating_request) in [
        (
            "extra envelope field",
            r#"{"jsonrpc":"2.0","method":"tools/list","params":{},"id":2,"extra":true}"#,
        ),
        (
            "non-object params",
            r#"{"jsonrpc":"2.0","method":"tools/list","params":null,"id":2}"#,
        ),
    ] {
        let output = run_legacy_fixture_with_requests(&[
            LEGACY_PLANTED_INITIALIZE_REQUEST,
            LEGACY_PLANTED_INITIALIZED_NOTIFICATION,
            malformed_operating_request,
        ]);
        assert!(
            !output.status.success(),
            "legacy fixture must reject operating request with {case_name}"
        );
        let wire = observed_protocol_wire(&output);
        assert_eq!(
            wire.len(),
            3,
            "rejected operating request must follow initialize and initialized notification"
        );
        assert_exact_legacy_initialize_request(&wire[0]);
        assert_exact_legacy_initialized_notification_request(&wire[1]);
        let expected: serde_json::Value = serde_json::from_str(malformed_operating_request)
            .expect("planted operating request must be JSON");
        assert_eq!(
            wire[2], expected,
            "fixture must reject the observed operating request with {case_name}"
        );
    }
}

#[test]
fn e2e_modern_discovery_fixture_planted_negatives_reject_noncanonical_envelopes() {
    for (case_name, malformed_discovery) in [
        (
            "extra envelope field",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1,"extra":true}"#,
        ),
        (
            "wrong jsonrpc",
            r#"{"jsonrpc":"1.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
        (
            "missing jsonrpc",
            r#"{"method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
        (
            "wrong method",
            r#"{"jsonrpc":"2.0","method":"server/unknown","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
        (
            "wrong id",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":2}"#,
        ),
        (
            "missing id",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}}}"#,
        ),
        (
            "null params",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":null,"id":1}"#,
        ),
        (
            "extra params field",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"},"extra":true},"id":1}"#,
        ),
        (
            "null metadata",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":null},"id":1}"#,
        ),
        (
            "non-object client capabilities",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":null,"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
        (
            "extra client info field",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0","extra":true},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
        (
            "extra metadata field",
            r#"{"jsonrpc":"2.0","method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{},"io.modelcontextprotocol/clientInfo":{"name":"planted-modern-client","version":"1.0.0"},"io.modelcontextprotocol/protocolVersion":"2026-07-28","extra":true}},"id":1}"#,
        ),
    ] {
        let output = run_modern_fixture_with_request(malformed_discovery);
        assert!(
            !output.status.success(),
            "modern fixture must reject discovery with {case_name}"
        );
        let wire = observed_protocol_wire(&output);
        assert_eq!(
            wire.len(),
            1,
            "rejected {case_name} must be the only observed discovery request"
        );
        let expected: serde_json::Value =
            serde_json::from_str(malformed_discovery).expect("planted discovery must be JSON");
        assert_eq!(
            wire[0], expected,
            "fixture must reject the actual discovery request with {case_name}"
        );
    }
}

#[test]
fn e2e_legacy_initialize_fixture_planted_negatives_reject_malformed_shapes() {
    for (case_name, malformed_initialize) in [
        (
            "null protocolVersion",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":null,"capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"}},"id":1}"#,
        ),
        (
            "wrong protocolVersion",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"}},"id":1}"#,
        ),
        (
            "non-object capabilities",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":[],"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"}},"id":1}"#,
        ),
        (
            "null clientInfo name",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":null,"version":"1.0.0"}},"id":1}"#,
        ),
        (
            "numeric clientInfo version",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":1}},"id":1}"#,
        ),
        (
            "extra clientInfo field",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0","extra":true}},"id":1}"#,
        ),
        (
            "extra legacy parameter",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"},"extra":true},"id":1}"#,
        ),
        (
            "final-only metadata",
            r#"{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"planted-legacy-client","version":"1.0.0"},"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28"}},"id":1}"#,
        ),
    ] {
        let output = run_legacy_fixture_with_requests(&[malformed_initialize]);
        assert!(
            !output.status.success(),
            "legacy fixture must reject {case_name}"
        );
        let wire = observed_protocol_wire(&output);
        assert_eq!(
            wire.len(),
            1,
            "rejected {case_name} must be the only observed wire request"
        );
        let expected: serde_json::Value =
            serde_json::from_str(malformed_initialize).expect("planted initialize must be JSON");
        assert_eq!(
            wire[0], expected,
            "fixture must reject the actual malformed {case_name} request"
        );
    }
}

#[test]
fn e2e_cli_inspect_modern_only_planted_negative_never_falls_back_to_legacy() {
    let output = inspect_protocol_fixture("modern-only", "text", LEGACY_FALLBACK_INSPECT_FIXTURE);
    assert!(
        !output.status.success(),
        "ModernOnly must reject a legacy-only peer instead of silently falling back"
    );
    let wire = observed_protocol_wire(&output);
    assert_modern_negotiation_wire(&wire);
    assert_no_legacy_initialize(&wire);
}

#[cfg(feature = "legacy-2024-11-05")]
#[test]
fn e2e_cli_inspect_auto_planted_negative_rejects_unauthorized_discovery_failure() {
    let output = inspect_protocol_fixture(
        "auto",
        "json",
        UNAUTHORIZED_DISCOVERY_REJECTION_INSPECT_FIXTURE,
    );
    assert!(
        !output.status.success(),
        "Auto must not fall back on a discovery error outside the authorized legacy class"
    );
    let wire = observed_protocol_wire(&output);
    assert_modern_negotiation_wire(&wire);
    assert_no_legacy_initialize(&wire);
}

#[cfg(target_os = "linux")]
#[test]
fn reality_check_regression_e2e_test_cleans_descendants_before_success() {
    const FORKING_SERVER_WRAPPER: &str = r#"
trap '' HUP
/bin/sh -c 'trap "" HUP; exec </dev/null >/dev/null 2>/dev/null; while :; do /bin/sleep 3600; done' &
stubborn_pid=$!
printf "FASTMCP_TEST_STUBBORN_PID=%s\n" "$stubborn_pid" >&2
# A non-interactive shell may otherwise connect an asynchronous list's stdin
# to /dev/null. Save the inherited MCP request pipe before starting the
# asynchronous list, then close both extra descriptor copies promptly.
exec 3<&0
/bin/sh -c 'exec 3<&-; trap "" HUP; printf "FASTMCP_TEST_DESCENDANT_PID=%s\n" "$$" >&2; exec /bin/sh "$1"' fastmcp-descendant "$1" <&3 &
exec 3<&-
exit 0
"#;

    let output = run_cli(&[
        "test",
        "--json",
        "--idle-timeout",
        "30",
        "--absolute-timeout",
        "120",
        "/bin/sh",
        "--",
        "-c",
        FORKING_SERVER_WRAPPER,
        "fastmcp-forking-server",
        STATIC_MCP_SERVER_FIXTURE,
    ]);
    assert!(
        output.status.success(),
        "the descendant-served protocol probe should succeed: status={}; stdout={}; stderr={}",
        output.status,
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );

    let report: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("parse descendant cleanup report");
    assert_eq!(report["success"], true);
    let cleanup = report["tests"]
        .as_array()
        .and_then(|tests| tests.iter().find(|test| test["name"] == "cleanup"))
        .expect("successful report must include explicit cleanup verification");
    assert_eq!(cleanup["success"], true);
    assert_eq!(cleanup["details"], "owned subprocess group stopped");

    let stderr = std::str::from_utf8(&output.stderr).expect("peer marker must be UTF-8");
    for (marker, description) in [
        ("FASTMCP_TEST_DESCENDANT_PID=", "protocol descendant"),
        ("FASTMCP_TEST_STUBBORN_PID=", "stubborn descendant"),
    ] {
        let descendant_pid = stderr
            .lines()
            .find_map(|line| line.strip_prefix(marker))
            .and_then(|value| value.parse::<u32>().ok())
            .unwrap_or_else(|| panic!("forked {description} must report its PID"));
        let deadline = Instant::now() + PROCESS_CLEANUP_DEADLINE;
        loop {
            match std::fs::read_to_string(format!("/proc/{descendant_pid}/stat")) {
                Ok(stat) => {
                    let (state, _) = linux_process_state_and_group(&stat)
                        .expect("descendant process state must be parseable");
                    if matches!(state, 'Z' | 'X' | 'x') {
                        break;
                    }
                }
                Err(error) if proc_process_disappeared(&error) => break,
                Err(error) => panic!("failed to inspect descendant process state: {error}"),
            }
            assert!(
                Instant::now() < deadline,
                "fastmcp test reported success while {description} {descendant_pid} remained live"
            );
            std::thread::sleep(PROCESS_POLL_INTERVAL);
        }
    }
}

#[cfg(target_os = "linux")]
#[test]
fn reality_check_regression_e2e_test_owner_death_stops_anchored_group() {
    const DELAYED_SERVER: &str = r#"
printf "FASTMCP_TEST_OWNER_DEATH_PID=%s\n" "$$" >&2
/bin/sleep 30
exec /bin/sh "$1"
"#;

    let mut command = Command::new(fastmcp_bin());
    command
        .args([
            "test",
            "--json",
            "--idle-timeout",
            "30",
            "--absolute-timeout",
            "120",
            "/bin/sh",
            "--",
            "-c",
            DELAYED_SERVER,
            "fastmcp-owner-death-server",
            STATIC_MCP_SERVER_FIXTURE,
        ])
        .env("FASTMCP_CHECK_FOR_UPDATES", "0")
        .stdout(Stdio::null())
        .stderr(Stdio::piped());
    let mut process = ProcessGroupGuard::spawn(&mut command);
    let stderr = process
        .child_mut()
        .stderr
        .take()
        .expect("owner-death stderr");
    let (marker_sender, marker_receiver) = mpsc::channel();
    let reader = std::thread::spawn(move || {
        let mut line = String::new();
        let result = BufReader::new(stderr).read_line(&mut line);
        // Sending the completed payload is this worker's final action.
        let _ = marker_sender.send((result, line));
    });
    let (read_result, marker) = marker_receiver
        .recv_timeout(Duration::from_secs(5))
        .unwrap_or_else(|error| panic!("owner-death fixture did not report its PID: {error}"));
    reader
        .join()
        .expect("stderr marker reader must join after its final acknowledgement");
    read_result.expect("read owner-death PID marker");
    let target_pid = marker
        .trim()
        .strip_prefix("FASTMCP_TEST_OWNER_DEATH_PID=")
        .and_then(|value| value.parse::<u32>().ok())
        .expect("parse owner-death PID marker");

    process
        .child_mut()
        .kill()
        .expect("kill only the fastmcp CLI owner");
    let owner_status = process
        .wait_until(Duration::from_secs(5))
        .unwrap_or_else(|error| panic!("reap fastmcp CLI owner: {error:?}"));
    assert!(!owner_status.success(), "killed CLI owner cannot succeed");
    let deadline = Instant::now() + PROCESS_CLEANUP_DEADLINE;
    loop {
        match std::fs::read_to_string(format!("/proc/{target_pid}/stat")) {
            Ok(stat) => {
                let (state, _) = linux_process_state_and_group(&stat)
                    .expect("owner-death process state must be parseable");
                if matches!(state, 'Z' | 'X' | 'x') {
                    break;
                }
            }
            Err(error) if proc_process_disappeared(&error) => break,
            Err(error) => panic!("failed to inspect owner-death target: {error}"),
        }
        assert!(
            Instant::now() < deadline,
            "anchor did not stop target {target_pid} after CLI owner death"
        );
        std::thread::sleep(PROCESS_POLL_INTERVAL);
    }
}

#[cfg(feature = "e2e-fixture")]
#[test]
fn e2e_test_json_report_against_compiled_fastmcp_server() {
    let server = env!("CARGO_BIN_EXE_fastmcp_cli_e2e_server");

    let output = run_cli(&[
        "test",
        "--json",
        "--idle-timeout",
        "30",
        "--absolute-timeout",
        "120",
        server,
    ]);
    assert!(output.status.success());

    let report: serde_json::Value =
        serde_json::from_str(&stdout_str(&output)).expect("parse framework test report");
    assert_eq!(report["success"], true);
    let tests = report["tests"]
        .as_array()
        .expect("framework report test array");
    for (name, expected_details) in [
        ("initialize", "protocol 2024-11-05"),
        ("ping", "server responded"),
        ("list_tools", "1 tools"),
        ("list_resources", "1 resources"),
        ("list_prompts", "1 prompts"),
    ] {
        let result = tests
            .iter()
            .find(|test| test["name"] == name)
            .unwrap_or_else(|| panic!("framework report must include {name}"));
        assert_eq!(result["success"], true, "{name} must succeed");
        assert_ne!(
            result["skipped"].as_bool(),
            Some(true),
            "{name} must execute"
        );
        assert_eq!(result["details"], expected_details);
    }
}

#[cfg(feature = "e2e-fixture")]
#[test]
fn reality_check_regression_compiled_stdio_server_exits_when_worker_output_fails() {
    let server = env!("CARGO_BIN_EXE_fastmcp_cli_e2e_server");
    let mut command = Command::new(server);
    command
        .env("FASTMCP_NO_BANNER", "1")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null());
    let mut guard = ProcessGroupGuard::spawn(&mut command);
    let mut stdin = guard
        .child_mut()
        .stdin
        .take()
        .expect("compiled fixture stdin");
    let stdout = guard
        .child_mut()
        .stdout
        .take()
        .expect("compiled fixture stdout");
    drop(stdout);

    stdin
        .write_all(b"{\"jsonrpc\":\"2.0\",\"method\":\"ping\",\"id\":1}\n")
        .expect("request reaches compiled fixture");
    stdin.flush().expect("request flushes");
    let started = Instant::now();
    let status = guard
        .wait_until(Duration::from_secs(10))
        .unwrap_or_else(|timeout| panic!("compiled fixture did not stop: {timeout:?}"));

    assert!(!status.success());
    assert!(started.elapsed() < Duration::from_secs(10));
    drop(stdin);
}

#[cfg(feature = "e2e-fixture")]
#[test]
fn reality_check_regression_compiled_stdio_server_bounds_unread_output_pipe() {
    let server = env!("CARGO_BIN_EXE_fastmcp_cli_e2e_server");
    let mut command = Command::new(server);
    command
        .env("FASTMCP_NO_BANNER", "1")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null());
    let mut guard = ProcessGroupGuard::spawn(&mut command);
    let mut stdin = guard
        .child_mut()
        .stdin
        .take()
        .expect("compiled fixture stdin");
    let _unread_stdout = guard
        .child_mut()
        .stdout
        .take()
        .expect("compiled fixture stdout");

    let initialize = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": {
            "protocolVersion": "2024-11-05",
            "capabilities": {},
            "clientInfo": {"name": "pipe-saturation-test", "version": "1.0.0"}
        }
    });
    let call = serde_json::json!({
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {
            "name": "echo",
            "arguments": {"message": "x".repeat(1024 * 1024)},
            "_meta": {"progressToken": "saturation-progress"}
        }
    });
    for request in [initialize, call] {
        serde_json::to_writer(&mut stdin, &request).expect("encode saturation request");
        stdin.write_all(b"\n").expect("frame saturation request");
    }
    stdin.flush().expect("saturation requests flush");

    let started = Instant::now();
    let status = guard
        .wait_until(Duration::from_secs(10))
        .unwrap_or_else(|timeout| panic!("compiled fixture did not bound stdout: {timeout:?}"));

    assert!(!status.success());
    assert!(started.elapsed() < Duration::from_secs(10));
    drop(stdin);
}

#[cfg(unix)]
#[test]
fn e2e_static_protocol_fixture_ignores_nested_notification_ids() {
    let mut command = Command::new("/bin/sh");
    command.args([
        "-c",
        r#"printf '%s\n' '{"jsonrpc":"2.0","method":"notifications/progress","params":{"id":99}}' '{"jsonrpc":"2.0","method":"ping","id":7}' | /bin/sh "$1""#,
        "fixture-notification-probe",
        STATIC_MCP_SERVER_FIXTURE,
    ]);
    let output = run_with_deadline(command, Duration::from_secs(10))
        .expect("static protocol fixture must exit after its input closes");
    assert!(output.status.success());
    assert!(output.stderr.is_empty());

    let responses = std::str::from_utf8(&output.stdout)
        .expect("fixture output must be UTF-8")
        .lines()
        .collect::<Vec<_>>();
    assert_eq!(
        responses.len(),
        1,
        "notification params.id must not produce a response"
    );
    let response: serde_json::Value =
        serde_json::from_str(responses[0]).expect("fixture response must be valid JSON");
    assert_eq!(response["id"], 7);
    assert_eq!(response["result"], serde_json::json!({}));
}

#[cfg(unix)]
#[test]
fn e2e_test_absolute_timeout_bounds_silent_initialization() {
    assert_initialization_absolute_timeout("exec sleep 5", "silent peer");
}

#[test]
fn e2e_test_json_report_is_emitted_when_connect_fails() {
    let missing_server = format!(
        "/definitely-missing-fastmcp-e2e-server-{}",
        std::process::id()
    );
    let mut cmd = Command::new(fastmcp_bin());
    cmd.args(["test", "--json", &missing_server])
        .env("FASTMCP_CHECK_FOR_UPDATES", "0");

    let output = run_with_deadline(cmd, Duration::from_secs(10))
        .expect("missing executable failure must remain promptly bounded");

    assert!(!output.status.success());
    let report: serde_json::Value = serde_json::from_slice(&output.stdout)
        .expect("JSON mode must report connection failure on stdout");
    assert_eq!(report["server"].as_str(), Some(missing_server.as_str()));
    assert_eq!(report["success"], false);
    let tests = report["tests"]
        .as_array()
        .expect("connection failure report must contain tests");
    assert_eq!(tests.len(), 1);
    assert_eq!(tests[0]["name"], "initialize");
    assert_eq!(tests[0]["success"], false);
    assert!(
        tests[0]["error"]
            .as_str()
            .is_some_and(|error| !error.is_empty())
    );
    assert!(tests[0].get("timeout_source").is_none());
}

#[cfg(unix)]
#[test]
fn e2e_test_absolute_timeout_bounds_partial_initialization_frame() {
    assert_initialization_absolute_timeout(
        r#"printf '%s' '{"jsonrpc":"2.0"'; exec sleep 5"#,
        "partial-frame peer",
    );
}

#[cfg(unix)]
fn assert_initialization_absolute_timeout(server_script: &str, scenario: &str) {
    let mut cmd = Command::new(fastmcp_bin());
    cmd.args([
        "test",
        "--json",
        "--idle-timeout",
        "2",
        "--absolute-timeout",
        "1",
        "sh",
        "--",
        "-c",
        server_script,
    ])
    .env("FASTMCP_CHECK_FOR_UPDATES", "0");

    let started = Instant::now();
    let output = run_with_deadline(cmd, Duration::from_secs(10)).unwrap_or_else(|expired| {
        panic!(
            "the harness deadline expired for {scenario} instead of the product timeout; cleanup error: {:?}; captured stdout={} bytes, stderr={} bytes (content redacted)",
            expired.cleanup_error,
            expired.stdout.len(),
            expired.stderr.len()
        )
    });

    assert!(
        !output.status.success(),
        "{scenario} unexpectedly succeeded"
    );
    assert!(
        started.elapsed() < Duration::from_secs(4),
        "{scenario} was not bounded by the configured receive deadline"
    );
    let stderr = std::str::from_utf8(&output.stderr)
        .expect("timeout diagnostics must be strict UTF-8 terminal text");
    assert!(
        stderr.contains("Request timed out at the absolute deadline"),
        "{scenario} did not select the intentionally earlier absolute deadline: {stderr}"
    );
    let report: serde_json::Value = serde_json::from_slice(&output.stdout)
        .expect("JSON mode must report initialization timeout on stdout");
    assert_eq!(report["success"], false);
    let tests = report["tests"]
        .as_array()
        .expect("initialization failure report must contain tests");
    assert_eq!(tests.len(), 1);
    assert_eq!(tests[0]["name"], "initialize");
    assert_eq!(tests[0]["success"], false);
    assert_eq!(tests[0]["timeout_source"], "absolute");
    assert!(
        tests[0]["error"]
            .as_str()
            .is_some_and(|error| error.contains("Request timed out at the absolute deadline"))
    );
}

#[cfg(unix)]
#[test]
fn e2e_test_idle_timeout_reports_late_ping_response() {
    // Complete modern discovery, then delay the ping response beyond the product
    // deadline to cover the post-initialization request path separately from
    // the silent and partial-frame initialization cases above.
    const LATE_PING_SERVER: &str = r#"
IFS= read -r discovery || exit 20
printf '%s\n' '{"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","supportedVersions":["2026-07-28"],"capabilities":{},"_meta":{"io.modelcontextprotocol/serverInfo":{"name":"late-ping-fixture","version":"1.0.0"}},"ttlMs":0,"cacheScope":"private"}}'
IFS= read -r ping || exit 21
sleep 2
printf '%s\n' '{"jsonrpc":"2.0","id":2,"result":{}}'
"#;

    let mut cmd = Command::new(fastmcp_bin());
    cmd.args([
        "test",
        "--json",
        "--idle-timeout",
        "1",
        "--absolute-timeout",
        "3",
        "sh",
        "--",
        "-c",
        LATE_PING_SERVER,
    ])
    .env("FASTMCP_CHECK_FOR_UPDATES", "0");

    let output = run_with_deadline(cmd, Duration::from_secs(10)).unwrap_or_else(|expired| {
        panic!(
            "the harness deadline expired instead of the product timeout; cleanup error: {:?}; captured stdout={} bytes, stderr={} bytes (content redacted)",
            expired.cleanup_error,
            expired.stdout.len(),
            expired.stderr.len()
        )
    });

    assert!(!output.status.success());
    let report: serde_json::Value =
        serde_json::from_slice(&output.stdout).expect("timeout run should emit a JSON report");
    assert_eq!(
        report.get("success").and_then(serde_json::Value::as_bool),
        Some(false)
    );
    let ping = report
        .get("tests")
        .and_then(serde_json::Value::as_array)
        .and_then(|tests| {
            tests
                .iter()
                .find(|test| test.get("name").and_then(serde_json::Value::as_str) == Some("ping"))
        })
        .expect("timeout report should contain the ping result");
    assert_eq!(
        ping.get("success").and_then(serde_json::Value::as_bool),
        Some(false)
    );
    assert!(
        ping.get("error")
            .and_then(serde_json::Value::as_str)
            .is_some_and(|error| error.contains("Request timed out at the idle deadline")),
        "ping result should select the intentionally earlier idle deadline: {ping}"
    );
    assert_eq!(
        ping.get("timeout_source")
            .and_then(serde_json::Value::as_str),
        Some("idle")
    );
    let stderr =
        std::str::from_utf8(&output.stderr).expect("fastmcp test stderr must be valid UTF-8");
    assert!(
        stderr.contains("Some tests failed"),
        "top-level failure diagnostic missing: {stderr}"
    );
}