fastmcp-protocol 0.7.0

MCP protocol types and JSON-RPC implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
//! Frozen, runtime-neutral extension negotiation and request admission.
//!
//! This module owns no handler, transport, authorization, or client/server
//! runtime state. It does bind a developer's explicit local opt-in and both
//! peers' current settings to bounded, modern-only request admission.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use fastmcp_core::sha256_bounded;
use serde::Deserialize;
use serde_json::{Map, Value};

use crate::methods::{final_2026_07_28_method, legacy_2024_11_05_method};
use crate::protocol_policy::ProtocolEra;

/// Maximum descriptors retained by one registry.
pub const MAX_EXTENSION_DESCRIPTORS: usize = 128;
/// Maximum bytes in an extension identifier.
pub const MAX_EXTENSION_ID_BYTES: usize = 512;
/// Maximum bytes in one canonical descriptor-registry digest subject.
pub const MAX_EXTENSION_REGISTRY_CANONICAL_BYTES: usize = 256 * 1024;
/// Maximum extension settings entries preserved at the generic boundary.
pub const MAX_EXTENSION_SETTINGS_ENTRIES: usize = 128;
/// Maximum UTF-8 bytes in one extension settings key.
pub const MAX_EXTENSION_SETTINGS_KEY_BYTES: usize = 512;
/// Maximum canonical JSON bytes in one extension settings value.
pub const MAX_EXTENSION_SETTINGS_VALUE_BYTES: usize = 16 * 1024;
/// Maximum nesting depth admitted in a generic extension settings value.
pub const MAX_EXTENSION_SETTINGS_NESTING: usize = 32;
/// Maximum UTF-8 bytes in an extension-owned method or notification name.
pub const MAX_EXTENSION_MEMBER_NAME_BYTES: usize = 512;
/// Maximum extension-owned routing headers registered by one descriptor.
pub const MAX_EXTENSION_ROUTING_HEADERS: usize = 32;
/// Maximum UTF-8 bytes in an extension-owned routing header name.
pub const MAX_EXTENSION_ROUTING_HEADER_BYTES: usize = 256;
/// Maximum notification method names owned by one stdio correlation descriptor.
pub const MAX_STDIO_CORRELATION_METHODS: usize = 32;

/// Official Tasks extension identifier.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_EXTENSION_ID: &str = "io.modelcontextprotocol/tasks";
/// Official Tasks empty-settings schema identity for both peers.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID: &str = "tasks-2026-07-28-empty-object-v1";
/// Official Tasks empty-settings codec identity for both peers.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_EMPTY_SETTINGS_CODEC_ID: &str = "tasks-2026-07-28-empty-object-v1";
/// Official Tasks client-to-server request methods.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_METHODS: [&str; 3] = ["tasks/get", "tasks/update", "tasks/cancel"];
/// Official Tasks server-to-client notification method.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_NOTIFICATION: &str = "notifications/tasks";
/// Official Tasks `tools/call` result discriminator.
#[cfg(feature = "tasks")]
pub const OFFICIAL_TASKS_RESULT_DISCRIMINATOR: &str = "task";

/// Official MCP Apps extension identifier.
pub const OFFICIAL_MCP_APPS_EXTENSION_ID: &str = "io.modelcontextprotocol/ui";
/// Pinned MCP Apps Host/View protocol version represented by this vocabulary.
///
/// This version belongs to the Apps postMessage protocol. It does not alter
/// the MCP client/server protocol era or add an extension-owned RPC method.
pub const MCP_APPS_PROTOCOL_VERSION: &str = "2026-01-26";
/// MCP Apps HTML resource MIME type required for activation.
pub const MCP_APPS_HTML_MIME_TYPE: &str = "text/html;profile=mcp-app";
/// Stable MCP Apps client settings schema identity.
pub const MCP_APPS_CLIENT_SETTINGS_SCHEMA_ID: &str = "apps-2026-01-26-client-mime-types-v1";
/// Stable MCP Apps server empty-marker schema identity.
pub const MCP_APPS_SERVER_SETTINGS_SCHEMA_ID: &str =
    "fastmcp-2026-07-28-apps-empty-server-marker-v1";
/// Stable MCP Apps bilateral compatibility resolver identity.
pub const MCP_APPS_NEGOTIATION_RESOLVER_ID: &str = "fastmcp-apps-bilateral-resolver-v1";
/// Stable resolver-version component of the frozen MCP Apps descriptor.
pub const MCP_APPS_NEGOTIATION_RESOLVER_VERSION: u32 = 1;
/// Stable MCP Apps activation predicate identity.
pub const MCP_APPS_ACTIVATION_PREDICATE_ID: &str = "fastmcp-2026-07-28-apps-bilateral-mime-v1";
/// Maximum MIME types retained in one MCP Apps client advertisement.
pub const MAX_MCP_APPS_MIME_TYPES: usize = 128;
/// Maximum UTF-8 bytes in one MCP Apps MIME type.
pub const MAX_MCP_APPS_MIME_TYPE_BYTES: usize = 512;

/// MCP Apps View-to-Host request method names.
pub const MCP_APPS_OPEN_LINK_METHOD: &str = "ui/open-link";
/// MCP Apps View-to-Host request method name.
pub const MCP_APPS_DOWNLOAD_FILE_METHOD: &str = "ui/download-file";
/// MCP Apps View-to-Host request method name.
pub const MCP_APPS_MESSAGE_METHOD: &str = "ui/message";
/// MCP Apps View-to-Host request method name.
pub const MCP_APPS_UPDATE_MODEL_CONTEXT_METHOD: &str = "ui/update-model-context";
/// MCP Apps Host-to-View request method name.
pub const MCP_APPS_RESOURCE_TEARDOWN_METHOD: &str = "ui/resource-teardown";
/// MCP Apps View-to-Host request method name.
pub const MCP_APPS_INITIALIZE_METHOD: &str = "ui/initialize";
/// MCP Apps View-to-Host request method name.
pub const MCP_APPS_REQUEST_DISPLAY_MODE_METHOD: &str = "ui/request-display-mode";

/// MCP Apps notification method names.
pub const MCP_APPS_SANDBOX_PROXY_READY_NOTIFICATION: &str = "ui/notifications/sandbox-proxy-ready";
/// MCP Apps notification method name.
pub const MCP_APPS_SANDBOX_RESOURCE_READY_NOTIFICATION: &str =
    "ui/notifications/sandbox-resource-ready";
/// MCP Apps notification method name.
pub const MCP_APPS_SIZE_CHANGED_NOTIFICATION: &str = "ui/notifications/size-changed";
/// MCP Apps notification method name.
pub const MCP_APPS_TOOL_INPUT_NOTIFICATION: &str = "ui/notifications/tool-input";
/// MCP Apps notification method name.
pub const MCP_APPS_TOOL_INPUT_PARTIAL_NOTIFICATION: &str = "ui/notifications/tool-input-partial";
/// MCP Apps notification method name.
pub const MCP_APPS_TOOL_RESULT_NOTIFICATION: &str = "ui/notifications/tool-result";
/// MCP Apps notification method name.
pub const MCP_APPS_TOOL_CANCELLED_NOTIFICATION: &str = "ui/notifications/tool-cancelled";
/// MCP Apps notification method name.
pub const MCP_APPS_HOST_CONTEXT_CHANGED_NOTIFICATION: &str =
    "ui/notifications/host-context-changed";
/// MCP Apps notification method name.
pub const MCP_APPS_REQUEST_TEARDOWN_NOTIFICATION: &str = "ui/notifications/request-teardown";
/// MCP Apps notification method name.
pub const MCP_APPS_INITIALIZED_NOTIFICATION: &str = "ui/notifications/initialized";

/// A validated extension identifier, preserving its exact wire spelling.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ExtensionId(String);

impl ExtensionId {
    /// Validates the final metadata-key prefix/name grammar.
    pub fn parse(value: impl Into<String>) -> Result<Self, ExtensionRegistryError> {
        let value = value.into();
        if value.is_empty() || value.len() > MAX_EXTENSION_ID_BYTES {
            return Err(ExtensionRegistryError::InvalidIdentifier(value));
        }
        let Some((prefix, name)) = value.split_once('/') else {
            return Err(ExtensionRegistryError::InvalidIdentifier(value));
        };
        if value.matches('/').count() != 1 || !valid_prefix(prefix) || !valid_name(name) {
            return Err(ExtensionRegistryError::InvalidIdentifier(value));
        }
        // The official second DNS labels are reserved: `com.mcp.*` or
        // `org.modelcontextprotocol.*` would masquerade as official
        // namespaces. Deeper labels (`com.example.mcp`) stay available, and
        // the exact official prefix keeps resolving.
        if let Some(second_label) = prefix.split('.').nth(1) {
            let reserved = second_label.eq_ignore_ascii_case("mcp")
                || second_label.eq_ignore_ascii_case("modelcontextprotocol");
            if reserved && prefix != "io.modelcontextprotocol" {
                return Err(ExtensionRegistryError::ReservedNamespace(value));
            }
        }
        Ok(Self(value))
    }

    /// Returns the byte-preserved identifier.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ExtensionId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

fn valid_prefix(prefix: &str) -> bool {
    prefix.split('.').all(|label| {
        !label.is_empty()
            && label.as_bytes()[0].is_ascii_alphabetic()
            && label
                .as_bytes()
                .last()
                .is_some_and(|byte| byte.is_ascii_alphanumeric())
            && label
                .bytes()
                .all(|byte| byte.is_ascii_alphabetic() || byte.is_ascii_digit() || byte == b'-')
    })
}

fn valid_name(name: &str) -> bool {
    name.is_empty()
        || (name.as_bytes()[0].is_ascii_alphanumeric()
            && name
                .as_bytes()
                .last()
                .is_some_and(|byte| byte.is_ascii_alphanumeric())
            && name
                .bytes()
                .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-')))
}

/// One extension's generic JSON-object settings.
#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionSettings(Map<String, Value>);

impl ExtensionSettings {
    /// Admits exactly a JSON object, including an empty object.
    pub fn new(value: Value) -> Result<Self, ExtensionRegistryError> {
        let Value::Object(map) = value else {
            return Err(ExtensionRegistryError::SettingsNotObject);
        };
        validate_settings_map(&map)?;
        Ok(Self(map))
    }

    /// Returns the preserved generic object.
    #[must_use]
    pub const fn as_object(&self) -> &Map<String, Value> {
        &self.0
    }

    /// Returns the generic object as a JSON value without decoding it.
    #[must_use]
    pub fn into_value(self) -> Value {
        Value::Object(self.0)
    }

    /// Decodes this descriptor-scoped object through a caller-selected typed codec.
    pub fn decode<T>(&self) -> Result<T, ExtensionRegistryError>
    where
        T: serde::de::DeserializeOwned,
    {
        serde_json::from_value(Value::Object(self.0.clone()))
            .map_err(|_| ExtensionRegistryError::SettingsCodecRejected)
    }
}

/// Returns the sole settings object admitted by the official Tasks extension.
#[must_use]
#[cfg(feature = "tasks")]
pub fn official_tasks_empty_settings() -> ExtensionSettings {
    ExtensionSettings(Map::new())
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct McpAppsClientSettingsWire {
    mime_types: Vec<String>,
}

/// Strict, ordered MCP Apps client capability settings.
///
/// The wire object is deliberately closed and requires `mimeTypes`. Its array
/// preserves peer order and schema-valid duplicates exactly; support is granted
/// solely by the presence of the exact HTML profile MIME type.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct McpAppsClientSettings {
    mime_types: Vec<String>,
}

impl McpAppsClientSettings {
    /// Validates ordered client-advertised MIME types without normalizing them.
    pub fn new(mime_types: Vec<String>) -> Result<Self, ExtensionRegistryError> {
        if mime_types.len() > MAX_MCP_APPS_MIME_TYPES
            || mime_types
                .iter()
                .any(|mime_type| mime_type.len() > MAX_MCP_APPS_MIME_TYPE_BYTES)
        {
            return Err(ExtensionRegistryError::SettingsTooLarge);
        }

        // The typed limit is not permitted to bypass the generic discovery
        // bound. `to_extension_settings` relies on this validation when it
        // constructs the private generic settings value directly.
        validate_settings_map(&mcp_apps_client_settings_map(&mime_types))?;
        Ok(Self { mime_types })
    }

    /// Decodes the closed MCP Apps client settings object.
    pub fn from_extension_settings(
        settings: &ExtensionSettings,
    ) -> Result<Self, ExtensionRegistryError> {
        let wire = serde_json::from_value::<McpAppsClientSettingsWire>(Value::Object(
            settings.as_object().clone(),
        ))
        .map_err(|_| ExtensionRegistryError::SettingsCodecRejected)?;
        Self::new(wire.mime_types)
    }

    /// Returns advertised MIME types in their exact peer-supplied order.
    #[must_use]
    pub fn mime_types(&self) -> &[String] {
        &self.mime_types
    }

    /// Returns whether this host advertises the required MCP Apps HTML profile.
    #[must_use]
    pub fn supports_mcp_apps_html(&self) -> bool {
        self.mime_types
            .iter()
            .any(|mime_type| mime_type == MCP_APPS_HTML_MIME_TYPE)
    }

    /// Re-encodes the validated settings without changing order or duplicates.
    pub fn to_extension_settings(&self) -> ExtensionSettings {
        ExtensionSettings(mcp_apps_client_settings_map(&self.mime_types))
    }
}

fn mcp_apps_client_settings_map(mime_types: &[String]) -> Map<String, Value> {
    let mut map = Map::new();
    map.insert(
        "mimeTypes".to_owned(),
        Value::Array(mime_types.iter().cloned().map(Value::String).collect()),
    );
    map
}

/// Returns the exact empty MCP Apps server settings marker.
#[must_use]
pub fn official_mcp_apps_empty_server_settings() -> ExtensionSettings {
    ExtensionSettings(Map::new())
}

/// Validates the sole server settings marker admitted by official MCP Apps.
///
/// The official Apps descriptor is a server capability marker, not a
/// server-configurable settings object. Reject a non-empty marker while the
/// server is being configured so it cannot be advertised and fail later
/// during per-request negotiation.
pub fn validate_official_mcp_apps_server_settings(
    settings: &ExtensionSettings,
) -> Result<(), ExtensionRegistryError> {
    if settings.as_object().is_empty() {
        Ok(())
    } else {
        Err(ExtensionRegistryError::OfficialMcpAppsServerSettingsNotEmpty)
    }
}

/// Returns the validated identifier for the official MCP Apps extension.
#[must_use]
pub fn official_mcp_apps_extension_id() -> ExtensionId {
    ExtensionId::parse(OFFICIAL_MCP_APPS_EXTENSION_ID)
        .expect("the fixed official MCP Apps identifier satisfies the extension grammar")
}

/// Returns the descriptor for MCP Apps capability negotiation.
///
/// Apps bridge methods belong to the host/View postMessage channel, not the
/// client/server extension-dispatch surface. This descriptor therefore owns
/// only the bilateral capability settings contract.
#[must_use]
pub fn official_mcp_apps_descriptor() -> ExtensionDescriptor {
    ExtensionDescriptor {
        id: official_mcp_apps_extension_id(),
        client_settings: ExtensionSettingsSchema {
            schema_id: MCP_APPS_CLIENT_SETTINGS_SCHEMA_ID.to_owned(),
            codec_id: MCP_APPS_CLIENT_SETTINGS_SCHEMA_ID.to_owned(),
        },
        server_settings: ExtensionSettingsSchema {
            schema_id: MCP_APPS_SERVER_SETTINGS_SCHEMA_ID.to_owned(),
            codec_id: MCP_APPS_SERVER_SETTINGS_SCHEMA_ID.to_owned(),
        },
        resolver: ExtensionNegotiationResolver {
            id: MCP_APPS_NEGOTIATION_RESOLVER_ID.to_owned(),
            version: MCP_APPS_NEGOTIATION_RESOLVER_VERSION,
            fallback: ExtensionFallbackPolicy::InactiveOnEitherPeer,
        },
        method: None,
        notification: None,
        result_discriminator: None,
        routing_headers: Vec::new(),
        stdio_correlation: None,
    }
}

/// Validates the complete, method-free MCP Apps capability descriptor.
///
/// The official Apps extension advertises bilateral MIME support only. View
/// lifecycle and result messages use a separate Host/View channel, so an Apps
/// descriptor must not acquire a JSON-RPC method, notification, result
/// discriminator, routing header, or stdio-correlation owner by mutation.
pub fn validate_official_mcp_apps_descriptor(
    descriptor: &ExtensionDescriptor,
) -> Result<(), ExtensionRegistryError> {
    if descriptor == &official_mcp_apps_descriptor() {
        Ok(())
    } else {
        Err(ExtensionRegistryError::OfficialMcpAppsDescriptorMismatch)
    }
}

/// Registers the MCP Apps capability descriptor.
///
/// Registration never activates Apps. The local gates, exact empty server
/// marker, and a current client MIME advertisement must all be present before
/// [`resolve_official_mcp_apps_settings`] can activate it.
pub fn register_official_mcp_apps_extension(
    registry: &mut ExtensionDescriptorRegistry,
) -> Result<ExtensionId, ExtensionRegistryError> {
    let id = official_mcp_apps_extension_id();
    let descriptor = official_mcp_apps_descriptor();
    validate_official_mcp_apps_descriptor(&descriptor)?;
    registry.register(descriptor)?;
    Ok(id)
}

/// Typed MCP Apps compatibility resolver that delegates every non-Apps
/// descriptor to its supplied fallback.
///
/// One frozen registry may contain Apps alongside Tasks or private extensions.
/// This wrapper consumes only the official Apps descriptor, so callers can
/// compose it with their existing resolver rather than replacing it.
#[derive(Clone, Debug)]
pub struct McpAppsNegotiationResolver<R = RejectingExtensionNegotiationResolver> {
    fallback: R,
}

/// Fallback used when no optional extension resolver is selected.
///
/// This keeps MCP Apps usable without compiling the Tasks extension while
/// refusing any non-Apps descriptor rather than implicitly enabling a wire
/// capability that the crate feature excluded.
#[derive(Clone, Copy, Debug, Default)]
pub struct RejectingExtensionNegotiationResolver;

impl<R> McpAppsNegotiationResolver<R> {
    /// Wraps an existing resolver with the official MCP Apps settings rules.
    #[must_use]
    pub const fn with_fallback(fallback: R) -> Self {
        Self { fallback }
    }
}

/// Resolves the official Tasks descriptor when Apps and Tasks share a registry.
#[derive(Clone, Copy, Debug, Default)]
#[cfg(feature = "tasks")]
pub struct OfficialTasksNegotiationResolver;

/// Typed Tasks compatibility resolver that delegates every non-Tasks
/// descriptor to its supplied fallback.
///
/// This adapter preserves an inactive disposition selected by the fallback,
/// which lets Tasks compose with bilateral capabilities such as MCP Apps.
#[derive(Clone, Debug)]
#[cfg(feature = "tasks")]
pub struct TasksNegotiationResolver<R> {
    fallback: R,
}

#[cfg(feature = "tasks")]
impl<R> TasksNegotiationResolver<R> {
    /// Wraps an existing resolver with the official Tasks settings rules.
    #[must_use]
    pub const fn with_fallback(fallback: R) -> Self {
        Self { fallback }
    }
}

/// Returns the standalone typed resolver used by the official MCP Apps descriptor.
///
/// Use [`McpAppsNegotiationResolver::with_fallback`] when the registry also
/// contains private descriptors. When the `tasks` feature is selected, the
/// default helper also resolves the official Tasks descriptor.
#[must_use]
#[cfg(feature = "tasks")]
pub const fn official_mcp_apps_negotiation_resolver()
-> McpAppsNegotiationResolver<OfficialTasksNegotiationResolver> {
    McpAppsNegotiationResolver::with_fallback(OfficialTasksNegotiationResolver)
}

/// Returns the standalone typed resolver used by the official MCP Apps descriptor.
#[must_use]
#[cfg(not(feature = "tasks"))]
pub const fn official_mcp_apps_negotiation_resolver() -> McpAppsNegotiationResolver {
    McpAppsNegotiationResolver::with_fallback(RejectingExtensionNegotiationResolver)
}

/// Resolves a bilateral MCP Apps capability without comparing asymmetric peer objects.
///
/// On success, the effective settings retain the client's exact validated
/// `mimeTypes` array. Missing peer advertisements take the descriptor's
/// ordinary inactive fallback; malformed present settings reject negotiation.
pub fn resolve_official_mcp_apps_settings(
    descriptor: &ExtensionDescriptor,
    client: &ExtensionSettings,
    server: &ExtensionSettings,
) -> Result<ExtensionSettingsResolution, ExtensionNegotiationError> {
    if validate_official_mcp_apps_descriptor(descriptor).is_err() {
        return Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
            descriptor.id.to_string(),
        ));
    }
    validate_official_mcp_apps_server_settings(server).map_err(|_| {
        ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string())
    })?;
    let client = McpAppsClientSettings::from_extension_settings(client).map_err(|_| {
        ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string())
    })?;
    if !client.supports_mcp_apps_html() {
        return Ok(ExtensionSettingsResolution::Inactive);
    }
    Ok(ExtensionSettingsResolution::Active(
        client.to_extension_settings(),
    ))
}

#[cfg(feature = "tasks")]
fn resolve_official_tasks_settings(
    descriptor: &ExtensionDescriptor,
    client: &ExtensionSettings,
    server: &ExtensionSettings,
) -> Result<ExtensionSettings, ExtensionNegotiationError> {
    if descriptor.id.as_str() != OFFICIAL_TASKS_EXTENSION_ID
        || descriptor.client_settings.schema_id != OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID
        || descriptor.client_settings.codec_id != OFFICIAL_TASKS_EMPTY_SETTINGS_CODEC_ID
        || descriptor.server_settings.schema_id != OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID
        || descriptor.server_settings.codec_id != OFFICIAL_TASKS_EMPTY_SETTINGS_CODEC_ID
        || descriptor.resolver.id != OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID
        || descriptor.resolver.version != 1
        || descriptor.resolver.fallback != ExtensionFallbackPolicy::RejectOneSided
    {
        return Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
            descriptor.id.to_string(),
        ));
    }
    enforce_official_tasks_empty_settings(&descriptor.id, client)?;
    enforce_official_tasks_empty_settings(&descriptor.id, server)?;
    Ok(official_tasks_empty_settings())
}

fn validate_settings_map(map: &Map<String, Value>) -> Result<(), ExtensionRegistryError> {
    if map.len() > MAX_EXTENSION_SETTINGS_ENTRIES {
        return Err(ExtensionRegistryError::SettingsTooManyEntries);
    }
    for (key, value) in map {
        if key.len() > MAX_EXTENSION_SETTINGS_KEY_BYTES {
            return Err(ExtensionRegistryError::SettingsKeyTooLong);
        }
        validate_settings_value(value, 0)?;
        let encoded =
            serde_json::to_vec(value).map_err(|_| ExtensionRegistryError::SettingsTooLarge)?;
        if encoded.len() > MAX_EXTENSION_SETTINGS_VALUE_BYTES {
            return Err(ExtensionRegistryError::SettingsTooLarge);
        }
    }
    Ok(())
}

fn validate_settings_value(value: &Value, depth: usize) -> Result<(), ExtensionRegistryError> {
    if depth > MAX_EXTENSION_SETTINGS_NESTING {
        return Err(ExtensionRegistryError::SettingsTooDeep);
    }
    match value {
        Value::Array(values) => {
            for value in values {
                validate_settings_value(value, depth + 1)?;
            }
        }
        Value::Object(values) => {
            if values.len() > MAX_EXTENSION_SETTINGS_ENTRIES {
                return Err(ExtensionRegistryError::SettingsTooManyEntries);
            }
            for (key, value) in values {
                if key.len() > MAX_EXTENSION_SETTINGS_KEY_BYTES {
                    return Err(ExtensionRegistryError::SettingsKeyTooLong);
                }
                validate_settings_value(value, depth + 1)?;
            }
        }
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => {}
    }
    Ok(())
}

/// Public discovery input for one enabled local extension.
#[derive(Clone, Debug, PartialEq)]
pub struct ExtensionDiscovery {
    /// Registered extension identifier.
    pub id: ExtensionId,
    /// Exact generic settings advertised by this peer.
    pub settings: ExtensionSettings,
}

/// Public client discovery input; no client runtime is stored here.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ClientExtensionDiscovery {
    /// Enabled client extensions keyed by their IDs.
    pub extensions: BTreeMap<ExtensionId, ExtensionSettings>,
}

/// Public server discovery input; no server runtime is stored here.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ServerExtensionDiscovery {
    /// Enabled server extensions keyed by their IDs.
    pub extensions: BTreeMap<ExtensionId, ExtensionSettings>,
}

/// Direction of a registered RPC name.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionDirection {
    /// A client sends a request or notification to a server.
    ClientToServer,
    /// A server sends a request or notification to a client.
    ServerToClient,
}

/// Frozen HTTP-era classification for client-to-server extension methods.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionHttpEraDisposition {
    /// The method is audited absent from the exact legacy adapter.
    ModernExclusive,
    /// Method text alone cannot select an era.
    EraAmbiguous,
}

/// Declares the fallback expected by a descriptor's resolver.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionFallbackPolicy {
    /// Both peers must advertise compatible settings.
    RejectOneSided,
    /// A missing client advertisement selects the descriptor's inactive fallback.
    ServerInactiveFallback,
    /// A missing server advertisement selects the descriptor's inactive fallback.
    ClientInactiveFallback,
    /// Either missing peer advertisement selects the descriptor's inactive fallback.
    InactiveOnEitherPeer,
}

/// Stable, total settings compatibility resolver metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionNegotiationResolver {
    /// Stable resolver identifier included in the registry digest.
    pub id: String,
    /// Stable resolver version included in the registry digest.
    pub version: u32,
    /// One-sided behavior selected by this resolver.
    pub fallback: ExtensionFallbackPolicy,
}

/// Stable schema and typed-codec identity, not an executable runtime codec.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionSettingsSchema {
    /// Stable schema identity.
    pub schema_id: String,
    /// Stable typed codec identity.
    pub codec_id: String,
}

/// A registered method and its frozen era/fallback declarations.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionMethodDescriptor {
    /// Exact JSON-RPC method name.
    pub name: String,
    /// Message direction.
    pub direction: ExtensionDirection,
    /// Required only for client-to-server HTTP methods.
    pub http_era_disposition: Option<ExtensionHttpEraDisposition>,
    /// Whether exact legacy fallback is declared; exact legacy excludes extensions, so this must
    /// remain false.
    pub legacy_fallback: bool,
}

/// A registered notification name and direction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionNotificationDescriptor {
    /// Exact notification name.
    pub name: String,
    /// Message direction.
    pub direction: ExtensionDirection,
}

/// A routing-header name owned by an extension descriptor.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionRoutingHeaderDescriptor {
    /// Exact header name.
    pub name: String,
}

/// A stdio notification-correlation metadata key owned by an extension.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct StdioCorrelationDescriptor {
    /// Exact extension-owned metadata key.
    pub metadata_key: String,
    /// Notifications permitted to use the key.
    pub methods: Vec<String>,
    /// Direction required for every named notification.
    pub direction: ExtensionDirection,
}

/// One immutable extension descriptor.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionDescriptor {
    /// Descriptor owner.
    pub id: ExtensionId,
    /// Stable client settings schema and codec identity.
    pub client_settings: ExtensionSettingsSchema,
    /// Stable server settings schema and codec identity.
    pub server_settings: ExtensionSettingsSchema,
    /// Stable compatibility resolver identity and fallback.
    pub resolver: ExtensionNegotiationResolver,
    /// Registered request method, when this extension defines one.
    pub method: Option<ExtensionMethodDescriptor>,
    /// Registered notification, when this extension defines one.
    pub notification: Option<ExtensionNotificationDescriptor>,
    /// Registered result discriminator, when this extension defines one.
    pub result_discriminator: Option<String>,
    /// Routing headers this descriptor owns.
    pub routing_headers: Vec<ExtensionRoutingHeaderDescriptor>,
    /// Optional stdio correlation metadata descriptor.
    pub stdio_correlation: Option<StdioCorrelationDescriptor>,
}

/// Returns the validated identifier for the official Tasks extension.
#[must_use]
#[cfg(feature = "tasks")]
pub fn official_tasks_extension_id() -> ExtensionId {
    ExtensionId::parse(OFFICIAL_TASKS_EXTENSION_ID)
        .expect("the fixed official Tasks identifier satisfies the extension grammar")
}

/// Returns the complete official Tasks descriptor.
///
/// It owns exactly `tasks/get`, `tasks/update`, `tasks/cancel`,
/// `notifications/tasks`, and the `tools/call` result discriminator `task`.
/// Tasks settings are exactly the empty JSON object;
/// [`ExtensionDescriptorRegistry::negotiate`] enforces that invariant for the
/// two peer advertisements and the effective settings chosen by its resolver.
#[must_use]
#[cfg(feature = "tasks")]
pub fn official_tasks_descriptor() -> ExtensionDescriptor {
    ExtensionDescriptor {
        id: official_tasks_extension_id(),
        client_settings: ExtensionSettingsSchema {
            schema_id: OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID.to_owned(),
            codec_id: OFFICIAL_TASKS_EMPTY_SETTINGS_CODEC_ID.to_owned(),
        },
        server_settings: ExtensionSettingsSchema {
            schema_id: OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID.to_owned(),
            codec_id: OFFICIAL_TASKS_EMPTY_SETTINGS_CODEC_ID.to_owned(),
        },
        resolver: ExtensionNegotiationResolver {
            id: OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID.to_owned(),
            version: 1,
            fallback: ExtensionFallbackPolicy::RejectOneSided,
        },
        method: Some(official_tasks_method(OFFICIAL_TASKS_METHODS[0])),
        notification: Some(ExtensionNotificationDescriptor {
            name: OFFICIAL_TASKS_NOTIFICATION.to_owned(),
            direction: ExtensionDirection::ServerToClient,
        }),
        result_discriminator: Some(OFFICIAL_TASKS_RESULT_DISCRIMINATOR.to_owned()),
        routing_headers: Vec::new(),
        stdio_correlation: None,
    }
}

/// Registers the complete official Tasks surface atomically.
///
/// The resulting descriptor owns exactly the official Tasks request methods,
/// its one server notification, and its `tools/call` result discriminator.
/// Registration alone does not activate the extension; normal local enablement
/// and bilateral negotiation still apply.
#[cfg(feature = "tasks")]
pub fn register_official_tasks_extension(
    registry: &mut ExtensionDescriptorRegistry,
) -> Result<ExtensionId, ExtensionRegistryError> {
    let id = official_tasks_extension_id();
    let mut candidate = registry.clone();
    candidate.register(official_tasks_descriptor())?;
    for name in OFFICIAL_TASKS_METHODS.into_iter().skip(1) {
        candidate.register_method(&id, official_tasks_method(name))?;
    }
    *registry = candidate;
    Ok(id)
}

#[cfg(feature = "tasks")]
fn official_tasks_method(name: &str) -> ExtensionMethodDescriptor {
    ExtensionMethodDescriptor {
        name: name.to_owned(),
        direction: ExtensionDirection::ClientToServer,
        http_era_disposition: Some(ExtensionHttpEraDisposition::ModernExclusive),
        legacy_fallback: false,
    }
}

/// Immutable receipt returned by [`ExtensionDescriptorRegistry::freeze`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExtensionRegistryReceipt {
    digest: [u8; 32],
    descriptor_count: usize,
}

impl ExtensionRegistryReceipt {
    /// Returns the domain-separated descriptor-registry digest bytes.
    #[must_use]
    pub const fn digest(&self) -> &[u8; 32] {
        &self.digest
    }

    /// Returns the number of frozen descriptors.
    #[must_use]
    pub const fn descriptor_count(&self) -> usize {
        self.descriptor_count
    }
}

/// Stable registry-validation diagnostics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExtensionRegistryError {
    /// The identifier did not satisfy the mandatory prefix/name grammar.
    InvalidIdentifier(String),
    /// The identifier used a reserved official second DNS label.
    ReservedNamespace(String),
    /// Generic settings were not a JSON object.
    SettingsNotObject,
    /// Typed settings decoding rejected the otherwise preserved object.
    SettingsCodecRejected,
    /// Official MCP Apps server settings must be the exact empty marker.
    OfficialMcpAppsServerSettingsNotEmpty,
    /// The official MCP Apps descriptor differed from its frozen method-free shape.
    OfficialMcpAppsDescriptorMismatch,
    /// Generic settings exceeded the fixed number of retained members.
    SettingsTooManyEntries,
    /// A generic settings key exceeded its fixed byte limit.
    SettingsKeyTooLong,
    /// A generic settings value exceeded its fixed encoded-byte limit.
    SettingsTooLarge,
    /// A generic settings value exceeded its fixed nesting limit.
    SettingsTooDeep,
    /// A descriptor omitted a required stable owner value.
    MissingOwner(&'static str),
    /// A descriptor ID was registered twice.
    DuplicateExtensionId(String),
    /// A method was added to an extension that is not registered.
    UnregisteredExtensionId(String),
    /// A named field already belongs to a different descriptor.
    OwnershipCollision { field: &'static str, value: String },
    /// A client-to-server method omitted its frozen era disposition.
    MissingHttpEraDisposition(String),
    /// Legacy fallback contradicts the frozen HTTP-era disposition.
    LegacyFallbackContradiction(String),
    /// A descriptor attempted to claim a legacy/shared core method.
    CoreMethodCollision(String),
    /// A descriptor attempted to claim a legacy/shared core notification.
    CoreNotificationCollision(String),
    /// A descriptor attempted to claim a final-core result discriminator.
    CoreResultDiscriminatorCollision(String),
    /// A descriptor member exceeded its bounded wire-name limit.
    MemberNameTooLong { field: &'static str, value: String },
    /// A descriptor claimed the same local wire name in incompatible roles.
    LocalOwnershipCollision { field: &'static str, value: String },
    /// Registration occurred after the immutable registry was frozen.
    Frozen,
    /// The canonical digest subject exceeded its bounded limit.
    DigestTooLarge,
}

impl fmt::Display for ExtensionRegistryError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidIdentifier(value) => {
                write!(formatter, "invalid extension identifier: {value}")
            }
            Self::ReservedNamespace(value) => {
                write!(formatter, "reserved extension namespace: {value}")
            }
            Self::SettingsNotObject => {
                formatter.write_str("extension settings must be a JSON object")
            }
            Self::SettingsCodecRejected => {
                formatter.write_str("extension settings codec rejected object")
            }
            Self::OfficialMcpAppsServerSettingsNotEmpty => {
                formatter.write_str("official MCP Apps server settings must be empty")
            }
            Self::OfficialMcpAppsDescriptorMismatch => {
                formatter.write_str("official MCP Apps descriptor differs from its frozen shape")
            }
            Self::SettingsTooManyEntries => {
                formatter.write_str("extension settings exceed their entry limit")
            }
            Self::SettingsKeyTooLong => {
                formatter.write_str("extension settings key exceeds its byte limit")
            }
            Self::SettingsTooLarge => {
                formatter.write_str("extension settings value exceeds its byte limit")
            }
            Self::SettingsTooDeep => {
                formatter.write_str("extension settings exceed their nesting limit")
            }
            Self::MissingOwner(field) => {
                write!(formatter, "missing extension descriptor owner: {field}")
            }
            Self::DuplicateExtensionId(value) => {
                write!(formatter, "duplicate extension identifier: {value}")
            }
            Self::UnregisteredExtensionId(value) => {
                write!(formatter, "unregistered extension identifier: {value}")
            }
            Self::OwnershipCollision { field, value } => {
                write!(formatter, "extension {field} ownership collision: {value}")
            }
            Self::MissingHttpEraDisposition(value) => write!(
                formatter,
                "client-to-server method has no HTTP-era disposition: {value}"
            ),
            Self::LegacyFallbackContradiction(value) => write!(
                formatter,
                "legacy fallback contradicts HTTP-era disposition: {value}"
            ),
            Self::CoreMethodCollision(value) => write!(
                formatter,
                "extension method collides with legacy/shared core method: {value}"
            ),
            Self::CoreNotificationCollision(value) => write!(
                formatter,
                "extension notification collides with legacy/shared core notification: {value}"
            ),
            Self::CoreResultDiscriminatorCollision(value) => write!(
                formatter,
                "extension result discriminator collides with final-core result: {value}"
            ),
            Self::MemberNameTooLong { field, value } => write!(
                formatter,
                "extension {field} exceeds its byte limit: {value}"
            ),
            Self::LocalOwnershipCollision { field, value } => write!(
                formatter,
                "extension {field} has incompatible local ownership: {value}"
            ),
            Self::Frozen => formatter.write_str("extension descriptor registry is frozen"),
            Self::DigestTooLarge => {
                formatter.write_str("extension descriptor registry digest subject exceeds bound")
            }
        }
    }
}

impl std::error::Error for ExtensionRegistryError {}

/// Local feature and runtime opt-in state for the registered descriptors.
///
/// The default is deliberately empty: registering an extension does not enable
/// it for any request.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ExtensionLocalEnablement {
    compiled: BTreeSet<ExtensionId>,
    runtime: BTreeSet<ExtensionId>,
}

impl ExtensionLocalEnablement {
    /// Enables one descriptor at both the compile-feature and runtime layers.
    pub fn enable(&mut self, id: ExtensionId) {
        self.compiled.insert(id.clone());
        self.runtime.insert(id);
    }

    /// Records whether the local build includes a descriptor's compile feature.
    pub fn set_compiled(&mut self, id: ExtensionId, enabled: bool) {
        set_enabled(&mut self.compiled, id, enabled);
    }

    /// Records whether the current local runtime enables a descriptor.
    pub fn set_runtime(&mut self, id: ExtensionId, enabled: bool) {
        set_enabled(&mut self.runtime, id, enabled);
    }

    /// Returns whether both local enablement gates are satisfied.
    #[must_use]
    pub fn is_enabled(&self, id: &ExtensionId) -> bool {
        self.compiled.contains(id) && self.runtime.contains(id)
    }

    fn configured_ids(&self) -> impl Iterator<Item = &ExtensionId> {
        self.compiled.iter().chain(self.runtime.iter())
    }
}

fn set_enabled(set: &mut BTreeSet<ExtensionId>, id: ExtensionId, enabled: bool) {
    if enabled {
        set.insert(id);
    } else {
        set.remove(&id);
    }
}

/// The peer side that omitted an otherwise locally enabled extension.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionPeer {
    /// The current message omitted client extension settings.
    Client,
    /// Local server discovery omitted server extension settings.
    Server,
}

/// A non-activating extension outcome retained for diagnostics.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExtensionInactiveReason {
    /// The compile-time feature or runtime opt-in is absent.
    LocallyDisabled,
    /// Neither peer advertised the descriptor on this current exchange.
    NotAdvertised,
    /// The registered fallback was selected after the client omitted support.
    ServerInactiveFallback,
    /// The registered fallback was selected after the server omitted support.
    ClientInactiveFallback,
    /// Both peers advertised valid settings, but the typed resolver selected an inactive fallback.
    SettingsInactiveFallback,
}

/// Normalized typed settings produced by a descriptor's compatibility resolver.
///
/// Generic registry code retains client and server settings independently and
/// never compares their JSON objects for equality. A typed resolver receives
/// both objects and returns this one normalized, bounded value.
#[derive(Clone, Debug, PartialEq)]
pub struct EffectiveExtensionSettings {
    settings: ExtensionSettings,
    fingerprint: [u8; 32],
}

impl EffectiveExtensionSettings {
    /// Returns the typed-resolver's normalized settings object.
    #[must_use]
    pub const fn settings(&self) -> &ExtensionSettings {
        &self.settings
    }

    /// Returns the stable fingerprint bound to the descriptor and effective settings.
    #[must_use]
    pub const fn fingerprint(&self) -> &[u8; 32] {
        &self.fingerprint
    }
}

/// An enabled extension after the current-message bilateral negotiation.
#[derive(Clone, Debug, PartialEq)]
pub struct NegotiatedExtension {
    id: ExtensionId,
    effective_settings: EffectiveExtensionSettings,
}

impl NegotiatedExtension {
    /// Returns the registered descriptor identifier.
    #[must_use]
    pub const fn id(&self) -> &ExtensionId {
        &self.id
    }

    /// Returns the compatibility resolver's normalized effective settings.
    #[must_use]
    pub const fn effective_settings(&self) -> &EffectiveExtensionSettings {
        &self.effective_settings
    }
}

/// Frozen per-request extension state derived from both peers' current settings.
#[derive(Clone, Debug, PartialEq)]
pub struct NegotiatedExtensionSet {
    registry_receipt: ExtensionRegistryReceipt,
    protocol_era: ProtocolEra,
    active: BTreeMap<ExtensionId, NegotiatedExtension>,
    inactive: BTreeMap<ExtensionId, ExtensionInactiveReason>,
    unknown_client: BTreeMap<ExtensionId, ExtensionSettings>,
    unknown_server: BTreeMap<ExtensionId, ExtensionSettings>,
}

/// Opaque current-exchange receipt authorizing one MCP Apps Host/View bridge.
///
/// This can only be derived from a frozen registry and a negotiated extension
/// set in which the exact official Apps descriptor is active. It deliberately
/// exposes no literal-based constructor: callers must retain the current
/// negotiation result rather than recreating Apps activation from schema IDs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct McpAppsActivationReceipt {
    registry_digest: [u8; 32],
    effective_settings_fingerprint: [u8; 32],
}

impl NegotiatedExtensionSet {
    /// Returns the registry receipt that this set is bound to.
    #[must_use]
    pub const fn registry_receipt(&self) -> &ExtensionRegistryReceipt {
        &self.registry_receipt
    }

    /// Returns the exact modern protocol era that produced this request state.
    #[must_use]
    pub const fn protocol_era(&self) -> ProtocolEra {
        self.protocol_era
    }

    /// Returns a negotiated extension when it is active for this exchange.
    #[must_use]
    pub fn active(&self, id: &ExtensionId) -> Option<&NegotiatedExtension> {
        self.active.get(id)
    }

    /// Returns the recorded non-activating outcome for a registered descriptor.
    #[must_use]
    pub fn inactive_reason(&self, id: &ExtensionId) -> Option<ExtensionInactiveReason> {
        self.inactive.get(id).copied()
    }

    /// Returns enabled descriptors in deterministic identifier order.
    #[must_use]
    pub fn active_extensions(&self) -> impl ExactSizeIterator<Item = &NegotiatedExtension> {
        self.active.values()
    }

    /// Derives the sole receipt accepted by the Apps Host/View bridge for this
    /// current negotiated exchange.
    ///
    /// The receipt is unavailable for a legacy exchange, a different frozen
    /// registry, inactive Apps settings, or any descriptor that differs from
    /// the frozen official method-free Apps descriptor.
    #[must_use]
    pub fn mcp_apps_activation_receipt(
        &self,
        registry: &ExtensionDescriptorRegistry,
    ) -> Option<McpAppsActivationReceipt> {
        if self.protocol_era != ProtocolEra::Modern2026 || self.ensure_registry(registry).is_err() {
            return None;
        }
        let id = official_mcp_apps_extension_id();
        let descriptor = registry.descriptor(&id)?;
        if validate_official_mcp_apps_descriptor(descriptor).is_err() {
            return None;
        }
        let active = self.active(&id)?;
        let settings =
            McpAppsClientSettings::from_extension_settings(active.effective_settings().settings())
                .ok()?;
        if !settings.supports_mcp_apps_html() {
            return None;
        }
        Some(McpAppsActivationReceipt {
            registry_digest: *self.registry_receipt.digest(),
            effective_settings_fingerprint: *active.effective_settings().fingerprint(),
        })
    }

    /// Returns unknown current-message client settings preserved only for diagnostics.
    #[must_use]
    pub const fn unknown_client_extensions(&self) -> &BTreeMap<ExtensionId, ExtensionSettings> {
        &self.unknown_client
    }

    /// Returns unknown server discovery settings preserved only for diagnostics.
    #[must_use]
    pub const fn unknown_server_extensions(&self) -> &BTreeMap<ExtensionId, ExtensionSettings> {
        &self.unknown_server
    }
}

/// Error returned while deriving a per-request bilateral extension set.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExtensionNegotiationError {
    /// Extensions are excluded from exact MCP 2024-11-05 negotiation.
    LegacyProtocolExcluded,
    /// Descriptor registration must be frozen before negotiation.
    RegistryNotFrozen,
    /// A local feature/runtime configuration referenced an unregistered descriptor.
    UnregisteredLocalEnablement(String),
    /// A discovery map exceeded the generic retained-entry limit.
    DiscoveryTooManyExtensions(ExtensionPeer),
    /// Only one peer advertised a locally enabled descriptor without its matching fallback.
    OneSidedSupport { id: String, missing: ExtensionPeer },
    /// The selected typed resolver rejected both independently decoded settings objects.
    SettingsCompatibilityRejected(String),
    /// The normalized settings object could not be fingerprinted within its bound.
    EffectiveSettingsTooLarge(String),
}

impl fmt::Display for ExtensionNegotiationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LegacyProtocolExcluded => {
                formatter.write_str("extensions are excluded from exact MCP 2024-11-05")
            }
            Self::RegistryNotFrozen => {
                formatter.write_str("extension descriptor registry is not frozen")
            }
            Self::UnregisteredLocalEnablement(id) => {
                write!(
                    formatter,
                    "local extension enablement has no descriptor: {id}"
                )
            }
            Self::DiscoveryTooManyExtensions(peer) => {
                write!(
                    formatter,
                    "{peer:?} extension discovery exceeds its entry limit"
                )
            }
            Self::OneSidedSupport { id, missing } => {
                write!(formatter, "extension {id} is missing {missing:?} support")
            }
            Self::SettingsCompatibilityRejected(id) => {
                write!(formatter, "extension settings are incompatible: {id}")
            }
            Self::EffectiveSettingsTooLarge(id) => {
                write!(
                    formatter,
                    "effective extension settings exceed their bound: {id}"
                )
            }
        }
    }
}

impl std::error::Error for ExtensionNegotiationError {}

/// The activation outcome selected by a typed settings compatibility resolver.
#[derive(Clone, Debug, PartialEq)]
pub enum ExtensionSettingsResolution {
    /// Compatible settings activate the extension for this exchange.
    Active(ExtensionSettings),
    /// Valid settings select the descriptor's ordinary inactive fallback.
    Inactive,
}

/// A typed compatibility resolver selected by a frozen descriptor ID/version.
///
/// This protocol-only seam deliberately accepts no server or client runtime
/// object. Implementations decode the two settings objects independently,
/// check compatibility, and return a normalized effective object.
pub trait ExtensionSettingsCompatibilityResolver {
    /// Resolves both current settings objects into normalized effective settings.
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError>;

    /// Resolves settings and may select an ordinary inactive fallback.
    ///
    /// Existing resolvers that only implement [`Self::resolve`] remain active
    /// on success. Descriptors with a valid non-activating setting profile can
    /// override this method without treating that profile as malformed.
    fn resolve_with_disposition(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettingsResolution, ExtensionNegotiationError> {
        self.resolve(descriptor, client, server)
            .map(ExtensionSettingsResolution::Active)
    }
}

impl<F> ExtensionSettingsCompatibilityResolver for F
where
    F: FnMut(
        &ExtensionDescriptor,
        &ExtensionSettings,
        &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError>,
{
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError> {
        self(descriptor, client, server)
    }
}

impl ExtensionSettingsCompatibilityResolver for RejectingExtensionNegotiationResolver {
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        _client: &ExtensionSettings,
        _server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError> {
        Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
            descriptor.id.to_string(),
        ))
    }
}

#[cfg(feature = "tasks")]
impl ExtensionSettingsCompatibilityResolver for OfficialTasksNegotiationResolver {
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError> {
        if descriptor.id.as_str() == OFFICIAL_TASKS_EXTENSION_ID {
            resolve_official_tasks_settings(descriptor, client, server)
        } else {
            Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
                descriptor.id.to_string(),
            ))
        }
    }
}

#[cfg(feature = "tasks")]
impl<R> ExtensionSettingsCompatibilityResolver for TasksNegotiationResolver<R>
where
    R: ExtensionSettingsCompatibilityResolver,
{
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError> {
        match self.resolve_with_disposition(descriptor, client, server)? {
            ExtensionSettingsResolution::Active(settings) => Ok(settings),
            ExtensionSettingsResolution::Inactive => Err(
                ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string()),
            ),
        }
    }

    fn resolve_with_disposition(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettingsResolution, ExtensionNegotiationError> {
        if descriptor.id.as_str() == OFFICIAL_TASKS_EXTENSION_ID {
            resolve_official_tasks_settings(descriptor, client, server)
                .map(ExtensionSettingsResolution::Active)
        } else {
            self.fallback
                .resolve_with_disposition(descriptor, client, server)
        }
    }
}

impl<R> ExtensionSettingsCompatibilityResolver for McpAppsNegotiationResolver<R>
where
    R: ExtensionSettingsCompatibilityResolver,
{
    fn resolve(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettings, ExtensionNegotiationError> {
        match self.resolve_with_disposition(descriptor, client, server)? {
            ExtensionSettingsResolution::Active(settings) => Ok(settings),
            ExtensionSettingsResolution::Inactive => Err(
                ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string()),
            ),
        }
    }

    fn resolve_with_disposition(
        &mut self,
        descriptor: &ExtensionDescriptor,
        client: &ExtensionSettings,
        server: &ExtensionSettings,
    ) -> Result<ExtensionSettingsResolution, ExtensionNegotiationError> {
        if descriptor.id.as_str() == OFFICIAL_MCP_APPS_EXTENSION_ID {
            resolve_official_mcp_apps_settings(descriptor, client, server)
        } else {
            self.fallback
                .resolve_with_disposition(descriptor, client, server)
        }
    }
}

/// Direction-sensitive extension dispatch failure.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ExtensionDispatchError {
    /// Extensions are excluded from an exact MCP 2024-11-05 request.
    LegacyProtocolExcluded,
    /// The request era does not match the immutable era that negotiated this set.
    ProtocolEraMismatch {
        /// The exact era that created this set.
        negotiated: ProtocolEra,
        /// The exact era attached to the request being admitted.
        request: ProtocolEra,
    },
    /// Dispatch used a registry other than the frozen registry that negotiated the set.
    RegistryReceiptMismatch,
    /// The named extension was not activated by developer opt-in and bilateral negotiation.
    InactiveCapability(String),
    /// The active capability does not own the named request member.
    CapabilityDoesNotOwn {
        /// Active extension capability identifier.
        capability: String,
        /// Registered member category.
        field: &'static str,
        /// Request-supplied member spelling.
        value: String,
    },
    /// The caller supplied an unbounded member name.
    NameTooLong(String),
    /// No active extension owns the requested member in this direction.
    NoActiveOwner { field: &'static str, value: String },
    /// An owner exists but is declared in the opposite direction.
    DirectionMismatch {
        field: &'static str,
        value: String,
        expected: ExtensionDirection,
        actual: ExtensionDirection,
    },
    /// A registry invariant was violated by more than one active owner.
    AmbiguousActiveOwner { field: &'static str, value: String },
}

impl fmt::Display for ExtensionDispatchError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LegacyProtocolExcluded => {
                formatter.write_str("extensions are excluded from exact MCP 2024-11-05")
            }
            Self::ProtocolEraMismatch {
                negotiated,
                request,
            } => write!(
                formatter,
                "extension request era {request:?} does not match negotiated era {negotiated:?}"
            ),
            Self::RegistryReceiptMismatch => {
                formatter.write_str("extension dispatch registry does not match negotiation")
            }
            Self::InactiveCapability(capability) => {
                write!(
                    formatter,
                    "extension capability is not active: {capability}"
                )
            }
            Self::CapabilityDoesNotOwn {
                capability,
                field,
                value,
            } => write!(
                formatter,
                "extension capability {capability} does not own {field}: {value}"
            ),
            Self::NameTooLong(value) => {
                write!(
                    formatter,
                    "extension dispatch name exceeds its byte limit: {value}"
                )
            }
            Self::NoActiveOwner { field, value } => {
                write!(formatter, "no active extension owns {field}: {value}")
            }
            Self::DirectionMismatch {
                field,
                value,
                expected,
                actual,
            } => write!(
                formatter,
                "extension {field} has direction {actual:?}, not {expected:?}: {value}"
            ),
            Self::AmbiguousActiveOwner { field, value } => {
                write!(formatter, "multiple active extensions own {field}: {value}")
            }
        }
    }
}

impl std::error::Error for ExtensionDispatchError {}

/// Acyclic protocol-only descriptor registry.
#[derive(Clone, Debug, Default)]
pub struct ExtensionDescriptorRegistry {
    descriptors: BTreeMap<ExtensionId, ExtensionDescriptor>,
    additional_methods: BTreeMap<ExtensionId, BTreeMap<String, ExtensionMethodDescriptor>>,
    receipt: Option<ExtensionRegistryReceipt>,
}

impl ExtensionDescriptorRegistry {
    /// Builds an empty registry; extensions are disabled until a descriptor is registered.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds one descriptor after validating all descriptor-local and cross-owner rules.
    pub fn register(
        &mut self,
        descriptor: ExtensionDescriptor,
    ) -> Result<(), ExtensionRegistryError> {
        if self.receipt.is_some() {
            return Err(ExtensionRegistryError::Frozen);
        }
        if self.descriptors.len() >= MAX_EXTENSION_DESCRIPTORS {
            return Err(ExtensionRegistryError::DigestTooLarge);
        }
        validate_descriptor(&descriptor)?;
        if self.descriptors.contains_key(&descriptor.id) {
            return Err(ExtensionRegistryError::DuplicateExtensionId(
                descriptor.id.to_string(),
            ));
        }
        for existing in self.descriptors.values() {
            ensure_no_cross_owner_collision(existing, &descriptor)?;
        }
        if let Some(method) = &descriptor.method {
            for (existing_id, methods) in &self.additional_methods {
                if methods.contains_key(&method.name) {
                    return Err(ExtensionRegistryError::OwnershipCollision {
                        field: "method",
                        value: method.name.clone(),
                    });
                }
                if self
                    .descriptors
                    .get(existing_id)
                    .and_then(|existing| existing.notification.as_ref())
                    .is_some_and(|notification| notification.name == method.name)
                {
                    return Err(ExtensionRegistryError::OwnershipCollision {
                        field: "method/notification",
                        value: method.name.clone(),
                    });
                }
            }
        }
        if let Some(notification) = &descriptor.notification {
            for methods in self.additional_methods.values() {
                if methods.contains_key(&notification.name) {
                    return Err(ExtensionRegistryError::OwnershipCollision {
                        field: "method/notification",
                        value: notification.name.clone(),
                    });
                }
            }
        }
        self.descriptors.insert(descriptor.id.clone(), descriptor);
        Ok(())
    }

    /// Adds another request method to an already registered extension descriptor.
    ///
    /// One extension can own more than one request method. The extension's
    /// settings, resolver, notification, and result discriminator stay on the
    /// original descriptor so every additional method shares the same negotiated
    /// capability and frozen receipt.
    pub fn register_method(
        &mut self,
        id: &ExtensionId,
        method: ExtensionMethodDescriptor,
    ) -> Result<(), ExtensionRegistryError> {
        if self.receipt.is_some() {
            return Err(ExtensionRegistryError::Frozen);
        }
        let Some(descriptor) = self.descriptors.get(id) else {
            return Err(ExtensionRegistryError::UnregisteredExtensionId(
                id.to_string(),
            ));
        };
        validate_extension_method(&method)?;
        if descriptor
            .method
            .as_ref()
            .is_some_and(|registered| registered.name == method.name)
            || self
                .additional_methods
                .get(id)
                .is_some_and(|methods| methods.contains_key(&method.name))
        {
            return Err(ExtensionRegistryError::LocalOwnershipCollision {
                field: "method",
                value: method.name,
            });
        }
        if descriptor
            .notification
            .as_ref()
            .is_some_and(|notification| notification.name == method.name)
        {
            return Err(ExtensionRegistryError::LocalOwnershipCollision {
                field: "method/notification",
                value: method.name,
            });
        }
        for (existing_id, existing) in &self.descriptors {
            if existing_id == id {
                continue;
            }
            if existing
                .method
                .as_ref()
                .is_some_and(|registered| registered.name == method.name)
                || self
                    .additional_methods
                    .get(existing_id)
                    .is_some_and(|methods| methods.contains_key(&method.name))
            {
                return Err(ExtensionRegistryError::OwnershipCollision {
                    field: "method",
                    value: method.name,
                });
            }
            if existing
                .notification
                .as_ref()
                .is_some_and(|notification| notification.name == method.name)
            {
                return Err(ExtensionRegistryError::OwnershipCollision {
                    field: "method/notification",
                    value: method.name,
                });
            }
        }
        self.additional_methods
            .entry(id.clone())
            .or_default()
            .insert(method.name.clone(), method);
        Ok(())
    }

    /// Freezes this registry and returns its canonical domain-separated digest receipt.
    pub fn freeze(&mut self) -> Result<ExtensionRegistryReceipt, ExtensionRegistryError> {
        if let Some(receipt) = &self.receipt {
            return Ok(receipt.clone());
        }
        let canonical = self.canonical_subject()?;
        let digest = sha256_bounded(canonical.as_bytes(), MAX_EXTENSION_REGISTRY_CANONICAL_BYTES)
            .map_err(|_| ExtensionRegistryError::DigestTooLarge)?
            .into_bytes();
        let receipt = ExtensionRegistryReceipt {
            digest,
            descriptor_count: self.descriptors.len(),
        };
        self.receipt = Some(receipt.clone());
        Ok(receipt)
    }

    /// Returns the frozen receipt, if this registry has been frozen.
    #[must_use]
    pub fn receipt(&self) -> Option<&ExtensionRegistryReceipt> {
        self.receipt.as_ref()
    }

    /// Returns a descriptor by its exact registered identifier.
    #[must_use]
    pub fn descriptor(&self, id: &ExtensionId) -> Option<&ExtensionDescriptor> {
        self.descriptors.get(id)
    }

    fn method(&self, id: &ExtensionId, name: &str) -> Option<&ExtensionMethodDescriptor> {
        self.descriptors
            .get(id)
            .and_then(|descriptor| {
                descriptor
                    .method
                    .as_ref()
                    .filter(|method| method.name == name)
            })
            .or_else(|| self.additional_methods.get(id)?.get(name))
    }

    /// Returns the descriptor for one extension-owned request method.
    ///
    /// This includes the primary descriptor method and every additional method
    /// registered for the same extension capability. Server handler registries
    /// use it to reject handler configurations that could never be admitted at
    /// request dispatch.
    #[must_use]
    pub fn method_descriptor(
        &self,
        id: &ExtensionId,
        name: &str,
    ) -> Option<&ExtensionMethodDescriptor> {
        self.method(id, name)
    }

    /// Returns the frozen descriptors in deterministic identifier order.
    #[must_use]
    pub fn descriptors(&self) -> impl ExactSizeIterator<Item = &ExtensionDescriptor> {
        self.descriptors.values()
    }

    /// Preserves unknown peer settings as inert diagnostic data.
    #[must_use]
    pub fn preserve_unknown_peer_extensions(
        &self,
        peer: BTreeMap<ExtensionId, ExtensionSettings>,
    ) -> BTreeMap<ExtensionId, ExtensionSettings> {
        peer.into_iter()
            .filter(|(id, _)| !self.descriptors.contains_key(id))
            .collect()
    }

    /// Derives one frozen, bilateral extension set for the current exchange.
    ///
    /// The typed resolver is called only for descriptors enabled by both
    /// local gates and advertised by both peers on this exchange. The generic
    /// registry preserves the peer objects independently; it never treats
    /// JSON-object equality as compatibility.
    pub fn negotiate<R>(
        &self,
        protocol_era: ProtocolEra,
        local: &ExtensionLocalEnablement,
        client: &ClientExtensionDiscovery,
        server: &ServerExtensionDiscovery,
        resolver: &mut R,
    ) -> Result<NegotiatedExtensionSet, ExtensionNegotiationError>
    where
        R: ExtensionSettingsCompatibilityResolver,
    {
        if matches!(protocol_era, ProtocolEra::Legacy2024) {
            return Err(ExtensionNegotiationError::LegacyProtocolExcluded);
        }
        let Some(receipt) = self.receipt.clone() else {
            return Err(ExtensionNegotiationError::RegistryNotFrozen);
        };
        validate_discovery(&client.extensions, ExtensionPeer::Client)?;
        validate_discovery(&server.extensions, ExtensionPeer::Server)?;
        for id in local.configured_ids() {
            if !self.descriptors.contains_key(id) {
                return Err(ExtensionNegotiationError::UnregisteredLocalEnablement(
                    id.to_string(),
                ));
            }
        }

        let unknown_client = self.preserve_unknown_peer_extensions(client.extensions.clone());
        let unknown_server = self.preserve_unknown_peer_extensions(server.extensions.clone());
        let mut active = BTreeMap::new();
        let mut inactive = BTreeMap::new();

        for descriptor in self.descriptors.values() {
            let id = &descriptor.id;
            if !local.is_enabled(id) {
                inactive.insert(id.clone(), ExtensionInactiveReason::LocallyDisabled);
                continue;
            }

            match (client.extensions.get(id), server.extensions.get(id)) {
                (Some(client), Some(server)) => {
                    #[cfg(feature = "tasks")]
                    {
                        enforce_official_tasks_empty_settings(id, client)?;
                        enforce_official_tasks_empty_settings(id, server)?;
                    }
                    match resolver.resolve_with_disposition(descriptor, client, server)? {
                        ExtensionSettingsResolution::Active(effective) => {
                            #[cfg(feature = "tasks")]
                            enforce_official_tasks_empty_settings(id, &effective)?;
                            let fingerprint =
                                effective_settings_fingerprint(descriptor, &effective)?;
                            active.insert(
                                id.clone(),
                                NegotiatedExtension {
                                    id: id.clone(),
                                    effective_settings: EffectiveExtensionSettings {
                                        settings: effective,
                                        fingerprint,
                                    },
                                },
                            );
                        }
                        ExtensionSettingsResolution::Inactive => {
                            inactive.insert(
                                id.clone(),
                                ExtensionInactiveReason::SettingsInactiveFallback,
                            );
                        }
                    }
                }
                (None, None) => {
                    inactive.insert(id.clone(), ExtensionInactiveReason::NotAdvertised);
                }
                (None, Some(_)) => match descriptor.resolver.fallback {
                    ExtensionFallbackPolicy::ServerInactiveFallback
                    | ExtensionFallbackPolicy::InactiveOnEitherPeer => {
                        inactive
                            .insert(id.clone(), ExtensionInactiveReason::ServerInactiveFallback);
                    }
                    ExtensionFallbackPolicy::RejectOneSided
                    | ExtensionFallbackPolicy::ClientInactiveFallback => {
                        return Err(ExtensionNegotiationError::OneSidedSupport {
                            id: id.to_string(),
                            missing: ExtensionPeer::Client,
                        });
                    }
                },
                (Some(_), None) => match descriptor.resolver.fallback {
                    ExtensionFallbackPolicy::ClientInactiveFallback
                    | ExtensionFallbackPolicy::InactiveOnEitherPeer => {
                        inactive
                            .insert(id.clone(), ExtensionInactiveReason::ClientInactiveFallback);
                    }
                    ExtensionFallbackPolicy::RejectOneSided
                    | ExtensionFallbackPolicy::ServerInactiveFallback => {
                        return Err(ExtensionNegotiationError::OneSidedSupport {
                            id: id.to_string(),
                            missing: ExtensionPeer::Server,
                        });
                    }
                },
            }
        }

        Ok(NegotiatedExtensionSet {
            registry_receipt: receipt,
            protocol_era,
            active,
            inactive,
            unknown_client,
            unknown_server,
        })
    }

    fn canonical_subject(&self) -> Result<String, ExtensionRegistryError> {
        let rows = self
            .descriptors
            .values()
            .map(|descriptor| {
                canonical_descriptor_row(descriptor, self.additional_methods.get(&descriptor.id))
            })
            .collect::<Vec<_>>();
        let json = serde_json::to_string(&("fastmcp.ext-01.descriptor-registry.v1", rows))
            .map_err(|_| ExtensionRegistryError::DigestTooLarge)?;
        // EXT-01 freezes the canonical subject as escaped JSON token bytes,
        // rather than as an ordinary JSON document. Keep that framing before
        // hashing so the public receipt matches the frozen digest contract.
        let subject = json.replace('"', r#"\""#);
        if subject.len() > MAX_EXTENSION_REGISTRY_CANONICAL_BYTES {
            return Err(ExtensionRegistryError::DigestTooLarge);
        }
        Ok(subject)
    }
}

#[cfg(feature = "tasks")]
fn enforce_official_tasks_empty_settings(
    id: &ExtensionId,
    settings: &ExtensionSettings,
) -> Result<(), ExtensionNegotiationError> {
    if id.as_str() != OFFICIAL_TASKS_EXTENSION_ID || settings.as_object().is_empty() {
        return Ok(());
    }
    Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
        id.to_string(),
    ))
}

fn validate_discovery(
    extensions: &BTreeMap<ExtensionId, ExtensionSettings>,
    peer: ExtensionPeer,
) -> Result<(), ExtensionNegotiationError> {
    if extensions.len() > MAX_EXTENSION_DESCRIPTORS {
        return Err(ExtensionNegotiationError::DiscoveryTooManyExtensions(peer));
    }
    Ok(())
}

fn effective_settings_fingerprint(
    descriptor: &ExtensionDescriptor,
    effective: &ExtensionSettings,
) -> Result<[u8; 32], ExtensionNegotiationError> {
    let subject = serde_json::to_vec(&serde_json::json!({
        "domain": "fastmcp.ext-01.effective-settings.v1",
        "id": descriptor.id.as_str(),
        "resolver": [descriptor.resolver.id, descriptor.resolver.version],
        "clientSchema": descriptor.client_settings.schema_id,
        "serverSchema": descriptor.server_settings.schema_id,
        "effective": canonicalize_value(&Value::Object(effective.as_object().clone())),
    }))
    .map_err(|_| ExtensionNegotiationError::EffectiveSettingsTooLarge(descriptor.id.to_string()))?;
    if subject.len() > MAX_EXTENSION_REGISTRY_CANONICAL_BYTES {
        return Err(ExtensionNegotiationError::EffectiveSettingsTooLarge(
            descriptor.id.to_string(),
        ));
    }
    sha256_bounded(&subject, MAX_EXTENSION_REGISTRY_CANONICAL_BYTES)
        .map(|digest| digest.into_bytes())
        .map_err(|_| {
            ExtensionNegotiationError::EffectiveSettingsTooLarge(descriptor.id.to_string())
        })
}

fn canonicalize_value(value: &Value) -> Value {
    match value {
        Value::Array(values) => Value::Array(values.iter().map(canonicalize_value).collect()),
        Value::Object(values) => Value::Object(
            values
                .iter()
                .map(|(key, value)| (key.clone(), canonicalize_value(value)))
                .collect(),
        ),
        Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_) => value.clone(),
    }
}

impl NegotiatedExtensionSet {
    /// Admits an active extension capability for one modern request.
    pub fn admit_capability<'a>(
        &self,
        registry: &'a ExtensionDescriptorRegistry,
        request_era: ProtocolEra,
        capability: &ExtensionId,
    ) -> Result<&'a ExtensionDescriptor, ExtensionDispatchError> {
        self.ensure_request_era(request_era)?;
        self.ensure_registry(registry)?;
        if !self.active.contains_key(capability) {
            return Err(ExtensionDispatchError::InactiveCapability(
                capability.to_string(),
            ));
        }
        registry
            .descriptor(capability)
            .ok_or(ExtensionDispatchError::RegistryReceiptMismatch)
    }

    /// Admits a method owned by an active capability for one modern request.
    pub fn admit_method<'a>(
        &self,
        registry: &'a ExtensionDescriptorRegistry,
        request_era: ProtocolEra,
        capability: &ExtensionId,
        name: &str,
        direction: ExtensionDirection,
    ) -> Result<&'a ExtensionDescriptor, ExtensionDispatchError> {
        validate_dispatch_name(name)?;
        let descriptor = self.admit_capability(registry, request_era, capability)?;
        let Some(method) = registry.method(capability, name) else {
            return Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: capability.to_string(),
                field: "method",
                value: name.to_owned(),
            });
        };
        if method.direction != direction {
            return Err(ExtensionDispatchError::DirectionMismatch {
                field: "method",
                value: name.to_owned(),
                expected: direction,
                actual: method.direction,
            });
        }
        Ok(descriptor)
    }

    /// Admits a notification owned by an active capability for one modern request.
    pub fn admit_notification<'a>(
        &self,
        registry: &'a ExtensionDescriptorRegistry,
        request_era: ProtocolEra,
        capability: &ExtensionId,
        name: &str,
        direction: ExtensionDirection,
    ) -> Result<&'a ExtensionDescriptor, ExtensionDispatchError> {
        validate_dispatch_name(name)?;
        let descriptor = self.admit_capability(registry, request_era, capability)?;
        let Some(notification) = descriptor.notification.as_ref() else {
            return Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: capability.to_string(),
                field: "notification",
                value: name.to_owned(),
            });
        };
        if notification.name != name {
            return Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: capability.to_string(),
                field: "notification",
                value: name.to_owned(),
            });
        }
        if notification.direction != direction {
            return Err(ExtensionDispatchError::DirectionMismatch {
                field: "notification",
                value: name.to_owned(),
                expected: direction,
                actual: notification.direction,
            });
        }
        Ok(descriptor)
    }

    /// Admits a result discriminator owned by an active capability for one modern request.
    pub fn admit_result_discriminator<'a>(
        &self,
        registry: &'a ExtensionDescriptorRegistry,
        request_era: ProtocolEra,
        capability: &ExtensionId,
        discriminator: &str,
    ) -> Result<&'a ExtensionDescriptor, ExtensionDispatchError> {
        validate_dispatch_name(discriminator)?;
        let descriptor = self.admit_capability(registry, request_era, capability)?;
        if descriptor.result_discriminator.as_deref() != Some(discriminator) {
            return Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: capability.to_string(),
                field: "result discriminator",
                value: discriminator.to_owned(),
            });
        }
        Ok(descriptor)
    }

    fn ensure_registry(
        &self,
        registry: &ExtensionDescriptorRegistry,
    ) -> Result<(), ExtensionDispatchError> {
        if registry.receipt() == Some(&self.registry_receipt) {
            Ok(())
        } else {
            Err(ExtensionDispatchError::RegistryReceiptMismatch)
        }
    }

    fn ensure_request_era(&self, request_era: ProtocolEra) -> Result<(), ExtensionDispatchError> {
        if matches!(request_era, ProtocolEra::Legacy2024) {
            return Err(ExtensionDispatchError::LegacyProtocolExcluded);
        }
        if self.protocol_era != request_era {
            return Err(ExtensionDispatchError::ProtocolEraMismatch {
                negotiated: self.protocol_era,
                request: request_era,
            });
        }
        Ok(())
    }
}

fn validate_dispatch_name(name: &str) -> Result<(), ExtensionDispatchError> {
    if name.len() > MAX_EXTENSION_MEMBER_NAME_BYTES {
        return Err(ExtensionDispatchError::NameTooLong(name.to_owned()));
    }
    Ok(())
}

fn validate_descriptor(descriptor: &ExtensionDescriptor) -> Result<(), ExtensionRegistryError> {
    if descriptor.id.as_str() == OFFICIAL_MCP_APPS_EXTENSION_ID {
        validate_official_mcp_apps_descriptor(descriptor)?;
    }
    for (field, value) in [
        (
            "client settings schema",
            descriptor.client_settings.schema_id.as_str(),
        ),
        (
            "client settings codec",
            descriptor.client_settings.codec_id.as_str(),
        ),
        (
            "server settings schema",
            descriptor.server_settings.schema_id.as_str(),
        ),
        (
            "server settings codec",
            descriptor.server_settings.codec_id.as_str(),
        ),
        ("resolver", descriptor.resolver.id.as_str()),
    ] {
        validate_descriptor_identity(field, value)?;
    }
    if let Some(method) = &descriptor.method {
        validate_extension_method(method)?;
    }
    if let Some(notification) = &descriptor.notification {
        validate_member_name("notification", &notification.name)?;
        if core_or_legacy_method(&notification.name) {
            return Err(ExtensionRegistryError::CoreNotificationCollision(
                notification.name.clone(),
            ));
        }
        if descriptor
            .method
            .as_ref()
            .is_some_and(|method| method.name == notification.name)
        {
            return Err(ExtensionRegistryError::LocalOwnershipCollision {
                field: "method/notification",
                value: notification.name.clone(),
            });
        }
    }
    if let Some(discriminator) = &descriptor.result_discriminator {
        validate_member_name("result discriminator", discriminator)?;
        if matches!(discriminator.as_str(), "complete" | "input_required") {
            return Err(ExtensionRegistryError::CoreResultDiscriminatorCollision(
                discriminator.clone(),
            ));
        }
    }
    if descriptor.routing_headers.len() > MAX_EXTENSION_ROUTING_HEADERS {
        return Err(ExtensionRegistryError::LocalOwnershipCollision {
            field: "routing headers",
            value: descriptor.id.to_string(),
        });
    }
    for (index, header) in descriptor.routing_headers.iter().enumerate() {
        if header.name.is_empty() {
            return Err(ExtensionRegistryError::MissingOwner("routing header"));
        }
        if header.name.len() > MAX_EXTENSION_ROUTING_HEADER_BYTES {
            return Err(ExtensionRegistryError::MemberNameTooLong {
                field: "routing header",
                value: header.name.clone(),
            });
        }
        if descriptor.routing_headers[..index]
            .iter()
            .any(|prior| prior.name.eq_ignore_ascii_case(&header.name))
        {
            return Err(ExtensionRegistryError::LocalOwnershipCollision {
                field: "routing header",
                value: header.name.clone(),
            });
        }
    }
    if let Some(correlation) = &descriptor.stdio_correlation {
        if correlation.metadata_key.is_empty() {
            return Err(ExtensionRegistryError::MissingOwner("stdio correlation"));
        }
        ExtensionId::parse(correlation.metadata_key.clone())?;
        if correlation.methods.is_empty()
            || correlation.methods.len() > MAX_STDIO_CORRELATION_METHODS
        {
            return Err(ExtensionRegistryError::MissingOwner("stdio correlation"));
        }
        let Some(notification) = &descriptor.notification else {
            return Err(ExtensionRegistryError::MissingOwner("stdio notification"));
        };
        if notification.direction != correlation.direction
            || !correlation
                .methods
                .iter()
                .any(|method| method == &notification.name)
        {
            return Err(ExtensionRegistryError::LocalOwnershipCollision {
                field: "stdio correlation notification",
                value: correlation.metadata_key.clone(),
            });
        }
        for (index, method) in correlation.methods.iter().enumerate() {
            validate_member_name("stdio correlation method", method)?;
            if correlation.methods[..index].contains(method) {
                return Err(ExtensionRegistryError::LocalOwnershipCollision {
                    field: "stdio correlation method",
                    value: method.clone(),
                });
            }
        }
    }
    Ok(())
}

fn validate_extension_method(
    method: &ExtensionMethodDescriptor,
) -> Result<(), ExtensionRegistryError> {
    validate_member_name("method", &method.name)?;
    if core_or_legacy_method(&method.name) {
        return Err(ExtensionRegistryError::CoreMethodCollision(
            method.name.clone(),
        ));
    }
    if method.direction == ExtensionDirection::ClientToServer {
        if method.http_era_disposition.is_none() {
            return Err(ExtensionRegistryError::MissingHttpEraDisposition(
                method.name.clone(),
            ));
        }
        if method.legacy_fallback {
            return Err(ExtensionRegistryError::LegacyFallbackContradiction(
                method.name.clone(),
            ));
        }
    } else if method.http_era_disposition.is_some() {
        return Err(ExtensionRegistryError::MissingHttpEraDisposition(
            method.name.clone(),
        ));
    } else if method.legacy_fallback {
        return Err(ExtensionRegistryError::LegacyFallbackContradiction(
            method.name.clone(),
        ));
    }
    Ok(())
}

fn validate_descriptor_identity(
    field: &'static str,
    value: &str,
) -> Result<(), ExtensionRegistryError> {
    if value.is_empty() {
        return Err(ExtensionRegistryError::MissingOwner(field));
    }
    if value.len() > MAX_EXTENSION_MEMBER_NAME_BYTES {
        return Err(ExtensionRegistryError::MemberNameTooLong {
            field,
            value: value.to_owned(),
        });
    }
    Ok(())
}

fn validate_member_name(field: &'static str, value: &str) -> Result<(), ExtensionRegistryError> {
    if value.is_empty() {
        return Err(ExtensionRegistryError::MissingOwner(field));
    }
    if value.len() > MAX_EXTENSION_MEMBER_NAME_BYTES {
        return Err(ExtensionRegistryError::MemberNameTooLong {
            field,
            value: value.to_owned(),
        });
    }
    Ok(())
}

fn ensure_no_cross_owner_collision(
    left: &ExtensionDescriptor,
    right: &ExtensionDescriptor,
) -> Result<(), ExtensionRegistryError> {
    let collision = |field: &'static str, left: Option<&str>, right: Option<&str>| {
        (left.zip(right).filter(|(a, b)| a == b)).map(|(value, _)| {
            ExtensionRegistryError::OwnershipCollision {
                field,
                value: value.to_owned(),
            }
        })
    };
    if let Some(error) = collision(
        "method",
        left.method.as_ref().map(|m| m.name.as_str()),
        right.method.as_ref().map(|m| m.name.as_str()),
    ) {
        return Err(error);
    }
    if let Some(error) = collision(
        "notification",
        left.notification.as_ref().map(|n| n.name.as_str()),
        right.notification.as_ref().map(|n| n.name.as_str()),
    ) {
        return Err(error);
    }
    if let Some(error) = collision(
        "method/notification",
        left.method.as_ref().map(|m| m.name.as_str()),
        right.notification.as_ref().map(|n| n.name.as_str()),
    ) {
        return Err(error);
    }
    if let Some(error) = collision(
        "method/notification",
        left.notification.as_ref().map(|n| n.name.as_str()),
        right.method.as_ref().map(|m| m.name.as_str()),
    ) {
        return Err(error);
    }
    if let Some(error) = collision(
        "result discriminator",
        left.result_discriminator.as_deref(),
        right.result_discriminator.as_deref(),
    ) {
        return Err(error);
    }
    for lhs in &left.routing_headers {
        for rhs in &right.routing_headers {
            if lhs.name.eq_ignore_ascii_case(&rhs.name) {
                return Err(ExtensionRegistryError::OwnershipCollision {
                    field: "routing header",
                    value: lhs.name.clone(),
                });
            }
        }
    }
    if let (Some(lhs), Some(rhs)) = (&left.stdio_correlation, &right.stdio_correlation) {
        if lhs.metadata_key == rhs.metadata_key {
            return Err(ExtensionRegistryError::OwnershipCollision {
                field: "metadata key",
                value: lhs.metadata_key.clone(),
            });
        }
        for method in &lhs.methods {
            if rhs.methods.contains(method) && lhs.direction == rhs.direction {
                return Err(ExtensionRegistryError::OwnershipCollision {
                    field: "stdio correlation method",
                    value: method.clone(),
                });
            }
        }
    }
    Ok(())
}

fn core_or_legacy_method(method: &str) -> bool {
    final_2026_07_28_method(method).is_some() || legacy_2024_11_05_method(method).is_some()
}

fn canonical_descriptor_row(
    descriptor: &ExtensionDescriptor,
    additional_methods: Option<&BTreeMap<String, ExtensionMethodDescriptor>>,
) -> Value {
    if additional_methods.is_none_or(|methods| methods.is_empty()) {
        return serde_json::json!({
            "id": descriptor.id.as_str(),
            "clientSchema": descriptor.client_settings.schema_id,
            "clientCodec": descriptor.client_settings.codec_id,
            "serverSchema": descriptor.server_settings.schema_id,
            "serverCodec": descriptor.server_settings.codec_id,
            "resolver": [descriptor.resolver.id, descriptor.resolver.version, format!("{:?}", descriptor.resolver.fallback)],
            "method": descriptor.method.as_ref().map(|m| (&m.name, format!("{:?}", m.direction), m.http_era_disposition.map(|e| format!("{:?}", e)), m.legacy_fallback)),
            "notification": descriptor.notification.as_ref().map(|n| (&n.name, format!("{:?}", n.direction))),
            "resultDiscriminator": descriptor.result_discriminator,
            "routingHeaders": descriptor.routing_headers.iter().map(|h| &h.name).collect::<Vec<_>>(),
            "stdio": descriptor.stdio_correlation.as_ref().map(|s| (&s.metadata_key, &s.methods, format!("{:?}", s.direction))),
        });
    }
    let mut methods = descriptor
        .method
        .iter()
        .chain(
            additional_methods
                .into_iter()
                .flat_map(|methods| methods.values()),
        )
        .map(|method| {
            (
                method.name.clone(),
                format!("{:?}", method.direction),
                method
                    .http_era_disposition
                    .map(|disposition| format!("{disposition:?}")),
                method.legacy_fallback,
            )
        })
        .collect::<Vec<_>>();
    methods.sort_by(|left, right| left.0.cmp(&right.0));
    serde_json::json!({
        "id": descriptor.id.as_str(),
        "clientSchema": descriptor.client_settings.schema_id,
        "clientCodec": descriptor.client_settings.codec_id,
        "serverSchema": descriptor.server_settings.schema_id,
        "serverCodec": descriptor.server_settings.codec_id,
        "resolver": [descriptor.resolver.id, descriptor.resolver.version, format!("{:?}", descriptor.resolver.fallback)],
        "methods": methods,
        "notification": descriptor.notification.as_ref().map(|n| (&n.name, format!("{:?}", n.direction))),
        "resultDiscriminator": descriptor.result_discriminator,
        "routingHeaders": descriptor.routing_headers.iter().map(|h| &h.name).collect::<Vec<_>>(),
        "stdio": descriptor.stdio_correlation.as_ref().map(|s| (&s.metadata_key, &s.methods, format!("{:?}", s.direction))),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn descriptor(
        id: ExtensionId,
        method: &str,
        notification: &str,
        result_discriminator: &str,
    ) -> ExtensionDescriptor {
        ExtensionDescriptor {
            id,
            client_settings: ExtensionSettingsSchema {
                schema_id: "client-weather-v1".to_owned(),
                codec_id: "client-weather-codec-v1".to_owned(),
            },
            server_settings: ExtensionSettingsSchema {
                schema_id: "server-weather-v1".to_owned(),
                codec_id: "server-weather-codec-v1".to_owned(),
            },
            resolver: ExtensionNegotiationResolver {
                id: "weather-compatibility-v1".to_owned(),
                version: 1,
                fallback: ExtensionFallbackPolicy::RejectOneSided,
            },
            method: Some(ExtensionMethodDescriptor {
                name: method.to_owned(),
                direction: ExtensionDirection::ClientToServer,
                http_era_disposition: Some(ExtensionHttpEraDisposition::ModernExclusive),
                legacy_fallback: false,
            }),
            notification: Some(ExtensionNotificationDescriptor {
                name: notification.to_owned(),
                direction: ExtensionDirection::ServerToClient,
            }),
            result_discriminator: Some(result_discriminator.to_owned()),
            // Routing headers are exclusively owned across the registry, so
            // each helper-built extension derives a distinct header from its
            // method; a shared literal would collide on the second register.
            routing_headers: vec![ExtensionRoutingHeaderDescriptor {
                name: format!("Mcp-{}", method.rsplit('/').next().unwrap_or("weather")),
            }],
            stdio_correlation: None,
        }
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn ext_03_final_extension_identifier_wire_grammar_one_variable_negative() {
        let official = official_tasks_extension_id();
        assert_eq!(official.as_str(), OFFICIAL_TASKS_EXTENSION_ID);
        assert!(ExtensionId::parse("Example/tasks").is_ok());

        assert_eq!(
            ExtensionId::parse(format!("{OFFICIAL_TASKS_EXTENSION_ID}_")),
            Err(ExtensionRegistryError::InvalidIdentifier(
                "io.modelcontextprotocol/tasks_".to_owned()
            )),
            "only the terminal non-alphanumeric name byte changes from the admitted official key"
        );
    }

    #[test]
    fn apps_01_official_descriptor_negotiation_round_trip_positive() {
        let client_wire = json!({
            "mimeTypes": [
                MCP_APPS_HTML_MIME_TYPE,
                "application/vnd.example.dashboard+json",
                MCP_APPS_HTML_MIME_TYPE,
            ],
        });
        let client_settings = ExtensionSettings::new(client_wire.clone())
            .expect("the ordered, duplicated MCP Apps MIME advertisement is generic JSON");
        let decoded = McpAppsClientSettings::from_extension_settings(&client_settings)
            .expect("the required closed client settings object decodes");
        assert_eq!(
            decoded.to_extension_settings().into_value(),
            client_wire,
            "the typed MCP Apps codec preserves peer MIME ordering and duplicates"
        );
        assert!(decoded.supports_mcp_apps_html());

        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_mcp_apps_extension(&mut registry)
            .expect("the official MCP Apps descriptor registers");
        let descriptor = registry
            .descriptor(&id)
            .expect("registered MCP Apps descriptor remains available before freeze");
        assert_eq!(descriptor.id.as_str(), OFFICIAL_MCP_APPS_EXTENSION_ID);
        assert_eq!(
            descriptor.client_settings.schema_id,
            MCP_APPS_CLIENT_SETTINGS_SCHEMA_ID
        );
        assert_eq!(
            descriptor.server_settings.schema_id,
            MCP_APPS_SERVER_SETTINGS_SCHEMA_ID
        );
        assert_eq!(descriptor.resolver.id, MCP_APPS_NEGOTIATION_RESOLVER_ID);
        assert_eq!(
            descriptor.resolver.version,
            MCP_APPS_NEGOTIATION_RESOLVER_VERSION
        );
        assert_eq!(
            descriptor.resolver.fallback,
            ExtensionFallbackPolicy::InactiveOnEitherPeer
        );
        assert!(descriptor.method.is_none());
        assert!(descriptor.notification.is_none());
        assert_eq!(
            validate_official_mcp_apps_descriptor(descriptor),
            Ok(()),
            "the public descriptor is the exact method-free Apps capability shape"
        );
        registry.freeze().expect("MCP Apps registry freezes");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), client_settings)]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_mcp_apps_empty_server_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver = official_mcp_apps_negotiation_resolver();
        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("the exact bilateral MCP Apps settings activate the descriptor");

        assert_eq!(
            MCP_APPS_ACTIVATION_PREDICATE_ID,
            "fastmcp-2026-07-28-apps-bilateral-mime-v1"
        );
        assert_eq!(
            negotiated
                .active(&id)
                .expect("the enabled bilateral MCP Apps descriptor is active")
                .effective_settings()
                .settings()
                .clone()
                .into_value(),
            client_wire,
            "negotiation retains the same validated client settings object"
        );
    }

    #[test]
    fn apps_01_descriptor_rejects_one_method_plant_without_registration_mutation() {
        let accepted = official_mcp_apps_descriptor();
        let mut planted = accepted.clone();
        planted.method = Some(ExtensionMethodDescriptor {
            name: MCP_APPS_INITIALIZE_METHOD.to_owned(),
            direction: ExtensionDirection::ClientToServer,
            http_era_disposition: Some(ExtensionHttpEraDisposition::ModernExclusive),
            legacy_fallback: false,
        });

        assert_eq!(
            validate_official_mcp_apps_descriptor(&planted),
            Err(ExtensionRegistryError::OfficialMcpAppsDescriptorMismatch),
            "only adding a client/server method rejects the Host/View-only Apps descriptor"
        );
        let mut registry = ExtensionDescriptorRegistry::new();
        assert_eq!(
            registry.register(planted),
            Err(ExtensionRegistryError::OfficialMcpAppsDescriptorMismatch),
            "the registry must reject the same descriptor mutation before ownership changes"
        );
        assert_eq!(registry.descriptors().len(), 0);
        assert_eq!(
            validate_official_mcp_apps_descriptor(&accepted),
            Ok(()),
            "the rejected one-variable descriptor cannot mutate the admitted baseline"
        );
    }

    #[test]
    fn apps_01_server_marker_requires_the_exact_empty_object() {
        let accepted = official_mcp_apps_empty_server_settings();
        assert_eq!(
            validate_official_mcp_apps_server_settings(&accepted),
            Ok(()),
            "the official Apps server marker is exactly the empty object"
        );

        let rejected = ExtensionSettings::new(json!({ "unexpected": true }))
            .expect("the one-field alternate is still generic extension settings");
        assert_eq!(
            validate_official_mcp_apps_server_settings(&rejected),
            Err(ExtensionRegistryError::OfficialMcpAppsServerSettingsNotEmpty),
            "adding one server setting makes the official Apps marker invalid"
        );
        assert!(
            accepted.as_object().is_empty(),
            "rejecting the alternate cannot alter the admitted marker"
        );
    }

    #[test]
    fn apps_01_typed_mime_settings_cannot_bypass_generic_value_bound() {
        let oversized = vec!["x".repeat(MAX_MCP_APPS_MIME_TYPE_BYTES); MAX_MCP_APPS_MIME_TYPES];

        assert_eq!(
            McpAppsClientSettings::new(oversized),
            Err(ExtensionRegistryError::SettingsTooLarge),
            "the typed Apps constructor must enforce the generic per-value discovery bound"
        );
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn apps_01_typed_resolver_negotiates_apps_and_tasks_together() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let tasks = register_official_tasks_extension(&mut registry)
            .expect("the official Tasks descriptor registers");
        let apps = register_official_mcp_apps_extension(&mut registry)
            .expect("the official MCP Apps descriptor registers");
        registry.freeze().expect("official descriptors freeze");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([
                (tasks.clone(), official_tasks_empty_settings()),
                (
                    apps.clone(),
                    ExtensionSettings::new(json!({"mimeTypes": [MCP_APPS_HTML_MIME_TYPE]}))
                        .expect("bounded Apps client settings"),
                ),
            ]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([
                (tasks.clone(), official_tasks_empty_settings()),
                (apps.clone(), official_mcp_apps_empty_server_settings()),
            ]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(tasks.clone());
        local.enable(apps.clone());
        let mut resolver = official_mcp_apps_negotiation_resolver();

        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("the supplied resolver supports the official descriptor set");

        assert!(negotiated.active(&tasks).is_some());
        assert!(negotiated.active(&apps).is_some());
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn apps_01_tasks_wrapper_preserves_apps_inactive_disposition() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let tasks = register_official_tasks_extension(&mut registry)
            .expect("the official Tasks descriptor registers");
        let apps = register_official_mcp_apps_extension(&mut registry)
            .expect("the official MCP Apps descriptor registers");
        registry.freeze().expect("official descriptors freeze");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([
                (tasks.clone(), official_tasks_empty_settings()),
                (
                    apps.clone(),
                    McpAppsClientSettings::new(vec!["text/plain".to_owned()])
                        .expect("another bounded MIME type is valid Apps settings")
                        .to_extension_settings(),
                ),
            ]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([
                (tasks.clone(), official_tasks_empty_settings()),
                (apps.clone(), official_mcp_apps_empty_server_settings()),
            ]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(tasks.clone());
        local.enable(apps.clone());
        let mut resolver =
            TasksNegotiationResolver::with_fallback(official_mcp_apps_negotiation_resolver());

        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("inactive Apps does not reject a composed Tasks resolver");

        assert!(negotiated.active(&tasks).is_some());
        assert_eq!(
            negotiated.inactive_reason(&apps),
            Some(ExtensionInactiveReason::SettingsInactiveFallback)
        );
    }

    #[test]
    fn apps_01_other_valid_mime_type_selects_inactive_fallback() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_mcp_apps_extension(&mut registry)
            .expect("the official MCP Apps descriptor registers");
        registry.freeze().expect("MCP Apps registry freezes");
        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(
                id.clone(),
                ExtensionSettings::new(json!({"mimeTypes": ["text/plain"]}))
                    .expect("a closed client settings object with another MIME type is valid"),
            )]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_mcp_apps_empty_server_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver = official_mcp_apps_negotiation_resolver();

        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("valid client settings without the Apps HTML MIME choose fallback");

        assert!(negotiated.active(&id).is_none());
        assert_eq!(
            negotiated.inactive_reason(&id),
            Some(ExtensionInactiveReason::SettingsInactiveFallback)
        );
    }

    #[test]
    fn apps_01_official_descriptor_legacy_era_one_field_negative() {
        let client_wire = json!({"mimeTypes": [MCP_APPS_HTML_MIME_TYPE]});
        let client_settings =
            ExtensionSettings::new(client_wire.clone()).expect("valid MCP Apps client settings");
        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_mcp_apps_extension(&mut registry)
            .expect("the official MCP Apps descriptor registers");
        registry.freeze().expect("MCP Apps registry freezes");
        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), client_settings)]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_mcp_apps_empty_server_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let resolver_calls = std::cell::Cell::new(0);
        let mut resolver = |descriptor: &ExtensionDescriptor,
                            client: &ExtensionSettings,
                            server: &ExtensionSettings| {
            resolver_calls.set(resolver_calls.get() + 1);
            match resolve_official_mcp_apps_settings(descriptor, client, server)? {
                ExtensionSettingsResolution::Active(settings) => Ok(settings),
                ExtensionSettingsResolution::Inactive => {
                    Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
                        descriptor.id.to_string(),
                    ))
                }
            }
        };

        registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("the modern baseline activates MCP Apps");
        assert_eq!(resolver_calls.get(), 1);

        assert_eq!(
            registry.negotiate(
                ProtocolEra::Legacy2024,
                &local,
                &client,
                &server,
                &mut resolver,
            ),
            Err(ExtensionNegotiationError::LegacyProtocolExcluded),
            "changing only the protocol era rejects MCP Apps before resolver execution"
        );
        assert_eq!(resolver_calls.get(), 1);
        assert_eq!(
            client.extensions[&id].clone().into_value(),
            client_wire,
            "the rejected legacy-era negotiation cannot mutate the accepted modern wire"
        );
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn task_01_official_tasks_public_registry_positive() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_tasks_extension(&mut registry)
            .expect("the public official Tasks surface registers atomically");
        let descriptor = registry
            .descriptor(&id)
            .expect("the public Tasks registration retains its descriptor");
        assert_eq!(descriptor.id.as_str(), OFFICIAL_TASKS_EXTENSION_ID);
        assert_eq!(
            descriptor.client_settings.schema_id,
            OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID
        );
        assert_eq!(
            descriptor.server_settings.schema_id,
            OFFICIAL_TASKS_EMPTY_SETTINGS_SCHEMA_ID
        );
        assert_eq!(
            descriptor
                .method
                .as_ref()
                .map(|method| method.name.as_str()),
            Some(OFFICIAL_TASKS_METHODS[0])
        );
        assert_eq!(
            descriptor.result_discriminator.as_deref(),
            Some(OFFICIAL_TASKS_RESULT_DISCRIMINATOR)
        );
        registry.freeze().expect("Tasks registry freezes");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver =
            |_descriptor: &ExtensionDescriptor,
             _client: &ExtensionSettings,
             _server: &ExtensionSettings| { Ok(official_tasks_empty_settings()) };

        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("current client and server capabilities negotiate Tasks");
        for method in OFFICIAL_TASKS_METHODS {
            assert_eq!(
                negotiated
                    .admit_method(
                        &registry,
                        ProtocolEra::Modern2026,
                        &id,
                        method,
                        ExtensionDirection::ClientToServer,
                    )
                    .expect("registered Tasks request is admitted")
                    .id,
                id
            );
        }
        for method in ["tasks/list", "tasks/submit"] {
            assert_eq!(
                negotiated.admit_method(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    method,
                    ExtensionDirection::ClientToServer,
                ),
                Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                    capability: id.to_string(),
                    field: "method",
                    value: method.to_owned(),
                }),
                "the official Tasks registration owns no additional request methods"
            );
        }
        assert_eq!(
            negotiated
                .admit_notification(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    OFFICIAL_TASKS_NOTIFICATION,
                    ExtensionDirection::ServerToClient,
                )
                .expect("registered Tasks notification is admitted")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_result_discriminator(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    OFFICIAL_TASKS_RESULT_DISCRIMINATOR,
                )
                .expect("official Tasks tools/call result discriminator is admitted")
                .id,
            id
        );
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn task_01_official_tasks_undeclared_result_discriminator_one_variable_negative() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_tasks_extension(&mut registry)
            .expect("the public official Tasks surface registers");
        registry.freeze().expect("Tasks registry freezes");
        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver =
            |_descriptor: &ExtensionDescriptor,
             _client: &ExtensionSettings,
             _server: &ExtensionSettings| { Ok(official_tasks_empty_settings()) };
        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("current client and server capabilities negotiate Tasks");

        let wrong_discriminator = "task-other";
        assert_eq!(
            negotiated.admit_result_discriminator(
                &registry,
                ProtocolEra::Modern2026,
                &id,
                wrong_discriminator,
            ),
            Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: id.to_string(),
                field: "result discriminator",
                value: wrong_discriminator.to_owned(),
            }),
            "only the undeclared result discriminator differs from the admitted task value"
        );
    }

    #[test]
    #[cfg(feature = "tasks")]
    fn task_01_official_tasks_nonempty_client_settings_one_variable_negative() {
        let mut registry = ExtensionDescriptorRegistry::new();
        let id = register_official_tasks_extension(&mut registry)
            .expect("the public official Tasks surface registers");
        let receipt = registry.freeze().expect("Tasks registry freezes");
        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), official_tasks_empty_settings())]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let resolver_calls = std::cell::Cell::new(0);
        let mut resolver = |_descriptor: &ExtensionDescriptor,
                            _client: &ExtensionSettings,
                            _server: &ExtensionSettings| {
            resolver_calls.set(resolver_calls.get() + 1);
            Ok(official_tasks_empty_settings())
        };

        registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("the empty-settings baseline negotiates Tasks");
        assert_eq!(resolver_calls.get(), 1);

        let mut planted_client = client.clone();
        planted_client.extensions.insert(
            id.clone(),
            ExtensionSettings::new(json!({"unexpected": true}))
                .expect("the one-field mutation is generic extension JSON"),
        );

        assert_eq!(
            registry.negotiate(
                ProtocolEra::Modern2026,
                &local,
                &planted_client,
                &server,
                &mut resolver,
            ),
            Err(ExtensionNegotiationError::SettingsCompatibilityRejected(
                id.to_string()
            )),
            "only adding one client settings field rejects the exact empty Tasks settings"
        );
        assert_eq!(
            resolver_calls.get(),
            1,
            "rejected admission cannot invoke the resolver"
        );
        assert_eq!(registry.receipt(), Some(&receipt));
    }

    #[test]
    fn ext_03_final_core_method_collision_one_variable_negative() {
        let baseline = descriptor(
            ExtensionId::parse("com.example/discover").expect("valid extension ID"),
            "com.example/discover",
            "com.example/discover_changed",
            "com.example/discover_result",
        );
        assert!(validate_descriptor(&baseline).is_ok());

        let mut planted = baseline.clone();
        planted
            .method
            .as_mut()
            .expect("baseline owns an extension method")
            .name = crate::methods::SERVER_DISCOVER.to_owned();
        assert_eq!(
            validate_descriptor(&planted),
            Err(ExtensionRegistryError::CoreMethodCollision(
                crate::methods::SERVER_DISCOVER.to_owned()
            )),
            "only replacing the extension method with final server/discover makes it invalid"
        );
    }

    #[test]
    fn ext_01_unit_bilateral_negotiation_and_directional_dispatch_positive() {
        let id = ExtensionId::parse("com.example/weather").expect("valid extension ID");
        let mut registry = ExtensionDescriptorRegistry::new();
        registry
            .register(descriptor(
                id.clone(),
                "com.example/weather",
                "com.example/weather_changed",
                "com.example/weather_result",
            ))
            .expect("descriptor registers");
        registry
            .freeze()
            .expect("registry freezes before negotiation");

        let client_settings = ExtensionSettings::new(json!({
            "unit": "celsius",
            "preserved": [null, 1.5, {"nested": true}],
        }))
        .expect("current-message client settings are bounded JSON");
        let server_settings = ExtensionSettings::new(json!({"maxCities": 4}))
            .expect("server discovery settings are bounded JSON");
        let unknown_id = ExtensionId::parse("org.example/diagnostic")
            .expect("unknown but structurally valid ID");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([
                (id.clone(), client_settings),
                (
                    unknown_id.clone(),
                    ExtensionSettings::new(json!({"opaque": null}))
                        .expect("bounded unknown settings"),
                ),
            ]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(id.clone(), server_settings)]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver = |descriptor: &ExtensionDescriptor,
                            client: &ExtensionSettings,
                            server: &ExtensionSettings| {
            assert_eq!(descriptor.resolver.id, "weather-compatibility-v1");
            ExtensionSettings::new(json!({
                "unit": client.as_object()["unit"].clone(),
                "maxCities": server.as_object()["maxCities"].clone(),
            }))
            .map_err(|_| {
                ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string())
            })
        };
        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("bilateral current-message settings negotiate");

        assert_eq!(negotiated.protocol_era(), ProtocolEra::Modern2026);
        assert_eq!(negotiated.active_extensions().len(), 1);
        assert_eq!(
            negotiated
                .active(&id)
                .expect("registered bilateral extension is active")
                .effective_settings()
                .settings()
                .as_object()["unit"],
            json!("celsius")
        );
        assert_eq!(
            negotiated.unknown_client_extensions()[&unknown_id].as_object()["opaque"],
            Value::Null,
            "unknown peer data remains diagnostic and cannot activate dispatch"
        );
        assert_eq!(
            negotiated
                .admit_capability(&registry, ProtocolEra::Modern2026, &id)
                .expect("developer-opted-in bilateral capability is active")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_method(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    "com.example/weather",
                    ExtensionDirection::ClientToServer,
                )
                .expect("active method dispatch")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_notification(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    "com.example/weather_changed",
                    ExtensionDirection::ServerToClient,
                )
                .expect("active notification dispatch")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_result_discriminator(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    "com.example/weather_result",
                )
                .expect("active result discriminator dispatch")
                .id,
            id
        );
        assert_eq!(
            negotiated.admit_notification(
                &registry,
                ProtocolEra::Modern2026,
                &id,
                "com.example/weather_changed",
                ExtensionDirection::ClientToServer,
            ),
            Err(ExtensionDispatchError::DirectionMismatch {
                field: "notification",
                value: "com.example/weather_changed".to_owned(),
                expected: ExtensionDirection::ClientToServer,
                actual: ExtensionDirection::ServerToClient,
            }),
            "only the requested direction changes; the same active descriptor must not dispatch"
        );
    }

    fn negotiated_weather_extension() -> (
        ExtensionDescriptorRegistry,
        ExtensionId,
        NegotiatedExtensionSet,
    ) {
        let id = ExtensionId::parse("com.example/weather").expect("valid extension ID");
        let mut registry = ExtensionDescriptorRegistry::new();
        registry
            .register(descriptor(
                id.clone(),
                "com.example/weather",
                "com.example/weather_changed",
                "com.example/weather_result",
            ))
            .expect("descriptor registers");
        registry
            .freeze()
            .expect("registry freezes before negotiation");

        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(
                id.clone(),
                ExtensionSettings::new(json!({"unit": "celsius"}))
                    .expect("bounded client settings"),
            )]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(
                id.clone(),
                ExtensionSettings::new(json!({"maxCities": 4})).expect("bounded server settings"),
            )]),
        };
        let mut local = ExtensionLocalEnablement::default();
        local.enable(id.clone());
        let mut resolver = |descriptor: &ExtensionDescriptor,
                            client: &ExtensionSettings,
                            server: &ExtensionSettings| {
            ExtensionSettings::new(json!({
                "unit": client.as_object()["unit"].clone(),
                "maxCities": server.as_object()["maxCities"].clone(),
            }))
            .map_err(|_| {
                ExtensionNegotiationError::SettingsCompatibilityRejected(descriptor.id.to_string())
            })
        };
        let negotiated = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("developer opt-in and both peer settings negotiate");

        (registry, id, negotiated)
    }

    #[test]
    fn ext_02_executable_request_admission_positive() {
        let (registry, id, negotiated) = negotiated_weather_extension();

        assert_eq!(negotiated.protocol_era(), ProtocolEra::Modern2026);
        assert_eq!(negotiated.active_extensions().len(), 1);
        assert_eq!(
            negotiated
                .admit_capability(&registry, ProtocolEra::Modern2026, &id)
                .expect("active extension capability is admitted per request")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_method(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    "com.example/weather",
                    ExtensionDirection::ClientToServer,
                )
                .expect("active extension method is admitted per request")
                .id,
            id
        );
        assert_eq!(
            negotiated
                .admit_result_discriminator(
                    &registry,
                    ProtocolEra::Modern2026,
                    &id,
                    "com.example/weather_result",
                )
                .expect("active extension result discriminator is admitted per request")
                .id,
            id
        );
    }

    #[test]
    fn ext_02_executable_request_admission_one_variable_negatives() {
        let (registry, id, negotiated) = negotiated_weather_extension();
        let active_count = negotiated.active_extensions().len();

        assert_eq!(
            negotiated.admit_capability(&registry, ProtocolEra::Legacy2024, &id),
            Err(ExtensionDispatchError::LegacyProtocolExcluded),
            "changing only the request era must exclude exact legacy admission"
        );
        assert_eq!(
            negotiated.admit_method(
                &registry,
                ProtocolEra::Modern2026,
                &id,
                "com.example/weather-other",
                ExtensionDirection::ClientToServer,
            ),
            Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: id.to_string(),
                field: "method",
                value: "com.example/weather-other".to_owned(),
            }),
            "changing only the method spelling must reject dispatch"
        );
        assert_eq!(
            negotiated.admit_result_discriminator(
                &registry,
                ProtocolEra::Modern2026,
                &id,
                "com.example/weather_result-other",
            ),
            Err(ExtensionDispatchError::CapabilityDoesNotOwn {
                capability: id.to_string(),
                field: "result discriminator",
                value: "com.example/weather_result-other".to_owned(),
            }),
            "changing only the result discriminator must reject dispatch"
        );
        assert_eq!(
            negotiated.active_extensions().len(),
            active_count,
            "rejected requests cannot mutate the bounded negotiated state"
        );
    }

    #[test]
    fn ext_02_developer_opt_in_and_legacy_negotiation_fail_closed() {
        let id = ExtensionId::parse("com.example/weather").expect("valid extension ID");
        let mut registry = ExtensionDescriptorRegistry::new();
        registry
            .register(descriptor(
                id.clone(),
                "com.example/weather",
                "com.example/weather_changed",
                "com.example/weather_result",
            ))
            .expect("descriptor registers");
        let receipt = registry
            .freeze()
            .expect("registry freezes before negotiation");
        let client = ClientExtensionDiscovery {
            extensions: BTreeMap::from([(
                id.clone(),
                ExtensionSettings::new(json!({})).expect("bounded client settings"),
            )]),
        };
        let server = ServerExtensionDiscovery {
            extensions: BTreeMap::from([(
                id.clone(),
                ExtensionSettings::new(json!({})).expect("bounded server settings"),
            )]),
        };
        let local = ExtensionLocalEnablement::default();
        let resolver_calls = std::cell::Cell::new(0);
        let mut resolver = |_descriptor: &ExtensionDescriptor,
                            _client: &ExtensionSettings,
                            _server: &ExtensionSettings| {
            resolver_calls.set(resolver_calls.get() + 1);
            Ok(ExtensionSettings::new(json!({})).expect("bounded effective settings"))
        };

        let unopted = registry
            .negotiate(
                ProtocolEra::Modern2026,
                &local,
                &client,
                &server,
                &mut resolver,
            )
            .expect("registered descriptors remain inactive without developer opt-in");
        assert_eq!(resolver_calls.get(), 0);
        assert_eq!(
            unopted.inactive_reason(&id),
            Some(ExtensionInactiveReason::LocallyDisabled)
        );
        assert_eq!(
            unopted.admit_capability(&registry, ProtocolEra::Modern2026, &id),
            Err(ExtensionDispatchError::InactiveCapability(id.to_string()))
        );

        assert_eq!(
            registry.negotiate(
                ProtocolEra::Legacy2024,
                &local,
                &client,
                &server,
                &mut resolver,
            ),
            Err(ExtensionNegotiationError::LegacyProtocolExcluded),
            "changing only the negotiation era must reject exact legacy before resolver execution"
        );
        assert_eq!(resolver_calls.get(), 0);
        assert_eq!(registry.receipt(), Some(&receipt));
    }

    #[test]
    fn ext_02_oversized_discovery_is_rejected_before_bounded_state_allocation() {
        let mut registry = ExtensionDescriptorRegistry::new();
        registry.freeze().expect("empty registry freezes");
        let settings = ExtensionSettings::new(json!({})).expect("bounded settings");
        let client = ClientExtensionDiscovery {
            extensions: (0..=MAX_EXTENSION_DESCRIPTORS)
                .map(|index| {
                    (
                        ExtensionId::parse(format!("com.example/diagnostic-{index}"))
                            .expect("bounded synthetic identifier"),
                        settings.clone(),
                    )
                })
                .collect(),
        };
        let mut resolver_called = false;
        let mut resolver = |_descriptor: &ExtensionDescriptor,
                            _client: &ExtensionSettings,
                            _server: &ExtensionSettings| {
            resolver_called = true;
            Ok(ExtensionSettings::new(json!({})).expect("bounded effective settings"))
        };

        assert_eq!(
            registry.negotiate(
                ProtocolEra::Modern2026,
                &ExtensionLocalEnablement::default(),
                &client,
                &ServerExtensionDiscovery::default(),
                &mut resolver,
            ),
            Err(ExtensionNegotiationError::DiscoveryTooManyExtensions(
                ExtensionPeer::Client
            ))
        );
        assert!(!resolver_called);
    }

    #[test]
    fn ext_01_unit_one_variable_collision_negative() {
        let first_id = ExtensionId::parse("com.example/first").expect("first ID");
        let second_id = ExtensionId::parse("com.example/second").expect("second ID");
        let first = descriptor(
            first_id,
            "com.example/first",
            "com.example/first_changed",
            "com.example/first_result",
        );
        let candidate = descriptor(
            second_id,
            "com.example/second",
            "com.example/second_changed",
            "com.example/second_result",
        );
        let mut registry = ExtensionDescriptorRegistry::new();
        registry.register(first).expect("baseline owner registers");

        let mut non_colliding_baseline = registry.clone();
        non_colliding_baseline
            .register(candidate.clone())
            .expect("the unmodified candidate is a genuinely non-colliding extension");
        let baseline_count = registry.descriptors().len();

        let mut planted = candidate.clone();
        planted.result_discriminator = Some("com.example/first_result".to_owned());
        assert_eq!(
            registry.register(planted),
            Err(ExtensionRegistryError::OwnershipCollision {
                field: "result discriminator",
                value: "com.example/first_result".to_owned(),
            }),
            "the otherwise valid candidate differs in only the colliding discriminator"
        );
        assert_eq!(
            registry.descriptors().len(),
            baseline_count,
            "rejected registration cannot mutate the frozen dispatch owner set"
        );
    }

    #[test]
    fn ext_01_unit_one_level_over_settings_bound_is_rejected() {
        let mut accepted_value = Value::Null;
        for _ in 0..MAX_EXTENSION_SETTINGS_NESTING {
            accepted_value = Value::Array(vec![accepted_value]);
        }
        let accepted = ExtensionSettings::new(json!({"nested": accepted_value.clone()}))
            .expect("the exact nesting bound is admitted");

        let planted = Value::Array(vec![accepted_value.clone()]);
        assert_eq!(
            ExtensionSettings::new(json!({"nested": planted})),
            Err(ExtensionRegistryError::SettingsTooDeep),
            "only one additional nesting level changes the accepted settings object"
        );
        assert_eq!(
            accepted.as_object()["nested"],
            json!(accepted_value),
            "rejected settings cannot mutate the previously admitted object"
        );
    }
}