anymd 8.1.0

anymd: any file to clean Markdown for AI agents. Pure-Rust MCP server + CLI binary for @sylphx/anymd
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
pub mod cli;
mod command_provider;
pub mod discover_compat;
pub mod document;
pub mod evidence;
pub mod http_transport;
pub mod lean;
mod ocr_evidence;
mod page_selection;
pub mod pdf_compare;
pub mod pdf_evidence;
pub mod read_pdf;
mod region_analysis_evidence;
pub mod schema;
pub mod search;
pub mod setup;
pub mod source_access;
pub mod tool_routes;
mod visual_evidence;

use rmcp::{
    handler::server::router::tool::ToolRouter,
    handler::server::tool::ToolCallContext,
    handler::server::wrapper::Parameters,
    model::{
        CacheScope, CallToolResponse, CustomRequest, CustomResult, Implementation, ListToolsResult,
        MetaObject, PaginatedRequestParams, ProtocolVersion, ResultType, ServerCapabilities,
        ServerInfo,
    },
    service::{RequestContext, RoleServer},
    tool, tool_handler, tool_router, ErrorData, ServerHandler,
};

use crate::schema::{
    ComparePdfArgs, InspectArgs, InspectOperation, PdfEvidenceArgs, PdfEvidenceOperation, ReadArgs,
    ReadPdfArgs, SearchArgs, SearchPdfArgs,
};
use crate::source_access::SourceAccessPolicy;
use serde_json::Value;

pub const SERVER_NAME: &str = "anymd";
/// The product version: the workspace version, set with `bun scripts/set-version.ts`.
pub const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const SERVER_INFO_META_KEY: &str = "io.modelcontextprotocol/serverInfo";
pub const SERVER_INSTRUCTIONS: &str =
    "Local document reader for agents. read turns any file, URL, or directory listing into clean \
Markdown (PDF, Office, EPUB, HTML, CSV, images, media) with page/slide/sheet markers and a cursor \
for long documents. search finds text across files and directories with page locators. inspect \
renders, crops, OCRs, diffs, or returns structured JSON for PDFs. No cloud API key is required.";

fn omit_absent_optional_fields(value: Value) -> Value {
    match value {
        Value::Object(object) => Value::Object(
            object
                .into_iter()
                .filter_map(|(key, value)| {
                    (!value.is_null()).then(|| (key, omit_absent_optional_fields(value)))
                })
                .collect(),
        ),
        Value::Array(values) => Value::Array(
            values
                .into_iter()
                .map(omit_absent_optional_fields)
                .collect(),
        ),
        other => other,
    }
}

/// Formats defined by the JSON Schema specification (draft 2020-12). schemars
/// annotates Rust integer/float types with non-standard formats ("uint32",
/// "uint64", "double") that spec-strict client validators (AJV strict mode,
/// Zod, ...) reject or log as unknown on every tool call.
fn is_standard_schema_format(format: &str) -> bool {
    matches!(
        format,
        "date-time"
            | "date"
            | "time"
            | "duration"
            | "email"
            | "idn-email"
            | "hostname"
            | "idn-hostname"
            | "ipv4"
            | "ipv6"
            | "uri"
            | "uri-reference"
            | "iri"
            | "iri-reference"
            | "uri-template"
            | "json-pointer"
            | "relative-json-pointer"
            | "regex"
            | "uuid"
    )
}

fn sanitize_schema_formats(object: &mut serde_json::Map<String, Value>) {
    if matches!(object.get("format"), Some(Value::String(format)) if !is_standard_schema_format(format))
    {
        object.remove("format");
    }
    for value in object.values_mut() {
        match value {
            Value::Object(child) => sanitize_schema_formats(child),
            Value::Array(items) => {
                for item in items {
                    if let Value::Object(child) = item {
                        sanitize_schema_formats(child);
                    }
                }
            }
            _ => {}
        }
    }
}

fn sanitized_tools(router: &ToolRouter<PdfReaderMcp>) -> Vec<rmcp::model::Tool> {
    let mut tools = router.list_all();
    for tool in &mut tools {
        sanitize_schema_formats(std::sync::Arc::make_mut(&mut tool.input_schema));
        if let Some(output_schema) = &mut tool.output_schema {
            sanitize_schema_formats(std::sync::Arc::make_mut(output_schema));
        }
    }
    tools
}

#[derive(Clone)]
pub struct PdfReaderMcp {
    pub tool_router: ToolRouter<Self>,
    source_access: SourceAccessPolicy,
}

impl PdfReaderMcp {
    pub fn new() -> Self {
        Self::with_source_access(SourceAccessPolicy::unrestricted())
    }

    pub fn with_source_access(source_access: SourceAccessPolicy) -> Self {
        Self {
            tool_router: Self::tool_router(),
            source_access,
        }
    }
}

impl Default for PdfReaderMcp {
    fn default() -> Self {
        Self::new()
    }
}

fn server_result_meta(implementation: &Implementation) -> MetaObject {
    let mut meta = MetaObject::new();
    meta.insert(
        SERVER_INFO_META_KEY.to_string(),
        serde_json::to_value(implementation).expect("Implementation serialization cannot fail"),
    );
    meta
}

fn uses_2026_envelope(context: &RequestContext<RoleServer>) -> bool {
    context
        .protocol_version()
        .is_some_and(|version| version >= ProtocolVersion::V_2026_07_28)
}

#[tool_router]
impl PdfReaderMcp {
    #[tool(
        description = "Read any document as clean Markdown: PDF, Word (DOCX), PowerPoint (PPTX), Excel (XLSX/XLS/ODS), CSV, EPUB, HTML or a web URL, Markdown/text, images (metadata + OCR), audio/video (metadata, chapters, subtitles). Pages/slides/sheets carry <!-- page N --> style markers for citation. Long documents stop at max_tokens (default 20000) and end with a cursor to continue; choose pages with pages: \"1-5,8\". A directory returns its readable files."
    )]
    pub async fn read(
        &self,
        Parameters(args): Parameters<ReadArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate()
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        let policy = self.source_access.clone();
        tokio::task::spawn_blocking(move || lean::read(&args, &policy))
            .await
            .map_err(|error| ErrorData::internal_error(format!("read worker failed: {error}"), None))?
    }

    #[tool(
        description = "Search documents for text: one file, many files, whole directories (recursive, .gitignore aware), or URLs, across every format read supports. Returns each hit as file + page/slide/sheet + a snippet with the match in bold. mode auto (default) finds the exact phrase and falls back to BM25-ranked passages when there is none; literal or ranked force one. Narrow directories with glob, e.g. \"*.pdf\"."
    )]
    pub async fn search(
        &self,
        Parameters(args): Parameters<SearchArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate()
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        let policy = self.source_access.clone();
        tokio::task::spawn_blocking(move || lean::search(&args, &policy))
            .await
            .map_err(|error| ErrorData::internal_error(format!("search worker failed: {error}"), None))?
    }

    #[tool(
        description = "Deep PDF inspection when Markdown is not enough. operation: inspect (page facts, metadata), render_page (PNG images), extract_regions (crop bounding boxes), ocr_pages / analyze_regions (configured OCR or vision provider), structure (JSON with document map, elements, geometry; profile quality|research adds trust and accessibility reports), compare (page-level diff of sources[0] vs sources[1])."
    )]
    pub async fn inspect(
        &self,
        Parameters(args): Parameters<InspectArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        self.run_inspect(args).await
    }
}

/// Legacy tool names, still callable (not listed) for one major version.
impl PdfReaderMcp {
    pub async fn read_pdf(
        &self,
        Parameters(mut args): Parameters<ReadPdfArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate()
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        self.source_access
            .admit_pdf_sources(&mut args.sources)
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        if !lean::read_wants_legacy(&args) {
            let policy = self.source_access.clone();
            return tokio::task::spawn_blocking(move || lean::read_pdf(&args, &policy))
                .await
                .map_err(|error| {
                    ErrorData::internal_error(format!("read_pdf worker failed: {error}"), None)
                })?;
        }
        args.max_tokens = None;
        args.cursor = None;
        let provider_operation = args.include_ocr_text_layer == Some(true);
        let value = serde_json::to_value(args)
            .map(omit_absent_optional_fields)
            .map_err(|error| {
                ErrorData::invalid_params(format!("Failed to encode read_pdf args: {error}"), None)
            })?;
        if provider_operation {
            tokio::task::spawn_blocking(move || read_pdf::read_pdf(value))
                .await
                .map_err(|error| {
                    ErrorData::internal_error(format!("read_pdf worker failed: {error}"), None)
                })?
        } else {
            read_pdf::read_pdf(value)
        }
    }

    pub async fn pdf_compare(
        &self,
        Parameters(args): Parameters<ComparePdfArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate().map_err(|message| ErrorData::invalid_params(message, None))?;
        let value = serde_json::to_value(args).map_err(|error| {
            ErrorData::invalid_params(format!("Failed to encode pdf_compare args: {error}"), None)
        })?;
        pdf_compare::pdf_compare(value)
    }

    pub async fn search_pdf(
        &self,
        Parameters(mut args): Parameters<SearchPdfArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate()
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        self.source_access
            .admit_pdf_sources(&mut args.sources)
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        if !lean::search_wants_legacy(&args) {
            let policy = self.source_access.clone();
            return tokio::task::spawn_blocking(move || lean::search_pdf(&args, &policy))
                .await
                .map_err(|error| {
                    ErrorData::internal_error(format!("search_pdf worker failed: {error}"), None)
                })?;
        }
        args.detail = None;
        let provider_operation = args.include_ocr_text_layer == Some(true);
        let value = serde_json::to_value(args)
            .map(omit_absent_optional_fields)
            .map_err(|error| {
                ErrorData::invalid_params(
                    format!("Failed to encode search_pdf args: {error}"),
                    None,
                )
            })?;
        if provider_operation {
            tokio::task::spawn_blocking(move || search::search_pdf(value))
                .await
                .map_err(|error| {
                    ErrorData::internal_error(format!("search_pdf worker failed: {error}"), None)
                })?
        } else {
            search::search_pdf(value)
        }
    }

    pub async fn pdf_evidence(
        &self,
        Parameters(mut args): Parameters<PdfEvidenceArgs>,
    ) -> Result<rmcp::model::CallToolResult, ErrorData> {
        args.validate()
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        self.source_access
            .admit_evidence_sources(&mut args.sources)
            .map_err(|message| ErrorData::invalid_params(message, None))?;
        let provider_operation = matches!(
            args.operation,
            PdfEvidenceOperation::OcrPages | PdfEvidenceOperation::AnalyzeRegions
        );
        let value = serde_json::to_value(args)
            .map(omit_absent_optional_fields)
            .map_err(|error| {
                ErrorData::invalid_params(
                    format!("Failed to encode pdf_evidence args: {error}"),
                    None,
                )
            })?;
        if provider_operation {
            tokio::task::spawn_blocking(move || pdf_evidence::pdf_evidence(value))
                .await
                .map_err(|error| {
                    ErrorData::internal_error(format!("Provider worker failed: {error}"), None)
                })?
        } else {
            pdf_evidence::pdf_evidence(value)
        }
    }
}

/// Legacy tool names that stay callable (unlisted) for one major version.
pub const LEGACY_TOOL_NAMES: &[&str] = &["read_pdf", "search_pdf", "pdf_evidence", "pdf_compare"];

fn legacy_args<T: serde::de::DeserializeOwned>(
    request: &rmcp::model::CallToolRequestParams,
) -> Result<T, ErrorData> {
    let value = Value::Object(request.arguments.clone().unwrap_or_default());
    serde_json::from_value(value).map_err(|error| {
        ErrorData::invalid_params(format!("Invalid arguments for {}: {error}", request.name), None)
    })
}

impl PdfReaderMcp {
    async fn run_inspect(&self, args: InspectArgs) -> Result<rmcp::model::CallToolResult, ErrorData> {
        match args.operation {
            InspectOperation::Compare => {
                let paths: Vec<String> = args
                    .sources
                    .iter()
                    .filter_map(|source| source.path.clone())
                    .collect();
                let [before, after] = paths.as_slice() else {
                    return Err(ErrorData::invalid_params(
                        "compare needs exactly two local PDF sources: [before, after].",
                        None,
                    ));
                };
                let before = self
                    .source_access
                    .admit_path(before)
                    .map_err(|message| ErrorData::invalid_params(message, None))?;
                let after = self
                    .source_access
                    .admit_path(after)
                    .map_err(|message| ErrorData::invalid_params(message, None))?;
                self.pdf_compare(Parameters(ComparePdfArgs {
                    before,
                    after,
                    max_file_bytes: None,
                    context_chars: None,
                }))
                .await
            }
            InspectOperation::Structure => {
                let profile = args.profile.clone().unwrap_or_else(|| "fast".into());
                if profile.eq_ignore_ascii_case("markdown") {
                    return Err(ErrorData::invalid_params(
                        "structure returns JSON; use read for Markdown.",
                        None,
                    ));
                }
                self.read_pdf(Parameters(ReadPdfArgs {
                    sources: args.sources.iter().map(|source| source.as_pdf_source()).collect(),
                    profile: Some(profile),
                    ..Default::default()
                }))
                .await
            }
            operation => {
                let operation = match operation {
                    InspectOperation::Inspect => PdfEvidenceOperation::Inspect,
                    InspectOperation::RenderPage => PdfEvidenceOperation::RenderPage,
                    InspectOperation::ExtractRegions => PdfEvidenceOperation::ExtractRegions,
                    InspectOperation::OcrPages => PdfEvidenceOperation::OcrPages,
                    InspectOperation::AnalyzeRegions => PdfEvidenceOperation::AnalyzeRegions,
                    InspectOperation::Structure | InspectOperation::Compare => unreachable!(),
                };
                self.pdf_evidence(Parameters(PdfEvidenceArgs {
                    operation,
                    sources: args.sources,
                    sample_pages: args.sample_pages,
                    include_metadata: args.include_metadata,
                    scale: args.scale,
                    max_pages: args.max_pages,
                    max_regions: args.max_regions,
                    max_pixels_per_page: args.max_pixels_per_page,
                    include_image: args.include_image,
                    timeout_ms: args.timeout_ms,
                    max_output_chars: args.max_output_chars,
                    languages: args.languages,
                }))
                .await
            }
        }
    }

    async fn call_legacy(
        &self,
        request: &rmcp::model::CallToolRequestParams,
    ) -> Option<Result<rmcp::model::CallToolResult, ErrorData>> {
        Some(match request.name.as_ref() {
            "read_pdf" => match legacy_args(request) {
                Ok(args) => self.read_pdf(Parameters(args)).await,
                Err(error) => Err(error),
            },
            "search_pdf" => match legacy_args(request) {
                Ok(args) => self.search_pdf(Parameters(args)).await,
                Err(error) => Err(error),
            },
            "pdf_evidence" => match legacy_args(request) {
                Ok(args) => self.pdf_evidence(Parameters(args)).await,
                Err(error) => Err(error),
            },
            "pdf_compare" => match legacy_args(request) {
                Ok(args) => self.pdf_compare(Parameters(args)).await,
                Err(error) => Err(error),
            },
            _ => return None,
        })
    }
}

#[tool_handler]
impl ServerHandler for PdfReaderMcp {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
            .with_server_info(
                Implementation::new(SERVER_NAME, SERVER_VERSION)
                    .with_description(
                        "@sylphx/anymd sole-Rust MCP server (native binary; no TypeScript PDF runtime)",
                    )
                    .with_website_url("https://sylphxai.github.io/anymd/"),
            )
            .with_instructions(SERVER_INSTRUCTIONS)
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, ErrorData> {
        let supports_2026 = uses_2026_envelope(&context);
        let result = ListToolsResult {
            result_type: Some(ResultType::COMPLETE),
            tools: sanitized_tools(&self.tool_router),
            meta: supports_2026.then(|| server_result_meta(&self.get_info().server_info)),
            next_cursor: None,
            ttl_ms: supports_2026.then_some(0),
            cache_scope: supports_2026.then_some(CacheScope::Public),
        };
        Ok(result)
    }

    async fn call_tool(
        &self,
        request: rmcp::model::CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, ErrorData> {
        let supports_2026 = uses_2026_envelope(&context);
        let mut response = match self.call_legacy(&request).await {
            Some(result) => CallToolResponse::Complete(result?),
            None => {
                let tool_context = ToolCallContext::new(self, request, context);
                self.tool_router.call(tool_context).await?
            }
        };
        if supports_2026 {
            if let CallToolResponse::Complete(ref mut result) = response {
                result.meta = Some(server_result_meta(&self.get_info().server_info));
            }
        }
        Ok(response)
    }

    /// Post-init `server/discover` (SEP-2575). Pre-init is handled by
    /// [`discover_compat::DiscoverAwareTransport`].
    fn on_custom_request(
        &self,
        request: CustomRequest,
        _context: RequestContext<RoleServer>,
    ) -> impl std::future::Future<Output = Result<CustomResult, ErrorData>> + Send + '_ {
        async move {
            if request.method == discover_compat::SERVER_DISCOVER_METHOD {
                return Ok(CustomResult::new(discover_compat::discover_result_value(
                    &self.get_info(),
                )));
            }
            Err(ErrorData::new(
                rmcp::model::ErrorCode::METHOD_NOT_FOUND,
                request.method,
                None,
            ))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{is_standard_schema_format, sanitize_schema_formats, sanitized_tools, PdfReaderMcp};
    use rmcp::handler::server::wrapper::Parameters;
    use serde_json::Value;
    use std::path::PathBuf;

    #[test]
    fn exposes_three_obvious_tools_and_keeps_legacy_names_callable() {
        let tools = PdfReaderMcp::new().tool_router.list_all();
        let mut names: Vec<_> = tools.iter().map(|tool| tool.name.to_string()).collect();
        names.sort();
        assert_eq!(names, ["inspect", "read", "search"]);
        for legacy in super::LEGACY_TOOL_NAMES {
            assert!(!names.contains(&legacy.to_string()), "{legacy} must not be listed");
        }
    }

    #[test]
    fn tools_list_exposes_typed_object_schemas_not_empty_value() {
        let tools = PdfReaderMcp::new().tool_router.list_all();
        for tool in tools {
            let schema_value = serde_json::to_value(&tool.input_schema).expect("schema json");
            assert_eq!(
                schema_value.get("type").and_then(|v| v.as_str()),
                Some("object"),
                "tool {} schema type must be object, got {schema_value}",
                tool.name
            );
            let props = schema_value
                .get("properties")
                .and_then(|v| v.as_object())
                .expect("properties object");
            let required: &[&str] = match tool.name.as_ref() {
                "read" => &["source", "pages", "max_tokens", "cursor"],
                "search" => &["query", "sources", "mode"],
                "inspect" => &["operation", "sources"],
                other => panic!("unexpected tool {other}"),
            };
            for key in required {
                assert!(props.contains_key(*key), "tool {} must document {key}", tool.name);
            }
        }
    }

    #[test]
    fn tools_list_strips_non_standard_schema_formats() {
        // Raw router schemas carry schemars annotations (uint32, uint64,
        // double) that spec-strict client validators report as unknown
        // formats; the tools/list surface must not advertise them.
        let raw = serde_json::to_value(&PdfReaderMcp::new().tool_router.list_all()[0].input_schema)
            .expect("raw schema json");
        let raw_formats = schema_format_values(&raw);
        assert!(
            raw_formats.iter().any(|format| format == "uint32"),
            "precondition: raw schemars schema must contain a uint32 format, got {raw_formats:?}"
        );

        let tools = sanitized_tools(&PdfReaderMcp::new().tool_router);
        for tool in tools {
            let schema = serde_json::to_value(&tool.input_schema).expect("schema json");
            let formats = schema_format_values(&schema);
            for format in &formats {
                assert!(
                    is_standard_schema_format(format),
                    "tool {} advertises non-standard format {format}",
                    tool.name
                );
            }
            // Bounds are the contract; losing the format annotation must not
            // lose the numeric limits that back it.
            let raw_tool = serde_json::to_value(
                &PdfReaderMcp::new()
                    .tool_router
                    .list_all()
                    .into_iter()
                    .find(|raw_tool| raw_tool.name == tool.name)
                    .expect("raw tool")
                    .input_schema,
            )
            .expect("raw schema json");
            assert_eq!(
                schema_numeric_bounds(&schema),
                schema_numeric_bounds(&raw_tool),
                "tool {} numeric bounds changed during sanitization",
                tool.name
            );
        }
    }

    #[test]
    fn sanitize_schema_formats_keeps_standard_formats_and_bounds() {
        let mut object = serde_json::json!({
            "type": ["null", "integer"],
            "format": "uint32",
            "minimum": 1000,
            "maximum": 1000000,
            "standard": { "format": "uuid" },
            "nested": { "format": "double" },
            "list": [{ "format": "date-time" }, { "format": "uint64" }]
        })
        .as_object()
        .expect("object")
        .clone();
        sanitize_schema_formats(&mut object);
        let value = Value::Object(object);
        assert!(value.get("format").is_none());
        assert_eq!(value["minimum"], 1000);
        assert_eq!(value["maximum"], 1000000);
        assert_eq!(value["standard"]["format"], "uuid");
        assert!(value["nested"].get("format").is_none());
        assert_eq!(value["list"][0]["format"], "date-time");
        assert!(value["list"][1].get("format").is_none());
    }

    fn schema_format_values(value: &Value) -> Vec<String> {
        let mut formats = Vec::new();
        match value {
            Value::Object(object) => {
                if let Some(Value::String(format)) = object.get("format") {
                    formats.push(format.clone());
                }
                for child in object.values() {
                    formats.extend(schema_format_values(child));
                }
            }
            Value::Array(items) => {
                for item in items {
                    formats.extend(schema_format_values(item));
                }
            }
            _ => {}
        }
        formats
    }

    fn schema_numeric_bounds(value: &Value) -> Vec<(String, f64)> {
        let mut bounds = Vec::new();
        match value {
            Value::Object(object) => {
                for key in ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"] {
                    if let Some(number) = object.get(key).and_then(|value| value.as_f64()) {
                        bounds.push((key.to_string(), number));
                    }
                }
                for child in object.values() {
                    bounds.extend(schema_numeric_bounds(child));
                }
            }
            Value::Array(items) => {
                for item in items {
                    bounds.extend(schema_numeric_bounds(item));
                }
            }
            _ => {}
        }
        bounds
    }

    #[tokio::test]
    async fn read_pdf_contains_malformed_cff_custom_encoding_panic() {
        // Regression for SylphxAI/citra#660: the CFF Custom encoding
        // panic must cross the MCP boundary as ErrorData, not kill the native
        // server process or leave the tool call timing out.
        let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(
            "../../test/fixtures/differential/v3014-bug660-cff-custom-encoding-short-charset-v1.pdf",
        );
        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": fixture}],
            "include_full_text": true,
        }))
        .expect("structurally valid read args");
        let error = PdfReaderMcp::new()
            .read_pdf(Parameters(read))
            .await
            .expect_err("read_pdf must return a structured MCP error");
        assert!(error.message.contains("malformed font encoding"));
    }

    #[tokio::test]
    async fn read_pdf_reports_malformed_inline_image_as_error() {
        // Regression for SylphxAI/citra#675: a malformed inline
        // image (missing `/CS` without `/IM`) panics inside lopdf 0.42's
        // content parser. It must cross the MCP boundary as ErrorData
        // naming the page, not kill the native server or time out the call.
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("inline-malformed.pdf");
        let content = b"BI /W 1 /H 1 /BPC 1 ID \x00 EI\n";
        let objects = [
            "<< /Type /Catalog /Pages 2 0 R >>".to_string(),
            "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R /Resources << >> >>"
                .to_string(),
            format!(
                "<< /Length {} >>\nstream\n{}\nendstream",
                content.len(),
                String::from_utf8_lossy(content)
            ),
        ];
        let mut pdf = b"%PDF-1.4\n".to_vec();
        let mut offsets = Vec::new();
        for (index, object) in objects.iter().enumerate() {
            offsets.push(pdf.len());
            pdf.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", index + 1).as_bytes());
        }
        let xref_offset = pdf.len();
        pdf.extend_from_slice(format!("xref\n0 {}\n", objects.len() + 1).as_bytes());
        pdf.extend_from_slice(b"0000000000 65535 f \n");
        for offset in offsets {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n",
                objects.len() + 1
            )
            .as_bytes(),
        );
        std::fs::write(&path, pdf).expect("write PDF");
        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": path}],
            "include_full_text": true,
        }))
        .expect("structurally valid read args");
        let error = PdfReaderMcp::new()
            .read_pdf(Parameters(read))
            .await
            .expect_err("read_pdf must return a structured MCP error");
        assert!(
            error.message.contains("invalid content stream (page 1)"),
            "unexpected message: {}",
            error.message
        );
    }

    #[tokio::test]
    async fn read_pdf_survives_wellformed_inline_image() {
        // The #675 reproducer itself must read cleanly through the tool.
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("inline-ok.pdf");
        let content = b"BI /W 1 /H 1 /IM true /BPC 1 ID \x00 EI\n";
        let objects = [
            "<< /Type /Catalog /Pages 2 0 R >>".to_string(),
            "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
            "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] /Contents 4 0 R /Resources << >> >>"
                .to_string(),
            format!(
                "<< /Length {} >>\nstream\n{}\nendstream",
                content.len(),
                String::from_utf8_lossy(content)
            ),
        ];
        let mut pdf = b"%PDF-1.4\n".to_vec();
        let mut offsets = Vec::new();
        for (index, object) in objects.iter().enumerate() {
            offsets.push(pdf.len());
            pdf.extend_from_slice(format!("{} 0 obj\n{object}\nendobj\n", index + 1).as_bytes());
        }
        let xref_offset = pdf.len();
        pdf.extend_from_slice(format!("xref\n0 {}\n", objects.len() + 1).as_bytes());
        pdf.extend_from_slice(b"0000000000 65535 f \n");
        for offset in offsets {
            pdf.extend_from_slice(format!("{offset:010} 00000 n \n").as_bytes());
        }
        pdf.extend_from_slice(
            format!(
                "trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref_offset}\n%%EOF\n",
                objects.len() + 1
            )
            .as_bytes(),
        );
        std::fs::write(&path, pdf).expect("write PDF");
        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": path}],
            "include_full_text": true,
        }))
        .expect("structurally valid read args");
        let result = PdfReaderMcp::new()
            .read_pdf(Parameters(read))
            .await
            .expect("read_pdf must complete on an inline-image PDF");
        assert!(!result.content.is_empty(), "expected a structured read_pdf result");
    }

    #[tokio::test]
    async fn search_pdf_handles_malformed_cid_cmap_fixture_without_aborting() {
        // Regression for SylphxAI/citra#608: a pdfTeX ToUnicode CMap
        // using 1-byte beginbfrange destinations (like <C5> <D6> <C5>) made the
        // upstream adobe-cmap-parser panic with "bad length of hexstring",
        // aborting the whole MCP server. The tool entrypoint must return a
        // structured result instead.
        let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test/fixtures/differential/v3014-bug608-cid-bfrange-odd-v1.pdf");
        let server = PdfReaderMcp::new();
        let search = serde_json::from_value(serde_json::json!({
            "sources": [{"path": fixture}],
            "query": "a"
        }))
        .expect("structurally valid search args");
        let result = server
            .search_pdf(Parameters(search))
            .await
            .expect("search_pdf must complete on a malformed CMap PDF");
        assert!(
            !result.content.is_empty(),
            "expected a structured search_pdf result"
        );

        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": fixture}]
        }))
        .expect("structurally valid read args");
        let result = server
            .read_pdf(Parameters(read))
            .await
            .expect("read_pdf must complete on a malformed CMap PDF");
        assert!(
            !result.content.is_empty(),
            "expected a structured read_pdf result"
        );
    }

    #[tokio::test]
    async fn tool_entrypoints_reject_values_outside_v3_0_14_runtime_bounds() {
        let server = PdfReaderMcp::new();
        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": "sample.pdf"}],
            "sample_pages": 21
        }))
        .expect("structurally valid read args");
        assert!(server.read_pdf(Parameters(read)).await.is_err());

        let search = serde_json::from_value(serde_json::json!({
            "sources": [{"path": "sample.pdf"}],
            "query": "needle",
            "context_chars": 1001
        }))
        .expect("structurally valid search args");
        assert!(server.search_pdf(Parameters(search)).await.is_err());

        let evidence = serde_json::from_value(serde_json::json!({
            "operation": "inspect",
            "sources": [{"path": "sample.pdf"}],
            "scale": 4.01
        }))
        .expect("structurally valid evidence args");
        assert!(server.pdf_evidence(Parameters(evidence)).await.is_err());
    }

    #[tokio::test]
    async fn all_tool_entrypoints_reject_paths_outside_the_native_allowlist() {
        let temp = tempfile::tempdir().expect("tempdir");
        let allowed = temp.path().join("allowed");
        std::fs::create_dir(&allowed).expect("allowed root");
        let outside = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test/fixtures/sample.pdf")
            .canonicalize()
            .expect("outside fixture");
        let policy = crate::source_access::SourceAccessPolicy::restricted_for_test(
            temp.path().to_path_buf(),
            &allowed,
        )
        .expect("restricted policy");
        let server = PdfReaderMcp::with_source_access(policy);

        let read = serde_json::from_value(serde_json::json!({
            "sources": [{"path": outside}]
        }))
        .expect("read args");
        let read_error = server
            .read_pdf(Parameters(read))
            .await
            .expect_err("read_pdf must reject outside path");
        assert!(read_error.message.contains("Access denied"));

        let search = serde_json::from_value(serde_json::json!({
            "sources": [{"path": outside}],
            "query": "needle"
        }))
        .expect("search args");
        let search_error = server
            .search_pdf(Parameters(search))
            .await
            .expect_err("search_pdf must reject outside path");
        assert!(search_error.message.contains("Access denied"));

        let evidence = serde_json::from_value(serde_json::json!({
            "operation": "inspect",
            "sources": [{"path": outside}]
        }))
        .expect("evidence args");
        let evidence_error = server
            .pdf_evidence(Parameters(evidence))
            .await
            .expect_err("pdf_evidence must reject outside path");
        assert!(evidence_error.message.contains("Access denied"));
    }
}