stateknot-integrations 0.1.0-alpha.1

Bounded protocol and model-provider adapters for StateKnot
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
// Copyright 2026 StateKnot contributors
// SPDX-License-Identifier: Apache-2.0

//! Immutable, schema-validating MCP 2026-07-28 Tool server application layer.
//!
//! The public API in this module is owned by `StateKnot`. The official MCP Rust
//! SDK remains a private wire adapter so an SDK release cannot silently become
//! the framework's domain model. Definitions and JSON Schema validators are
//! frozen at startup; request dispatch performs decoded authorization, bounded
//! input validation, handler execution, bounded output validation, and no
//! network schema retrieval.

use std::{borrow::Cow, collections::BTreeMap, fmt, sync::Arc, time::Duration};

use base64::{Engine as _, engine::general_purpose::STANDARD};
use jsonschema::Validator;
use rmcp::{
    ErrorData, RoleServer, ServerHandler,
    model::{
        CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, ClientCapabilities,
        ContentBlock, DiscoverResult, ElicitationCapability, Implementation, InputRequest,
        InputRequiredResult, ListToolsResult, PaginatedRequestParams, ProgressNotificationParam,
        ProtocolVersion, RequestStateCodec, RootsCapabilities, SamplingCapability, SealOptions,
        ServerCapabilities, ServerConfig, Tool, ToolAnnotations,
    },
    service::{Peer, RequestContext},
};
use serde_json::{Map, Value};
use stateknot_core::{BoxFuture, Digest};
use thiserror::Error;
use tokio_util::sync::CancellationToken;

use crate::{McpServerPrincipal, mcp_server_principal};

const JSON_SCHEMA_2020_12: &str = "https://json-schema.org/draft/2020-12/schema";
const MEBIBYTE: usize = 1024 * 1024;
const MAX_TOOL_NAME_BYTES: usize = 64;
const MAX_DISPLAY_BYTES: usize = 16 * 1024;
const MAX_SCOPE_COUNT: usize = 128;
const MAX_SCOPE_BYTES: usize = 256;
const MAX_CONTENT_BLOCK_BYTES: usize = 16 * MEBIBYTE;
const MAX_BINARY_CONTENT_BYTES: usize = 12 * MEBIBYTE;
const MAX_TEXT_CONTENT_BYTES: usize = 4 * MEBIBYTE;
const MAX_CONTENT_BLOCKS: usize = 256;
const MAX_STRUCTURED_CONTENT_BYTES: usize = 16 * MEBIBYTE;
const MAX_TOOL_RESULT_BYTES: usize = 32 * MEBIBYTE;
const MAX_INPUT_REQUEST_BYTES: usize = 2 * MEBIBYTE;
const MAX_INPUT_REQUESTS: usize = 128;
const MAX_INPUT_REQUEST_ID_BYTES: usize = 128;
const MAX_REQUEST_STATE_BYTES: usize = 128 * 1024;
const MAX_REQUEST_STATE_PAYLOAD_BYTES: usize = 64 * 1024;
const MAX_REQUEST_STATE_BINDING_BYTES: usize = 16 * 1024;
const MAX_REQUEST_STATE_TTL: Duration = Duration::from_secs(24 * 60 * 60);
const MAX_PROGRESS_MESSAGE_BYTES: usize = 2048;

/// MCP cache scope without exposing the wire SDK type.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum McpServerCacheScope {
    /// The catalog is identical across authorization contexts.
    Public,
    /// The catalog is scoped to the requesting principal.
    #[default]
    Private,
}

impl From<McpServerCacheScope> for CacheScope {
    fn from(value: McpServerCacheScope) -> Self {
        match value {
            McpServerCacheScope::Public => Self::Public,
            McpServerCacheScope::Private => Self::Private,
        }
    }
}

/// Non-authoritative MCP Tool annotations.
///
/// These values are hints for clients. `StateKnot` never uses them as policy or
/// side-effect evidence; authoritative policy belongs in the decoded
/// authorization boundary.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct McpServerToolAnnotations {
    title: Option<Box<str>>,
    read_only_hint: Option<bool>,
    destructive_hint: Option<bool>,
    idempotent_hint: Option<bool>,
    open_world_hint: Option<bool>,
}

impl McpServerToolAnnotations {
    /// Creates annotations with no hints.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            title: None,
            read_only_hint: None,
            destructive_hint: None,
            idempotent_hint: None,
            open_world_hint: None,
        }
    }

    /// Adds a bounded display title.
    pub fn with_title(
        mut self,
        title: impl Into<String>,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let title = title.into();
        validate_display(&title)?;
        self.title = Some(title.into_boxed_str());
        Ok(self)
    }

    /// Sets the protocol read-only hint.
    #[must_use]
    pub const fn with_read_only_hint(mut self, value: bool) -> Self {
        self.read_only_hint = Some(value);
        self
    }

    /// Sets the protocol destructive hint.
    #[must_use]
    pub const fn with_destructive_hint(mut self, value: bool) -> Self {
        self.destructive_hint = Some(value);
        self
    }

    /// Sets the protocol idempotent hint.
    #[must_use]
    pub const fn with_idempotent_hint(mut self, value: bool) -> Self {
        self.idempotent_hint = Some(value);
        self
    }

    /// Sets the protocol open-world hint.
    #[must_use]
    pub const fn with_open_world_hint(mut self, value: bool) -> Self {
        self.open_world_hint = Some(value);
        self
    }

    fn to_protocol(&self) -> ToolAnnotations {
        ToolAnnotations::from_raw(
            self.title.as_deref().map(str::to_owned),
            self.read_only_hint,
            self.destructive_hint,
            self.idempotent_hint,
            self.open_world_hint,
        )
    }
}

/// Immutable StateKnot-owned MCP Tool definition.
#[derive(Clone)]
pub struct McpServerToolDefinition {
    name: Arc<str>,
    title: Option<Arc<str>>,
    description: Option<Arc<str>>,
    input_schema: Arc<Value>,
    output_schema: Option<Arc<Value>>,
    annotations: Option<McpServerToolAnnotations>,
    required_scopes: Arc<[Box<str>]>,
    canonical_schema_bytes: usize,
}

impl McpServerToolDefinition {
    /// Creates a definition after validating its name and input schema.
    ///
    /// JSON Schema defaults to draft 2020-12 when `$schema` is absent. If the
    /// keyword is present it must name that exact dialect. Network `$ref`
    /// retrieval is not attempted; unresolved references fail registry build.
    pub fn new(
        name: impl Into<String>,
        input_schema: Value,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let name = name.into();
        validate_tool_name(&name)?;
        let canonical_schema_bytes = validate_schema_document(&input_schema)?;
        Ok(Self {
            name: Arc::from(name),
            title: None,
            description: None,
            input_schema: Arc::new(input_schema),
            output_schema: None,
            annotations: None,
            required_scopes: Arc::from([]),
            canonical_schema_bytes,
        })
    }

    /// Adds a bounded human-readable title.
    pub fn with_title(
        mut self,
        title: impl Into<String>,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let title = title.into();
        validate_display(&title)?;
        self.title = Some(Arc::from(title));
        Ok(self)
    }

    /// Adds a bounded human-readable description.
    pub fn with_description(
        mut self,
        description: impl Into<String>,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let description = description.into();
        validate_display(&description)?;
        self.description = Some(Arc::from(description));
        Ok(self)
    }

    /// Adds and validates an output schema.
    pub fn with_output_schema(
        mut self,
        output_schema: Value,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let bytes = validate_schema_document(&output_schema)?;
        self.canonical_schema_bytes = self
            .canonical_schema_bytes
            .checked_add(bytes)
            .ok_or(McpServerToolDefinitionError::SchemaTooLarge)?;
        if self.canonical_schema_bytes > McpServerToolRegistryLimits::HARD_MAXIMUM_SCHEMA_BYTES * 2
        {
            return Err(McpServerToolDefinitionError::SchemaTooLarge);
        }
        self.output_schema = Some(Arc::new(output_schema));
        Ok(self)
    }

    /// Adds non-authoritative client hints.
    #[must_use]
    pub fn with_annotations(mut self, annotations: McpServerToolAnnotations) -> Self {
        self.annotations = Some(annotations);
        self
    }

    /// Requires every listed OAuth-style scope before dispatch.
    pub fn with_required_scopes(
        mut self,
        scopes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self, McpServerToolDefinitionError> {
        let mut scopes = scopes.into_iter().map(Into::into).collect::<Vec<_>>();
        if scopes.len() > MAX_SCOPE_COUNT {
            return Err(McpServerToolDefinitionError::TooManyScopes);
        }
        for scope in &scopes {
            validate_scope(scope)?;
        }
        scopes.sort_unstable();
        if scopes.windows(2).any(|pair| pair[0] == pair[1]) {
            return Err(McpServerToolDefinitionError::DuplicateScope);
        }
        self.required_scopes = scopes
            .into_iter()
            .map(String::into_boxed_str)
            .collect::<Vec<_>>()
            .into();
        Ok(self)
    }

    /// Returns the stable tool name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the optional display title.
    #[must_use]
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Returns the optional description.
    #[must_use]
    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    /// Returns the exact input schema.
    #[must_use]
    pub fn input_schema(&self) -> &Value {
        &self.input_schema
    }

    /// Returns the optional output schema.
    #[must_use]
    pub fn output_schema(&self) -> Option<&Value> {
        self.output_schema.as_deref()
    }

    /// Returns required scopes in canonical ASCII order.
    pub fn required_scopes(&self) -> impl ExactSizeIterator<Item = &str> {
        self.required_scopes.iter().map(AsRef::as_ref)
    }

    fn to_protocol(&self) -> Tool {
        let input = self
            .input_schema
            .as_object()
            .expect("validated MCP input schema remains an object")
            .clone();
        let mut tool = Tool::new_with_raw(
            self.name.to_string(),
            self.description
                .as_deref()
                .map(|value| Cow::Owned(value.to_owned())),
            input,
        );
        tool.title = self.title.as_deref().map(str::to_owned);
        if let Some(output) = &self.output_schema {
            tool = tool.with_raw_output_schema(Arc::new(
                output
                    .as_object()
                    .expect("validated MCP output schema remains an object")
                    .clone(),
            ));
        }
        if let Some(annotations) = &self.annotations {
            tool = tool.with_annotations(annotations.to_protocol());
        }
        tool
    }
}

impl fmt::Debug for McpServerToolDefinition {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerToolDefinition")
            .field("name", &self.name)
            .field("title", &self.title)
            .field("description", &self.description)
            .field("has_output_schema", &self.output_schema.is_some())
            .field("required_scopes", &self.required_scopes)
            .field("canonical_schema_bytes", &self.canonical_schema_bytes)
            .finish_non_exhaustive()
    }
}

/// Invalid MCP Tool definition.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolDefinitionError {
    /// Names must contain 1–64 characters from the SEP-986 portable grammar.
    #[error("invalid MCP tool name")]
    InvalidName,
    /// A title or description was empty, padded, oversized, or contained controls.
    #[error("invalid MCP tool display text")]
    InvalidDisplayText,
    /// A schema root was not a JSON object.
    #[error("MCP tool schema root must be an object")]
    SchemaRootNotObject,
    /// The schema selected a dialect other than JSON Schema 2020-12.
    #[error("MCP tool schema must use JSON Schema 2020-12")]
    UnsupportedSchemaDialect,
    /// The schema violated the 2020-12 meta-schema.
    #[error("invalid MCP JSON Schema 2020-12 document: {diagnostic}")]
    InvalidSchema {
        /// Bounded startup diagnostic.
        diagnostic: Box<str>,
    },
    /// Canonical schema bytes exceeded the implementation ceiling.
    #[error("MCP tool schema exceeds the hard byte ceiling")]
    SchemaTooLarge,
    /// More than the hard required-scope count was supplied.
    #[error("too many required MCP tool scopes")]
    TooManyScopes,
    /// A required scope was outside the OAuth scope-token grammar.
    #[error("invalid required MCP tool scope")]
    InvalidScope,
    /// A required scope appeared more than once.
    #[error("duplicate required MCP tool scope")]
    DuplicateScope,
}

fn validate_tool_name(name: &str) -> Result<(), McpServerToolDefinitionError> {
    if name.is_empty()
        || name.len() > MAX_TOOL_NAME_BYTES
        || !name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'.' | b'/' | b'-'))
    {
        return Err(McpServerToolDefinitionError::InvalidName);
    }
    Ok(())
}

fn validate_display(value: &str) -> Result<(), McpServerToolDefinitionError> {
    if value.is_empty()
        || value.len() > MAX_DISPLAY_BYTES
        || value.trim() != value
        || value.chars().any(char::is_control)
    {
        return Err(McpServerToolDefinitionError::InvalidDisplayText);
    }
    Ok(())
}

fn validate_scope(scope: &str) -> Result<(), McpServerToolDefinitionError> {
    if scope.is_empty()
        || scope.len() > MAX_SCOPE_BYTES
        || scope
            .bytes()
            .any(|byte| !matches!(byte, b'!' | b'#'..=b'[' | b']'..=b'~'))
    {
        return Err(McpServerToolDefinitionError::InvalidScope);
    }
    Ok(())
}

fn validate_schema_document(value: &Value) -> Result<usize, McpServerToolDefinitionError> {
    let object = value
        .as_object()
        .ok_or(McpServerToolDefinitionError::SchemaRootNotObject)?;
    if object
        .get("$schema")
        .and_then(Value::as_str)
        .is_some_and(|dialect| dialect != JSON_SCHEMA_2020_12)
    {
        return Err(McpServerToolDefinitionError::UnsupportedSchemaDialect);
    }
    jsonschema::draft202012::meta::validate(value).map_err(|source| {
        McpServerToolDefinitionError::InvalidSchema {
            diagnostic: bounded_diagnostic(&source.to_string()),
        }
    })?;
    let canonical = serde_json_canonicalizer::to_vec(value)
        .map_err(|_| McpServerToolDefinitionError::SchemaTooLarge)?;
    if canonical.len() > McpServerToolRegistryLimits::HARD_MAXIMUM_SCHEMA_BYTES {
        return Err(McpServerToolDefinitionError::SchemaTooLarge);
    }
    Ok(canonical.len())
}

fn bounded_diagnostic(value: &str) -> Box<str> {
    let end = value
        .char_indices()
        .map(|(index, _)| index)
        .take_while(|index| *index <= 2048)
        .last()
        .unwrap_or(0);
    if value.len() <= 2048 {
        value.to_owned().into_boxed_str()
    } else {
        value[..end].to_owned().into_boxed_str()
    }
}

/// Resource ceilings for one immutable Tool registry.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(clippy::struct_field_names)]
pub struct McpServerToolRegistryLimits {
    maximum_tools: usize,
    maximum_schema_bytes: usize,
    maximum_total_schema_bytes: usize,
}

impl McpServerToolRegistryLimits {
    /// Absolute tool count ceiling.
    pub const HARD_MAXIMUM_TOOLS: usize = 4096;
    /// Absolute canonical byte ceiling for one input or output schema.
    pub const HARD_MAXIMUM_SCHEMA_BYTES: usize = 8 * MEBIBYTE;
    /// Absolute aggregate schema byte ceiling.
    pub const HARD_MAXIMUM_TOTAL_SCHEMA_BYTES: usize = 256 * MEBIBYTE;

    /// Constructs explicit positive limits within hard ceilings.
    pub const fn new(
        maximum_tools: usize,
        maximum_schema_bytes: usize,
        maximum_total_schema_bytes: usize,
    ) -> Result<Self, McpServerToolRegistryLimitsError> {
        if maximum_tools == 0 || maximum_schema_bytes == 0 || maximum_total_schema_bytes == 0 {
            return Err(McpServerToolRegistryLimitsError::ZeroLimit);
        }
        if maximum_schema_bytes > Self::HARD_MAXIMUM_SCHEMA_BYTES
            || maximum_total_schema_bytes > Self::HARD_MAXIMUM_TOTAL_SCHEMA_BYTES
            || maximum_tools > Self::HARD_MAXIMUM_TOOLS
            || maximum_total_schema_bytes < maximum_schema_bytes
        {
            return Err(McpServerToolRegistryLimitsError::InvalidLimit);
        }
        Ok(Self {
            maximum_tools,
            maximum_schema_bytes,
            maximum_total_schema_bytes,
        })
    }

    /// Returns the tool count ceiling.
    #[must_use]
    pub const fn maximum_tools(self) -> usize {
        self.maximum_tools
    }

    /// Returns the per-schema byte ceiling.
    #[must_use]
    pub const fn maximum_schema_bytes(self) -> usize {
        self.maximum_schema_bytes
    }

    /// Returns the aggregate schema byte ceiling.
    #[must_use]
    pub const fn maximum_total_schema_bytes(self) -> usize {
        self.maximum_total_schema_bytes
    }
}

impl Default for McpServerToolRegistryLimits {
    fn default() -> Self {
        Self {
            maximum_tools: 1024,
            maximum_schema_bytes: 2 * MEBIBYTE,
            maximum_total_schema_bytes: 64 * MEBIBYTE,
        }
    }
}

/// Invalid registry resource policy.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolRegistryLimitsError {
    /// Every resource ceiling must be positive.
    #[error("MCP tool registry limits must be positive")]
    ZeroLimit,
    /// A limit exceeded a hard maximum or aggregate capacity was inconsistent.
    #[error("invalid MCP tool registry limit")]
    InvalidLimit,
}

/// One bounded protocol content block owned by `StateKnot`.
#[derive(Clone, Debug)]
pub struct McpServerContent {
    inner: ContentBlock,
    wire_bytes: usize,
}

impl McpServerContent {
    /// Creates bounded text content.
    pub fn text(text: impl Into<String>) -> Result<Self, McpServerContentError> {
        let text = text.into();
        if text.len() > MAX_TEXT_CONTENT_BYTES {
            return Err(McpServerContentError::TooLarge);
        }
        Self::from_protocol(ContentBlock::text(text))
    }

    /// Creates image content from raw bytes and canonical standard Base64.
    pub fn image(
        data: impl AsRef<[u8]>,
        mime_type: impl Into<String>,
    ) -> Result<Self, McpServerContentError> {
        let data = data.as_ref();
        validate_binary(data)?;
        let mime_type = validate_mime(mime_type.into(), "image")?;
        Self::from_protocol(ContentBlock::image(STANDARD.encode(data), mime_type))
    }

    /// Creates audio content from raw bytes and canonical standard Base64.
    pub fn audio(
        data: impl AsRef<[u8]>,
        mime_type: impl Into<String>,
    ) -> Result<Self, McpServerContentError> {
        let data = data.as_ref();
        validate_binary(data)?;
        let mime_type = validate_mime(mime_type.into(), "audio")?;
        Self::from_protocol(ContentBlock::audio(STANDARD.encode(data), mime_type))
    }

    /// Validates an extension content block represented as protocol JSON.
    ///
    /// Prefer typed constructors for ordinary content. This escape hatch
    /// retains future MCP variants without exposing SDK types.
    pub fn try_from_protocol_json(value: Value) -> Result<Self, McpServerContentError> {
        let wire_bytes = canonical_json_len(&value).map_err(|_| McpServerContentError::Invalid)?;
        if wire_bytes > MAX_CONTENT_BLOCK_BYTES {
            return Err(McpServerContentError::TooLarge);
        }
        let inner: ContentBlock =
            serde_json::from_value(value).map_err(|_| McpServerContentError::Invalid)?;
        validate_protocol_content(&inner)?;
        Ok(Self { inner, wire_bytes })
    }

    fn from_protocol(inner: ContentBlock) -> Result<Self, McpServerContentError> {
        validate_protocol_content(&inner)?;
        let value = serde_json::to_value(&inner).map_err(|_| McpServerContentError::Invalid)?;
        let wire_bytes = canonical_json_len(&value).map_err(|_| McpServerContentError::Invalid)?;
        if wire_bytes > MAX_CONTENT_BLOCK_BYTES {
            return Err(McpServerContentError::TooLarge);
        }
        Ok(Self { inner, wire_bytes })
    }

    pub(crate) fn protocol_clone(&self) -> ContentBlock {
        self.inner.clone()
    }

    pub(crate) const fn wire_bytes(&self) -> usize {
        self.wire_bytes
    }
}

/// Invalid or oversized MCP content.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerContentError {
    /// The content did not match an MCP content block or had invalid media data.
    #[error("invalid MCP content block")]
    Invalid,
    /// The content exceeded a hard byte ceiling.
    #[error("MCP content block is too large")]
    TooLarge,
}

fn validate_binary(data: &[u8]) -> Result<(), McpServerContentError> {
    if data.len() > MAX_BINARY_CONTENT_BYTES {
        return Err(McpServerContentError::TooLarge);
    }
    Ok(())
}

fn validate_mime(value: String, expected_top_level: &str) -> Result<String, McpServerContentError> {
    if value.len() > 255
        || value.trim() != value
        || value
            .parse::<mime::Mime>()
            .map_or(true, |mime| mime.type_().as_str() != expected_top_level)
    {
        return Err(McpServerContentError::Invalid);
    }
    Ok(value)
}

fn validate_protocol_content(content: &ContentBlock) -> Result<(), McpServerContentError> {
    match content {
        ContentBlock::Text(value) => {
            if value.text.len() > MAX_TEXT_CONTENT_BYTES {
                return Err(McpServerContentError::TooLarge);
            }
        }
        ContentBlock::Image(value) => {
            validate_encoded_binary(&value.data)?;
            validate_mime(value.mime_type.clone(), "image")?;
        }
        ContentBlock::Audio(value) => {
            validate_encoded_binary(&value.data)?;
            validate_mime(value.mime_type.clone(), "audio")?;
        }
        ContentBlock::Resource(value) => match &value.resource {
            rmcp::model::ResourceContents::TextResourceContents { uri, text, .. } => {
                validate_resource_uri(uri)?;
                if text.len() > MAX_TEXT_CONTENT_BYTES {
                    return Err(McpServerContentError::TooLarge);
                }
            }
            rmcp::model::ResourceContents::BlobResourceContents { uri, blob, .. } => {
                validate_resource_uri(uri)?;
                validate_encoded_binary(blob)?;
            }
            _ => return Err(McpServerContentError::Invalid),
        },
        ContentBlock::ResourceLink(value) => validate_resource_uri(&value.uri)?,
        _ => return Err(McpServerContentError::Invalid),
    }
    Ok(())
}

fn validate_encoded_binary(value: &str) -> Result<(), McpServerContentError> {
    let decoded = STANDARD
        .decode(value)
        .map_err(|_| McpServerContentError::Invalid)?;
    validate_binary(&decoded)
}

fn validate_resource_uri(value: &str) -> Result<(), McpServerContentError> {
    if value.is_empty()
        || value.len() > 4096
        || value.chars().any(char::is_control)
        || value
            .parse::<http::Uri>()
            .map_or(true, |uri| uri.scheme().is_none())
    {
        return Err(McpServerContentError::Invalid);
    }
    Ok(())
}

/// Bounded complete MCP Tool result.
#[derive(Clone, Debug)]
pub struct McpServerToolResult {
    content: Arc<[McpServerContent]>,
    structured_content: Option<Arc<Value>>,
    is_error: bool,
}

impl McpServerToolResult {
    /// Creates an unstructured successful result.
    pub fn success(
        content: impl IntoIterator<Item = McpServerContent>,
    ) -> Result<Self, McpServerToolResultError> {
        Self::new(content, None, false)
    }

    /// Creates a structured successful result.
    pub fn structured(
        content: impl IntoIterator<Item = McpServerContent>,
        structured_content: Value,
    ) -> Result<Self, McpServerToolResultError> {
        Self::new(content, Some(structured_content), false)
    }

    /// Creates a caller-visible tool-level error.
    pub fn error(
        content: impl IntoIterator<Item = McpServerContent>,
    ) -> Result<Self, McpServerToolResultError> {
        Self::new(content, None, true)
    }

    fn new(
        content: impl IntoIterator<Item = McpServerContent>,
        structured_content: Option<Value>,
        is_error: bool,
    ) -> Result<Self, McpServerToolResultError> {
        let content = content.into_iter().collect::<Vec<_>>();
        if content.len() > MAX_CONTENT_BLOCKS {
            return Err(McpServerToolResultError::TooManyContentBlocks);
        }
        let mut total = content
            .iter()
            .try_fold(0_usize, |total, item| total.checked_add(item.wire_bytes));
        let structured_content = if let Some(value) = structured_content {
            let bytes = canonical_json_len(&value)
                .map_err(|_| McpServerToolResultError::InvalidStructuredContent)?;
            if bytes > MAX_STRUCTURED_CONTENT_BYTES {
                return Err(McpServerToolResultError::TooLarge);
            }
            total = total.and_then(|total| total.checked_add(bytes));
            Some(Arc::new(value))
        } else {
            None
        };
        if total.is_none_or(|total| total > MAX_TOOL_RESULT_BYTES) {
            return Err(McpServerToolResultError::TooLarge);
        }
        Ok(Self {
            content: content.into(),
            structured_content,
            is_error,
        })
    }

    fn into_protocol(self) -> CallToolResult {
        let content = self
            .content
            .iter()
            .map(|value| value.inner.clone())
            .collect();
        let mut result = if self.is_error {
            CallToolResult::error(content)
        } else {
            CallToolResult::success(content)
        };
        result.structured_content = self.structured_content.as_deref().cloned();
        result
    }
}

/// Invalid bounded Tool result.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolResultError {
    /// The result contained too many content blocks.
    #[error("too many MCP tool result content blocks")]
    TooManyContentBlocks,
    /// Structured content could not be canonically represented.
    #[error("invalid MCP structured content")]
    InvalidStructuredContent,
    /// The result exceeded a hard byte ceiling.
    #[error("MCP tool result is too large")]
    TooLarge,
}

/// Validated server-initiated MRTR request without an SDK type in the API.
#[derive(Clone, Debug)]
pub struct McpServerInputRequest {
    inner: InputRequest,
}

impl McpServerInputRequest {
    /// Validates one `elicitation/create`, `sampling/createMessage`, or
    /// `roots/list` request represented as protocol JSON.
    pub fn try_from_protocol_json(value: Value) -> Result<Self, McpServerInputRequiredError> {
        if canonical_json_len(&value).map_or(true, |bytes| bytes > MAX_INPUT_REQUEST_BYTES) {
            return Err(McpServerInputRequiredError::InvalidRequest);
        }
        let inner = serde_json::from_value(value)
            .map_err(|_| McpServerInputRequiredError::InvalidRequest)?;
        Ok(Self { inner })
    }
}

/// Bounded MRTR `input_required` result.
#[derive(Clone, Debug)]
pub struct McpServerInputRequired {
    requests: Arc<BTreeMap<String, McpServerInputRequest>>,
    request_state: Option<Box<str>>,
}

impl McpServerInputRequired {
    /// Creates an input-required result with at least one request or state.
    pub fn new(
        requests: BTreeMap<String, McpServerInputRequest>,
        request_state: Option<impl Into<String>>,
    ) -> Result<Self, McpServerInputRequiredError> {
        if requests.len() > MAX_INPUT_REQUESTS {
            return Err(McpServerInputRequiredError::TooManyRequests);
        }
        for id in requests.keys() {
            if id.is_empty()
                || id.len() > MAX_INPUT_REQUEST_ID_BYTES
                || !id
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'.' | b'-'))
            {
                return Err(McpServerInputRequiredError::InvalidRequestId);
            }
        }
        let request_state = request_state.map(Into::into);
        if requests.is_empty() && request_state.is_none() {
            return Err(McpServerInputRequiredError::Empty);
        }
        if request_state
            .as_ref()
            .is_some_and(|value| value.is_empty() || value.len() > MAX_REQUEST_STATE_BYTES)
        {
            return Err(McpServerInputRequiredError::InvalidRequestState);
        }
        Ok(Self {
            requests: Arc::new(requests),
            request_state: request_state.map(String::into_boxed_str),
        })
    }

    pub(crate) fn into_protocol(self) -> InputRequiredResult {
        let requests = if self.requests.is_empty() {
            None
        } else {
            Some(
                self.requests
                    .iter()
                    .map(|(key, value)| (key.clone(), value.inner.clone()))
                    .collect(),
            )
        };
        InputRequiredResult::new(requests, self.request_state.map(Into::into))
    }
}

/// Invalid MRTR input-required result.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerInputRequiredError {
    /// Neither input requests nor request state was supplied.
    #[error("MCP input-required result is empty")]
    Empty,
    /// Too many simultaneous input requests were supplied.
    #[error("too many MCP input requests")]
    TooManyRequests,
    /// A request identifier was invalid.
    #[error("invalid MCP input request identifier")]
    InvalidRequestId,
    /// A request did not match an allowed MRTR request envelope.
    #[error("invalid MCP input request")]
    InvalidRequest,
    /// Request state was empty or oversized.
    #[error("invalid MCP request state")]
    InvalidRequestState,
}

/// StateKnot-owned wrapper around integrity-protected MCP `requestState`.
///
/// Tokens are authenticated, not encrypted. Bind them to
/// [`McpServerToolContext::request_state_binding`] and apply a TTL. Single-use
/// redemption remains application-owned durable state.
#[derive(Clone)]
pub struct McpServerRequestStateCodec {
    inner: RequestStateCodec,
}

impl McpServerRequestStateCodec {
    /// Builds a codec from at least 32 bytes of high-entropy key material.
    pub fn new(key: impl Into<Vec<u8>>) -> Result<Self, McpServerRequestStateCodecBuildError> {
        RequestStateCodec::try_new(key)
            .map(|inner| Self { inner })
            .map_err(|_| McpServerRequestStateCodecBuildError::InvalidConfiguration)
    }

    /// Builds a rotating keyring that emits the active `rs2` key id.
    pub fn with_keyring<K, V>(
        active_key_id: impl Into<String>,
        keys: impl IntoIterator<Item = (K, V)>,
    ) -> Result<Self, McpServerRequestStateCodecBuildError>
    where
        K: Into<String>,
        V: Into<Vec<u8>>,
    {
        RequestStateCodec::new_with_keyring(active_key_id, keys)
            .map(|inner| Self { inner })
            .map_err(|_| McpServerRequestStateCodecBuildError::InvalidConfiguration)
    }

    /// Seals bounded JSON with caller binding and mandatory positive TTL.
    pub fn seal_json(
        &self,
        value: &Value,
        associated_data: &[u8],
        ttl: Duration,
    ) -> Result<String, McpServerRequestStateError> {
        validate_state_operation(value, associated_data, ttl)?;
        let options = SealOptions::new().associated_data(associated_data).ttl(ttl);
        let sealed = self
            .inner
            .seal_json_with(value, &options)
            .map_err(|_| McpServerRequestStateError::InvalidState)?;
        if sealed.len() > MAX_REQUEST_STATE_BYTES {
            return Err(McpServerRequestStateError::StateTooLarge);
        }
        Ok(sealed)
    }

    /// Opens bounded JSON with the exact same caller binding.
    ///
    /// All malformed, expired, unknown-key, and integrity failures collapse to
    /// [`McpServerRequestStateError::InvalidState`] to avoid an oracle.
    pub fn open_json(
        &self,
        sealed: &str,
        associated_data: &[u8],
    ) -> Result<Value, McpServerRequestStateError> {
        if sealed.is_empty()
            || sealed.len() > MAX_REQUEST_STATE_BYTES
            || associated_data.len() > MAX_REQUEST_STATE_BINDING_BYTES
        {
            return Err(McpServerRequestStateError::InvalidState);
        }
        let value = self
            .inner
            .open_json_with(sealed, associated_data)
            .map_err(|_| McpServerRequestStateError::InvalidState)?;
        if canonical_json_len(&value).map_or(true, |bytes| bytes > MAX_REQUEST_STATE_PAYLOAD_BYTES)
        {
            return Err(McpServerRequestStateError::InvalidState);
        }
        Ok(value)
    }
}

impl fmt::Debug for McpServerRequestStateCodec {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerRequestStateCodec")
            .field("keys", &"[REDACTED]")
            .finish()
    }
}

/// Invalid request-state key or keyring configuration.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerRequestStateCodecBuildError {
    /// The key was short or the keyring/key identifiers were inconsistent.
    #[error("invalid MCP request-state key configuration")]
    InvalidConfiguration,
}

/// Safe request-state processing failure.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerRequestStateError {
    /// Payload, associated data, or TTL violated a hard bound.
    #[error("invalid MCP request-state operation")]
    InvalidOperation,
    /// The generated token exceeded the hard transport ceiling.
    #[error("MCP request state is too large")]
    StateTooLarge,
    /// State was malformed, expired, signed by an unknown key, or failed integrity.
    #[error("invalid MCP request state")]
    InvalidState,
}

fn validate_state_operation(
    value: &Value,
    associated_data: &[u8],
    ttl: Duration,
) -> Result<(), McpServerRequestStateError> {
    if ttl.is_zero()
        || ttl > MAX_REQUEST_STATE_TTL
        || associated_data.is_empty()
        || associated_data.len() > MAX_REQUEST_STATE_BINDING_BYTES
        || canonical_json_len(value).map_or(true, |bytes| bytes > MAX_REQUEST_STATE_PAYLOAD_BYTES)
    {
        return Err(McpServerRequestStateError::InvalidOperation);
    }
    Ok(())
}

/// Complete or input-required Tool outcome.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum McpServerToolOutcome {
    /// Tool execution completed, including caller-visible Tool errors.
    Complete(McpServerToolResult),
    /// The client must satisfy MRTR input and retry the same logical call.
    InputRequired(McpServerInputRequired),
}

impl From<McpServerToolResult> for McpServerToolOutcome {
    fn from(value: McpServerToolResult) -> Self {
        Self::Complete(value)
    }
}

impl From<McpServerInputRequired> for McpServerToolOutcome {
    fn from(value: McpServerInputRequired) -> Self {
        Self::InputRequired(value)
    }
}

/// Handler-internal failure category with no arbitrary diagnostic leakage.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolHandlerError {
    /// A downstream dependency is temporarily unavailable.
    #[error("MCP tool dependency is unavailable")]
    Unavailable,
    /// Execution observed cooperative cancellation.
    #[error("MCP tool execution was cancelled")]
    Cancelled,
    /// An invariant failed inside the Tool implementation.
    #[error("MCP tool execution failed internally")]
    Internal,
    /// The operation requires a core capability the client did not declare.
    #[error("MCP client is missing a required capability")]
    MissingRequiredClientCapability(McpServerRequiredClientCapability),
}

/// Core client capability a Tool may require before it can continue.
///
/// This StateKnot-owned enum prevents the wire SDK capability model from
/// becoming part of the public application API. Extensions, including Tasks,
/// require their own independently versioned profile.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum McpServerRequiredClientCapability {
    /// Interactive Elicitation requests.
    Elicitation,
    /// Server-initiated Sampling requests.
    Sampling,
    /// Client Roots discovery.
    Roots,
}

/// Executes one registered Tool.
pub trait McpServerToolHandler: Send + Sync + 'static {
    /// Executes a validated, authorized request.
    fn call(
        &self,
        call: McpServerToolCall,
        context: McpServerToolContext,
    ) -> BoxFuture<'_, Result<McpServerToolOutcome, McpServerToolHandlerError>>;
}

/// StateKnot-owned inbound Tool call.
#[derive(Clone, Debug)]
pub struct McpServerToolCall {
    name: Arc<str>,
    arguments: Arc<Map<String, Value>>,
    input_responses: Arc<BTreeMap<String, Value>>,
    request_state: Option<Arc<str>>,
}

impl McpServerToolCall {
    /// Returns the registered Tool name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns syntactically decoded arguments.
    ///
    /// The decoded authorization policy runs before input-schema validation to
    /// avoid exposing schema diagnostics to a denied caller. Authorization
    /// code must therefore treat these values as untrusted.
    #[must_use]
    pub fn arguments(&self) -> &Map<String, Value> {
        &self.arguments
    }

    /// Returns untrusted MRTR responses supplied by the client.
    #[must_use]
    pub fn input_responses(&self) -> &BTreeMap<String, Value> {
        &self.input_responses
    }

    /// Returns untrusted request state echoed by the client.
    #[must_use]
    pub fn request_state(&self) -> Option<&str> {
        self.request_state.as_deref()
    }
}

#[derive(Clone)]
struct McpServerProgressSink {
    peer: Peer<RoleServer>,
    token: rmcp::model::ProgressToken,
}

/// Authenticated, request-scoped Tool execution context.
#[derive(Clone)]
pub struct McpServerToolContext {
    principal: McpServerPrincipal,
    client_name: Option<Arc<str>>,
    client_version: Option<Arc<str>>,
    client_capabilities: Arc<Value>,
    cancellation: CancellationToken,
    progress: Option<McpServerProgressSink>,
    request_state_binding: Arc<[u8]>,
}

impl McpServerToolContext {
    /// Returns the authenticated principal.
    #[must_use]
    pub const fn principal(&self) -> &McpServerPrincipal {
        &self.principal
    }

    /// Returns the validated client implementation name.
    #[must_use]
    pub fn client_name(&self) -> Option<&str> {
        self.client_name.as_deref()
    }

    /// Returns the validated client implementation version.
    #[must_use]
    pub fn client_version(&self) -> Option<&str> {
        self.client_version.as_deref()
    }

    /// Returns the request-scoped client capability document.
    #[must_use]
    pub fn client_capabilities(&self) -> &Value {
        &self.client_capabilities
    }

    /// Returns associated data bound to principal, Tool, and canonical arguments.
    #[must_use]
    pub fn request_state_binding(&self) -> &[u8] {
        &self.request_state_binding
    }

    /// Returns whether the client cancelled this request.
    #[must_use]
    pub fn is_cancelled(&self) -> bool {
        self.cancellation.is_cancelled()
    }

    /// Waits for cooperative cancellation.
    pub async fn cancelled(&self) {
        self.cancellation.cancelled().await;
    }

    /// Sends bounded progress when the request supplied a progress token.
    ///
    /// Returns `Ok(false)` when the client did not request progress.
    pub async fn report_progress(
        &self,
        progress: f64,
        total: Option<f64>,
        message: Option<&str>,
    ) -> Result<bool, McpServerToolProgressError> {
        if !progress.is_finite()
            || progress < 0.0
            || total.is_some_and(|value| !value.is_finite() || value <= 0.0 || progress > value)
            || message.is_some_and(|value| {
                value.len() > MAX_PROGRESS_MESSAGE_BYTES || value.chars().any(char::is_control)
            })
        {
            return Err(McpServerToolProgressError::InvalidProgress);
        }
        let Some(sink) = &self.progress else {
            return Ok(false);
        };
        let mut notification = ProgressNotificationParam::new(sink.token.clone(), progress);
        if let Some(total) = total {
            notification = notification.with_total(total);
        }
        if let Some(message) = message {
            notification = notification.with_message(message);
        }
        sink.peer
            .notify_progress(notification)
            .await
            .map_err(|_| McpServerToolProgressError::TransportClosed)?;
        Ok(true)
    }
}

impl fmt::Debug for McpServerToolContext {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerToolContext")
            .field("principal", &self.principal)
            .field("client_name", &self.client_name)
            .field("client_version", &self.client_version)
            .field("cancelled", &self.cancellation.is_cancelled())
            .field("progress_requested", &self.progress.is_some())
            .field("request_state_binding", &"[DIGEST]")
            .finish_non_exhaustive()
    }
}

/// Progress notification failure.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolProgressError {
    /// Progress, total, or message violated protocol bounds.
    #[error("invalid MCP progress notification")]
    InvalidProgress,
    /// The client transport closed before notification delivery.
    #[error("MCP client transport is closed")]
    TransportClosed,
}

/// Owned facts for decoded Tool authorization.
#[derive(Clone, Debug)]
pub struct McpServerToolAuthorizationRequest {
    principal: McpServerPrincipal,
    definition: McpServerToolDefinition,
    call: McpServerToolCall,
}

impl McpServerToolAuthorizationRequest {
    /// Returns the authenticated principal.
    #[must_use]
    pub const fn principal(&self) -> &McpServerPrincipal {
        &self.principal
    }

    /// Returns the immutable registered definition.
    #[must_use]
    pub const fn definition(&self) -> &McpServerToolDefinition {
        &self.definition
    }

    /// Returns the decoded, resource-bounded call.
    ///
    /// Arguments remain untrusted until authorization succeeds.
    #[must_use]
    pub const fn call(&self) -> &McpServerToolCall {
        &self.call
    }
}

/// Decoded per-call authorization policy.
pub trait McpServerToolAuthorization: Send + Sync + 'static {
    /// Authorizes one call after Tool lookup and resource-bound checks, but
    /// before input-schema validation.
    fn authorize(
        &self,
        request: McpServerToolAuthorizationRequest,
    ) -> BoxFuture<'_, Result<(), McpServerToolAuthorizationError>>;
}

/// Explicit policy that admits every scope-qualified Tool call.
#[derive(Clone, Copy, Debug, Default)]
pub struct AllowMcpServerToolAuthorization;

impl McpServerToolAuthorization for AllowMcpServerToolAuthorization {
    fn authorize(
        &self,
        _request: McpServerToolAuthorizationRequest,
    ) -> BoxFuture<'_, Result<(), McpServerToolAuthorizationError>> {
        Box::pin(async { Ok(()) })
    }
}

/// Public-safe decoded authorization failure.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolAuthorizationError {
    /// Policy denied this call.
    #[error("MCP tool call is forbidden")]
    Forbidden,
    /// The policy authority is temporarily unavailable.
    #[error("MCP tool authorization is unavailable")]
    Unavailable,
}

struct PendingTool {
    definition: McpServerToolDefinition,
    handler: Arc<dyn McpServerToolHandler>,
}

struct RegisteredTool {
    definition: McpServerToolDefinition,
    protocol: Tool,
    input_validator: Validator,
    output_validator: Option<Validator>,
    handler: Arc<dyn McpServerToolHandler>,
}

/// Startup-only builder for an immutable executable Tool registry.
pub struct McpServerToolRegistryBuilder {
    limits: McpServerToolRegistryLimits,
    pending: BTreeMap<String, PendingTool>,
    total_schema_bytes: usize,
}

impl McpServerToolRegistryBuilder {
    /// Creates an empty builder with explicit ceilings.
    #[must_use]
    pub fn new(limits: McpServerToolRegistryLimits) -> Self {
        Self {
            limits,
            pending: BTreeMap::new(),
            total_schema_bytes: 0,
        }
    }

    /// Creates an empty builder with production defaults.
    #[must_use]
    pub fn with_default_limits() -> Self {
        Self::new(McpServerToolRegistryLimits::default())
    }

    /// Registers one definition and handler without mutating on failure.
    pub fn register<H>(
        &mut self,
        definition: McpServerToolDefinition,
        handler: H,
    ) -> Result<(), McpServerToolRegistryError>
    where
        H: McpServerToolHandler,
    {
        self.register_shared(definition, Arc::new(handler))
    }

    /// Registers one definition with an already shared handler.
    pub fn register_shared(
        &mut self,
        definition: McpServerToolDefinition,
        handler: Arc<dyn McpServerToolHandler>,
    ) -> Result<(), McpServerToolRegistryError> {
        if self.pending.len() == self.limits.maximum_tools {
            return Err(McpServerToolRegistryError::TooManyTools);
        }
        if self.pending.contains_key(definition.name()) {
            return Err(McpServerToolRegistryError::DuplicateTool);
        }
        let input_bytes = canonical_json_len(definition.input_schema())
            .map_err(|_| McpServerToolRegistryError::SchemaTooLarge)?;
        let output_bytes = definition
            .output_schema()
            .map(canonical_json_len)
            .transpose()
            .map_err(|_| McpServerToolRegistryError::SchemaTooLarge)?
            .unwrap_or(0);
        if input_bytes > self.limits.maximum_schema_bytes
            || output_bytes > self.limits.maximum_schema_bytes
        {
            return Err(McpServerToolRegistryError::SchemaTooLarge);
        }
        let added = input_bytes
            .checked_add(output_bytes)
            .ok_or(McpServerToolRegistryError::TotalSchemaBytesTooLarge)?;
        let total = self
            .total_schema_bytes
            .checked_add(added)
            .ok_or(McpServerToolRegistryError::TotalSchemaBytesTooLarge)?;
        if total > self.limits.maximum_total_schema_bytes {
            return Err(McpServerToolRegistryError::TotalSchemaBytesTooLarge);
        }
        self.pending.insert(
            definition.name().to_owned(),
            PendingTool {
                definition,
                handler,
            },
        );
        self.total_schema_bytes = total;
        Ok(())
    }

    /// Eagerly compiles every validator in offline mode and freezes the catalog.
    pub fn build(self) -> Result<McpServerToolRegistry, McpServerToolRegistryError> {
        if self.pending.is_empty() {
            return Err(McpServerToolRegistryError::Empty);
        }
        let mut registered = BTreeMap::new();
        let mut digest_material = Vec::new();
        for (name, pending) in self.pending {
            let input_validator = compile_schema(name.as_str(), pending.definition.input_schema())?;
            let output_validator = pending
                .definition
                .output_schema()
                .map(|schema| compile_schema(name.as_str(), schema))
                .transpose()?;
            append_digest_part(&mut digest_material, name.as_bytes());
            let input = serde_json_canonicalizer::to_vec(pending.definition.input_schema())
                .map_err(|_| McpServerToolRegistryError::SchemaTooLarge)?;
            append_digest_part(&mut digest_material, &input);
            if let Some(output) = pending.definition.output_schema() {
                let output = serde_json_canonicalizer::to_vec(output)
                    .map_err(|_| McpServerToolRegistryError::SchemaTooLarge)?;
                append_digest_part(&mut digest_material, &output);
            } else {
                append_digest_part(&mut digest_material, &[]);
            }
            let protocol = pending.definition.to_protocol();
            registered.insert(
                name,
                Arc::new(RegisteredTool {
                    definition: pending.definition,
                    protocol,
                    input_validator,
                    output_validator,
                    handler: pending.handler,
                }),
            );
        }
        let digest = Digest::sha256(digest_material).to_string();
        Ok(McpServerToolRegistry {
            inner: Arc::new(McpServerToolRegistryInner {
                tools: registered,
                digest: Arc::from(
                    digest
                        .strip_prefix("sha256:")
                        .expect("StateKnot SHA-256 digest has stable prefix"),
                ),
                total_schema_bytes: self.total_schema_bytes,
            }),
        })
    }
}

impl Default for McpServerToolRegistryBuilder {
    fn default() -> Self {
        Self::with_default_limits()
    }
}

impl fmt::Debug for McpServerToolRegistryBuilder {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerToolRegistryBuilder")
            .field("limits", &self.limits)
            .field("tool_count", &self.pending.len())
            .field("total_schema_bytes", &self.total_schema_bytes)
            .finish_non_exhaustive()
    }
}

fn compile_schema(
    tool_name: &str,
    schema: &Value,
) -> Result<Validator, McpServerToolRegistryError> {
    jsonschema::draft202012::options()
        .should_validate_formats(true)
        .offline()
        .build(schema)
        .map_err(|source| McpServerToolRegistryError::SchemaCompilation {
            tool_name: tool_name.to_owned().into_boxed_str(),
            diagnostic: bounded_diagnostic(&source.to_string()),
        })
}

pub(crate) fn append_digest_part(target: &mut Vec<u8>, value: &[u8]) {
    target.extend_from_slice(&(value.len() as u64).to_be_bytes());
    target.extend_from_slice(value);
}

/// Immutable executable Tool registry.
#[derive(Clone)]
pub struct McpServerToolRegistry {
    inner: Arc<McpServerToolRegistryInner>,
}

struct McpServerToolRegistryInner {
    tools: BTreeMap<String, Arc<RegisteredTool>>,
    digest: Arc<str>,
    total_schema_bytes: usize,
}

impl McpServerToolRegistry {
    /// Returns the registered Tool count.
    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.tools.len()
    }

    /// Returns whether no Tool is registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.tools.is_empty()
    }

    /// Returns one StateKnot-owned definition.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&McpServerToolDefinition> {
        self.inner.tools.get(name).map(|entry| &entry.definition)
    }

    /// Iterates definitions in stable ASCII name order.
    pub fn definitions(&self) -> impl ExactSizeIterator<Item = &McpServerToolDefinition> {
        self.inner.tools.values().map(|entry| &entry.definition)
    }

    /// Returns retained canonical input/output schema bytes.
    #[must_use]
    pub fn total_schema_bytes(&self) -> usize {
        self.inner.total_schema_bytes
    }

    fn contains_scoped_tools(&self) -> bool {
        self.inner
            .tools
            .values()
            .any(|entry| !entry.definition.required_scopes.is_empty())
    }
}

impl fmt::Debug for McpServerToolRegistry {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerToolRegistry")
            .field("tool_count", &self.len())
            .field("catalog_digest", &self.inner.digest)
            .field("total_schema_bytes", &self.total_schema_bytes())
            .finish_non_exhaustive()
    }
}

/// Startup failure for an executable registry.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolRegistryError {
    /// At least one Tool is required.
    #[error("MCP tool registry is empty")]
    Empty,
    /// A name appeared more than once.
    #[error("duplicate MCP tool")]
    DuplicateTool,
    /// The configured Tool count ceiling was reached.
    #[error("too many MCP tools")]
    TooManyTools,
    /// An input or output schema exceeded the configured ceiling.
    #[error("MCP tool schema is too large")]
    SchemaTooLarge,
    /// Aggregate schema bytes exceeded the configured ceiling.
    #[error("MCP tool aggregate schema bytes are too large")]
    TotalSchemaBytesTooLarge,
    /// Offline validator compilation failed, including unresolved `$ref`.
    #[error("failed to compile schema for MCP tool {tool_name}: {diagnostic}")]
    SchemaCompilation {
        /// Tool whose schema failed.
        tool_name: Box<str>,
        /// Bounded startup diagnostic.
        diagnostic: Box<str>,
    },
}

/// Stable service identity, pagination, and cache policy.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct McpServerApplicationOptions {
    pub(crate) server_name: Arc<str>,
    pub(crate) server_version: Arc<str>,
    pub(crate) instructions: Option<Arc<str>>,
    pub(crate) page_size: usize,
    pub(crate) cache_ttl_ms: u64,
    pub(crate) cache_scope: McpServerCacheScope,
}

impl McpServerApplicationOptions {
    /// Hard page size ceiling.
    pub const HARD_MAXIMUM_PAGE_SIZE: usize = 256;
    /// Hard cache TTL ceiling.
    pub const HARD_MAXIMUM_CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);

    /// Constructs explicit service metadata and catalog policy.
    pub fn new(
        server_name: impl Into<String>,
        server_version: impl Into<String>,
        page_size: usize,
        cache_ttl: Duration,
        cache_scope: McpServerCacheScope,
    ) -> Result<Self, McpServerApplicationOptionsError> {
        let server_name = server_name.into();
        let server_version = server_version.into();
        validate_service_component(&server_name)?;
        validate_service_component(&server_version)?;
        if page_size == 0 || page_size > Self::HARD_MAXIMUM_PAGE_SIZE {
            return Err(McpServerApplicationOptionsError::InvalidPageSize);
        }
        if cache_ttl > Self::HARD_MAXIMUM_CACHE_TTL {
            return Err(McpServerApplicationOptionsError::InvalidCacheTtl);
        }
        Ok(Self {
            server_name: Arc::from(server_name),
            server_version: Arc::from(server_version),
            instructions: None,
            page_size,
            cache_ttl_ms: u64::try_from(cache_ttl.as_millis())
                .map_err(|_| McpServerApplicationOptionsError::InvalidCacheTtl)?,
            cache_scope,
        })
    }

    /// Adds bounded client guidance.
    pub fn with_instructions(
        mut self,
        instructions: impl Into<String>,
    ) -> Result<Self, McpServerApplicationOptionsError> {
        let instructions = instructions.into();
        if instructions.is_empty()
            || instructions.len() > MAX_DISPLAY_BYTES
            || instructions.trim() != instructions
            || instructions
                .chars()
                .any(|character| character.is_control() && !matches!(character, '\n' | '\r' | '\t'))
        {
            return Err(McpServerApplicationOptionsError::InvalidInstructions);
        }
        self.instructions = Some(Arc::from(instructions));
        Ok(self)
    }
}

fn validate_service_component(value: &str) -> Result<(), McpServerApplicationOptionsError> {
    if value.is_empty()
        || value.len() > 256
        || value.trim() != value
        || value.chars().any(char::is_control)
    {
        return Err(McpServerApplicationOptionsError::InvalidIdentity);
    }
    Ok(())
}

/// Invalid Tool service configuration.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerApplicationOptionsError {
    /// Server name or version was invalid.
    #[error("invalid MCP server identity")]
    InvalidIdentity,
    /// The page size was zero or exceeded the hard maximum.
    #[error("invalid MCP tool catalog page size")]
    InvalidPageSize,
    /// Cache TTL exceeded the hard maximum.
    #[error("invalid MCP tool catalog cache TTL")]
    InvalidCacheTtl,
    /// Instructions were empty, padded, oversized, or contained unsafe controls.
    #[error("invalid MCP server instructions")]
    InvalidInstructions,
}

/// Production MCP Tool handler backed by an immutable `StateKnot` registry.
#[derive(Clone)]
pub struct McpServerToolService {
    registry: McpServerToolRegistry,
    options: McpServerApplicationOptions,
    authorization: Arc<dyn McpServerToolAuthorization>,
}

impl McpServerToolService {
    /// Creates a service with an explicit decoded authorization policy.
    pub fn new<A>(
        registry: McpServerToolRegistry,
        options: McpServerApplicationOptions,
        authorization: A,
    ) -> Result<Self, McpServerToolServiceBuildError>
    where
        A: McpServerToolAuthorization,
    {
        Self::with_shared_authorization(registry, options, Arc::new(authorization))
    }

    /// Creates a service with an already shared authorization policy.
    pub fn with_shared_authorization(
        registry: McpServerToolRegistry,
        options: McpServerApplicationOptions,
        authorization: Arc<dyn McpServerToolAuthorization>,
    ) -> Result<Self, McpServerToolServiceBuildError> {
        if registry.contains_scoped_tools()
            && matches!(options.cache_scope, McpServerCacheScope::Public)
        {
            return Err(McpServerToolServiceBuildError::PublicCacheWithScopedTools);
        }
        Ok(Self {
            registry,
            options,
            authorization,
        })
    }

    fn page(
        &self,
        principal: &McpServerPrincipal,
        cursor: Option<&str>,
    ) -> Result<(Vec<Tool>, Option<String>), ErrorData> {
        let scope_tag = self.catalog_scope_tag(principal);
        let start = cursor.map_or(Ok(0), |cursor| self.parse_cursor(cursor, &scope_tag))?;
        let visible = self
            .registry
            .inner
            .tools
            .values()
            .filter(|entry| {
                entry
                    .definition
                    .required_scopes()
                    .all(|scope| principal.has_scope(scope))
            })
            .collect::<Vec<_>>();
        if start > visible.len() {
            return Err(ErrorData::invalid_params(
                "Invalid tool catalog cursor",
                None,
            ));
        }
        let end = start
            .saturating_add(self.options.page_size)
            .min(visible.len());
        let tools = visible
            .iter()
            .skip(start)
            .take(end - start)
            .map(|entry| entry.protocol.clone())
            .collect();
        let next = (end < visible.len()).then(|| self.cursor(&scope_tag, end));
        Ok((tools, next))
    }

    fn catalog_scope_tag(&self, principal: &McpServerPrincipal) -> String {
        if matches!(self.options.cache_scope, McpServerCacheScope::Public) {
            return "public".to_owned();
        }
        let mut material = principal.subject().as_bytes().to_vec();
        for scope in principal.scopes() {
            append_digest_part(&mut material, scope.as_bytes());
        }
        let digest = Digest::sha256(material).to_string();
        digest
            .strip_prefix("sha256:")
            .expect("StateKnot SHA-256 digest has stable prefix")
            .to_owned()
    }

    fn cursor(&self, scope_tag: &str, offset: usize) -> String {
        format!("v1.{}.{}.{}", self.registry.inner.digest, scope_tag, offset)
    }

    fn parse_cursor(&self, cursor: &str, scope_tag: &str) -> Result<usize, ErrorData> {
        if cursor.len() > 192 {
            return Err(ErrorData::invalid_params(
                "Invalid tool catalog cursor",
                None,
            ));
        }
        let mut parts = cursor.split('.');
        let valid_version = parts.next() == Some("v1");
        let valid_digest = parts.next() == Some(self.registry.inner.digest.as_ref());
        let valid_scope = parts.next() == Some(scope_tag);
        let offset = parts.next().and_then(|value| value.parse::<usize>().ok());
        if !valid_version
            || !valid_digest
            || !valid_scope
            || parts.next().is_some()
            || offset.is_none()
        {
            return Err(ErrorData::invalid_params(
                "Invalid tool catalog cursor",
                None,
            ));
        }
        Ok(offset.expect("validated cursor offset exists"))
    }

    async fn dispatch(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        let Some(registered) = self
            .registry
            .inner
            .tools
            .get(request.name.as_ref())
            .cloned()
        else {
            return Err(unavailable_tool());
        };
        let principal = mcp_server_principal(&context)
            .cloned()
            .ok_or_else(|| ErrorData::internal_error("MCP server boundary is missing", None))?;
        if registered
            .definition
            .required_scopes()
            .any(|scope| !principal.has_scope(scope))
        {
            return Err(unavailable_tool());
        }

        let arguments = request.arguments.unwrap_or_default();
        let input_responses = request.input_responses.unwrap_or_default();
        validate_inbound_mrtr(&input_responses, request.request_state.as_deref())?;
        let call = McpServerToolCall {
            name: Arc::from(request.name.into_owned()),
            arguments: Arc::new(arguments),
            input_responses: Arc::new(input_responses),
            request_state: request.request_state.map(Arc::from),
        };
        let authorization_request = McpServerToolAuthorizationRequest {
            principal: principal.clone(),
            definition: registered.definition.clone(),
            call: call.clone(),
        };
        match self.authorization.authorize(authorization_request).await {
            Ok(()) => {}
            Err(McpServerToolAuthorizationError::Forbidden) => return Err(unavailable_tool()),
            Err(McpServerToolAuthorizationError::Unavailable) => {
                return Err(ErrorData::internal_error(
                    "MCP tool authorization is unavailable",
                    None,
                ));
            }
        }

        let argument_value = Value::Object((*call.arguments).clone());
        if !registered.input_validator.is_valid(&argument_value) {
            return Err(ErrorData::invalid_params(
                "Tool arguments do not match input schema",
                None,
            ));
        }

        let tool_context = build_tool_context(&context, principal, &call)?;
        let outcome = registered
            .handler
            .call(call, tool_context)
            .await
            .map_err(handler_error_result)?;
        match outcome {
            McpServerToolOutcome::Complete(result) => {
                if !result.is_error
                    && let Some(validator) = &registered.output_validator
                {
                    let Some(structured) = result.structured_content.as_deref() else {
                        return Err(ErrorData::internal_error(
                            "MCP tool omitted required structured output",
                            None,
                        ));
                    };
                    if !validator.is_valid(structured) {
                        return Err(ErrorData::internal_error(
                            "MCP tool output failed schema validation",
                            None,
                        ));
                    }
                }
                Ok(result.into_protocol().into())
            }
            McpServerToolOutcome::InputRequired(result) => Ok(result.into_protocol().into()),
        }
    }
}

impl fmt::Debug for McpServerToolService {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpServerToolService")
            .field("registry", &self.registry)
            .field("options", &self.options)
            .field("authorization", &"[POLICY]")
            .finish_non_exhaustive()
    }
}

impl ServerHandler for McpServerToolService {
    fn supported_protocol_versions(&self) -> Cow<'static, [ProtocolVersion]> {
        Cow::Borrowed(&[ProtocolVersion::V_2026_07_28])
    }

    fn get_info(&self) -> ServerConfig {
        let mut info = ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
            .with_server_info(Implementation::new(
                self.options.server_name.to_string(),
                self.options.server_version.to_string(),
            ));
        if let Some(instructions) = &self.options.instructions {
            info = info.with_instructions(instructions.to_string());
        }
        info
    }

    async fn discover(
        &self,
        _context: RequestContext<RoleServer>,
    ) -> Result<DiscoverResult, ErrorData> {
        Ok(
            DiscoverResult::from_server_info(vec![ProtocolVersion::V_2026_07_28], self.get_info())
                .with_ttl_ms(self.options.cache_ttl_ms)
                .with_cache_scope(self.options.cache_scope.into()),
        )
    }

    fn get_tool(&self, name: &str) -> Option<Tool> {
        self.registry
            .inner
            .tools
            .get(name)
            .map(|entry| entry.protocol.clone())
    }

    async fn list_tools(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, ErrorData> {
        let principal = mcp_server_principal(&context)
            .ok_or_else(|| ErrorData::internal_error("MCP server boundary is missing", None))?;
        let (tools, next_cursor) = self.page(
            principal,
            request.as_ref().and_then(|value| value.cursor.as_deref()),
        )?;
        let mut result = ListToolsResult::with_all_items(tools)
            .with_ttl_ms(self.options.cache_ttl_ms)
            .with_cache_scope(self.options.cache_scope.into());
        result.next_cursor = next_cursor;
        Ok(result)
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        self.dispatch(request, context).await
    }
}

/// Invalid combination of frozen catalog and service policy.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum McpServerToolServiceBuildError {
    /// A catalog with scope-restricted entries cannot be shared-cacheable.
    #[error("scope-restricted MCP tools require a private catalog cache")]
    PublicCacheWithScopedTools,
}

fn unavailable_tool() -> ErrorData {
    ErrorData::invalid_params("Unknown or unavailable tool", None)
}

pub(crate) fn validate_inbound_mrtr(
    responses: &BTreeMap<String, Value>,
    request_state: Option<&str>,
) -> Result<(), ErrorData> {
    if responses.len() > MAX_INPUT_REQUESTS
        || request_state.is_some_and(|value| {
            value.is_empty()
                || value.len() > MAX_REQUEST_STATE_BYTES
                || value.chars().any(char::is_control)
        })
    {
        return Err(ErrorData::invalid_params(
            "Invalid MCP input response",
            None,
        ));
    }
    let value = serde_json::to_value(responses)
        .map_err(|_| ErrorData::invalid_params("Invalid MCP input response", None))?;
    if canonical_json_len(&value).map_or(true, |bytes| bytes > 8 * MEBIBYTE) {
        return Err(ErrorData::invalid_params(
            "Invalid MCP input response",
            None,
        ));
    }
    Ok(())
}

fn build_tool_context(
    context: &RequestContext<RoleServer>,
    principal: McpServerPrincipal,
    call: &McpServerToolCall,
) -> Result<McpServerToolContext, ErrorData> {
    let client = context.client_info();
    if let Some(client) = &client {
        validate_client_component(&client.name)?;
        validate_client_component(&client.version)?;
    }
    let capabilities = context.client_capabilities().unwrap_or_default();
    let capabilities_value = serde_json::to_value(&capabilities)
        .map_err(|_| ErrorData::internal_error("Failed to retain client capabilities", None))?;
    let binding = request_state_binding(&principal, call)?;
    let progress = context
        .meta
        .get_progress_token()
        .map(|token| McpServerProgressSink {
            peer: context.peer.clone(),
            token,
        });
    Ok(McpServerToolContext {
        principal,
        client_name: client.as_ref().map(|value| Arc::from(value.name.as_str())),
        client_version: client
            .as_ref()
            .map(|value| Arc::from(value.version.as_str())),
        client_capabilities: Arc::new(capabilities_value),
        cancellation: context.ct.clone(),
        progress,
        request_state_binding: binding.into(),
    })
}

fn validate_client_component(value: &str) -> Result<(), ErrorData> {
    if value.is_empty()
        || value.len() > 512
        || value.trim() != value
        || value.chars().any(char::is_control)
    {
        return Err(ErrorData::invalid_params(
            "Invalid MCP client identity",
            None,
        ));
    }
    Ok(())
}

fn request_state_binding(
    principal: &McpServerPrincipal,
    call: &McpServerToolCall,
) -> Result<Vec<u8>, ErrorData> {
    let arguments = serde_json_canonicalizer::to_vec(&Value::Object((*call.arguments).clone()))
        .map_err(|_| ErrorData::internal_error("Failed to bind MCP request state", None))?;
    let mut material = b"stateknot/mcp-server/request-state/v1".to_vec();
    append_digest_part(&mut material, principal.subject().as_bytes());
    append_digest_part(&mut material, call.name().as_bytes());
    append_digest_part(&mut material, &arguments);
    Ok(Digest::sha256(material).as_bytes().to_vec())
}

fn handler_error_result(error: McpServerToolHandlerError) -> ErrorData {
    match error {
        McpServerToolHandlerError::Unavailable => {
            ErrorData::internal_error("MCP tool dependency is unavailable", None)
        }
        McpServerToolHandlerError::Cancelled => {
            ErrorData::internal_error("MCP tool execution was cancelled", None)
        }
        McpServerToolHandlerError::Internal => {
            ErrorData::internal_error("MCP tool execution failed", None)
        }
        McpServerToolHandlerError::MissingRequiredClientCapability(capability) => {
            let mut required = ClientCapabilities::default();
            match capability {
                McpServerRequiredClientCapability::Elicitation => {
                    required.elicitation = Some(ElicitationCapability::default());
                }
                McpServerRequiredClientCapability::Sampling => {
                    required.sampling = Some(SamplingCapability::default());
                }
                McpServerRequiredClientCapability::Roots => {
                    required.roots = Some(RootsCapabilities::default());
                }
            }
            ErrorData::missing_required_client_capability(required)
        }
    }
}

fn canonical_json_len(value: &Value) -> Result<usize, serde_json::Error> {
    serde_json_canonicalizer::to_vec(value).map(|bytes| bytes.len())
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use bytes::Bytes;
    use http::{Request, StatusCode};
    use http_body_util::{BodyExt as _, Full};
    use serde_json::json;
    use tower_service::Service as _;

    use super::*;
    use crate::{McpServerAuthentication, McpServerHttpOptions, McpServerHttpService};

    #[derive(Clone)]
    struct EchoHandler {
        calls: Arc<AtomicUsize>,
    }

    impl McpServerToolHandler for EchoHandler {
        fn call(
            &self,
            call: McpServerToolCall,
            execution_context: McpServerToolContext,
        ) -> BoxFuture<'_, Result<McpServerToolOutcome, McpServerToolHandlerError>> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            let subject = execution_context.principal().subject().to_owned();
            let value = call
                .arguments()
                .get("value")
                .cloned()
                .unwrap_or(Value::Null);
            Box::pin(async move {
                let block = McpServerContent::text(format!("{subject}:{value}"))
                    .map_err(|_| McpServerToolHandlerError::Internal)?;
                McpServerToolResult::structured([block], json!({ "echo": value }))
                    .map(Into::into)
                    .map_err(|_| McpServerToolHandlerError::Internal)
            })
        }
    }

    #[derive(Clone, Copy)]
    struct MissingCapabilityHandler;

    impl McpServerToolHandler for MissingCapabilityHandler {
        fn call(
            &self,
            _call: McpServerToolCall,
            _context: McpServerToolContext,
        ) -> BoxFuture<'_, Result<McpServerToolOutcome, McpServerToolHandlerError>> {
            Box::pin(async {
                Err(McpServerToolHandlerError::MissingRequiredClientCapability(
                    McpServerRequiredClientCapability::Sampling,
                ))
            })
        }
    }

    #[derive(Clone)]
    struct DenyAuthorization {
        calls: Arc<AtomicUsize>,
    }

    impl McpServerToolAuthorization for DenyAuthorization {
        fn authorize(
            &self,
            _request: McpServerToolAuthorizationRequest,
        ) -> BoxFuture<'_, Result<(), McpServerToolAuthorizationError>> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async { Err(McpServerToolAuthorizationError::Forbidden) })
        }
    }

    fn echo_definition(name: &str) -> McpServerToolDefinition {
        McpServerToolDefinition::new(
            name,
            json!({
                "$schema": JSON_SCHEMA_2020_12,
                "type": "object",
                "properties": { "value": { "type": "string" } },
                "required": ["value"],
                "additionalProperties": false
            }),
        )
        .unwrap()
        .with_output_schema(json!({
            "$schema": JSON_SCHEMA_2020_12,
            "type": "object",
            "properties": { "echo": { "type": "string" } },
            "required": ["echo"],
            "additionalProperties": false
        }))
        .unwrap()
    }

    fn tool_service<A>(
        definitions: impl IntoIterator<Item = McpServerToolDefinition>,
        handler_calls: &Arc<AtomicUsize>,
        authorization: A,
    ) -> McpServerHttpService<McpServerToolService>
    where
        A: McpServerToolAuthorization,
    {
        let mut builder = McpServerToolRegistryBuilder::default();
        for definition in definitions {
            builder
                .register(
                    definition,
                    EchoHandler {
                        calls: handler_calls.clone(),
                    },
                )
                .unwrap();
        }
        let options = McpServerApplicationOptions::new(
            "stateknot-tool-test",
            "0.0.0",
            1,
            Duration::from_secs(60),
            McpServerCacheScope::Private,
        )
        .unwrap();
        let handler =
            McpServerToolService::new(builder.build().unwrap(), options, authorization).unwrap();
        McpServerHttpService::new(
            handler,
            McpServerHttpOptions::loopback(32124).unwrap(),
            McpServerAuthentication::anonymous_loopback(),
        )
        .unwrap()
    }

    fn protocol_request(
        method: &str,
        mut params: Value,
        name: Option<&str>,
    ) -> Request<Full<Bytes>> {
        params.as_object_mut().unwrap().insert(
            "_meta".to_owned(),
            json!({
                "io.modelcontextprotocol/protocolVersion": "2026-07-28",
                "io.modelcontextprotocol/clientInfo": {
                    "name": "stateknot-tool-test-client",
                    "version": "0.0.0"
                },
                "io.modelcontextprotocol/clientCapabilities": {}
            }),
        );
        let body = serde_json::to_vec(&json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": method,
            "params": params
        }))
        .unwrap();
        let mut builder = Request::builder()
            .method("POST")
            .uri("http://127.0.0.1:32124/mcp")
            .header("host", "127.0.0.1:32124")
            .header("origin", "http://127.0.0.1:32124")
            .header("content-type", "application/json")
            .header("accept", "application/json, text/event-stream")
            .header("mcp-protocol-version", "2026-07-28")
            .header("mcp-method", method);
        if let Some(name) = name {
            builder = builder.header("mcp-name", name);
        }
        builder.body(Full::new(Bytes::from(body))).unwrap()
    }

    async fn invoke(
        service: &mut McpServerHttpService<McpServerToolService>,
        method: &str,
        params: Value,
        name: Option<&str>,
    ) -> (StatusCode, Value) {
        let response = service
            .call(protocol_request(method, params, name))
            .await
            .unwrap();
        let status = response.status();
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let value = serde_json::from_slice(&bytes).unwrap_or_else(|error| {
            panic!(
                "invalid MCP response ({status}): {}; {error}",
                String::from_utf8_lossy(&bytes)
            )
        });
        (status, value)
    }

    #[test]
    fn registry_is_sorted_offline_and_rejects_remote_refs() {
        let calls = Arc::new(AtomicUsize::new(0));
        let mut builder = McpServerToolRegistryBuilder::default();
        for name in ["zeta", "alpha"] {
            builder
                .register(
                    echo_definition(name),
                    EchoHandler {
                        calls: calls.clone(),
                    },
                )
                .unwrap();
        }
        let registry = builder.build().unwrap();
        assert_eq!(
            registry
                .definitions()
                .map(McpServerToolDefinition::name)
                .collect::<Vec<_>>(),
            ["alpha", "zeta"]
        );

        let mut remote = McpServerToolRegistryBuilder::default();
        remote
            .register(
                McpServerToolDefinition::new(
                    "remote",
                    json!({
                        "$schema": JSON_SCHEMA_2020_12,
                        "$ref": "https://schemas.example.test/remote.json"
                    }),
                )
                .unwrap(),
                EchoHandler { calls },
            )
            .unwrap();
        assert!(matches!(
            remote.build(),
            Err(McpServerToolRegistryError::SchemaCompilation { .. })
        ));
    }

    #[test]
    fn content_and_results_are_bounded() {
        assert!(McpServerContent::image([1_u8, 2, 3], "image/png").is_ok());
        assert!(matches!(
            McpServerContent::audio([1_u8], "image/png"),
            Err(McpServerContentError::Invalid)
        ));
        assert!(matches!(
            McpServerContent::text("x".repeat(MAX_TEXT_CONTENT_BYTES + 1)),
            Err(McpServerContentError::TooLarge)
        ));
    }

    #[test]
    fn request_state_is_bound_expires_and_redacts_keys() {
        let codec = McpServerRequestStateCodec::new([7_u8; 32]).unwrap();
        assert!(!format!("{codec:?}").contains("777"));
        let binding = b"tenant-a|tool-a";
        let sealed = codec
            .seal_json(&json!({ "round": 1 }), binding, Duration::from_secs(60))
            .unwrap();
        assert_eq!(
            codec.open_json(&sealed, binding).unwrap(),
            json!({ "round": 1 })
        );
        assert!(matches!(
            codec.open_json(&sealed, b"tenant-b|tool-a"),
            Err(McpServerRequestStateError::InvalidState)
        ));
        assert!(matches!(
            codec.seal_json(&json!({}), binding, Duration::ZERO),
            Err(McpServerRequestStateError::InvalidOperation)
        ));
    }

    #[test]
    fn tool_names_and_scopes_fail_closed() {
        assert!(matches!(
            McpServerToolDefinition::new("not allowed", json!({ "type": "object" })),
            Err(McpServerToolDefinitionError::InvalidName)
        ));
        assert!(matches!(
            echo_definition("echo").with_required_scopes(["tool:call", "tool:call"]),
            Err(McpServerToolDefinitionError::DuplicateScope)
        ));
    }

    #[tokio::test]
    async fn frozen_registry_pages_and_dispatches_through_the_http_boundary() {
        let handler_calls = Arc::new(AtomicUsize::new(0));
        let mut service = tool_service(
            [echo_definition("zeta"), echo_definition("alpha")],
            &handler_calls,
            AllowMcpServerToolAuthorization,
        );

        let (status, first) = invoke(&mut service, "tools/list", json!({}), None).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(first.pointer("/result/tools/0/name"), Some(&json!("alpha")));
        assert_eq!(first.pointer("/result/ttlMs"), Some(&json!(60_000)));
        assert_eq!(first.pointer("/result/cacheScope"), Some(&json!("private")));
        let cursor = first
            .pointer("/result/nextCursor")
            .and_then(Value::as_str)
            .unwrap();
        let (status, second) = invoke(
            &mut service,
            "tools/list",
            json!({ "cursor": cursor }),
            None,
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(second.pointer("/result/tools/0/name"), Some(&json!("zeta")));
        assert!(second.pointer("/result/nextCursor").is_none());

        let (status, invalid) = invoke(
            &mut service,
            "tools/call",
            json!({ "name": "alpha", "arguments": { "value": 7 } }),
            Some("alpha"),
        )
        .await;
        assert_eq!(status, StatusCode::BAD_REQUEST);
        assert_eq!(invalid.pointer("/error/code"), Some(&json!(-32602)));
        assert_eq!(handler_calls.load(Ordering::SeqCst), 0);

        let (status, valid) = invoke(
            &mut service,
            "tools/call",
            json!({ "name": "alpha", "arguments": { "value": "ok" } }),
            Some("alpha"),
        )
        .await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(
            valid.pointer("/result/structuredContent/echo"),
            Some(&json!("ok"))
        );
        assert_eq!(valid.pointer("/result/isError"), Some(&json!(false)));
        assert_eq!(handler_calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn decoded_authorization_precedes_schema_diagnostics_and_dispatch() {
        let handler_calls = Arc::new(AtomicUsize::new(0));
        let authorization_calls = Arc::new(AtomicUsize::new(0));
        let mut service = tool_service(
            [echo_definition("protected")],
            &handler_calls,
            DenyAuthorization {
                calls: authorization_calls.clone(),
            },
        );
        let (status, denied) = invoke(
            &mut service,
            "tools/call",
            json!({ "name": "protected", "arguments": { "value": 7 } }),
            Some("protected"),
        )
        .await;
        assert_eq!(status, StatusCode::BAD_REQUEST);
        assert_eq!(denied.pointer("/error/code"), Some(&json!(-32602)));
        assert_eq!(
            denied.pointer("/error/message"),
            Some(&json!("Unknown or unavailable tool"))
        );
        assert_eq!(authorization_calls.load(Ordering::SeqCst), 1);
        assert_eq!(handler_calls.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn handler_can_report_a_missing_core_client_capability() {
        let mut builder = McpServerToolRegistryBuilder::default();
        builder
            .register(echo_definition("needs_sampling"), MissingCapabilityHandler)
            .unwrap();
        let options = McpServerApplicationOptions::new(
            "stateknot-capability-test",
            "0.0.0",
            10,
            Duration::from_secs(60),
            McpServerCacheScope::Private,
        )
        .unwrap();
        let handler = McpServerToolService::new(
            builder.build().unwrap(),
            options,
            AllowMcpServerToolAuthorization,
        )
        .unwrap();
        let mut service = McpServerHttpService::new(
            handler,
            McpServerHttpOptions::loopback(32124).unwrap(),
            McpServerAuthentication::anonymous_loopback(),
        )
        .unwrap();

        let (status, response) = invoke(
            &mut service,
            "tools/call",
            json!({ "name": "needs_sampling", "arguments": { "value": "ok" } }),
            Some("needs_sampling"),
        )
        .await;
        assert_eq!(status, StatusCode::BAD_REQUEST);
        assert_eq!(response.pointer("/error/code"), Some(&json!(-32021)));
        assert_eq!(
            response.pointer("/error/data/requiredCapabilities/sampling"),
            Some(&json!({}))
        );
    }
}