xberg 1.1.4

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! Xberg MCP server implementation.
//!
//! This module provides the main MCP server struct and startup functions.

use super::format::build_config;
use crate::ExtractionConfig;
use crate::service::{ExtractionRequest, ExtractionServiceBuilder};
use rmcp::{
    ServerHandler, ServiceExt,
    handler::server::{
        prompt::PromptContext,
        router::{prompt::PromptRouter, tool::ToolRouter},
        tool::ToolCallContext,
        wrapper::Parameters,
    },
    model::*,
    task_manager::{TaskExit, TaskManager, TaskOptions},
    tool, tool_handler, tool_router,
    transport::stdio,
};
use tower::util::BoxCloneService;

/// Tool names materialized as SEP-2663 tasks (`tasks/get`, `tasks/update`,
/// `tasks/cancel`) when the calling client declares the tasks extension
/// capability. These are the operations whose latency (large documents,
/// batches of inputs, or multi-gigabyte model downloads) benefits from
/// asynchronous polling instead of holding the `tools/call` request open
/// for the duration of the work. All other tools stay synchronous.
const TASK_ELIGIBLE_TOOLS: &[&str] = &["extract", "extract_batch", "cache_warm"];

/// Task TTL for `extract`/`extract_batch`: generous headroom above the
/// documented OCR performance targets (<2s single page, <10s 10-page
/// document) to cover pathological large or heavily-scanned documents.
const EXTRACTION_TASK_TTL_MS: u64 = 15 * 60 * 1000;

/// Task TTL for `cache_warm`: model downloads run over the network against
/// third-party hosts and can legitimately take longer than extraction.
const CACHE_WARM_TASK_TTL_MS: u64 = 30 * 60 * 1000;

#[cfg(any(
    test,
    paddle_ocr,
    feature = "layout-detection",
    feature = "embeddings",
    feature = "ner-onnx"
))]
#[allow(dead_code)] // Individual dispositions are feature-dependent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CacheWarmDisposition {
    AvailabilityConfirmed,
    Downloaded,
    AlreadyCached,
}

#[cfg(any(
    test,
    paddle_ocr,
    feature = "layout-detection",
    feature = "embeddings",
    feature = "ner-onnx"
))]
fn record_warmed_model(
    available: &mut Vec<String>,
    downloaded: &mut Vec<String>,
    already_cached: &mut Vec<String>,
    label: String,
    disposition: CacheWarmDisposition,
) {
    available.push(label.clone());
    match disposition {
        CacheWarmDisposition::AvailabilityConfirmed => {}
        CacheWarmDisposition::Downloaded => downloaded.push(label),
        CacheWarmDisposition::AlreadyCached => already_cached.push(label),
    }
}

#[cfg(feature = "mcp-http")]
use rmcp::transport::streamable_http_server::{
    StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
};

/// Xberg MCP server.
///
/// Provides document extraction capabilities via MCP tools.
///
/// The server loads a default extraction configuration from xberg.toml/yaml/json
/// via discovery. Per-request OCR settings override the defaults.
#[cfg_attr(alef, alef(skip))]
pub struct XbergMcp {
    tool_router: ToolRouter<XbergMcp>,
    /// Prompt router for the three guided-workflow prompts.
    prompt_router: PromptRouter<XbergMcp>,
    /// Default extraction configuration loaded from config file via discovery
    default_config: std::sync::Arc<ExtractionConfig>,
    /// Tower service for extraction requests with tracing and optional metrics layers.
    ///
    /// Wrapped in `Mutex` because `BoxCloneService` is `Send` but not `Sync`,
    /// while `XbergMcp` must be `Sync` for the MCP handler trait.
    /// The lock is held only long enough to clone the service.
    extraction_service:
        std::sync::Mutex<BoxCloneService<ExtractionRequest, crate::types::ExtractedDocument, crate::XbergError>>,
    /// SEP-2663 task store for tools in [`TASK_ELIGIBLE_TOOLS`]. Cheaply
    /// cloneable; all clones of an `XbergMcp` share the same task state.
    tasks: TaskManager,
}

impl Clone for XbergMcp {
    fn clone(&self) -> Self {
        let svc = self
            .extraction_service
            .lock()
            .expect("extraction service lock poisoned")
            .clone();
        Self {
            tool_router: self.tool_router.clone(),
            prompt_router: self.prompt_router.clone(),
            default_config: self.default_config.clone(),
            extraction_service: std::sync::Mutex::new(svc),
            tasks: self.tasks.clone(),
        }
    }
}

#[tool_router]
impl XbergMcp {
    /// Create a new Xberg MCP server instance with default config.
    ///
    /// Uses `ExtractionConfig::discover()` to search for xberg.toml/yaml/json
    /// in current and parent directories. Falls back to default configuration if
    /// no config file is found.
    #[allow(clippy::manual_unwrap_or_default)]
    pub(crate) fn new() -> crate::Result<Self> {
        let config = match ExtractionConfig::discover()? {
            Some(config) => {
                #[cfg(feature = "api")]
                tracing::info!("Loaded extraction config from discovered file");
                config
            }
            None => {
                #[cfg(feature = "api")]
                tracing::info!("No config file found, using default configuration");
                ExtractionConfig::default()
            }
        };

        Ok(Self::with_config(config))
    }

    /// Create a new Xberg MCP server instance with explicit config.
    ///
    /// # Arguments
    ///
    /// * `config` - Default extraction configuration for all tool calls
    pub(crate) fn with_config(config: ExtractionConfig) -> Self {
        let extraction_service_builder = ExtractionServiceBuilder::new().with_tracing();
        #[cfg(feature = "otel")]
        let extraction_service_builder = extraction_service_builder.with_metrics();
        let extraction_service = extraction_service_builder
            .build()
            .expect("the built-in MCP extraction service uses a valid concurrency limit");

        Self {
            tool_router: Self::tool_router(),
            prompt_router: super::prompts::build_prompt_router(),
            default_config: std::sync::Arc::new(config),
            extraction_service: std::sync::Mutex::new(extraction_service),
            tasks: TaskManager::new(),
        }
    }

    /// Extract content from bytes or a URI.
    #[tool(
        description = "Extract content from bytes, a local path, file:// URI, remote document URL, or website URL.",
        annotations(title = "Extract", read_only_hint = true, idempotent_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::ExtractionResult>()
    )]
    async fn extract(
        &self,
        Parameters(params): Parameters<super::params::ExtractParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        use super::errors::map_xberg_error_to_mcp;

        let use_toon = params
            .response_format
            .as_deref()
            .is_some_and(|f| f.eq_ignore_ascii_case("toon"));

        let mut config =
            build_config(&self.default_config, params.config).map_err(|e| rmcp::ErrorData::invalid_params(e, None))?;
        apply_pdf_password(&mut config, params.pdf_password)?;
        let input = parse_extract_input(params.input)?;

        let output = crate::extract(input, &config).await.map_err(map_xberg_error_to_mcp)?;
        let response = format_extraction_result_for_wire(&output, use_toon);
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&output).ok();
        Ok(tool_result)
    }

    /// Extract content from multiple bytes or URI inputs.
    #[tool(
        description = "Extract content from multiple bytes, local paths, file:// URIs, remote document URLs, or website URLs.",
        annotations(title = "Extract Batch", read_only_hint = true, idempotent_hint = true, open_world_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::ExtractionResult>()
    )]
    async fn extract_batch(
        &self,
        Parameters(params): Parameters<super::params::ExtractBatchParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        use super::errors::map_xberg_error_to_mcp;

        let use_toon = params
            .response_format
            .as_deref()
            .is_some_and(|f| f.eq_ignore_ascii_case("toon"));

        let mut config =
            build_config(&self.default_config, params.config).map_err(|e| rmcp::ErrorData::invalid_params(e, None))?;
        apply_pdf_password(&mut config, params.pdf_password)?;
        let inputs = params
            .inputs
            .into_iter()
            .map(parse_extract_input)
            .collect::<Result<Vec<_>, _>>()?;

        let output = crate::extract_batch(inputs, &config)
            .await
            .map_err(map_xberg_error_to_mcp)?;
        let response = format_extraction_result_for_wire(&output, use_toon);
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&output).ok();
        Ok(tool_result)
    }

    /// Detect the MIME type of a file.
    ///
    /// This tool identifies the file format, useful for determining which extractor to use.
    #[tool(
        description = "Detect the MIME type of a file. Returns the detected MIME type string.",
        annotations(title = "Detect MIME Type", read_only_hint = true, idempotent_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::DetectMimeTypeOutput>()
    )]
    fn detect_mime_type(
        &self,
        Parameters(params): Parameters<super::params::DetectMimeTypeParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        use super::errors::map_xberg_error_to_mcp;
        use crate::detect_mime_type;

        let mime_type = detect_mime_type(params.path.clone(), params.use_content).map_err(map_xberg_error_to_mcp)?;

        let dto = super::schema::DetectMimeTypeOutput {
            mime_type: mime_type.clone(),
        };
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(mime_type)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// Get cache statistics.
    ///
    /// This tool returns statistics about the cache including total files, size, and disk space.
    #[tool(
        description = "Get cache statistics including total files, size, and available disk space.",
        annotations(title = "Cache Stats", read_only_hint = true, idempotent_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::CacheStatsOutput>()
    )]
    fn cache_stats(
        &self,
        Parameters(_): Parameters<super::params::EmptyParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        use super::errors::map_xberg_error_to_mcp;
        use crate::cache;

        let cache_dir = crate::cache_dir::resolve_cache_base();

        let stats = cache::get_cache_metadata(cache_dir.to_str().unwrap_or(".")).map_err(map_xberg_error_to_mcp)?;

        let response = format!(
            "Cache Statistics\n\
             ================\n\
             Directory: {}\n\
             Total files: {}\n\
             Total size: {:.2} MB\n\
             Available space: {:.2} MB\n\
             Oldest file age: {:.2} days\n\
             Newest file age: {:.2} days",
            cache_dir.to_string_lossy(),
            stats.total_files,
            stats.total_size_mb,
            stats.available_space_mb,
            stats.oldest_file_age_days,
            stats.newest_file_age_days
        );

        let dto = super::schema::CacheStatsOutput {
            directory: cache_dir.to_string_lossy().into_owned(),
            total_files: stats.total_files as u64,
            total_size_mb: stats.total_size_mb,
            available_space_mb: stats.available_space_mb,
        };
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// List all supported document formats.
    ///
    /// This tool returns all file extensions and MIME types that Xberg can process.
    #[tool(
        description = "List all supported document formats with their file extensions and MIME types.",
        annotations(title = "List Formats", read_only_hint = true, idempotent_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::ListFormatsOutput>()
    )]
    fn list_formats(
        &self,
        Parameters(_): Parameters<super::params::EmptyParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        let formats = crate::core::mime::list_supported_formats();
        let response = serde_json::to_string_pretty(&formats).unwrap_or_default();
        let dto = super::schema::ListFormatsOutput {
            formats: formats
                .into_iter()
                .map(|f| serde_json::to_value(f).unwrap_or_default())
                .collect(),
        };
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// Clear the Xberg-managed cache.
    ///
    /// Shared Hugging Face Hub model cache files are intentionally excluded.
    #[tool(
        description = "Clear Xberg-managed cache files. Shared Hugging Face Hub model cache files are not removed.",
        annotations(title = "Clear Cache", read_only_hint = false, destructive_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::CacheClearOutput>()
    )]
    fn cache_clear(
        &self,
        Parameters(_): Parameters<super::params::EmptyParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        use super::errors::map_xberg_error_to_mcp;
        use crate::cache;

        let cache_dir = crate::cache_dir::resolve_cache_base();

        let (removed_files, freed_mb) =
            cache::clear_cache_directory(cache_dir.to_str().unwrap_or(".")).map_err(map_xberg_error_to_mcp)?;

        let response = format!(
            "Xberg-managed cache cleared successfully\n\
             Directory: {}\n\
             Removed files: {}\n\
             Freed space: {:.2} MB\n\
             Shared Hugging Face cache cleared: no",
            cache_dir.to_string_lossy(),
            removed_files,
            freed_mb
        );

        let dto = super::schema::CacheClearOutput {
            directory: cache_dir.to_string_lossy().into_owned(),
            removed_files: removed_files as u64,
            freed_mb,
        };
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// Get Xberg version information.
    ///
    /// Returns the current version of the Xberg library.
    #[tool(
        description = "Get the current Xberg library version.",
        annotations(title = "Get Version", read_only_hint = true, idempotent_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::VersionOutput>()
    )]
    fn get_version(
        &self,
        Parameters(_): Parameters<super::params::EmptyParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        let version = env!("CARGO_PKG_VERSION");
        let dto = super::schema::VersionOutput {
            version: version.to_string(),
        };
        let response = serde_json::to_string_pretty(&dto).unwrap_or_default();
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// Get model manifest with expected model files and checksums.
    ///
    /// Returns a manifest of all model files Xberg expects, including
    /// their sizes and SHA256 checksums.
    #[tool(
        description = "Get model manifest listing expected model files, sizes, and SHA256 checksums.",
        annotations(title = "Cache Manifest", read_only_hint = true, idempotent_hint = true),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::CacheManifestOutput>()
    )]
    fn cache_manifest(
        &self,
        Parameters(_): Parameters<super::params::EmptyParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        #[allow(unused_mut)]
        let mut entries: Vec<serde_json::Value> = Vec::new();

        #[cfg(paddle_ocr)]
        {
            let manifest = crate::paddle_ocr::ModelManager::manifest();
            for entry in manifest {
                entries.push(serde_json::to_value(&entry).unwrap_or_default());
            }
        }

        #[cfg(feature = "layout-detection")]
        {
            let manifest = crate::layout::LayoutModelManager::manifest();
            for entry in manifest {
                entries.push(serde_json::to_value(&entry).unwrap_or_default());
            }
        }

        #[cfg(feature = "formula-recognition")]
        {
            let manifest = crate::formula_recognition::manifest();
            for entry in manifest {
                entries.push(serde_json::to_value(&entry).unwrap_or_default());
            }
        }

        #[cfg(feature = "ner-onnx")]
        {
            let manifest = crate::text::ner::manifest();
            for entry in manifest {
                entries.push(serde_json::to_value(&entry).unwrap_or_default());
            }
        }

        let total_size_bytes: u64 = entries
            .iter()
            .filter_map(|e| e.get("size_bytes").and_then(|v| v.as_u64()))
            .sum();
        let version = env!("CARGO_PKG_VERSION");

        let dto = super::schema::CacheManifestOutput {
            xberg_version: version.to_string(),
            model_count: entries.len(),
            total_size_bytes,
            models: entries,
        };
        let response = serde_json::to_string_pretty(&dto).unwrap_or_default();
        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(response)]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }

    /// Download and cache model files.
    ///
    /// Eagerly downloads model files so they are available for offline use.
    /// Hugging Face artifacts remain in the standard shared HF cache.
    #[tool(
        description = "Download model files for offline use. Hugging Face artifacts, including GLiNER NER models, remain in the standard shared HF cache.",
        annotations(
            title = "Cache Warm",
            read_only_hint = false,
            destructive_hint = false,
            open_world_hint = true
        ),
        output_schema = rmcp::handler::server::common::schema_for_output::<super::schema::CacheWarmOutput>()
    )]
    #[allow(unused_mut)]
    fn cache_warm(
        &self,
        Parameters(params): Parameters<super::params::CacheWarmParams>,
    ) -> Result<CallToolResult, rmcp::ErrorData> {
        if let Some(ref name) = params.embedding_model
            && name.trim().is_empty()
        {
            return Err(rmcp::ErrorData::invalid_params(
                "Field 'embedding_model' must not be empty. Omit the field or provide a valid preset name.".to_string(),
                None,
            ));
        }
        if let Some(ref name) = params.ner_model
            && name.trim().is_empty()
        {
            return Err(rmcp::ErrorData::invalid_params(
                "Field 'ner_model' must not be empty. Omit the field or provide a valid model name.".to_string(),
                None,
            ));
        }

        let cache_base = resolve_cache_base();

        let mut available: Vec<String> = Vec::new();
        let mut downloaded: Vec<String> = Vec::new();
        let mut already_cached: Vec<String> = Vec::new();

        #[cfg(paddle_ocr)]
        {
            let paddle_dir = cache_base.join("paddle-ocr");
            let manager = crate::paddle_ocr::ModelManager::new(paddle_dir);
            manager.ensure_all_models().map_err(|e| {
                rmcp::ErrorData::internal_error(format!("Failed to download PaddleOCR models: {}", e), None)
            })?;
            record_warmed_model(
                &mut available,
                &mut downloaded,
                &mut already_cached,
                "paddle-ocr v2 (server+mobile det, cls, doc_ori, unified+per-script rec)".to_string(),
                CacheWarmDisposition::AvailabilityConfirmed,
            );
        }

        #[cfg(feature = "formula-recognition")]
        {
            let formula_dir = cache_base.join("formula-recognition");
            let was_cached = crate::formula_recognition::models_cached_in(Some(&formula_dir));
            crate::formula_recognition::ensure_models_in(Some(&formula_dir)).map_err(|e| {
                rmcp::ErrorData::internal_error(format!("Failed to download formula recognition models: {e}"), None)
            })?;
            record_warmed_model(
                &mut available,
                &mut downloaded,
                &mut already_cached,
                "formula-recognition (latex_ocr)".to_string(),
                if was_cached {
                    CacheWarmDisposition::AlreadyCached
                } else {
                    CacheWarmDisposition::Downloaded
                },
            );
        }

        #[cfg(feature = "layout-detection")]
        {
            let layout_dir = cache_base.join("layout");
            let manager = crate::layout::LayoutModelManager::new(Some(layout_dir));
            let rtdetr_was_cached = manager.is_rtdetr_cached();
            let tatr_was_cached = manager.is_tatr_cached();
            if rtdetr_was_cached && tatr_was_cached {
                record_warmed_model(
                    &mut available,
                    &mut downloaded,
                    &mut already_cached,
                    "layout (rtdetr, tatr)".to_string(),
                    CacheWarmDisposition::AlreadyCached,
                );
            } else {
                manager.ensure_all_models().map_err(|e| {
                    rmcp::ErrorData::internal_error(format!("Failed to download layout models: {}", e), None)
                })?;
                let disposition = if !rtdetr_was_cached && !tatr_was_cached {
                    CacheWarmDisposition::Downloaded
                } else {
                    CacheWarmDisposition::AvailabilityConfirmed
                };
                record_warmed_model(
                    &mut available,
                    &mut downloaded,
                    &mut already_cached,
                    "layout (rtdetr, tatr)".to_string(),
                    disposition,
                );
            }
        }

        #[cfg(feature = "embeddings")]
        {
            let embeddings_dir = cache_base.join("embeddings");
            let presets_to_warm: Vec<crate::EmbeddingPreset> = if params.all_embeddings {
                crate::embeddings::EMBEDDING_PRESETS.clone()
            } else if let Some(ref name) = params.embedding_model {
                match crate::embeddings::get_preset(name) {
                    Some(preset) => vec![preset],
                    None => {
                        let available: Vec<String> = crate::embeddings::list_presets();
                        return Err(rmcp::ErrorData::invalid_params(
                            format!(
                                "Unknown embedding preset '{}'. Available: {}",
                                name,
                                available.join(", ")
                            ),
                            None,
                        ));
                    }
                }
            } else {
                vec![]
            };

            for preset in &presets_to_warm {
                let label = format!("embedding ({})", preset.name);
                crate::embeddings::warm_model(
                    &crate::core::config::EmbeddingModelType::Preset {
                        name: preset.name.clone(),
                    },
                    Some(embeddings_dir.clone()),
                )
                .map_err(|e| {
                    rmcp::ErrorData::internal_error(
                        format!("Failed to download embedding model '{}': {}", preset.name, e),
                        None,
                    )
                })?;
                record_warmed_model(
                    &mut available,
                    &mut downloaded,
                    &mut already_cached,
                    label,
                    CacheWarmDisposition::AvailabilityConfirmed,
                );
            }
        }

        #[cfg(not(feature = "embeddings"))]
        {
            if params.all_embeddings || params.embedding_model.is_some() {
                return Err(rmcp::ErrorData::invalid_params(
                    "Embedding model warming requires the 'embeddings' feature to be enabled".to_string(),
                    None,
                ));
            }
        }

        #[cfg(feature = "ner-onnx")]
        {
            if params.ner || params.all_ner_models || params.ner_model.is_some() {
                let models_to_warm: Vec<String> = if params.all_ner_models {
                    crate::text::ner::known_models().iter().map(|s| s.to_string()).collect()
                } else if let Some(ref name) = params.ner_model {
                    vec![name.clone()]
                } else {
                    vec![crate::text::ner::default_model_name().to_string()]
                };

                for model in &models_to_warm {
                    let path = crate::text::ner::download_model(model, None).map_err(|e| {
                        rmcp::ErrorData::internal_error(
                            format!("Failed to download NER model '{}': {}", model, e),
                            None,
                        )
                    })?;
                    record_warmed_model(
                        &mut available,
                        &mut downloaded,
                        &mut already_cached,
                        format!("ner gliner ({model}) -> {} (Hugging Face cache)", path.display()),
                        CacheWarmDisposition::AvailabilityConfirmed,
                    );
                }
            }
        }

        #[cfg(not(feature = "ner-onnx"))]
        {
            if params.ner || params.all_ner_models || params.ner_model.is_some() {
                return Err(rmcp::ErrorData::invalid_params(
                    "NER model warming requires the 'ner-onnx' feature to be enabled".to_string(),
                    None,
                ));
            }
        }

        let dto = super::schema::CacheWarmOutput {
            cache_dir: cache_base.to_string_lossy().into_owned(),
            available,
            downloaded,
            already_cached,
        };

        let response = serde_json::json!({
            "cache_dir": cache_base.to_string_lossy(),
            "xberg_cache_dir": cache_base.to_string_lossy(),
            "hugging_face_cache": if params.ner || params.all_ner_models || params.ner_model.is_some() {
                Some("HF_HUB_CACHE/HF_HOME/platform default")
            } else {
                None
            },
            "available": dto.available.clone(),
            "downloaded": dto.downloaded.clone(),
            "already_cached": dto.already_cached.clone(),
        });

        let mut tool_result = CallToolResult::success(vec![ContentBlock::text(
            serde_json::to_string_pretty(&response).unwrap_or_default(),
        )]);
        tool_result.structured_content = serde_json::to_value(&dto).ok();
        Ok(tool_result)
    }
}

impl XbergMcp {
    /// Materialize a SEP-2663 task for `request` and return its
    /// `CreateTaskResult` handle. Only called for tool names in
    /// [`TASK_ELIGIBLE_TOOLS`] once the caller has confirmed the client
    /// declared the tasks extension capability; unknown names are rejected
    /// rather than silently falling back to synchronous execution, since a
    /// caller reaching this method already committed to the task path.
    ///
    /// Cancellation is cooperative (SEP-2663): a `tasks/cancel` unblocks
    /// [`rmcp::task_manager::TaskContext::cancelled`] and the task settles as
    /// `cancelled` immediately, but the underlying extraction/download may
    /// keep running in the background to completion since the core
    /// extraction APIs do not accept a cancellation token today.
    fn spawn_task_for_tool(&self, request: CallToolRequestParams) -> Result<CallToolResponse, rmcp::ErrorData> {
        let arguments = serde_json::Value::Object(request.arguments.unwrap_or_default());
        let server = self.clone();

        let task = match request.name.as_ref() {
            "extract" => {
                let params: super::params::ExtractParams = serde_json::from_value(arguments)
                    .map_err(|e| rmcp::ErrorData::invalid_params(format!("Invalid extract params: {e}"), None))?;
                self.tasks
                    .spawn(TaskOptions::new().with_ttl_ms(EXTRACTION_TASK_TTL_MS), move |ctx| {
                        Box::pin(async move {
                            tokio::select! {
                                _ = ctx.cancelled() => Err(TaskExit::Cancelled),
                                result = server.extract(Parameters(params)) => result.map_err(TaskExit::Error),
                            }
                        })
                    })
            }
            "extract_batch" => {
                let params: super::params::ExtractBatchParams = serde_json::from_value(arguments)
                    .map_err(|e| rmcp::ErrorData::invalid_params(format!("Invalid extract_batch params: {e}"), None))?;
                self.tasks
                    .spawn(TaskOptions::new().with_ttl_ms(EXTRACTION_TASK_TTL_MS), move |ctx| {
                        Box::pin(async move {
                            tokio::select! {
                                _ = ctx.cancelled() => Err(TaskExit::Cancelled),
                                result = server.extract_batch(Parameters(params)) => result.map_err(TaskExit::Error),
                            }
                        })
                    })
            }
            "cache_warm" => {
                let params: super::params::CacheWarmParams = serde_json::from_value(arguments)
                    .map_err(|e| rmcp::ErrorData::invalid_params(format!("Invalid cache_warm params: {e}"), None))?;
                self.tasks
                    .spawn(TaskOptions::new().with_ttl_ms(CACHE_WARM_TASK_TTL_MS), move |ctx| {
                        Box::pin(async move {
                            // cache_warm is synchronous (blocking downloads/IO); run it off the
                            // async runtime per the async-and-concurrency rule.
                            let handle = tokio::task::spawn_blocking(move || server.cache_warm(Parameters(params)));
                            tokio::select! {
                                _ = ctx.cancelled() => Err(TaskExit::Cancelled),
                                joined = handle => match joined {
                                    Ok(result) => result.map_err(TaskExit::Error),
                                    Err(join_error) => Err(TaskExit::Error(rmcp::ErrorData::internal_error(
                                        format!("cache_warm task panicked: {join_error}"),
                                        None,
                                    ))),
                                },
                            }
                        })
                    })
            }
            other => {
                return Err(rmcp::ErrorData::invalid_params(
                    format!("'{other}' is not a task-eligible tool"),
                    None,
                ));
            }
        };

        Ok(CallToolResponse::Task(CreateTaskResult::new(task)))
    }
}

/// Resolve the cache base directory.
fn resolve_cache_base() -> std::path::PathBuf {
    crate::cache_dir::resolve_cache_base()
}

fn parse_extract_input(value: serde_json::Value) -> Result<crate::ExtractInput, rmcp::ErrorData> {
    if let Some(config) = value.get("config") {
        crate::core::config::request_security::validate_caller_extraction_config(config)
            .map_err(|message| rmcp::ErrorData::invalid_params(message, None))?;
    }
    serde_json::from_value::<crate::ExtractInput>(value)
        .map_err(|error| rmcp::ErrorData::invalid_params(format!("Invalid ExtractInput: {error}"), None))
}

fn format_extraction_result_for_wire(output: &crate::ExtractionResult, use_toon: bool) -> String {
    if use_toon {
        serde_toon::to_string(output).unwrap_or_else(|error| {
            tracing::error!(%error, "Failed to serialize extraction result to TOON, falling back to JSON");
            serde_json::to_string_pretty(output).unwrap_or_default()
        })
    } else {
        serde_json::to_string_pretty(output).unwrap_or_default()
    }
}

fn apply_pdf_password(config: &mut ExtractionConfig, password: Option<String>) -> Result<(), rmcp::ErrorData> {
    let Some(password) = password else {
        return Ok(());
    };
    if password.is_empty() {
        return Err(rmcp::ErrorData::invalid_params(
            "pdf_password must not be empty when set".to_string(),
            None,
        ));
    }

    #[cfg(feature = "pdf")]
    {
        let pdf_options = config
            .pdf_options
            .get_or_insert_with(crate::core::config::pdf::PdfConfig::default);
        pdf_options.passwords.get_or_insert_with(Vec::new).push(password);
        Ok(())
    }

    #[cfg(not(feature = "pdf"))]
    {
        let _ = config;
        Err(rmcp::ErrorData::invalid_params(
            "pdf_password requires the 'pdf' feature to be enabled".to_string(),
            None,
        ))
    }
}

/// Handle completion requests for prompt arguments and resource URIs.
fn complete_impl(request: CompleteRequestParams) -> Result<CompleteResult, rmcp::ErrorData> {
    use rmcp::model::{CompletionInfo, Reference};

    let arg_name = &request.argument.name;
    let arg_value = &request.argument.value;

    let candidates: Vec<String> = match &request.r#ref {
        Reference::Prompt(prompt_ref) => match (prompt_ref.name.as_str(), arg_name.as_str()) {
            (_, "languages") => complete_ocr_languages(arg_value),
            (_, "preset") => complete_embedding_presets(arg_value),
            (_, "chunker_type") => complete_chunker_types(arg_value),
            (_, "output_format") => complete_output_formats(arg_value),
            _ => vec![],
        },
        Reference::Resource(_) => vec![],
        _ => vec![],
    };

    let completion = CompletionInfo::with_all_values(candidates).unwrap_or_default();
    Ok(CompleteResult::new(completion))
}

/// Return OCR language code completions filtered by the given prefix.
fn complete_ocr_languages(prefix: &str) -> Vec<String> {
    let all = [
        "afr", "amh", "ara", "asm", "aze", "bel", "ben", "bod", "bos", "bul", "cat", "ceb", "ces", "chi_sim",
        "chi_tra", "chr", "cos", "cym", "dan", "deu", "div", "dzo", "ell", "eng", "enm", "epo", "est", "eus", "fao",
        "fas", "fil", "fin", "fra", "frm", "gle", "glg", "grc", "guj", "hat", "heb", "hin", "hrv", "hun", "hye", "iku",
        "ind", "isl", "ita", "ita_old", "jav", "jpn", "kan", "kat", "kaz", "khm", "kir", "kor", "kur", "lao", "lat",
        "lav", "lit", "ltz", "mal", "mar", "mkd", "mlt", "mon", "mri", "msa", "mya", "nep", "nor", "oci", "ori", "pan",
        "pol", "por", "pus", "ron", "rus", "san", "sin", "slk", "slv", "snd", "spa", "spa_old", "sqi", "srp", "swa",
        "swe", "syr", "tam", "tat", "tel", "tgk", "tgl", "tha", "tir", "ton", "tur", "uig", "ukr", "urd", "uzb", "vie",
        "yid", "yor",
    ];
    let last = prefix.split(',').next_back().unwrap_or(prefix).trim();
    all.iter()
        .filter(|lang| lang.starts_with(last))
        .map(|s| s.to_string())
        .take(20)
        .collect()
}

/// Return embedding preset completions filtered by prefix.
fn complete_embedding_presets(prefix: &str) -> Vec<String> {
    let presets = ["speed", "balanced", "quality"];
    presets
        .iter()
        .filter(|p| p.starts_with(prefix))
        .map(|s| s.to_string())
        .collect()
}

/// Return chunker type completions filtered by prefix.
fn complete_chunker_types(prefix: &str) -> Vec<String> {
    let types = ["text", "markdown", "yaml", "semantic"];
    types
        .iter()
        .filter(|t| t.starts_with(prefix))
        .map(|s| s.to_string())
        .collect()
}

/// Return output format completions filtered by prefix.
fn complete_output_formats(prefix: &str) -> Vec<String> {
    let formats = ["json", "toon"];
    formats
        .iter()
        .filter(|f| f.starts_with(prefix))
        .map(|s| s.to_string())
        .collect()
}

#[tool_handler]
impl ServerHandler for XbergMcp {
    fn get_info(&self) -> ServerInfo {
        let capabilities = ServerCapabilities::builder()
            .enable_tools()
            .enable_resources()
            .enable_prompts()
            .enable_completions()
            .enable_tasks()
            .build();

        let server_info = Implementation::new("xberg-mcp", env!("CARGO_PKG_VERSION"))
            .with_title("Xberg Document Intelligence MCP Server")
            .with_description(
                "Document intelligence library for extracting content from PDFs, images, office documents, and more.",
            )
            .with_website_url("https://docs.xberg.io");

        InitializeResult::new(capabilities)
            .with_server_info(server_info)
            .with_instructions(
                "Extract content from documents in various formats. Supports PDFs, Word documents, \
                 Excel spreadsheets, images (with OCR), HTML, emails, and more. Use enable_ocr=true \
                 for scanned documents, force_ocr=true to always use OCR even if text extraction \
                 succeeds. Use disable_ocr=true to skip OCR entirely (images return metadata only).",
            )
    }

    /// Dispatch `tools/call`, materializing a SEP-2663 task for
    /// [`TASK_ELIGIBLE_TOOLS`] when the client declared the tasks extension
    /// capability, and falling back to the standard synchronous tool router
    /// otherwise (including for clients that never declared the capability).
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> Result<CallToolResponse, rmcp::ErrorData> {
        let client_supports_tasks = context.client_capabilities().is_some_and(|caps| caps.supports_tasks());

        if client_supports_tasks && TASK_ELIGIBLE_TOOLS.contains(&request.name.as_ref()) {
            return self.spawn_task_for_tool(request);
        }

        let tcc = ToolCallContext::new(self, request, context);
        self.tool_router.call(tcc).await
    }

    /// SEP-2663 `tasks/get`: return the current task state.
    async fn get_task(
        &self,
        request: GetTaskParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> Result<GetTaskResult, rmcp::ErrorData> {
        Ok(GetTaskResult::new(self.tasks.get_task(&request.task_id)?))
    }

    /// SEP-2663 `tasks/update`: none of our task-eligible tools currently
    /// surface mid-task `inputRequests` (elicitation/sampling/roots), so
    /// this only exists to acknowledge the request per spec; it delegates to
    /// the shared [`TaskManager`], which ignores unknown/stale keys.
    async fn update_task(
        &self,
        request: UpdateTaskParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> Result<(), rmcp::ErrorData> {
        self.tasks.update_task(&request.task_id, request.input_responses)
    }

    /// SEP-2663 `tasks/cancel`: cooperative cancellation, see
    /// [`XbergMcp::spawn_task_for_tool`] for what "cooperative" means here.
    async fn cancel_task(
        &self,
        request: CancelTaskParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> Result<(), rmcp::ErrorData> {
        self.tasks.cancel_task(&request.task_id)
    }

    fn list_resources(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListResourcesResult, rmcp::ErrorData>> + rmcp::service::MaybeSendFuture + '_
    {
        std::future::ready(Ok(super::resources::list_resources()))
    }

    fn list_resource_templates(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListResourceTemplatesResult, rmcp::ErrorData>>
    + rmcp::service::MaybeSendFuture
    + '_ {
        std::future::ready(Ok(super::resources::list_resource_templates()))
    }

    fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ReadResourceResponse, rmcp::ErrorData>> + rmcp::service::MaybeSendFuture + '_
    {
        std::future::ready(super::resources::read_resource(&request.uri).map(Into::into))
    }

    fn list_prompts(
        &self,
        _request: Option<PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<ListPromptsResult, rmcp::ErrorData>> + rmcp::service::MaybeSendFuture + '_
    {
        let prompts = self.prompt_router.list_all();
        std::future::ready(Ok(ListPromptsResult::with_all_items(prompts)))
    }

    fn get_prompt(
        &self,
        request: GetPromptRequestParams,
        context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<GetPromptResponse, rmcp::ErrorData>> + rmcp::service::MaybeSendFuture + '_
    {
        let pr = self.prompt_router.clone();
        let pc = PromptContext::new(self, request.name, request.arguments, context);
        async move { pr.get_prompt(pc).await }
    }

    fn complete(
        &self,
        request: CompleteRequestParams,
        _context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
    ) -> impl std::future::Future<Output = Result<CompleteResult, rmcp::ErrorData>> + rmcp::service::MaybeSendFuture + '_
    {
        std::future::ready(complete_impl(request))
    }
}

impl Default for XbergMcp {
    fn default() -> Self {
        Self::new().unwrap_or_else(|e| {
            #[cfg(feature = "api")]
            tracing::warn!("Failed to discover config, using default: {}", e);
            #[cfg(not(feature = "api"))]
            tracing::debug!("Warning: Failed to discover config, using default: {}", e);
            Self::with_config(ExtractionConfig::default())
        })
    }
}

/// Start the Xberg MCP server.
///
/// This function initializes and runs the MCP server using stdio transport.
/// It will block until the server is shut down.
///
/// # Errors
///
/// Returns an error if the server fails to start or encounters a fatal error.
///
/// # Example
///
/// ```rust,no_run
/// use xberg::mcp::start_mcp_server;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
///     start_mcp_server().await?;
///     Ok(())
/// }
/// ```
#[cfg_attr(alef, alef(skip))]
pub async fn start_mcp_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let service = XbergMcp::new()?.serve(stdio()).await?;

    service.waiting().await?;
    Ok(())
}

/// Start MCP server with custom extraction config.
///
/// This variant allows specifying a custom extraction configuration
/// (e.g., loaded from a file) instead of using defaults.
#[cfg_attr(alef, alef(skip))]
pub async fn start_mcp_server_with_config(
    config: ExtractionConfig,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let service = XbergMcp::with_config(config).serve(stdio()).await?;

    service.waiting().await?;
    Ok(())
}

/// Wait for a shutdown signal: SIGTERM on Unix platforms or Ctrl-C on all platforms.
///
/// The future resolves as soon as the first signal arrives, allowing axum's
/// `with_graceful_shutdown` to drain in-flight connections before the process exits.
#[cfg(feature = "mcp-http")]
async fn mcp_shutdown_signal() {
    #[cfg(unix)]
    {
        use tokio::signal::unix::{SignalKind, signal};

        let mut sigterm = match signal(SignalKind::terminate()) {
            Ok(s) => s,
            Err(e) => {
                tracing::warn!("Failed to install SIGTERM handler: {}", e);
                tokio::signal::ctrl_c()
                    .await
                    .unwrap_or_else(|e| tracing::warn!("Failed to listen for Ctrl-C: {}", e));
                tracing::info!("MCP server shutting down gracefully on signal...");
                return;
            }
        };

        tokio::select! {
            _ = sigterm.recv() => {
                tracing::info!("MCP server shutting down gracefully on signal...");
            }
            result = tokio::signal::ctrl_c() => {
                if let Err(e) = result {
                    tracing::warn!("Failed to listen for Ctrl-C: {}", e);
                }
                tracing::info!("MCP server shutting down gracefully on signal...");
            }
        }
    }

    #[cfg(not(unix))]
    {
        tokio::signal::ctrl_c()
            .await
            .unwrap_or_else(|e| tracing::warn!("Failed to listen for Ctrl-C: {}", e));
        tracing::info!("MCP server shutting down gracefully on signal...");
    }
}

/// Build the rmcp Streamable HTTP server config, extending (never replacing) the
/// built-in loopback-only `allowed_hosts` default with any caller-supplied hosts.
///
/// Extending rather than replacing keeps `localhost`/`127.0.0.1`/`::1` working even when
/// the server also needs to accept a reverse-proxy or ingress hostname in the `Host`
/// header. Entries are trimmed and de-duplicated against the existing list; blank hosts
/// are ignored. An empty `extra_allowed_hosts` leaves rmcp's default unchanged.
#[cfg(feature = "mcp-http")]
fn build_streamable_http_config(extra_allowed_hosts: &[String]) -> StreamableHttpServerConfig {
    let mut config = StreamableHttpServerConfig::default();
    for host in extra_allowed_hosts {
        let host = host.trim();
        if !host.is_empty() && !config.allowed_hosts.iter().any(|existing| existing == host) {
            config.allowed_hosts.push(host.to_string());
        }
    }
    config
}

/// Start MCP server with HTTP Stream transport.
///
/// Uses rmcp's built-in StreamableHttpService for HTTP/SSE support per MCP spec.
///
/// # Arguments
///
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
/// * `port` - Port number (e.g., 8001)
/// * `extra_allowed_hosts` - Additional `Host` header values to accept, on top of rmcp's
///   loopback-only default (`localhost`, `127.0.0.1`, `::1`). Needed when the server runs
///   behind a reverse proxy or ingress that forwards a different hostname. Pass an empty
///   slice to keep the default loopback-only behavior.
///
/// # Example
///
/// ```no_run
/// use xberg::mcp::start_mcp_server_http;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
///     start_mcp_server_http("127.0.0.1", 8001, &[]).await?;
///     Ok(())
/// }
/// ```
#[cfg(feature = "mcp-http")]
#[cfg_attr(alef, alef(skip))]
pub async fn start_mcp_server_http(
    host: impl AsRef<str>,
    port: u16,
    extra_allowed_hosts: &[String],
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    use axum::Router;
    use std::net::SocketAddr;

    let http_service = StreamableHttpService::new(
        || XbergMcp::new().map_err(|e| std::io::Error::other(e.to_string())),
        LocalSessionManager::default().into(),
        build_streamable_http_config(extra_allowed_hosts),
    );

    let router = Router::new().nest_service("/mcp", http_service);

    let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
        .parse()
        .map_err(|e| format!("Invalid address: {}", e))?;

    #[cfg(feature = "api")]
    tracing::info!("Starting MCP HTTP server on http://{}", addr);

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, router)
        .with_graceful_shutdown(mcp_shutdown_signal())
        .await?;

    Ok(())
}

/// Start MCP HTTP server with custom extraction config.
///
/// This variant allows specifying a custom extraction configuration
/// while using HTTP Stream transport.
///
/// # Arguments
///
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
/// * `port` - Port number (e.g., 8001)
/// * `config` - Custom extraction configuration
/// * `extra_allowed_hosts` - Additional `Host` header values to accept, on top of rmcp's
///   loopback-only default (`localhost`, `127.0.0.1`, `::1`). Needed when the server runs
///   behind a reverse proxy or ingress that forwards a different hostname. Pass an empty
///   slice to keep the default loopback-only behavior.
///
/// # Example
///
/// ```no_run
/// use xberg::mcp::start_mcp_server_http_with_config;
/// use xberg::ExtractionConfig;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
///     let config = ExtractionConfig::default();
///     start_mcp_server_http_with_config("127.0.0.1", 8001, config, &[]).await?;
///     Ok(())
/// }
/// ```
#[cfg(feature = "mcp-http")]
#[cfg_attr(alef, alef(skip))]
pub async fn start_mcp_server_http_with_config(
    host: impl AsRef<str>,
    port: u16,
    config: ExtractionConfig,
    extra_allowed_hosts: &[String],
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    use axum::Router;
    use std::net::SocketAddr;

    let http_service = StreamableHttpService::new(
        move || Ok(XbergMcp::with_config(config.clone())),
        LocalSessionManager::default().into(),
        build_streamable_http_config(extra_allowed_hosts),
    );

    let router = Router::new().nest_service("/mcp", http_service);

    let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
        .parse()
        .map_err(|e| format!("Invalid address: {}", e))?;

    #[cfg(feature = "api")]
    tracing::info!("Starting MCP HTTP server on http://{}", addr);

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, router)
        .with_graceful_shutdown(mcp_shutdown_signal())
        .await?;

    Ok(())
}

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

    #[tokio::test]
    async fn test_tool_router_has_routes() {
        let router = XbergMcp::tool_router();
        assert!(router.has_route("extract"));
        assert!(router.has_route("extract_batch"));
        assert!(router.has_route("detect_mime_type"));
        assert!(router.has_route("list_formats"));
        assert!(router.has_route("cache_stats"));
        assert!(router.has_route("cache_clear"));
        assert!(router.has_route("get_version"));
        assert!(router.has_route("cache_manifest"));
        assert!(router.has_route("cache_warm"));
    }

    #[test]
    fn test_server_info() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert_eq!(info.server_info.name, "xberg-mcp");
        assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
        assert!(info.capabilities.tools.is_some());
    }

    #[test]
    fn test_with_config_stores_provided_config() {
        let custom_config = ExtractionConfig {
            force_ocr: true,
            use_cache: false,
            ..Default::default()
        };

        let server = XbergMcp::with_config(custom_config);

        assert!(server.default_config.force_ocr);
        assert!(!server.default_config.use_cache);
    }

    #[test]
    fn should_reject_llm_transport_config_in_mcp_per_input_override() {
        let value = serde_json::json!({
            "kind": "bytes",
            "data": [115, 97, 102, 101],
            "mime_type": "text/plain",
            "config": {
                "summarization": {
                    "llm": {"model": "openai/gpt-4o-mini", "api_key": "must-not-leak"}
                }
            }
        });

        let error = parse_extract_input(value).expect_err("caller credential must be rejected");

        assert_eq!(
            error.message,
            "Caller extraction config may not set summarization.llm.api_key"
        );
        assert!(
            !error.message.contains("must-not-leak"),
            "rejection must not include caller-controlled values"
        );
    }

    #[test]
    fn test_new_creates_server_with_default_config() {
        let server = XbergMcp::new();
        assert!(server.is_ok());
    }

    #[test]
    fn test_default_creates_server_without_panic() {
        let server = XbergMcp::default();
        let info = server.get_info();
        assert_eq!(info.server_info.name, "xberg-mcp");
    }

    #[test]
    fn test_server_info_has_correct_fields() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert_eq!(info.server_info.name, "xberg-mcp");
        assert_eq!(
            info.server_info.title,
            Some("Xberg Document Intelligence MCP Server".to_string())
        );
        assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
        assert_eq!(info.server_info.website_url, Some("https://docs.xberg.io".to_string()));
        assert!(info.instructions.is_some());
        assert!(info.capabilities.tools.is_some());
    }

    #[test]
    fn test_mcp_server_info_protocol_version() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert_eq!(info.protocol_version, ProtocolVersion::default());
    }

    #[test]
    fn test_mcp_server_info_has_all_required_fields() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert!(!info.server_info.name.is_empty());
        assert!(!info.server_info.version.is_empty());

        assert!(info.server_info.title.is_some());
        assert!(info.server_info.website_url.is_some());
        assert!(info.instructions.is_some());
    }

    #[test]
    fn test_mcp_server_capabilities_declares_tools() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert!(info.capabilities.tools.is_some());
    }

    #[test]
    fn test_mcp_server_name_follows_convention() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert_eq!(info.server_info.name, "xberg-mcp");
        assert!(!info.server_info.name.contains('_'));
        assert!(!info.server_info.name.contains(' '));
    }

    #[test]
    fn test_mcp_version_matches_cargo_version() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn test_mcp_instructions_are_helpful() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();

        let instructions = info.instructions.expect("Instructions should be present");

        assert!(instructions.contains("extract") || instructions.contains("Extract"));
        assert!(instructions.contains("OCR") || instructions.contains("ocr"));
        assert!(instructions.contains("document"));
    }

    #[tokio::test]
    async fn test_all_tools_are_registered() {
        let router = XbergMcp::tool_router();

        let expected_tools = vec![
            "extract",
            "extract_batch",
            "detect_mime_type",
            "list_formats",
            "cache_stats",
            "cache_clear",
            "get_version",
            "cache_manifest",
            "cache_warm",
        ];

        for tool_name in expected_tools.iter() {
            assert!(router.has_route(tool_name), "Tool '{}' should be registered", tool_name);
        }
    }

    #[tokio::test]
    async fn test_tool_count_is_correct() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        assert_eq!(tools.len(), 9, "Expected 9 tools, found {}", tools.len());
    }

    #[tokio::test]
    async fn test_tools_have_descriptions() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        for tool in tools {
            assert!(
                tool.description.is_some(),
                "Tool '{}' should have a description",
                tool.name
            );
            let desc = tool.description.as_ref().unwrap();
            assert!(!desc.is_empty(), "Tool '{}' description should not be empty", tool.name);
        }
    }

    #[tokio::test]
    async fn test_tool_annotations_reflect_behavior() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        let annotations_for = |name: &str| {
            tools
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{name}' should exist"))
                .annotations
                .clone()
                .unwrap_or_else(|| panic!("tool '{name}' should have annotations"))
        };

        for name in [
            "detect_mime_type",
            "cache_stats",
            "list_formats",
            "get_version",
            "cache_manifest",
        ] {
            let a = annotations_for(name);
            assert_eq!(a.read_only_hint, Some(true), "{name} should be read-only");
            assert_eq!(a.idempotent_hint, Some(true), "{name} should be idempotent");
            assert_ne!(a.open_world_hint, Some(true), "{name} should be closed-world");
        }

        for name in ["extract", "extract_batch"] {
            let a = annotations_for(name);
            assert_eq!(a.read_only_hint, Some(true), "{name} should be read-only");
            assert_eq!(a.idempotent_hint, Some(true), "{name} should be idempotent");
            assert_eq!(a.open_world_hint, Some(true), "{name} may fetch URLs");
        }

        let clear = annotations_for("cache_clear");
        assert_eq!(
            clear.read_only_hint,
            Some(false),
            "cache_clear modifies the environment"
        );
        assert_eq!(clear.destructive_hint, Some(true), "cache_clear is destructive");

        let warm = annotations_for("cache_warm");
        assert_eq!(warm.read_only_hint, Some(false), "cache_warm writes the cache");
        assert_eq!(
            warm.destructive_hint,
            Some(false),
            "cache_warm is additive, not destructive"
        );
        assert_eq!(warm.open_world_hint, Some(true), "cache_warm fetches from HuggingFace");
    }

    #[tokio::test]
    async fn test_extract_tool_has_correct_schema() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        let extract_tool = tools
            .iter()
            .find(|t| t.name == "extract")
            .expect("extract tool should exist");

        assert!(extract_tool.description.is_some());

        assert!(!extract_tool.input_schema.is_empty());
    }

    #[tokio::test]
    async fn test_all_tools_have_input_schemas() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        for tool in tools {
            assert!(
                !tool.input_schema.is_empty(),
                "Tool '{}' should have an input schema with fields",
                tool.name
            );
        }
    }

    #[test]
    fn test_server_creation_with_custom_config() {
        let custom_config = ExtractionConfig {
            force_ocr: true,
            use_cache: false,
            ocr: Some(crate::OcrConfig {
                backend: "tesseract".to_string(),
                language: vec!["spa".to_string()],
                ..Default::default()
            }),
            ..Default::default()
        };

        let server = XbergMcp::with_config(custom_config.clone());

        assert_eq!(server.default_config.force_ocr, custom_config.force_ocr);
        assert_eq!(server.default_config.use_cache, custom_config.use_cache);
    }

    #[test]
    fn test_server_clone_preserves_config() {
        let custom_config = ExtractionConfig {
            force_ocr: true,
            ..Default::default()
        };

        let server1 = XbergMcp::with_config(custom_config);
        let server2 = server1.clone();

        assert_eq!(server1.default_config.force_ocr, server2.default_config.force_ocr);
    }

    #[tokio::test]
    async fn test_server_is_thread_safe() {
        let server = XbergMcp::with_config(ExtractionConfig::default());

        let server1 = server.clone();
        let server2 = server.clone();

        let handle1 = tokio::spawn(async move { server1.get_info() });

        let handle2 = tokio::spawn(async move { server2.get_info() });

        let info1 = handle1.await.unwrap();
        let info2 = handle2.await.unwrap();

        assert_eq!(info1.server_info.name, info2.server_info.name);
    }

    #[test]
    fn test_get_version_returns_version() {
        let server = XbergMcp::with_config(ExtractionConfig::default());

        let result = server.get_version(rmcp::handler::server::wrapper::Parameters(
            crate::mcp::params::EmptyParams {},
        ));

        assert!(result.is_ok());
        let call_result = result.unwrap();
        if let Some(content) = call_result.content.first() {
            match content {
                ContentBlock::Text(text) => {
                    let parsed: serde_json::Value = serde_json::from_str(&text.text).expect("Should be valid JSON");
                    assert_eq!(parsed["version"], env!("CARGO_PKG_VERSION"));
                }
                _ => panic!("Expected text content"),
            }
        } else {
            panic!("Expected content in result");
        }
        assert!(
            call_result.structured_content.is_some(),
            "get_version should have structured_content"
        );
        let sc = call_result.structured_content.unwrap();
        assert_eq!(sc["version"], env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn test_cache_manifest_returns_json() {
        let server = XbergMcp::with_config(ExtractionConfig::default());

        let result = server.cache_manifest(rmcp::handler::server::wrapper::Parameters(
            crate::mcp::params::EmptyParams {},
        ));

        assert!(result.is_ok());
        let call_result = result.unwrap();
        if let Some(content) = call_result.content.first() {
            match content {
                ContentBlock::Text(text) => {
                    let parsed: serde_json::Value = serde_json::from_str(&text.text).expect("Should be valid JSON");
                    assert!(parsed.get("xberg_version").is_some());
                    assert!(parsed.get("model_count").is_some());
                    assert!(parsed.get("models").is_some());
                }
                _ => panic!("Expected text content"),
            }
        } else {
            panic!("Expected content in result");
        }
        assert!(
            call_result.structured_content.is_some(),
            "cache_manifest should have structured_content"
        );
    }

    #[tokio::test]
    async fn test_extract_batch_empty_inputs_returns_empty_envelope() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let params = crate::mcp::params::ExtractBatchParams {
            inputs: vec![],
            config: None,
            pdf_password: None,
            response_format: None,
        };

        let result = server
            .extract_batch(rmcp::handler::server::wrapper::Parameters(params))
            .await;
        assert!(result.is_ok());
        let result = result.unwrap();
        let structured = result.structured_content.expect("structured content should exist");
        assert_eq!(structured["summary"]["inputs"], 0);
        assert_eq!(structured["summary"]["results"], 0);
        assert_eq!(structured["summary"]["errors"], 0);
    }

    #[test]
    fn test_capabilities_declare_resources_prompts_completions() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();
        assert!(
            info.capabilities.resources.is_some(),
            "resources capability should be declared"
        );
        assert!(
            info.capabilities.prompts.is_some(),
            "prompts capability should be declared"
        );
        assert!(
            info.capabilities.completions.is_some(),
            "completions capability should be declared"
        );
        assert!(info.capabilities.tools.is_some(), "tools capability should be declared");
    }

    #[tokio::test]
    async fn test_output_schema_present_on_structured_tools() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();
        let structured_tools = [
            "extract",
            "extract_batch",
            "detect_mime_type",
            "get_version",
            "list_formats",
            "cache_stats",
            "cache_manifest",
            "cache_clear",
            "cache_warm",
        ];
        for name in structured_tools {
            let tool = tools
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{}' not found", name));
            assert!(
                tool.output_schema.is_some(),
                "tool '{}' should have output_schema",
                name
            );
        }
    }

    /// Every registered MCP tool must declare a typed output schema (#250).
    ///
    /// The test above pins a hardcoded list, so a tool added later without an
    /// `output_schema` would pass it unnoticed. This one is exhaustive over
    /// whatever the router actually exposes, and pins the exact tool set so a
    /// silent addition or removal has to be acknowledged here.
    #[tokio::test]
    async fn should_declare_output_schema_on_every_registered_tool() {
        let router = XbergMcp::tool_router();
        let tools = router.list_all();

        let mut names: Vec<String> = tools.iter().map(|tool| tool.name.to_string()).collect();
        names.sort();
        assert_eq!(
            names,
            vec![
                "cache_clear",
                "cache_manifest",
                "cache_stats",
                "cache_warm",
                "detect_mime_type",
                "extract",
                "extract_batch",
                "get_version",
                "list_formats",
            ],
            "unexpected MCP tool set"
        );

        let missing: Vec<String> = tools
            .iter()
            .filter(|tool| tool.output_schema.is_none())
            .map(|tool| tool.name.to_string())
            .collect();
        assert_eq!(
            missing,
            Vec::<String>::new(),
            "every MCP tool must declare output_schema so clients can parse structured_content"
        );
    }

    #[test]
    fn test_record_warmed_model_reports_only_confirmed_cache_dispositions() {
        let mut available = Vec::new();
        let mut downloaded = Vec::new();
        let mut already_cached = Vec::new();

        record_warmed_model(
            &mut available,
            &mut downloaded,
            &mut already_cached,
            "availability-only".to_string(),
            CacheWarmDisposition::AvailabilityConfirmed,
        );
        record_warmed_model(
            &mut available,
            &mut downloaded,
            &mut already_cached,
            "downloaded".to_string(),
            CacheWarmDisposition::Downloaded,
        );
        record_warmed_model(
            &mut available,
            &mut downloaded,
            &mut already_cached,
            "cached".to_string(),
            CacheWarmDisposition::AlreadyCached,
        );

        assert_eq!(
            available,
            vec!["availability-only", "downloaded", "cached"],
            "every successful warm operation must be reported as available"
        );
        assert_eq!(
            downloaded,
            vec!["downloaded"],
            "only a confirmed download belongs in downloaded"
        );
        assert_eq!(
            already_cached,
            vec!["cached"],
            "only a confirmed cache hit belongs in already_cached"
        );
    }

    #[test]
    fn test_list_resources_returns_expected_uris() {
        let result = crate::mcp::resources::list_resources();
        let uris: Vec<&str> = result.resources.iter().map(|r| r.uri.as_str()).collect();
        assert!(uris.contains(&"xberg://formats"), "formats resource missing");
        assert!(uris.contains(&"xberg://models"), "models resource missing");
        assert!(
            uris.contains(&"xberg://languages/ocr"),
            "ocr languages resource missing"
        );
    }

    #[test]
    fn test_read_resource_formats_roundtrip() {
        let result =
            crate::mcp::resources::read_resource("xberg://formats").expect("formats resource should be readable");
        assert!(!result.contents.is_empty());
        if let ResourceContents::TextResourceContents { text, .. } = &result.contents[0] {
            let _: serde_json::Value = serde_json::from_str(text).expect("formats should be valid JSON");
        } else {
            panic!("Expected TextResourceContents");
        }
    }

    #[test]
    fn test_list_prompts_returns_workflows() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let prompts = server.prompt_router.list_all();
        let names: Vec<&str> = prompts.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"extract_document"), "extract_document prompt missing");
        assert!(names.contains(&"extract_with_ocr"), "extract_with_ocr prompt missing");
        assert!(names.contains(&"semantic_search"), "semantic_search prompt missing");
    }

    #[test]
    fn test_complete_ocr_language_by_prefix() {
        let candidates = complete_ocr_languages("en");
        assert!(!candidates.is_empty(), "should return candidates for prefix 'en'");
        assert!(
            candidates.iter().any(|c| c == "eng"),
            "eng should be in completions for prefix 'en'"
        );
    }

    #[test]
    fn test_complete_embedding_presets() {
        let candidates = complete_embedding_presets("b");
        assert_eq!(candidates, vec!["balanced"]);
    }

    #[test]
    fn test_complete_chunker_types_empty_prefix_returns_all() {
        let candidates = complete_chunker_types("");
        assert_eq!(candidates.len(), 4);
    }

    #[test]
    fn test_complete_output_formats() {
        let candidates = complete_output_formats("j");
        assert_eq!(candidates, vec!["json"]);
    }

    #[cfg(feature = "mcp-http")]
    #[test]
    fn test_build_streamable_http_config_empty_preserves_rmcp_default() {
        let config = build_streamable_http_config(&[]);
        let default_config = StreamableHttpServerConfig::default();
        assert_eq!(
            config.allowed_hosts, default_config.allowed_hosts,
            "empty extra hosts must leave rmcp's default untouched"
        );
    }

    #[cfg(feature = "mcp-http")]
    #[test]
    fn test_build_streamable_http_config_extends_default_without_replacing_it() {
        let default_hosts = StreamableHttpServerConfig::default().allowed_hosts;
        let config = build_streamable_http_config(&["proxy.example.com".to_string()]);

        for host in &default_hosts {
            assert!(
                config.allowed_hosts.contains(host),
                "loopback host '{host}' must still be present after extending"
            );
        }
        assert!(
            config.allowed_hosts.contains(&"proxy.example.com".to_string()),
            "supplied host must be added"
        );
    }

    #[cfg(feature = "mcp-http")]
    #[test]
    fn test_build_streamable_http_config_trims_and_deduplicates_hosts() {
        let config = build_streamable_http_config(&[" proxy.example.com ".to_string(), "localhost".to_string()]);

        let occurrences = config.allowed_hosts.iter().filter(|h| *h == "localhost").count();
        assert_eq!(
            occurrences, 1,
            "duplicate of an existing default host must not be added again"
        );
        assert!(config.allowed_hosts.contains(&"proxy.example.com".to_string()));
    }

    #[test]
    fn test_capabilities_declare_tasks_extension() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let info = server.get_info();
        assert!(
            info.capabilities.supports_tasks(),
            "SEP-2663 tasks extension capability should be declared"
        );
    }

    #[test]
    fn test_spawn_task_for_tool_rejects_non_task_eligible_name() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let request = CallToolRequestParams::new("get_version");
        let result = server.spawn_task_for_tool(request);
        assert!(
            result.is_err(),
            "non-task-eligible tool names must not materialize a task"
        );
    }

    #[test]
    fn test_spawn_task_for_tool_extract_rejects_invalid_params_synchronously() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        // No arguments at all: ExtractParams.input is required, so this must
        // fail during deserialization before any task is spawned.
        let request = CallToolRequestParams::new("extract");
        let result = server.spawn_task_for_tool(request);
        assert!(
            result.is_err(),
            "missing required 'input' field should be rejected before spawning a task"
        );
    }

    #[tokio::test]
    async fn test_spawn_task_for_tool_extract_task_reaches_terminal_state() {
        let server = XbergMcp::with_config(ExtractionConfig::default());
        let mut arguments = rmcp::model::JsonObject::new();
        arguments.insert(
            "input".to_string(),
            serde_json::json!({"kind": "uri", "uri": "/nonexistent/xberg-mcp-task-test.bin"}),
        );
        let request = CallToolRequestParams::new("extract").with_arguments(arguments);

        let response = server
            .spawn_task_for_tool(request)
            .expect("valid extract params should materialize a task");
        let task_id = match response {
            CallToolResponse::Task(create) => create.task.task_id,
            other => panic!("expected CreateTaskResult, got {other:?}"),
        };

        let mut detailed = server
            .tasks
            .get_task(&task_id)
            .expect("task should exist right after spawn");
        for _ in 0..200 {
            if detailed.status().is_terminal() {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            detailed = server
                .tasks
                .get_task(&task_id)
                .expect("task should still be tracked while polling");
        }

        assert_eq!(
            detailed.status(),
            TaskStatus::Failed,
            "extraction from a nonexistent path should settle as a failed task, not hang"
        );
    }
}