kreuzberg 4.0.0.pre.rc.11 → 4.0.0.pre.rc.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +14 -14
- data/.rspec +3 -3
- data/.rubocop.yaml +1 -1
- data/.rubocop.yml +538 -538
- data/Gemfile +8 -8
- data/Gemfile.lock +2 -105
- data/README.md +454 -454
- data/Rakefile +25 -25
- data/Steepfile +47 -47
- data/examples/async_patterns.rb +341 -341
- data/ext/kreuzberg_rb/extconf.rb +45 -45
- data/ext/kreuzberg_rb/native/.cargo/config.toml +2 -2
- data/ext/kreuzberg_rb/native/Cargo.lock +6941 -6941
- data/ext/kreuzberg_rb/native/Cargo.toml +54 -54
- data/ext/kreuzberg_rb/native/README.md +425 -425
- data/ext/kreuzberg_rb/native/build.rs +15 -15
- data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -11
- data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -14
- data/ext/kreuzberg_rb/native/include/strings.h +20 -20
- data/ext/kreuzberg_rb/native/include/unistd.h +47 -47
- data/ext/kreuzberg_rb/native/src/lib.rs +3158 -3158
- data/extconf.rb +28 -28
- data/kreuzberg.gemspec +214 -214
- data/lib/kreuzberg/api_proxy.rb +142 -142
- data/lib/kreuzberg/cache_api.rb +81 -81
- data/lib/kreuzberg/cli.rb +55 -55
- data/lib/kreuzberg/cli_proxy.rb +127 -127
- data/lib/kreuzberg/config.rb +724 -724
- data/lib/kreuzberg/error_context.rb +80 -80
- data/lib/kreuzberg/errors.rb +118 -118
- data/lib/kreuzberg/extraction_api.rb +340 -340
- data/lib/kreuzberg/mcp_proxy.rb +186 -186
- data/lib/kreuzberg/ocr_backend_protocol.rb +113 -113
- data/lib/kreuzberg/post_processor_protocol.rb +86 -86
- data/lib/kreuzberg/result.rb +279 -279
- data/lib/kreuzberg/setup_lib_path.rb +80 -80
- data/lib/kreuzberg/validator_protocol.rb +89 -89
- data/lib/kreuzberg/version.rb +5 -5
- data/lib/kreuzberg.rb +109 -109
- data/lib/{libpdfium.dylib → pdfium.dll} +0 -0
- data/sig/kreuzberg/internal.rbs +184 -184
- data/sig/kreuzberg.rbs +546 -546
- data/spec/binding/cache_spec.rb +227 -227
- data/spec/binding/cli_proxy_spec.rb +85 -85
- data/spec/binding/cli_spec.rb +55 -55
- data/spec/binding/config_spec.rb +345 -345
- data/spec/binding/config_validation_spec.rb +283 -283
- data/spec/binding/error_handling_spec.rb +213 -213
- data/spec/binding/errors_spec.rb +66 -66
- data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
- data/spec/binding/plugins/postprocessor_spec.rb +269 -269
- data/spec/binding/plugins/validator_spec.rb +274 -274
- data/spec/fixtures/config.toml +39 -39
- data/spec/fixtures/config.yaml +41 -41
- data/spec/fixtures/invalid_config.toml +4 -4
- data/spec/smoke/package_spec.rb +178 -178
- data/spec/spec_helper.rb +42 -42
- data/vendor/Cargo.toml +2 -1
- data/vendor/kreuzberg/Cargo.toml +2 -2
- data/vendor/kreuzberg/README.md +230 -230
- data/vendor/kreuzberg/benches/otel_overhead.rs +48 -48
- data/vendor/kreuzberg/build.rs +843 -843
- data/vendor/kreuzberg/src/api/error.rs +81 -81
- data/vendor/kreuzberg/src/api/handlers.rs +199 -199
- data/vendor/kreuzberg/src/api/mod.rs +79 -79
- data/vendor/kreuzberg/src/api/server.rs +353 -353
- data/vendor/kreuzberg/src/api/types.rs +170 -170
- data/vendor/kreuzberg/src/cache/mod.rs +1167 -1167
- data/vendor/kreuzberg/src/chunking/mod.rs +1877 -1877
- data/vendor/kreuzberg/src/chunking/processor.rs +220 -220
- data/vendor/kreuzberg/src/core/batch_mode.rs +95 -95
- data/vendor/kreuzberg/src/core/config.rs +1080 -1080
- data/vendor/kreuzberg/src/core/extractor.rs +1156 -1156
- data/vendor/kreuzberg/src/core/io.rs +329 -329
- data/vendor/kreuzberg/src/core/mime.rs +605 -605
- data/vendor/kreuzberg/src/core/mod.rs +47 -47
- data/vendor/kreuzberg/src/core/pipeline.rs +1184 -1184
- data/vendor/kreuzberg/src/embeddings.rs +500 -500
- data/vendor/kreuzberg/src/error.rs +431 -431
- data/vendor/kreuzberg/src/extraction/archive.rs +954 -954
- data/vendor/kreuzberg/src/extraction/docx.rs +398 -398
- data/vendor/kreuzberg/src/extraction/email.rs +854 -854
- data/vendor/kreuzberg/src/extraction/excel.rs +688 -688
- data/vendor/kreuzberg/src/extraction/html.rs +601 -601
- data/vendor/kreuzberg/src/extraction/image.rs +491 -491
- data/vendor/kreuzberg/src/extraction/libreoffice.rs +574 -562
- data/vendor/kreuzberg/src/extraction/markdown.rs +213 -213
- data/vendor/kreuzberg/src/extraction/mod.rs +81 -81
- data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
- data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
- data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
- data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -130
- data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +284 -284
- data/vendor/kreuzberg/src/extraction/pptx.rs +3100 -3100
- data/vendor/kreuzberg/src/extraction/structured.rs +490 -490
- data/vendor/kreuzberg/src/extraction/table.rs +328 -328
- data/vendor/kreuzberg/src/extraction/text.rs +269 -269
- data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
- data/vendor/kreuzberg/src/extractors/archive.rs +447 -447
- data/vendor/kreuzberg/src/extractors/bibtex.rs +470 -470
- data/vendor/kreuzberg/src/extractors/docbook.rs +504 -504
- data/vendor/kreuzberg/src/extractors/docx.rs +400 -400
- data/vendor/kreuzberg/src/extractors/email.rs +157 -157
- data/vendor/kreuzberg/src/extractors/epub.rs +708 -708
- data/vendor/kreuzberg/src/extractors/excel.rs +345 -345
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +492 -492
- data/vendor/kreuzberg/src/extractors/html.rs +407 -407
- data/vendor/kreuzberg/src/extractors/image.rs +219 -219
- data/vendor/kreuzberg/src/extractors/jats.rs +1054 -1054
- data/vendor/kreuzberg/src/extractors/jupyter.rs +368 -368
- data/vendor/kreuzberg/src/extractors/latex.rs +653 -653
- data/vendor/kreuzberg/src/extractors/markdown.rs +701 -701
- data/vendor/kreuzberg/src/extractors/mod.rs +429 -429
- data/vendor/kreuzberg/src/extractors/odt.rs +628 -628
- data/vendor/kreuzberg/src/extractors/opml.rs +635 -635
- data/vendor/kreuzberg/src/extractors/orgmode.rs +529 -529
- data/vendor/kreuzberg/src/extractors/pdf.rs +749 -722
- data/vendor/kreuzberg/src/extractors/pptx.rs +267 -267
- data/vendor/kreuzberg/src/extractors/rst.rs +577 -577
- data/vendor/kreuzberg/src/extractors/rtf.rs +809 -809
- data/vendor/kreuzberg/src/extractors/security.rs +484 -484
- data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -367
- data/vendor/kreuzberg/src/extractors/structured.rs +142 -142
- data/vendor/kreuzberg/src/extractors/text.rs +265 -265
- data/vendor/kreuzberg/src/extractors/typst.rs +651 -651
- data/vendor/kreuzberg/src/extractors/xml.rs +147 -147
- data/vendor/kreuzberg/src/image/dpi.rs +164 -164
- data/vendor/kreuzberg/src/image/mod.rs +6 -6
- data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
- data/vendor/kreuzberg/src/image/resize.rs +89 -89
- data/vendor/kreuzberg/src/keywords/config.rs +154 -154
- data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
- data/vendor/kreuzberg/src/keywords/processor.rs +275 -275
- data/vendor/kreuzberg/src/keywords/rake.rs +293 -293
- data/vendor/kreuzberg/src/keywords/types.rs +68 -68
- data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
- data/vendor/kreuzberg/src/language_detection/mod.rs +985 -985
- data/vendor/kreuzberg/src/language_detection/processor.rs +219 -219
- data/vendor/kreuzberg/src/lib.rs +113 -113
- data/vendor/kreuzberg/src/mcp/mod.rs +35 -35
- data/vendor/kreuzberg/src/mcp/server.rs +2076 -2076
- data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
- data/vendor/kreuzberg/src/ocr/error.rs +37 -37
- data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
- data/vendor/kreuzberg/src/ocr/mod.rs +58 -58
- data/vendor/kreuzberg/src/ocr/processor.rs +863 -863
- data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
- data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +452 -452
- data/vendor/kreuzberg/src/ocr/types.rs +393 -393
- data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
- data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
- data/vendor/kreuzberg/src/panic_context.rs +154 -154
- data/vendor/kreuzberg/src/pdf/bindings.rs +44 -44
- data/vendor/kreuzberg/src/pdf/bundled.rs +346 -346
- data/vendor/kreuzberg/src/pdf/error.rs +130 -130
- data/vendor/kreuzberg/src/pdf/images.rs +139 -139
- data/vendor/kreuzberg/src/pdf/metadata.rs +489 -489
- data/vendor/kreuzberg/src/pdf/mod.rs +68 -68
- data/vendor/kreuzberg/src/pdf/rendering.rs +368 -368
- data/vendor/kreuzberg/src/pdf/table.rs +420 -420
- data/vendor/kreuzberg/src/pdf/text.rs +240 -240
- data/vendor/kreuzberg/src/plugins/extractor.rs +1044 -1044
- data/vendor/kreuzberg/src/plugins/mod.rs +212 -212
- data/vendor/kreuzberg/src/plugins/ocr.rs +639 -639
- data/vendor/kreuzberg/src/plugins/processor.rs +650 -650
- data/vendor/kreuzberg/src/plugins/registry.rs +1339 -1339
- data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
- data/vendor/kreuzberg/src/plugins/validator.rs +967 -967
- data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
- data/vendor/kreuzberg/src/text/mod.rs +25 -25
- data/vendor/kreuzberg/src/text/quality.rs +697 -697
- data/vendor/kreuzberg/src/text/quality_processor.rs +219 -219
- data/vendor/kreuzberg/src/text/string_utils.rs +217 -217
- data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
- data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -796
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -902
- data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
- data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
- data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -147
- data/vendor/kreuzberg/src/types.rs +1055 -1055
- data/vendor/kreuzberg/src/utils/mod.rs +17 -17
- data/vendor/kreuzberg/src/utils/quality.rs +959 -959
- data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
- data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
- data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
- data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
- data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
- data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
- data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
- data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
- data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
- data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
- data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
- data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
- data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
- data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
- data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
- data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
- data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
- data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
- data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
- data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
- data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
- data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
- data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
- data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
- data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
- data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
- data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
- data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
- data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
- data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
- data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
- data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
- data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
- data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
- data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
- data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
- data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
- data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
- data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
- data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
- data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
- data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
- data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
- data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
- data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
- data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
- data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
- data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
- data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
- data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
- data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
- data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
- data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
- data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
- data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
- data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
- data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
- data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
- data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
- data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
- data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
- data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
- data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
- data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -52
- data/vendor/kreuzberg/tests/api_tests.rs +966 -966
- data/vendor/kreuzberg/tests/archive_integration.rs +545 -545
- data/vendor/kreuzberg/tests/batch_orchestration.rs +556 -556
- data/vendor/kreuzberg/tests/batch_processing.rs +318 -318
- data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -421
- data/vendor/kreuzberg/tests/concurrency_stress.rs +533 -533
- data/vendor/kreuzberg/tests/config_features.rs +612 -612
- data/vendor/kreuzberg/tests/config_loading_tests.rs +416 -416
- data/vendor/kreuzberg/tests/core_integration.rs +510 -510
- data/vendor/kreuzberg/tests/csv_integration.rs +414 -414
- data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +500 -500
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -122
- data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -370
- data/vendor/kreuzberg/tests/email_integration.rs +327 -327
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -275
- data/vendor/kreuzberg/tests/error_handling.rs +402 -402
- data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -228
- data/vendor/kreuzberg/tests/format_integration.rs +164 -164
- data/vendor/kreuzberg/tests/helpers/mod.rs +142 -142
- data/vendor/kreuzberg/tests/html_table_test.rs +551 -551
- data/vendor/kreuzberg/tests/image_integration.rs +255 -255
- data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -139
- data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -639
- data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -704
- data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
- data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
- data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -496
- data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -490
- data/vendor/kreuzberg/tests/mime_detection.rs +429 -429
- data/vendor/kreuzberg/tests/ocr_configuration.rs +514 -514
- data/vendor/kreuzberg/tests/ocr_errors.rs +698 -698
- data/vendor/kreuzberg/tests/ocr_quality.rs +629 -629
- data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
- data/vendor/kreuzberg/tests/odt_extractor_tests.rs +674 -674
- data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -616
- data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -822
- data/vendor/kreuzberg/tests/pdf_integration.rs +45 -45
- data/vendor/kreuzberg/tests/pdfium_linking.rs +374 -374
- data/vendor/kreuzberg/tests/pipeline_integration.rs +1436 -1436
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +776 -776
- data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +560 -560
- data/vendor/kreuzberg/tests/plugin_system.rs +927 -927
- data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +587 -587
- data/vendor/kreuzberg/tests/rst_extractor_tests.rs +694 -694
- data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +775 -775
- data/vendor/kreuzberg/tests/security_validation.rs +416 -416
- data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
- data/vendor/kreuzberg/tests/test_fastembed.rs +631 -631
- data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1260 -1260
- data/vendor/kreuzberg/tests/typst_extractor_tests.rs +648 -648
- data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
- data/vendor/kreuzberg-ffi/Cargo.toml +3 -3
- data/vendor/kreuzberg-ffi/README.md +851 -851
- data/vendor/kreuzberg-ffi/build.rs +176 -176
- data/vendor/kreuzberg-ffi/cbindgen.toml +27 -27
- data/vendor/kreuzberg-ffi/kreuzberg-ffi-install.pc +12 -12
- data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +12 -12
- data/vendor/kreuzberg-ffi/kreuzberg.h +1087 -1087
- data/vendor/kreuzberg-ffi/src/lib.rs +3616 -3616
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +247 -247
- data/vendor/kreuzberg-ffi/tests.disabled/README.md +48 -48
- data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +299 -299
- data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +346 -346
- data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +232 -232
- data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +470 -470
- data/vendor/kreuzberg-tesseract/.commitlintrc.json +13 -13
- data/vendor/kreuzberg-tesseract/.crate-ignore +2 -2
- data/vendor/kreuzberg-tesseract/Cargo.lock +2933 -2933
- data/vendor/kreuzberg-tesseract/Cargo.toml +2 -2
- data/vendor/kreuzberg-tesseract/LICENSE +22 -22
- data/vendor/kreuzberg-tesseract/README.md +399 -399
- data/vendor/kreuzberg-tesseract/build.rs +1354 -1354
- data/vendor/kreuzberg-tesseract/patches/README.md +71 -71
- data/vendor/kreuzberg-tesseract/patches/tesseract.diff +199 -199
- data/vendor/kreuzberg-tesseract/src/api.rs +1371 -1371
- data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +77 -77
- data/vendor/kreuzberg-tesseract/src/enums.rs +297 -297
- data/vendor/kreuzberg-tesseract/src/error.rs +81 -81
- data/vendor/kreuzberg-tesseract/src/lib.rs +145 -145
- data/vendor/kreuzberg-tesseract/src/monitor.rs +57 -57
- data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +197 -197
- data/vendor/kreuzberg-tesseract/src/page_iterator.rs +253 -253
- data/vendor/kreuzberg-tesseract/src/result_iterator.rs +286 -286
- data/vendor/kreuzberg-tesseract/src/result_renderer.rs +183 -183
- data/vendor/kreuzberg-tesseract/tests/integration_test.rs +211 -211
- data/vendor/rb-sys/.cargo_vcs_info.json +5 -5
- data/vendor/rb-sys/Cargo.lock +393 -393
- data/vendor/rb-sys/Cargo.toml +70 -70
- data/vendor/rb-sys/Cargo.toml.orig +57 -57
- data/vendor/rb-sys/LICENSE-APACHE +190 -190
- data/vendor/rb-sys/LICENSE-MIT +21 -21
- data/vendor/rb-sys/build/features.rs +111 -111
- data/vendor/rb-sys/build/main.rs +286 -286
- data/vendor/rb-sys/build/stable_api_config.rs +155 -155
- data/vendor/rb-sys/build/version.rs +50 -50
- data/vendor/rb-sys/readme.md +36 -36
- data/vendor/rb-sys/src/bindings.rs +21 -21
- data/vendor/rb-sys/src/hidden.rs +11 -11
- data/vendor/rb-sys/src/lib.rs +35 -35
- data/vendor/rb-sys/src/macros.rs +371 -371
- data/vendor/rb-sys/src/memory.rs +53 -53
- data/vendor/rb-sys/src/ruby_abi_version.rs +38 -38
- data/vendor/rb-sys/src/special_consts.rs +31 -31
- data/vendor/rb-sys/src/stable_api/compiled.c +179 -179
- data/vendor/rb-sys/src/stable_api/compiled.rs +257 -257
- data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +324 -324
- data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +332 -332
- data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +325 -325
- data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +323 -323
- data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +339 -339
- data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +339 -339
- data/vendor/rb-sys/src/stable_api.rs +260 -260
- data/vendor/rb-sys/src/symbol.rs +31 -31
- data/vendor/rb-sys/src/tracking_allocator.rs +330 -330
- data/vendor/rb-sys/src/utils.rs +89 -89
- data/vendor/rb-sys/src/value_type.rs +7 -7
- metadata +7 -80
|
@@ -1,2076 +1,2076 @@
|
|
|
1
|
-
//! MCP server implementation for Kreuzberg.
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides the core MCP server that exposes document extraction
|
|
4
|
-
//! as tools for AI assistants via the Model Context Protocol.
|
|
5
|
-
|
|
6
|
-
use base64::prelude::*;
|
|
7
|
-
use rmcp::{
|
|
8
|
-
ErrorData as McpError, ServerHandler, ServiceExt,
|
|
9
|
-
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
|
10
|
-
model::*,
|
|
11
|
-
schemars, tool, tool_handler, tool_router,
|
|
12
|
-
transport::stdio,
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
#[cfg(feature = "mcp-http")]
|
|
16
|
-
use rmcp::transport::streamable_http_server::{StreamableHttpService, session::local::LocalSessionManager};
|
|
17
|
-
|
|
18
|
-
use crate::{
|
|
19
|
-
ExtractionConfig, ExtractionResult as KreuzbergResult, KreuzbergError, batch_extract_file, batch_extract_file_sync,
|
|
20
|
-
cache, detect_mime_type, extract_bytes, extract_bytes_sync, extract_file, extract_file_sync,
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/// Request parameters for file extraction.
|
|
24
|
-
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
25
|
-
pub struct ExtractFileParams {
|
|
26
|
-
/// Path to the file to extract
|
|
27
|
-
pub path: String,
|
|
28
|
-
/// Optional MIME type hint (auto-detected if not provided)
|
|
29
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
30
|
-
pub mime_type: Option<String>,
|
|
31
|
-
/// Enable OCR for scanned documents
|
|
32
|
-
#[serde(default)]
|
|
33
|
-
pub enable_ocr: bool,
|
|
34
|
-
/// Force OCR even if text extraction succeeds
|
|
35
|
-
#[serde(default)]
|
|
36
|
-
pub force_ocr: bool,
|
|
37
|
-
/// Use async extraction (default: false for sync)
|
|
38
|
-
#[serde(default)]
|
|
39
|
-
pub r#async: bool,
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/// Request parameters for bytes extraction.
|
|
43
|
-
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
44
|
-
pub struct ExtractBytesParams {
|
|
45
|
-
/// Base64-encoded file content
|
|
46
|
-
pub data: String,
|
|
47
|
-
/// Optional MIME type hint (auto-detected if not provided)
|
|
48
|
-
#[serde(skip_serializing_if = "Option::is_none")]
|
|
49
|
-
pub mime_type: Option<String>,
|
|
50
|
-
/// Enable OCR for scanned documents
|
|
51
|
-
#[serde(default)]
|
|
52
|
-
pub enable_ocr: bool,
|
|
53
|
-
/// Force OCR even if text extraction succeeds
|
|
54
|
-
#[serde(default)]
|
|
55
|
-
pub force_ocr: bool,
|
|
56
|
-
/// Use async extraction (default: false for sync)
|
|
57
|
-
#[serde(default)]
|
|
58
|
-
pub r#async: bool,
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/// Request parameters for batch file extraction.
|
|
62
|
-
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
63
|
-
pub struct BatchExtractFilesParams {
|
|
64
|
-
/// Paths to files to extract
|
|
65
|
-
pub paths: Vec<String>,
|
|
66
|
-
/// Enable OCR for scanned documents
|
|
67
|
-
#[serde(default)]
|
|
68
|
-
pub enable_ocr: bool,
|
|
69
|
-
/// Force OCR even if text extraction succeeds
|
|
70
|
-
#[serde(default)]
|
|
71
|
-
pub force_ocr: bool,
|
|
72
|
-
/// Use async extraction (default: false for sync)
|
|
73
|
-
#[serde(default)]
|
|
74
|
-
pub r#async: bool,
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/// Request parameters for MIME type detection.
|
|
78
|
-
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
79
|
-
pub struct DetectMimeTypeParams {
|
|
80
|
-
/// Path to the file
|
|
81
|
-
pub path: String,
|
|
82
|
-
/// Use content-based detection (default: true)
|
|
83
|
-
#[serde(default = "default_use_content")]
|
|
84
|
-
pub use_content: bool,
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
fn default_use_content() -> bool {
|
|
88
|
-
true
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/// Map Kreuzberg errors to MCP error responses with appropriate error codes.
|
|
92
|
-
///
|
|
93
|
-
/// This function ensures different error types are properly differentiated in MCP responses:
|
|
94
|
-
/// - `Validation` errors → `INVALID_PARAMS` (-32602)
|
|
95
|
-
/// - `UnsupportedFormat` errors → `INVALID_PARAMS` (-32602)
|
|
96
|
-
/// - `Parsing` errors → `PARSE_ERROR` (-32700)
|
|
97
|
-
/// - `Io` errors → `INTERNAL_ERROR` (-32603) with context preserved
|
|
98
|
-
/// - All other errors → `INTERNAL_ERROR` (-32603)
|
|
99
|
-
///
|
|
100
|
-
/// The error message and source chain are preserved to aid debugging.
|
|
101
|
-
#[doc(hidden)]
|
|
102
|
-
pub fn map_kreuzberg_error_to_mcp(error: KreuzbergError) -> McpError {
|
|
103
|
-
match error {
|
|
104
|
-
KreuzbergError::Validation { message, source } => {
|
|
105
|
-
let mut error_message = format!("Validation error: {}", message);
|
|
106
|
-
if let Some(src) = source {
|
|
107
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
108
|
-
}
|
|
109
|
-
McpError::invalid_params(error_message, None)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
KreuzbergError::UnsupportedFormat(mime_type) => {
|
|
113
|
-
McpError::invalid_params(format!("Unsupported format: {}", mime_type), None)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
KreuzbergError::MissingDependency(dep) => McpError::invalid_params(
|
|
117
|
-
format!(
|
|
118
|
-
"Missing required dependency: {}. Please install it to use this feature.",
|
|
119
|
-
dep
|
|
120
|
-
),
|
|
121
|
-
None,
|
|
122
|
-
),
|
|
123
|
-
|
|
124
|
-
KreuzbergError::Parsing { message, source } => {
|
|
125
|
-
let mut error_message = format!("Parsing error: {}", message);
|
|
126
|
-
if let Some(src) = source {
|
|
127
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
128
|
-
}
|
|
129
|
-
McpError::parse_error(error_message, None)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// OSError/RuntimeError must bubble up - system errors need user reports ~keep
|
|
133
|
-
KreuzbergError::Io(io_err) => McpError::internal_error(format!("System I/O error: {}", io_err), None),
|
|
134
|
-
|
|
135
|
-
KreuzbergError::Ocr { message, source } => {
|
|
136
|
-
let mut error_message = format!("OCR processing error: {}", message);
|
|
137
|
-
if let Some(src) = source {
|
|
138
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
139
|
-
}
|
|
140
|
-
McpError::internal_error(error_message, None)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
KreuzbergError::Cache { message, source } => {
|
|
144
|
-
let mut error_message = format!("Cache error: {}", message);
|
|
145
|
-
if let Some(src) = source {
|
|
146
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
147
|
-
}
|
|
148
|
-
McpError::internal_error(error_message, None)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
KreuzbergError::ImageProcessing { message, source } => {
|
|
152
|
-
let mut error_message = format!("Image processing error: {}", message);
|
|
153
|
-
if let Some(src) = source {
|
|
154
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
155
|
-
}
|
|
156
|
-
McpError::internal_error(error_message, None)
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
KreuzbergError::Serialization { message, source } => {
|
|
160
|
-
let mut error_message = format!("Serialization error: {}", message);
|
|
161
|
-
if let Some(src) = source {
|
|
162
|
-
error_message.push_str(&format!(" (caused by: {})", src));
|
|
163
|
-
}
|
|
164
|
-
McpError::internal_error(error_message, None)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
KreuzbergError::Plugin { message, plugin_name } => {
|
|
168
|
-
McpError::internal_error(format!("Plugin '{}' error: {}", plugin_name, message), None)
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
KreuzbergError::LockPoisoned(msg) => McpError::internal_error(format!("Internal lock poisoned: {}", msg), None),
|
|
172
|
-
|
|
173
|
-
KreuzbergError::Other(msg) => McpError::internal_error(msg, None),
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/// Kreuzberg MCP server.
|
|
178
|
-
///
|
|
179
|
-
/// Provides document extraction capabilities via MCP tools.
|
|
180
|
-
///
|
|
181
|
-
/// The server loads a default extraction configuration from kreuzberg.toml/yaml/json
|
|
182
|
-
/// via discovery. Per-request OCR settings override the defaults.
|
|
183
|
-
#[derive(Clone)]
|
|
184
|
-
pub struct KreuzbergMcp {
|
|
185
|
-
tool_router: ToolRouter<KreuzbergMcp>,
|
|
186
|
-
/// Default extraction configuration loaded from config file via discovery
|
|
187
|
-
default_config: std::sync::Arc<ExtractionConfig>,
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
#[tool_router]
|
|
191
|
-
impl KreuzbergMcp {
|
|
192
|
-
/// Create a new Kreuzberg MCP server instance with default config.
|
|
193
|
-
///
|
|
194
|
-
/// Uses `ExtractionConfig::discover()` to search for kreuzberg.toml/yaml/json
|
|
195
|
-
/// in current and parent directories. Falls back to default configuration if
|
|
196
|
-
/// no config file is found.
|
|
197
|
-
#[allow(clippy::manual_unwrap_or_default)]
|
|
198
|
-
pub fn new() -> crate::Result<Self> {
|
|
199
|
-
let config = match ExtractionConfig::discover()? {
|
|
200
|
-
Some(config) => {
|
|
201
|
-
#[cfg(feature = "api")]
|
|
202
|
-
tracing::info!("Loaded extraction config from discovered file");
|
|
203
|
-
config
|
|
204
|
-
}
|
|
205
|
-
None => {
|
|
206
|
-
#[cfg(feature = "api")]
|
|
207
|
-
tracing::info!("No config file found, using default configuration");
|
|
208
|
-
ExtractionConfig::default()
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
Ok(Self::with_config(config))
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/// Create a new Kreuzberg MCP server instance with explicit config.
|
|
216
|
-
///
|
|
217
|
-
/// # Arguments
|
|
218
|
-
///
|
|
219
|
-
/// * `config` - Default extraction configuration for all tool calls
|
|
220
|
-
pub fn with_config(config: ExtractionConfig) -> Self {
|
|
221
|
-
Self {
|
|
222
|
-
tool_router: Self::tool_router(),
|
|
223
|
-
default_config: std::sync::Arc::new(config),
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/// Extract content from a file.
|
|
228
|
-
///
|
|
229
|
-
/// This tool extracts text, metadata, and tables from documents in various formats
|
|
230
|
-
/// including PDFs, Word documents, Excel spreadsheets, images (with OCR), and more.
|
|
231
|
-
#[tool(
|
|
232
|
-
description = "Extract content from a file by path. Supports PDFs, Word, Excel, images (with OCR), HTML, and more."
|
|
233
|
-
)]
|
|
234
|
-
async fn extract_file(
|
|
235
|
-
&self,
|
|
236
|
-
Parameters(params): Parameters<ExtractFileParams>,
|
|
237
|
-
) -> Result<CallToolResult, McpError> {
|
|
238
|
-
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
239
|
-
|
|
240
|
-
let result = if params.r#async {
|
|
241
|
-
extract_file(¶ms.path, params.mime_type.as_deref(), &config)
|
|
242
|
-
.await
|
|
243
|
-
.map_err(map_kreuzberg_error_to_mcp)?
|
|
244
|
-
} else {
|
|
245
|
-
extract_file_sync(¶ms.path, params.mime_type.as_deref(), &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
let response = format_extraction_result(&result);
|
|
249
|
-
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/// Extract content from base64-encoded bytes.
|
|
253
|
-
///
|
|
254
|
-
/// This tool extracts text, metadata, and tables from base64-encoded document data.
|
|
255
|
-
#[tool(
|
|
256
|
-
description = "Extract content from base64-encoded file data. Returns extracted text, metadata, and tables."
|
|
257
|
-
)]
|
|
258
|
-
async fn extract_bytes(
|
|
259
|
-
&self,
|
|
260
|
-
Parameters(params): Parameters<ExtractBytesParams>,
|
|
261
|
-
) -> Result<CallToolResult, McpError> {
|
|
262
|
-
let bytes = BASE64_STANDARD
|
|
263
|
-
.decode(¶ms.data)
|
|
264
|
-
.map_err(|e| McpError::invalid_params(format!("Invalid base64: {}", e), None))?;
|
|
265
|
-
|
|
266
|
-
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
267
|
-
|
|
268
|
-
let mime_type = params.mime_type.as_deref().unwrap_or("");
|
|
269
|
-
|
|
270
|
-
let result = if params.r#async {
|
|
271
|
-
extract_bytes(&bytes, mime_type, &config)
|
|
272
|
-
.await
|
|
273
|
-
.map_err(map_kreuzberg_error_to_mcp)?
|
|
274
|
-
} else {
|
|
275
|
-
extract_bytes_sync(&bytes, mime_type, &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
let response = format_extraction_result(&result);
|
|
279
|
-
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/// Extract content from multiple files in parallel.
|
|
283
|
-
///
|
|
284
|
-
/// This tool efficiently processes multiple documents simultaneously, useful for batch operations.
|
|
285
|
-
#[tool(description = "Extract content from multiple files in parallel. Returns results for all files.")]
|
|
286
|
-
async fn batch_extract_files(
|
|
287
|
-
&self,
|
|
288
|
-
Parameters(params): Parameters<BatchExtractFilesParams>,
|
|
289
|
-
) -> Result<CallToolResult, McpError> {
|
|
290
|
-
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
291
|
-
|
|
292
|
-
let results = if params.r#async {
|
|
293
|
-
batch_extract_file(params.paths.clone(), &config)
|
|
294
|
-
.await
|
|
295
|
-
.map_err(map_kreuzberg_error_to_mcp)?
|
|
296
|
-
} else {
|
|
297
|
-
batch_extract_file_sync(params.paths.clone(), &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
let mut response = String::new();
|
|
301
|
-
for (i, result) in results.iter().enumerate() {
|
|
302
|
-
response.push_str(&format!("=== Document {}: {} ===\n", i + 1, params.paths[i]));
|
|
303
|
-
response.push_str(&format_extraction_result(result));
|
|
304
|
-
response.push_str("\n\n");
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/// Detect the MIME type of a file.
|
|
311
|
-
///
|
|
312
|
-
/// This tool identifies the file format, useful for determining which extractor to use.
|
|
313
|
-
#[tool(description = "Detect the MIME type of a file. Returns the detected MIME type string.")]
|
|
314
|
-
fn detect_mime_type(
|
|
315
|
-
&self,
|
|
316
|
-
Parameters(params): Parameters<DetectMimeTypeParams>,
|
|
317
|
-
) -> Result<CallToolResult, McpError> {
|
|
318
|
-
let mime_type = detect_mime_type(¶ms.path, params.use_content).map_err(map_kreuzberg_error_to_mcp)?;
|
|
319
|
-
|
|
320
|
-
Ok(CallToolResult::success(vec![Content::text(mime_type)]))
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/// Get cache statistics.
|
|
324
|
-
///
|
|
325
|
-
/// This tool returns statistics about the cache including total files, size, and disk space.
|
|
326
|
-
#[tool(description = "Get cache statistics including total files, size, and available disk space.")]
|
|
327
|
-
fn cache_stats(&self, Parameters(_): Parameters<()>) -> Result<CallToolResult, McpError> {
|
|
328
|
-
let cache_dir = std::env::current_dir()
|
|
329
|
-
.unwrap_or_else(|_| std::path::PathBuf::from("."))
|
|
330
|
-
.join(".kreuzberg");
|
|
331
|
-
|
|
332
|
-
let stats = cache::get_cache_metadata(cache_dir.to_str().unwrap_or(".")).map_err(map_kreuzberg_error_to_mcp)?;
|
|
333
|
-
|
|
334
|
-
let response = format!(
|
|
335
|
-
"Cache Statistics\n\
|
|
336
|
-
================\n\
|
|
337
|
-
Directory: {}\n\
|
|
338
|
-
Total files: {}\n\
|
|
339
|
-
Total size: {:.2} MB\n\
|
|
340
|
-
Available space: {:.2} MB\n\
|
|
341
|
-
Oldest file age: {:.2} days\n\
|
|
342
|
-
Newest file age: {:.2} days",
|
|
343
|
-
cache_dir.to_string_lossy(),
|
|
344
|
-
stats.total_files,
|
|
345
|
-
stats.total_size_mb,
|
|
346
|
-
stats.available_space_mb,
|
|
347
|
-
stats.oldest_file_age_days,
|
|
348
|
-
stats.newest_file_age_days
|
|
349
|
-
);
|
|
350
|
-
|
|
351
|
-
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/// Clear the cache.
|
|
355
|
-
///
|
|
356
|
-
/// This tool removes all cached files and returns the number of files removed and space freed.
|
|
357
|
-
#[tool(description = "Clear all cached files. Returns the number of files removed and space freed in MB.")]
|
|
358
|
-
fn cache_clear(&self, Parameters(_): Parameters<()>) -> Result<CallToolResult, McpError> {
|
|
359
|
-
let cache_dir = std::env::current_dir()
|
|
360
|
-
.unwrap_or_else(|_| std::path::PathBuf::from("."))
|
|
361
|
-
.join(".kreuzberg");
|
|
362
|
-
|
|
363
|
-
let (removed_files, freed_mb) =
|
|
364
|
-
cache::clear_cache_directory(cache_dir.to_str().unwrap_or(".")).map_err(map_kreuzberg_error_to_mcp)?;
|
|
365
|
-
|
|
366
|
-
let response = format!(
|
|
367
|
-
"Cache cleared successfully\n\
|
|
368
|
-
Directory: {}\n\
|
|
369
|
-
Removed files: {}\n\
|
|
370
|
-
Freed space: {:.2} MB",
|
|
371
|
-
cache_dir.to_string_lossy(),
|
|
372
|
-
removed_files,
|
|
373
|
-
freed_mb
|
|
374
|
-
);
|
|
375
|
-
|
|
376
|
-
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
#[tool_handler]
|
|
381
|
-
impl ServerHandler for KreuzbergMcp {
|
|
382
|
-
fn get_info(&self) -> ServerInfo {
|
|
383
|
-
ServerInfo {
|
|
384
|
-
protocol_version: ProtocolVersion::default(),
|
|
385
|
-
capabilities: ServerCapabilities {
|
|
386
|
-
tools: Some(ToolsCapability::default()),
|
|
387
|
-
..Default::default()
|
|
388
|
-
},
|
|
389
|
-
server_info: Implementation {
|
|
390
|
-
name: "kreuzberg-mcp".to_string(),
|
|
391
|
-
title: Some("Kreuzberg Document Intelligence MCP Server".to_string()),
|
|
392
|
-
version: env!("CARGO_PKG_VERSION").to_string(),
|
|
393
|
-
icons: None,
|
|
394
|
-
website_url: Some("https://goldziher.github.io/kreuzberg/".to_string()),
|
|
395
|
-
},
|
|
396
|
-
instructions: Some(
|
|
397
|
-
"Extract content from documents in various formats. Supports PDFs, Word documents, \
|
|
398
|
-
Excel spreadsheets, images (with OCR), HTML, emails, and more. Use enable_ocr=true \
|
|
399
|
-
for scanned documents, force_ocr=true to always use OCR even if text extraction \
|
|
400
|
-
succeeds."
|
|
401
|
-
.to_string(),
|
|
402
|
-
),
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
impl Default for KreuzbergMcp {
|
|
408
|
-
fn default() -> Self {
|
|
409
|
-
Self::new().unwrap_or_else(|e| {
|
|
410
|
-
#[cfg(feature = "api")]
|
|
411
|
-
tracing::warn!("Failed to discover config, using default: {}", e);
|
|
412
|
-
#[cfg(not(feature = "api"))]
|
|
413
|
-
tracing::debug!("Warning: Failed to discover config, using default: {}", e);
|
|
414
|
-
Self::with_config(ExtractionConfig::default())
|
|
415
|
-
})
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/// Start the Kreuzberg MCP server.
|
|
420
|
-
///
|
|
421
|
-
/// This function initializes and runs the MCP server using stdio transport.
|
|
422
|
-
/// It will block until the server is shut down.
|
|
423
|
-
///
|
|
424
|
-
/// # Errors
|
|
425
|
-
///
|
|
426
|
-
/// Returns an error if the server fails to start or encounters a fatal error.
|
|
427
|
-
///
|
|
428
|
-
/// # Example
|
|
429
|
-
///
|
|
430
|
-
/// ```rust,no_run
|
|
431
|
-
/// use kreuzberg::mcp::start_mcp_server;
|
|
432
|
-
///
|
|
433
|
-
/// #[tokio::main]
|
|
434
|
-
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
435
|
-
/// start_mcp_server().await?;
|
|
436
|
-
/// Ok(())
|
|
437
|
-
/// }
|
|
438
|
-
/// ```
|
|
439
|
-
pub async fn start_mcp_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
440
|
-
let service = KreuzbergMcp::new()?.serve(stdio()).await?;
|
|
441
|
-
|
|
442
|
-
service.waiting().await?;
|
|
443
|
-
Ok(())
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
/// Start MCP server with custom extraction config.
|
|
447
|
-
///
|
|
448
|
-
/// This variant allows specifying a custom extraction configuration
|
|
449
|
-
/// (e.g., loaded from a file) instead of using defaults.
|
|
450
|
-
pub async fn start_mcp_server_with_config(
|
|
451
|
-
config: ExtractionConfig,
|
|
452
|
-
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
453
|
-
let service = KreuzbergMcp::with_config(config).serve(stdio()).await?;
|
|
454
|
-
|
|
455
|
-
service.waiting().await?;
|
|
456
|
-
Ok(())
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/// Start MCP server with HTTP Stream transport.
|
|
460
|
-
///
|
|
461
|
-
/// Uses rmcp's built-in StreamableHttpService for HTTP/SSE support per MCP spec.
|
|
462
|
-
///
|
|
463
|
-
/// # Arguments
|
|
464
|
-
///
|
|
465
|
-
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
|
|
466
|
-
/// * `port` - Port number (e.g., 8001)
|
|
467
|
-
///
|
|
468
|
-
/// # Example
|
|
469
|
-
///
|
|
470
|
-
/// ```no_run
|
|
471
|
-
/// use kreuzberg::mcp::start_mcp_server_http;
|
|
472
|
-
///
|
|
473
|
-
/// #[tokio::main]
|
|
474
|
-
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
475
|
-
/// start_mcp_server_http("127.0.0.1", 8001).await?;
|
|
476
|
-
/// Ok(())
|
|
477
|
-
/// }
|
|
478
|
-
/// ```
|
|
479
|
-
#[cfg(feature = "mcp-http")]
|
|
480
|
-
pub async fn start_mcp_server_http(
|
|
481
|
-
host: impl AsRef<str>,
|
|
482
|
-
port: u16,
|
|
483
|
-
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
484
|
-
use axum::Router;
|
|
485
|
-
use std::net::SocketAddr;
|
|
486
|
-
|
|
487
|
-
let http_service = StreamableHttpService::new(
|
|
488
|
-
|| KreuzbergMcp::new().map_err(|e| std::io::Error::other(e.to_string())),
|
|
489
|
-
LocalSessionManager::default().into(),
|
|
490
|
-
Default::default(),
|
|
491
|
-
);
|
|
492
|
-
|
|
493
|
-
let router = Router::new().nest_service("/mcp", http_service);
|
|
494
|
-
|
|
495
|
-
let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
|
|
496
|
-
.parse()
|
|
497
|
-
.map_err(|e| format!("Invalid address: {}", e))?;
|
|
498
|
-
|
|
499
|
-
#[cfg(feature = "api")]
|
|
500
|
-
tracing::info!("Starting MCP HTTP server on http://{}", addr);
|
|
501
|
-
|
|
502
|
-
let listener = tokio::net::TcpListener::bind(addr).await?;
|
|
503
|
-
axum::serve(listener, router).await?;
|
|
504
|
-
|
|
505
|
-
Ok(())
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/// Start MCP HTTP server with custom extraction config.
|
|
509
|
-
///
|
|
510
|
-
/// This variant allows specifying a custom extraction configuration
|
|
511
|
-
/// while using HTTP Stream transport.
|
|
512
|
-
///
|
|
513
|
-
/// # Arguments
|
|
514
|
-
///
|
|
515
|
-
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
|
|
516
|
-
/// * `port` - Port number (e.g., 8001)
|
|
517
|
-
/// * `config` - Custom extraction configuration
|
|
518
|
-
///
|
|
519
|
-
/// # Example
|
|
520
|
-
///
|
|
521
|
-
/// ```no_run
|
|
522
|
-
/// use kreuzberg::mcp::start_mcp_server_http_with_config;
|
|
523
|
-
/// use kreuzberg::ExtractionConfig;
|
|
524
|
-
///
|
|
525
|
-
/// #[tokio::main]
|
|
526
|
-
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
527
|
-
/// let config = ExtractionConfig::default();
|
|
528
|
-
/// start_mcp_server_http_with_config("127.0.0.1", 8001, config).await?;
|
|
529
|
-
/// Ok(())
|
|
530
|
-
/// }
|
|
531
|
-
/// ```
|
|
532
|
-
#[cfg(feature = "mcp-http")]
|
|
533
|
-
pub async fn start_mcp_server_http_with_config(
|
|
534
|
-
host: impl AsRef<str>,
|
|
535
|
-
port: u16,
|
|
536
|
-
config: ExtractionConfig,
|
|
537
|
-
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
538
|
-
use axum::Router;
|
|
539
|
-
use std::net::SocketAddr;
|
|
540
|
-
|
|
541
|
-
let http_service = StreamableHttpService::new(
|
|
542
|
-
move || Ok(KreuzbergMcp::with_config(config.clone())),
|
|
543
|
-
LocalSessionManager::default().into(),
|
|
544
|
-
Default::default(),
|
|
545
|
-
);
|
|
546
|
-
|
|
547
|
-
let router = Router::new().nest_service("/mcp", http_service);
|
|
548
|
-
|
|
549
|
-
let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
|
|
550
|
-
.parse()
|
|
551
|
-
.map_err(|e| format!("Invalid address: {}", e))?;
|
|
552
|
-
|
|
553
|
-
#[cfg(feature = "api")]
|
|
554
|
-
tracing::info!("Starting MCP HTTP server on http://{}", addr);
|
|
555
|
-
|
|
556
|
-
let listener = tokio::net::TcpListener::bind(addr).await?;
|
|
557
|
-
axum::serve(listener, router).await?;
|
|
558
|
-
|
|
559
|
-
Ok(())
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
/// Build extraction config from MCP parameters.
|
|
563
|
-
///
|
|
564
|
-
/// Starts with the default config and overlays OCR settings from request parameters.
|
|
565
|
-
fn build_config(default_config: &ExtractionConfig, enable_ocr: bool, force_ocr: bool) -> ExtractionConfig {
|
|
566
|
-
let mut config = default_config.clone();
|
|
567
|
-
|
|
568
|
-
config.ocr = if enable_ocr {
|
|
569
|
-
Some(crate::OcrConfig {
|
|
570
|
-
backend: "tesseract".to_string(),
|
|
571
|
-
language: "eng".to_string(),
|
|
572
|
-
tesseract_config: None,
|
|
573
|
-
})
|
|
574
|
-
} else {
|
|
575
|
-
None
|
|
576
|
-
};
|
|
577
|
-
config.force_ocr = force_ocr;
|
|
578
|
-
|
|
579
|
-
config
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/// Format extraction result as human-readable text.
|
|
583
|
-
fn format_extraction_result(result: &KreuzbergResult) -> String {
|
|
584
|
-
let mut response = String::new();
|
|
585
|
-
|
|
586
|
-
response.push_str(&format!("Content ({} characters):\n", result.content.len()));
|
|
587
|
-
response.push_str(&result.content);
|
|
588
|
-
response.push_str("\n\n");
|
|
589
|
-
|
|
590
|
-
response.push_str("Metadata:\n");
|
|
591
|
-
response.push_str(&serde_json::to_string_pretty(&result.metadata).unwrap_or_default());
|
|
592
|
-
response.push_str("\n\n");
|
|
593
|
-
|
|
594
|
-
if !result.tables.is_empty() {
|
|
595
|
-
response.push_str(&format!("Tables ({}):\n", result.tables.len()));
|
|
596
|
-
for (i, table) in result.tables.iter().enumerate() {
|
|
597
|
-
response.push_str(&format!("\nTable {} (page {}):\n", i + 1, table.page_number));
|
|
598
|
-
response.push_str(&table.markdown);
|
|
599
|
-
response.push('\n');
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
response
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
#[cfg(test)]
|
|
607
|
-
mod tests {
|
|
608
|
-
use super::*;
|
|
609
|
-
use std::path::PathBuf;
|
|
610
|
-
|
|
611
|
-
/// Get the path to a test document relative to workspace root.
|
|
612
|
-
fn get_test_path(relative_path: &str) -> String {
|
|
613
|
-
let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
614
|
-
.parent()
|
|
615
|
-
.unwrap()
|
|
616
|
-
.parent()
|
|
617
|
-
.unwrap()
|
|
618
|
-
.to_path_buf();
|
|
619
|
-
|
|
620
|
-
workspace_root
|
|
621
|
-
.join("test_documents")
|
|
622
|
-
.join(relative_path)
|
|
623
|
-
.to_string_lossy()
|
|
624
|
-
.to_string()
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
#[tokio::test]
|
|
628
|
-
async fn test_tool_router_has_routes() {
|
|
629
|
-
let router = KreuzbergMcp::tool_router();
|
|
630
|
-
assert!(router.has_route("extract_file"));
|
|
631
|
-
assert!(router.has_route("extract_bytes"));
|
|
632
|
-
assert!(router.has_route("batch_extract_files"));
|
|
633
|
-
assert!(router.has_route("detect_mime_type"));
|
|
634
|
-
assert!(router.has_route("cache_stats"));
|
|
635
|
-
assert!(router.has_route("cache_clear"));
|
|
636
|
-
|
|
637
|
-
let tools = router.list_all();
|
|
638
|
-
assert_eq!(tools.len(), 6);
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
#[test]
|
|
642
|
-
fn test_server_info() {
|
|
643
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
644
|
-
let info = server.get_info();
|
|
645
|
-
|
|
646
|
-
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
647
|
-
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
648
|
-
assert!(info.capabilities.tools.is_some());
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
#[test]
|
|
652
|
-
fn test_build_config() {
|
|
653
|
-
let default_config = ExtractionConfig::default();
|
|
654
|
-
|
|
655
|
-
let config = build_config(&default_config, false, false);
|
|
656
|
-
assert!(config.ocr.is_none());
|
|
657
|
-
assert!(!config.force_ocr);
|
|
658
|
-
|
|
659
|
-
let config = build_config(&default_config, true, false);
|
|
660
|
-
assert!(config.ocr.is_some());
|
|
661
|
-
assert!(!config.force_ocr);
|
|
662
|
-
|
|
663
|
-
let config = build_config(&default_config, true, true);
|
|
664
|
-
assert!(config.ocr.is_some());
|
|
665
|
-
assert!(config.force_ocr);
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
#[test]
|
|
669
|
-
fn test_with_config_stores_provided_config() {
|
|
670
|
-
let custom_config = ExtractionConfig {
|
|
671
|
-
force_ocr: true,
|
|
672
|
-
use_cache: false,
|
|
673
|
-
..Default::default()
|
|
674
|
-
};
|
|
675
|
-
|
|
676
|
-
let server = KreuzbergMcp::with_config(custom_config);
|
|
677
|
-
|
|
678
|
-
assert!(server.default_config.force_ocr);
|
|
679
|
-
assert!(!server.default_config.use_cache);
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
#[test]
|
|
683
|
-
fn test_map_validation_error_to_invalid_params() {
|
|
684
|
-
let error = KreuzbergError::validation("invalid file path");
|
|
685
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
686
|
-
|
|
687
|
-
assert_eq!(mcp_error.code.0, -32602);
|
|
688
|
-
assert!(mcp_error.message.contains("Validation error"));
|
|
689
|
-
assert!(mcp_error.message.contains("invalid file path"));
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
#[test]
|
|
693
|
-
fn test_map_validation_error_with_source_preserves_chain() {
|
|
694
|
-
let source = std::io::Error::new(std::io::ErrorKind::InvalidInput, "bad param");
|
|
695
|
-
let error = KreuzbergError::validation_with_source("invalid configuration", source);
|
|
696
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
697
|
-
|
|
698
|
-
assert_eq!(mcp_error.code.0, -32602);
|
|
699
|
-
assert!(mcp_error.message.contains("Validation error"));
|
|
700
|
-
assert!(mcp_error.message.contains("invalid configuration"));
|
|
701
|
-
assert!(mcp_error.message.contains("caused by"));
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
#[test]
|
|
705
|
-
fn test_map_unsupported_format_to_invalid_params() {
|
|
706
|
-
let error = KreuzbergError::UnsupportedFormat("application/unknown".to_string());
|
|
707
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
708
|
-
|
|
709
|
-
assert_eq!(mcp_error.code.0, -32602);
|
|
710
|
-
assert!(mcp_error.message.contains("Unsupported format"));
|
|
711
|
-
assert!(mcp_error.message.contains("application/unknown"));
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
#[test]
|
|
715
|
-
fn test_map_missing_dependency_to_invalid_params() {
|
|
716
|
-
let error = KreuzbergError::MissingDependency("tesseract".to_string());
|
|
717
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
718
|
-
|
|
719
|
-
assert_eq!(mcp_error.code.0, -32602);
|
|
720
|
-
assert!(mcp_error.message.contains("Missing required dependency"));
|
|
721
|
-
assert!(mcp_error.message.contains("tesseract"));
|
|
722
|
-
assert!(mcp_error.message.contains("Please install"));
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
#[test]
|
|
726
|
-
fn test_map_parsing_error_to_parse_error() {
|
|
727
|
-
let error = KreuzbergError::parsing("corrupt PDF file");
|
|
728
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
729
|
-
|
|
730
|
-
assert_eq!(mcp_error.code.0, -32700);
|
|
731
|
-
assert!(mcp_error.message.contains("Parsing error"));
|
|
732
|
-
assert!(mcp_error.message.contains("corrupt PDF file"));
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
#[test]
|
|
736
|
-
fn test_map_parsing_error_with_source_preserves_chain() {
|
|
737
|
-
let source = std::io::Error::new(std::io::ErrorKind::InvalidData, "malformed data");
|
|
738
|
-
let error = KreuzbergError::parsing_with_source("failed to parse document", source);
|
|
739
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
740
|
-
|
|
741
|
-
assert_eq!(mcp_error.code.0, -32700);
|
|
742
|
-
assert!(mcp_error.message.contains("Parsing error"));
|
|
743
|
-
assert!(mcp_error.message.contains("failed to parse document"));
|
|
744
|
-
assert!(mcp_error.message.contains("caused by"));
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
#[test]
|
|
748
|
-
fn test_map_io_error_to_internal_error() {
|
|
749
|
-
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
|
|
750
|
-
let error = KreuzbergError::Io(io_error);
|
|
751
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
752
|
-
|
|
753
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
754
|
-
assert!(mcp_error.message.contains("System I/O error"));
|
|
755
|
-
assert!(mcp_error.message.contains("file not found"));
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
#[test]
|
|
759
|
-
fn test_map_ocr_error_to_internal_error() {
|
|
760
|
-
let error = KreuzbergError::ocr("tesseract failed");
|
|
761
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
762
|
-
|
|
763
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
764
|
-
assert!(mcp_error.message.contains("OCR processing error"));
|
|
765
|
-
assert!(mcp_error.message.contains("tesseract failed"));
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
#[test]
|
|
769
|
-
fn test_map_cache_error_to_internal_error() {
|
|
770
|
-
let error = KreuzbergError::cache("cache write failed");
|
|
771
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
772
|
-
|
|
773
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
774
|
-
assert!(mcp_error.message.contains("Cache error"));
|
|
775
|
-
assert!(mcp_error.message.contains("cache write failed"));
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
#[test]
|
|
779
|
-
fn test_map_image_processing_error_to_internal_error() {
|
|
780
|
-
let error = KreuzbergError::image_processing("resize failed");
|
|
781
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
782
|
-
|
|
783
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
784
|
-
assert!(mcp_error.message.contains("Image processing error"));
|
|
785
|
-
assert!(mcp_error.message.contains("resize failed"));
|
|
786
|
-
}
|
|
787
|
-
|
|
788
|
-
#[test]
|
|
789
|
-
fn test_map_serialization_error_to_internal_error() {
|
|
790
|
-
let error = KreuzbergError::serialization("JSON encode failed");
|
|
791
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
792
|
-
|
|
793
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
794
|
-
assert!(mcp_error.message.contains("Serialization error"));
|
|
795
|
-
assert!(mcp_error.message.contains("JSON encode failed"));
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
#[test]
|
|
799
|
-
fn test_map_plugin_error_to_internal_error() {
|
|
800
|
-
let error = KreuzbergError::Plugin {
|
|
801
|
-
message: "extraction failed".to_string(),
|
|
802
|
-
plugin_name: "pdf-extractor".to_string(),
|
|
803
|
-
};
|
|
804
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
805
|
-
|
|
806
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
807
|
-
assert!(mcp_error.message.contains("Plugin 'pdf-extractor' error"));
|
|
808
|
-
assert!(mcp_error.message.contains("extraction failed"));
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
#[test]
|
|
812
|
-
fn test_map_lock_poisoned_error_to_internal_error() {
|
|
813
|
-
let error = KreuzbergError::LockPoisoned("registry lock poisoned".to_string());
|
|
814
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
815
|
-
|
|
816
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
817
|
-
assert!(mcp_error.message.contains("Internal lock poisoned"));
|
|
818
|
-
assert!(mcp_error.message.contains("registry lock poisoned"));
|
|
819
|
-
}
|
|
820
|
-
|
|
821
|
-
#[test]
|
|
822
|
-
fn test_map_other_error_to_internal_error() {
|
|
823
|
-
let error = KreuzbergError::Other("unexpected error".to_string());
|
|
824
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
825
|
-
|
|
826
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
827
|
-
assert!(mcp_error.message.contains("unexpected error"));
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
#[test]
|
|
831
|
-
fn test_error_type_differentiation() {
|
|
832
|
-
let validation = KreuzbergError::validation("test");
|
|
833
|
-
let parsing = KreuzbergError::parsing("test");
|
|
834
|
-
let io = KreuzbergError::Io(std::io::Error::other("test"));
|
|
835
|
-
|
|
836
|
-
let val_mcp = map_kreuzberg_error_to_mcp(validation);
|
|
837
|
-
let parse_mcp = map_kreuzberg_error_to_mcp(parsing);
|
|
838
|
-
let io_mcp = map_kreuzberg_error_to_mcp(io);
|
|
839
|
-
|
|
840
|
-
assert_eq!(val_mcp.code.0, -32602);
|
|
841
|
-
assert_eq!(parse_mcp.code.0, -32700);
|
|
842
|
-
assert_eq!(io_mcp.code.0, -32603);
|
|
843
|
-
|
|
844
|
-
assert_ne!(val_mcp.code.0, parse_mcp.code.0);
|
|
845
|
-
assert_ne!(val_mcp.code.0, io_mcp.code.0);
|
|
846
|
-
assert_ne!(parse_mcp.code.0, io_mcp.code.0);
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
#[test]
|
|
850
|
-
fn test_format_extraction_result_with_content() {
|
|
851
|
-
let result = KreuzbergResult {
|
|
852
|
-
content: "Sample extracted text".to_string(),
|
|
853
|
-
mime_type: "text/plain".to_string(),
|
|
854
|
-
metadata: crate::Metadata::default(),
|
|
855
|
-
tables: vec![],
|
|
856
|
-
detected_languages: None,
|
|
857
|
-
chunks: None,
|
|
858
|
-
images: None,
|
|
859
|
-
pages: None,
|
|
860
|
-
};
|
|
861
|
-
|
|
862
|
-
let formatted = format_extraction_result(&result);
|
|
863
|
-
|
|
864
|
-
assert!(formatted.contains("Content (21 characters)"));
|
|
865
|
-
assert!(formatted.contains("Sample extracted text"));
|
|
866
|
-
assert!(formatted.contains("Metadata:"));
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
#[test]
|
|
870
|
-
fn test_format_extraction_result_with_tables() {
|
|
871
|
-
let result = KreuzbergResult {
|
|
872
|
-
content: "Document with tables".to_string(),
|
|
873
|
-
mime_type: "application/pdf".to_string(),
|
|
874
|
-
metadata: crate::Metadata::default(),
|
|
875
|
-
tables: vec![
|
|
876
|
-
crate::Table {
|
|
877
|
-
cells: vec![
|
|
878
|
-
vec!["Col1".to_string(), "Col2".to_string()],
|
|
879
|
-
vec!["A".to_string(), "B".to_string()],
|
|
880
|
-
],
|
|
881
|
-
page_number: 1,
|
|
882
|
-
markdown: "| Col1 | Col2 |\n|------|------|\n| A | B |".to_string(),
|
|
883
|
-
},
|
|
884
|
-
crate::Table {
|
|
885
|
-
cells: vec![
|
|
886
|
-
vec!["X".to_string(), "Y".to_string()],
|
|
887
|
-
vec!["1".to_string(), "2".to_string()],
|
|
888
|
-
],
|
|
889
|
-
page_number: 2,
|
|
890
|
-
markdown: "| X | Y |\n|---|---|\n| 1 | 2 |".to_string(),
|
|
891
|
-
},
|
|
892
|
-
],
|
|
893
|
-
detected_languages: None,
|
|
894
|
-
chunks: None,
|
|
895
|
-
images: None,
|
|
896
|
-
pages: None,
|
|
897
|
-
};
|
|
898
|
-
|
|
899
|
-
let formatted = format_extraction_result(&result);
|
|
900
|
-
|
|
901
|
-
assert!(formatted.contains("Tables (2)"));
|
|
902
|
-
assert!(formatted.contains("Table 1 (page 1)"));
|
|
903
|
-
assert!(formatted.contains("Table 2 (page 2)"));
|
|
904
|
-
assert!(formatted.contains("| Col1 | Col2 |"));
|
|
905
|
-
assert!(formatted.contains("| X | Y |"));
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
#[test]
|
|
909
|
-
fn test_format_extraction_result_empty_content() {
|
|
910
|
-
let result = KreuzbergResult {
|
|
911
|
-
content: String::new(),
|
|
912
|
-
mime_type: "text/plain".to_string(),
|
|
913
|
-
metadata: crate::Metadata::default(),
|
|
914
|
-
tables: vec![],
|
|
915
|
-
detected_languages: None,
|
|
916
|
-
chunks: None,
|
|
917
|
-
images: None,
|
|
918
|
-
pages: None,
|
|
919
|
-
};
|
|
920
|
-
|
|
921
|
-
let formatted = format_extraction_result(&result);
|
|
922
|
-
|
|
923
|
-
assert!(formatted.contains("Content (0 characters)"));
|
|
924
|
-
assert!(formatted.contains("Metadata:"));
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
#[test]
|
|
928
|
-
fn test_format_extraction_result_no_tables() {
|
|
929
|
-
let result = KreuzbergResult {
|
|
930
|
-
content: "Simple text".to_string(),
|
|
931
|
-
mime_type: "text/plain".to_string(),
|
|
932
|
-
metadata: crate::Metadata::default(),
|
|
933
|
-
tables: vec![],
|
|
934
|
-
detected_languages: None,
|
|
935
|
-
chunks: None,
|
|
936
|
-
images: None,
|
|
937
|
-
pages: None,
|
|
938
|
-
};
|
|
939
|
-
|
|
940
|
-
let formatted = format_extraction_result(&result);
|
|
941
|
-
|
|
942
|
-
assert!(formatted.contains("Simple text"));
|
|
943
|
-
assert!(!formatted.contains("Tables"));
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
#[tokio::test]
|
|
947
|
-
async fn test_extract_file_sync_with_valid_pdf() {
|
|
948
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
949
|
-
let params = ExtractFileParams {
|
|
950
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
951
|
-
mime_type: None,
|
|
952
|
-
enable_ocr: false,
|
|
953
|
-
force_ocr: false,
|
|
954
|
-
r#async: true,
|
|
955
|
-
};
|
|
956
|
-
|
|
957
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
958
|
-
|
|
959
|
-
assert!(result.is_ok());
|
|
960
|
-
let call_result = result.unwrap();
|
|
961
|
-
if let Some(content) = call_result.content.first() {
|
|
962
|
-
match &content.raw {
|
|
963
|
-
RawContent::Text(text) => {
|
|
964
|
-
assert!(!text.text.is_empty());
|
|
965
|
-
assert!(text.text.contains("Content"));
|
|
966
|
-
}
|
|
967
|
-
_ => panic!("Expected text content"),
|
|
968
|
-
}
|
|
969
|
-
} else {
|
|
970
|
-
panic!("Expected content in result");
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
#[tokio::test]
|
|
975
|
-
async fn test_extract_file_async_with_valid_pdf() {
|
|
976
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
977
|
-
let params = ExtractFileParams {
|
|
978
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
979
|
-
mime_type: None,
|
|
980
|
-
enable_ocr: false,
|
|
981
|
-
force_ocr: false,
|
|
982
|
-
r#async: true,
|
|
983
|
-
};
|
|
984
|
-
|
|
985
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
986
|
-
|
|
987
|
-
assert!(result.is_ok());
|
|
988
|
-
let call_result = result.unwrap();
|
|
989
|
-
if let Some(content) = call_result.content.first() {
|
|
990
|
-
match &content.raw {
|
|
991
|
-
RawContent::Text(text) => {
|
|
992
|
-
assert!(!text.text.is_empty());
|
|
993
|
-
}
|
|
994
|
-
_ => panic!("Expected text content"),
|
|
995
|
-
}
|
|
996
|
-
} else {
|
|
997
|
-
panic!("Expected content in result");
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
#[tokio::test]
|
|
1002
|
-
async fn test_extract_file_with_invalid_path() {
|
|
1003
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1004
|
-
let params = ExtractFileParams {
|
|
1005
|
-
path: "/nonexistent/file.pdf".to_string(),
|
|
1006
|
-
mime_type: None,
|
|
1007
|
-
enable_ocr: false,
|
|
1008
|
-
force_ocr: false,
|
|
1009
|
-
r#async: true,
|
|
1010
|
-
};
|
|
1011
|
-
|
|
1012
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1013
|
-
|
|
1014
|
-
assert!(result.is_err());
|
|
1015
|
-
let error = result.unwrap_err();
|
|
1016
|
-
assert!(error.code.0 == -32602 || error.code.0 == -32603);
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
#[tokio::test]
|
|
1020
|
-
async fn test_extract_file_with_mime_type_hint() {
|
|
1021
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1022
|
-
let params = ExtractFileParams {
|
|
1023
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1024
|
-
mime_type: Some("application/pdf".to_string()),
|
|
1025
|
-
enable_ocr: false,
|
|
1026
|
-
force_ocr: false,
|
|
1027
|
-
r#async: true,
|
|
1028
|
-
};
|
|
1029
|
-
|
|
1030
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1031
|
-
|
|
1032
|
-
assert!(result.is_ok());
|
|
1033
|
-
}
|
|
1034
|
-
|
|
1035
|
-
#[tokio::test]
|
|
1036
|
-
async fn test_extract_file_with_ocr_enabled() {
|
|
1037
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1038
|
-
let params = ExtractFileParams {
|
|
1039
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1040
|
-
mime_type: None,
|
|
1041
|
-
enable_ocr: true,
|
|
1042
|
-
force_ocr: false,
|
|
1043
|
-
r#async: true,
|
|
1044
|
-
};
|
|
1045
|
-
|
|
1046
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1047
|
-
|
|
1048
|
-
assert!(result.is_ok() || result.is_err());
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
#[tokio::test]
|
|
1052
|
-
async fn test_extract_file_with_force_ocr() {
|
|
1053
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1054
|
-
let params = ExtractFileParams {
|
|
1055
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1056
|
-
mime_type: None,
|
|
1057
|
-
enable_ocr: true,
|
|
1058
|
-
force_ocr: true,
|
|
1059
|
-
r#async: true,
|
|
1060
|
-
};
|
|
1061
|
-
|
|
1062
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1063
|
-
|
|
1064
|
-
assert!(result.is_ok() || result.is_err());
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
#[tokio::test]
|
|
1068
|
-
async fn test_extract_bytes_sync_with_valid_data() {
|
|
1069
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1070
|
-
|
|
1071
|
-
let text_content = b"Hello, world!";
|
|
1072
|
-
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1073
|
-
|
|
1074
|
-
let params = ExtractBytesParams {
|
|
1075
|
-
data: encoded,
|
|
1076
|
-
mime_type: Some("text/plain".to_string()),
|
|
1077
|
-
enable_ocr: false,
|
|
1078
|
-
force_ocr: false,
|
|
1079
|
-
r#async: true,
|
|
1080
|
-
};
|
|
1081
|
-
|
|
1082
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1083
|
-
|
|
1084
|
-
assert!(result.is_ok());
|
|
1085
|
-
let call_result = result.unwrap();
|
|
1086
|
-
if let Some(content) = call_result.content.first() {
|
|
1087
|
-
match &content.raw {
|
|
1088
|
-
RawContent::Text(text) => {
|
|
1089
|
-
assert!(text.text.contains("Hello, world!"));
|
|
1090
|
-
}
|
|
1091
|
-
_ => panic!("Expected text content"),
|
|
1092
|
-
}
|
|
1093
|
-
} else {
|
|
1094
|
-
panic!("Expected content in result");
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
#[tokio::test]
|
|
1099
|
-
async fn test_extract_bytes_async_with_valid_data() {
|
|
1100
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1101
|
-
|
|
1102
|
-
let text_content = b"Async extraction test";
|
|
1103
|
-
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1104
|
-
|
|
1105
|
-
let params = ExtractBytesParams {
|
|
1106
|
-
data: encoded,
|
|
1107
|
-
mime_type: Some("text/plain".to_string()),
|
|
1108
|
-
enable_ocr: false,
|
|
1109
|
-
force_ocr: false,
|
|
1110
|
-
r#async: true,
|
|
1111
|
-
};
|
|
1112
|
-
|
|
1113
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1114
|
-
|
|
1115
|
-
assert!(result.is_ok());
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
#[tokio::test]
|
|
1119
|
-
async fn test_extract_bytes_with_invalid_base64() {
|
|
1120
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1121
|
-
|
|
1122
|
-
let params = ExtractBytesParams {
|
|
1123
|
-
data: "not-valid-base64!!!".to_string(),
|
|
1124
|
-
mime_type: None,
|
|
1125
|
-
enable_ocr: false,
|
|
1126
|
-
force_ocr: false,
|
|
1127
|
-
r#async: true,
|
|
1128
|
-
};
|
|
1129
|
-
|
|
1130
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1131
|
-
|
|
1132
|
-
assert!(result.is_err());
|
|
1133
|
-
let error = result.unwrap_err();
|
|
1134
|
-
assert_eq!(error.code.0, -32602);
|
|
1135
|
-
assert!(error.message.contains("Invalid base64"));
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
#[tokio::test]
|
|
1139
|
-
async fn test_extract_bytes_without_mime_type() {
|
|
1140
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1141
|
-
|
|
1142
|
-
let text_content = b"Test content";
|
|
1143
|
-
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1144
|
-
|
|
1145
|
-
let params = ExtractBytesParams {
|
|
1146
|
-
data: encoded,
|
|
1147
|
-
mime_type: None,
|
|
1148
|
-
enable_ocr: false,
|
|
1149
|
-
force_ocr: false,
|
|
1150
|
-
r#async: true,
|
|
1151
|
-
};
|
|
1152
|
-
|
|
1153
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1154
|
-
|
|
1155
|
-
assert!(result.is_ok() || result.is_err());
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
#[tokio::test]
|
|
1159
|
-
async fn test_extract_bytes_with_ocr_enabled() {
|
|
1160
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1161
|
-
|
|
1162
|
-
let text_content = b"OCR test content";
|
|
1163
|
-
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1164
|
-
|
|
1165
|
-
let params = ExtractBytesParams {
|
|
1166
|
-
data: encoded,
|
|
1167
|
-
mime_type: Some("text/plain".to_string()),
|
|
1168
|
-
enable_ocr: true,
|
|
1169
|
-
force_ocr: false,
|
|
1170
|
-
r#async: true,
|
|
1171
|
-
};
|
|
1172
|
-
|
|
1173
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1174
|
-
|
|
1175
|
-
assert!(result.is_ok() || result.is_err());
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
#[tokio::test]
|
|
1179
|
-
async fn test_batch_extract_files_sync_with_valid_files() {
|
|
1180
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1181
|
-
let params = BatchExtractFilesParams {
|
|
1182
|
-
paths: vec![get_test_path("pdfs_with_tables/tiny.pdf").to_string()],
|
|
1183
|
-
enable_ocr: false,
|
|
1184
|
-
force_ocr: false,
|
|
1185
|
-
r#async: true,
|
|
1186
|
-
};
|
|
1187
|
-
|
|
1188
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1189
|
-
|
|
1190
|
-
assert!(result.is_ok());
|
|
1191
|
-
let call_result = result.unwrap();
|
|
1192
|
-
if let Some(content) = call_result.content.first() {
|
|
1193
|
-
match &content.raw {
|
|
1194
|
-
RawContent::Text(text) => {
|
|
1195
|
-
assert!(text.text.contains("Document 1"));
|
|
1196
|
-
assert!(text.text.contains("tiny.pdf"));
|
|
1197
|
-
}
|
|
1198
|
-
_ => panic!("Expected text content"),
|
|
1199
|
-
}
|
|
1200
|
-
} else {
|
|
1201
|
-
panic!("Expected content in result");
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
|
|
1205
|
-
#[tokio::test]
|
|
1206
|
-
async fn test_batch_extract_files_async_with_multiple_files() {
|
|
1207
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1208
|
-
let params = BatchExtractFilesParams {
|
|
1209
|
-
paths: vec![
|
|
1210
|
-
get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1211
|
-
get_test_path("pdfs_with_tables/medium.pdf").to_string(),
|
|
1212
|
-
],
|
|
1213
|
-
enable_ocr: false,
|
|
1214
|
-
force_ocr: false,
|
|
1215
|
-
r#async: true,
|
|
1216
|
-
};
|
|
1217
|
-
|
|
1218
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1219
|
-
|
|
1220
|
-
assert!(result.is_ok());
|
|
1221
|
-
let call_result = result.unwrap();
|
|
1222
|
-
if let Some(content) = call_result.content.first() {
|
|
1223
|
-
match &content.raw {
|
|
1224
|
-
RawContent::Text(text) => {
|
|
1225
|
-
assert!(text.text.contains("Document 1"));
|
|
1226
|
-
assert!(text.text.contains("Document 2"));
|
|
1227
|
-
}
|
|
1228
|
-
_ => panic!("Expected text content"),
|
|
1229
|
-
}
|
|
1230
|
-
} else {
|
|
1231
|
-
panic!("Expected content in result");
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
#[tokio::test]
|
|
1236
|
-
async fn test_batch_extract_files_with_empty_list() {
|
|
1237
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1238
|
-
let params = BatchExtractFilesParams {
|
|
1239
|
-
paths: vec![],
|
|
1240
|
-
enable_ocr: false,
|
|
1241
|
-
force_ocr: false,
|
|
1242
|
-
r#async: true,
|
|
1243
|
-
};
|
|
1244
|
-
|
|
1245
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1246
|
-
|
|
1247
|
-
assert!(result.is_ok());
|
|
1248
|
-
let call_result = result.unwrap();
|
|
1249
|
-
if let Some(content) = call_result.content.first() {
|
|
1250
|
-
match &content.raw {
|
|
1251
|
-
RawContent::Text(text) => {
|
|
1252
|
-
assert!(text.text.is_empty() || text.text.trim().is_empty());
|
|
1253
|
-
}
|
|
1254
|
-
_ => panic!("Expected text content"),
|
|
1255
|
-
}
|
|
1256
|
-
} else {
|
|
1257
|
-
panic!("Expected content in result");
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
#[tokio::test]
|
|
1262
|
-
async fn test_batch_extract_files_with_invalid_file() {
|
|
1263
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1264
|
-
let params = BatchExtractFilesParams {
|
|
1265
|
-
paths: vec!["/nonexistent/file.pdf".to_string()],
|
|
1266
|
-
enable_ocr: false,
|
|
1267
|
-
force_ocr: false,
|
|
1268
|
-
r#async: true,
|
|
1269
|
-
};
|
|
1270
|
-
|
|
1271
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1272
|
-
|
|
1273
|
-
assert!(result.is_ok() || result.is_err());
|
|
1274
|
-
}
|
|
1275
|
-
|
|
1276
|
-
#[tokio::test]
|
|
1277
|
-
async fn test_detect_mime_type_with_valid_file() {
|
|
1278
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1279
|
-
let params = DetectMimeTypeParams {
|
|
1280
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1281
|
-
use_content: true,
|
|
1282
|
-
};
|
|
1283
|
-
|
|
1284
|
-
let result = server.detect_mime_type(Parameters(params));
|
|
1285
|
-
|
|
1286
|
-
assert!(result.is_ok());
|
|
1287
|
-
let call_result = result.unwrap();
|
|
1288
|
-
if let Some(content) = call_result.content.first() {
|
|
1289
|
-
match &content.raw {
|
|
1290
|
-
RawContent::Text(text) => {
|
|
1291
|
-
assert!(text.text.contains("application/pdf") || text.text.contains("pdf"));
|
|
1292
|
-
}
|
|
1293
|
-
_ => panic!("Expected text content"),
|
|
1294
|
-
}
|
|
1295
|
-
} else {
|
|
1296
|
-
panic!("Expected content in result");
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
|
|
1300
|
-
#[tokio::test]
|
|
1301
|
-
async fn test_detect_mime_type_without_content_detection() {
|
|
1302
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1303
|
-
let params = DetectMimeTypeParams {
|
|
1304
|
-
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1305
|
-
use_content: false,
|
|
1306
|
-
};
|
|
1307
|
-
|
|
1308
|
-
let result = server.detect_mime_type(Parameters(params));
|
|
1309
|
-
|
|
1310
|
-
assert!(result.is_ok());
|
|
1311
|
-
}
|
|
1312
|
-
|
|
1313
|
-
#[tokio::test]
|
|
1314
|
-
async fn test_detect_mime_type_with_invalid_file() {
|
|
1315
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1316
|
-
let params = DetectMimeTypeParams {
|
|
1317
|
-
path: "/nonexistent/file.pdf".to_string(),
|
|
1318
|
-
use_content: true,
|
|
1319
|
-
};
|
|
1320
|
-
|
|
1321
|
-
let result = server.detect_mime_type(Parameters(params));
|
|
1322
|
-
|
|
1323
|
-
assert!(result.is_err());
|
|
1324
|
-
let error = result.unwrap_err();
|
|
1325
|
-
assert!(error.code.0 == -32602 || error.code.0 == -32603);
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1328
|
-
#[tokio::test]
|
|
1329
|
-
async fn test_cache_stats_returns_statistics() {
|
|
1330
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1331
|
-
|
|
1332
|
-
let result = server.cache_stats(Parameters(()));
|
|
1333
|
-
|
|
1334
|
-
assert!(result.is_ok());
|
|
1335
|
-
let call_result = result.unwrap();
|
|
1336
|
-
if let Some(content) = call_result.content.first() {
|
|
1337
|
-
match &content.raw {
|
|
1338
|
-
RawContent::Text(text) => {
|
|
1339
|
-
assert!(text.text.contains("Cache Statistics"));
|
|
1340
|
-
assert!(text.text.contains("Directory:"));
|
|
1341
|
-
assert!(text.text.contains("Total files:"));
|
|
1342
|
-
assert!(text.text.contains("Total size:"));
|
|
1343
|
-
assert!(text.text.contains("Available space:"));
|
|
1344
|
-
}
|
|
1345
|
-
_ => panic!("Expected text content"),
|
|
1346
|
-
}
|
|
1347
|
-
} else {
|
|
1348
|
-
panic!("Expected content in result");
|
|
1349
|
-
}
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
#[tokio::test]
|
|
1353
|
-
async fn test_cache_clear_returns_result() {
|
|
1354
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1355
|
-
|
|
1356
|
-
let result = server.cache_clear(Parameters(()));
|
|
1357
|
-
|
|
1358
|
-
assert!(result.is_ok());
|
|
1359
|
-
let call_result = result.unwrap();
|
|
1360
|
-
if let Some(content) = call_result.content.first() {
|
|
1361
|
-
match &content.raw {
|
|
1362
|
-
RawContent::Text(text) => {
|
|
1363
|
-
assert!(text.text.contains("Cache cleared"));
|
|
1364
|
-
assert!(text.text.contains("Directory:"));
|
|
1365
|
-
assert!(text.text.contains("Removed files:"));
|
|
1366
|
-
assert!(text.text.contains("Freed space:"));
|
|
1367
|
-
}
|
|
1368
|
-
_ => panic!("Expected text content"),
|
|
1369
|
-
}
|
|
1370
|
-
} else {
|
|
1371
|
-
panic!("Expected content in result");
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
#[test]
|
|
1376
|
-
fn test_new_creates_server_with_default_config() {
|
|
1377
|
-
let server = KreuzbergMcp::new();
|
|
1378
|
-
assert!(server.is_ok());
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
#[test]
|
|
1382
|
-
fn test_default_creates_server_without_panic() {
|
|
1383
|
-
let server = KreuzbergMcp::default();
|
|
1384
|
-
let info = server.get_info();
|
|
1385
|
-
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1386
|
-
}
|
|
1387
|
-
|
|
1388
|
-
#[test]
|
|
1389
|
-
fn test_server_info_has_correct_fields() {
|
|
1390
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1391
|
-
let info = server.get_info();
|
|
1392
|
-
|
|
1393
|
-
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1394
|
-
assert_eq!(
|
|
1395
|
-
info.server_info.title,
|
|
1396
|
-
Some("Kreuzberg Document Intelligence MCP Server".to_string())
|
|
1397
|
-
);
|
|
1398
|
-
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
1399
|
-
assert_eq!(
|
|
1400
|
-
info.server_info.website_url,
|
|
1401
|
-
Some("https://goldziher.github.io/kreuzberg/".to_string())
|
|
1402
|
-
);
|
|
1403
|
-
assert!(info.instructions.is_some());
|
|
1404
|
-
assert!(info.capabilities.tools.is_some());
|
|
1405
|
-
}
|
|
1406
|
-
|
|
1407
|
-
#[test]
|
|
1408
|
-
fn test_build_config_preserves_default_config_settings() {
|
|
1409
|
-
let default_config = ExtractionConfig {
|
|
1410
|
-
use_cache: false,
|
|
1411
|
-
..Default::default()
|
|
1412
|
-
};
|
|
1413
|
-
|
|
1414
|
-
let config = build_config(&default_config, false, false);
|
|
1415
|
-
|
|
1416
|
-
assert!(!config.use_cache);
|
|
1417
|
-
}
|
|
1418
|
-
|
|
1419
|
-
#[test]
|
|
1420
|
-
fn test_build_config_ocr_disabled_by_default() {
|
|
1421
|
-
let default_config = ExtractionConfig::default();
|
|
1422
|
-
|
|
1423
|
-
let config = build_config(&default_config, false, false);
|
|
1424
|
-
|
|
1425
|
-
assert!(config.ocr.is_none());
|
|
1426
|
-
assert!(!config.force_ocr);
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
#[test]
|
|
1430
|
-
fn test_build_config_ocr_enabled_creates_tesseract_config() {
|
|
1431
|
-
let default_config = ExtractionConfig::default();
|
|
1432
|
-
|
|
1433
|
-
let config = build_config(&default_config, true, false);
|
|
1434
|
-
|
|
1435
|
-
assert!(config.ocr.is_some());
|
|
1436
|
-
let ocr_config = config.ocr.unwrap();
|
|
1437
|
-
assert_eq!(ocr_config.backend, "tesseract");
|
|
1438
|
-
assert_eq!(ocr_config.language, "eng");
|
|
1439
|
-
}
|
|
1440
|
-
|
|
1441
|
-
#[test]
|
|
1442
|
-
fn test_extract_file_params_defaults() {
|
|
1443
|
-
let json = r#"{"path": "/test.pdf"}"#;
|
|
1444
|
-
let params: ExtractFileParams = serde_json::from_str(json).unwrap();
|
|
1445
|
-
|
|
1446
|
-
assert_eq!(params.path, "/test.pdf");
|
|
1447
|
-
assert_eq!(params.mime_type, None);
|
|
1448
|
-
assert!(!params.enable_ocr);
|
|
1449
|
-
assert!(!params.force_ocr);
|
|
1450
|
-
assert!(!params.r#async);
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
#[test]
|
|
1454
|
-
fn test_extract_bytes_params_defaults() {
|
|
1455
|
-
let json = r#"{"data": "SGVsbG8="}"#;
|
|
1456
|
-
let params: ExtractBytesParams = serde_json::from_str(json).unwrap();
|
|
1457
|
-
|
|
1458
|
-
assert_eq!(params.data, "SGVsbG8=");
|
|
1459
|
-
assert_eq!(params.mime_type, None);
|
|
1460
|
-
assert!(!params.enable_ocr);
|
|
1461
|
-
assert!(!params.force_ocr);
|
|
1462
|
-
assert!(!params.r#async);
|
|
1463
|
-
}
|
|
1464
|
-
|
|
1465
|
-
#[test]
|
|
1466
|
-
fn test_batch_extract_files_params_defaults() {
|
|
1467
|
-
let json = r#"{"paths": ["/a.pdf", "/b.pdf"]}"#;
|
|
1468
|
-
let params: BatchExtractFilesParams = serde_json::from_str(json).unwrap();
|
|
1469
|
-
|
|
1470
|
-
assert_eq!(params.paths.len(), 2);
|
|
1471
|
-
assert!(!params.enable_ocr);
|
|
1472
|
-
assert!(!params.force_ocr);
|
|
1473
|
-
assert!(!params.r#async);
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
#[test]
|
|
1477
|
-
fn test_detect_mime_type_params_defaults() {
|
|
1478
|
-
let json = r#"{"path": "/test.pdf"}"#;
|
|
1479
|
-
let params: DetectMimeTypeParams = serde_json::from_str(json).unwrap();
|
|
1480
|
-
|
|
1481
|
-
assert_eq!(params.path, "/test.pdf");
|
|
1482
|
-
assert!(params.use_content);
|
|
1483
|
-
}
|
|
1484
|
-
|
|
1485
|
-
#[test]
|
|
1486
|
-
fn test_detect_mime_type_params_use_content_false() {
|
|
1487
|
-
let json = r#"{"path": "/test.pdf", "use_content": false}"#;
|
|
1488
|
-
let params: DetectMimeTypeParams = serde_json::from_str(json).unwrap();
|
|
1489
|
-
|
|
1490
|
-
assert!(!params.use_content);
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1493
|
-
#[test]
|
|
1494
|
-
fn test_mcp_server_info_protocol_version() {
|
|
1495
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1496
|
-
let info = server.get_info();
|
|
1497
|
-
|
|
1498
|
-
assert_eq!(info.protocol_version, ProtocolVersion::default());
|
|
1499
|
-
}
|
|
1500
|
-
|
|
1501
|
-
#[test]
|
|
1502
|
-
fn test_mcp_server_info_has_all_required_fields() {
|
|
1503
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1504
|
-
let info = server.get_info();
|
|
1505
|
-
|
|
1506
|
-
assert!(!info.server_info.name.is_empty());
|
|
1507
|
-
assert!(!info.server_info.version.is_empty());
|
|
1508
|
-
|
|
1509
|
-
assert!(info.server_info.title.is_some());
|
|
1510
|
-
assert!(info.server_info.website_url.is_some());
|
|
1511
|
-
assert!(info.instructions.is_some());
|
|
1512
|
-
}
|
|
1513
|
-
|
|
1514
|
-
#[test]
|
|
1515
|
-
fn test_mcp_server_capabilities_declares_tools() {
|
|
1516
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1517
|
-
let info = server.get_info();
|
|
1518
|
-
|
|
1519
|
-
assert!(info.capabilities.tools.is_some());
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
|
-
#[test]
|
|
1523
|
-
fn test_mcp_server_name_follows_convention() {
|
|
1524
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1525
|
-
let info = server.get_info();
|
|
1526
|
-
|
|
1527
|
-
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1528
|
-
assert!(!info.server_info.name.contains('_'));
|
|
1529
|
-
assert!(!info.server_info.name.contains(' '));
|
|
1530
|
-
}
|
|
1531
|
-
|
|
1532
|
-
#[test]
|
|
1533
|
-
fn test_mcp_version_matches_cargo_version() {
|
|
1534
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1535
|
-
let info = server.get_info();
|
|
1536
|
-
|
|
1537
|
-
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
1538
|
-
}
|
|
1539
|
-
|
|
1540
|
-
#[test]
|
|
1541
|
-
fn test_mcp_instructions_are_helpful() {
|
|
1542
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1543
|
-
let info = server.get_info();
|
|
1544
|
-
|
|
1545
|
-
let instructions = info.instructions.expect("Instructions should be present");
|
|
1546
|
-
|
|
1547
|
-
assert!(instructions.contains("extract") || instructions.contains("Extract"));
|
|
1548
|
-
assert!(instructions.contains("OCR") || instructions.contains("ocr"));
|
|
1549
|
-
assert!(instructions.contains("document"));
|
|
1550
|
-
}
|
|
1551
|
-
|
|
1552
|
-
#[tokio::test]
|
|
1553
|
-
async fn test_all_tools_are_registered() {
|
|
1554
|
-
let router = KreuzbergMcp::tool_router();
|
|
1555
|
-
|
|
1556
|
-
let expected_tools = vec![
|
|
1557
|
-
"extract_file",
|
|
1558
|
-
"extract_bytes",
|
|
1559
|
-
"batch_extract_files",
|
|
1560
|
-
"detect_mime_type",
|
|
1561
|
-
"cache_stats",
|
|
1562
|
-
"cache_clear",
|
|
1563
|
-
];
|
|
1564
|
-
|
|
1565
|
-
for tool_name in expected_tools {
|
|
1566
|
-
assert!(router.has_route(tool_name), "Tool '{}' should be registered", tool_name);
|
|
1567
|
-
}
|
|
1568
|
-
}
|
|
1569
|
-
|
|
1570
|
-
#[tokio::test]
|
|
1571
|
-
async fn test_tool_count_is_correct() {
|
|
1572
|
-
let router = KreuzbergMcp::tool_router();
|
|
1573
|
-
let tools = router.list_all();
|
|
1574
|
-
|
|
1575
|
-
assert_eq!(tools.len(), 6, "Expected 6 tools, found {}", tools.len());
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
#[tokio::test]
|
|
1579
|
-
async fn test_tools_have_descriptions() {
|
|
1580
|
-
let router = KreuzbergMcp::tool_router();
|
|
1581
|
-
let tools = router.list_all();
|
|
1582
|
-
|
|
1583
|
-
for tool in tools {
|
|
1584
|
-
assert!(
|
|
1585
|
-
tool.description.is_some(),
|
|
1586
|
-
"Tool '{}' should have a description",
|
|
1587
|
-
tool.name
|
|
1588
|
-
);
|
|
1589
|
-
let desc = tool.description.as_ref().unwrap();
|
|
1590
|
-
assert!(!desc.is_empty(), "Tool '{}' description should not be empty", tool.name);
|
|
1591
|
-
}
|
|
1592
|
-
}
|
|
1593
|
-
|
|
1594
|
-
#[tokio::test]
|
|
1595
|
-
async fn test_extract_file_tool_has_correct_schema() {
|
|
1596
|
-
let router = KreuzbergMcp::tool_router();
|
|
1597
|
-
let tools = router.list_all();
|
|
1598
|
-
|
|
1599
|
-
let extract_file_tool = tools
|
|
1600
|
-
.iter()
|
|
1601
|
-
.find(|t| t.name == "extract_file")
|
|
1602
|
-
.expect("extract_file tool should exist");
|
|
1603
|
-
|
|
1604
|
-
assert!(extract_file_tool.description.is_some());
|
|
1605
|
-
|
|
1606
|
-
assert!(!extract_file_tool.input_schema.is_empty());
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
|
-
#[tokio::test]
|
|
1610
|
-
async fn test_all_tools_have_input_schemas() {
|
|
1611
|
-
let router = KreuzbergMcp::tool_router();
|
|
1612
|
-
let tools = router.list_all();
|
|
1613
|
-
|
|
1614
|
-
for tool in tools {
|
|
1615
|
-
assert!(
|
|
1616
|
-
!tool.input_schema.is_empty(),
|
|
1617
|
-
"Tool '{}' should have an input schema with fields",
|
|
1618
|
-
tool.name
|
|
1619
|
-
);
|
|
1620
|
-
}
|
|
1621
|
-
}
|
|
1622
|
-
|
|
1623
|
-
#[test]
|
|
1624
|
-
fn test_server_creation_with_custom_config() {
|
|
1625
|
-
let custom_config = ExtractionConfig {
|
|
1626
|
-
force_ocr: true,
|
|
1627
|
-
use_cache: false,
|
|
1628
|
-
ocr: Some(crate::OcrConfig {
|
|
1629
|
-
backend: "tesseract".to_string(),
|
|
1630
|
-
language: "spa".to_string(),
|
|
1631
|
-
tesseract_config: None,
|
|
1632
|
-
}),
|
|
1633
|
-
..Default::default()
|
|
1634
|
-
};
|
|
1635
|
-
|
|
1636
|
-
let server = KreuzbergMcp::with_config(custom_config.clone());
|
|
1637
|
-
|
|
1638
|
-
assert_eq!(server.default_config.force_ocr, custom_config.force_ocr);
|
|
1639
|
-
assert_eq!(server.default_config.use_cache, custom_config.use_cache);
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
#[test]
|
|
1643
|
-
fn test_server_clone_preserves_config() {
|
|
1644
|
-
let custom_config = ExtractionConfig {
|
|
1645
|
-
force_ocr: true,
|
|
1646
|
-
..Default::default()
|
|
1647
|
-
};
|
|
1648
|
-
|
|
1649
|
-
let server1 = KreuzbergMcp::with_config(custom_config);
|
|
1650
|
-
let server2 = server1.clone();
|
|
1651
|
-
|
|
1652
|
-
assert_eq!(server1.default_config.force_ocr, server2.default_config.force_ocr);
|
|
1653
|
-
}
|
|
1654
|
-
|
|
1655
|
-
#[tokio::test]
|
|
1656
|
-
async fn test_extract_bytes_with_empty_data() {
|
|
1657
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1658
|
-
|
|
1659
|
-
let params = ExtractBytesParams {
|
|
1660
|
-
data: String::new(),
|
|
1661
|
-
mime_type: Some("text/plain".to_string()),
|
|
1662
|
-
enable_ocr: false,
|
|
1663
|
-
force_ocr: false,
|
|
1664
|
-
r#async: true,
|
|
1665
|
-
};
|
|
1666
|
-
|
|
1667
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1668
|
-
|
|
1669
|
-
assert!(result.is_ok() || result.is_err());
|
|
1670
|
-
}
|
|
1671
|
-
|
|
1672
|
-
#[tokio::test]
|
|
1673
|
-
async fn test_extract_bytes_with_valid_pdf_bytes() {
|
|
1674
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1675
|
-
|
|
1676
|
-
let pdf_path = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1677
|
-
|
|
1678
|
-
if std::path::Path::new(&pdf_path).exists() {
|
|
1679
|
-
let pdf_bytes = std::fs::read(&pdf_path).unwrap();
|
|
1680
|
-
let encoded = BASE64_STANDARD.encode(&pdf_bytes);
|
|
1681
|
-
|
|
1682
|
-
let params = ExtractBytesParams {
|
|
1683
|
-
data: encoded,
|
|
1684
|
-
mime_type: Some("application/pdf".to_string()),
|
|
1685
|
-
enable_ocr: false,
|
|
1686
|
-
force_ocr: false,
|
|
1687
|
-
r#async: true,
|
|
1688
|
-
};
|
|
1689
|
-
|
|
1690
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1691
|
-
|
|
1692
|
-
assert!(result.is_ok(), "PDF bytes extraction should succeed");
|
|
1693
|
-
let call_result = result.unwrap();
|
|
1694
|
-
assert!(!call_result.content.is_empty());
|
|
1695
|
-
}
|
|
1696
|
-
}
|
|
1697
|
-
|
|
1698
|
-
#[tokio::test]
|
|
1699
|
-
async fn test_extract_bytes_mime_type_auto_detection() {
|
|
1700
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1701
|
-
|
|
1702
|
-
let text_content = b"Plain text content for testing";
|
|
1703
|
-
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1704
|
-
|
|
1705
|
-
let params = ExtractBytesParams {
|
|
1706
|
-
data: encoded,
|
|
1707
|
-
mime_type: None,
|
|
1708
|
-
enable_ocr: false,
|
|
1709
|
-
force_ocr: false,
|
|
1710
|
-
r#async: true,
|
|
1711
|
-
};
|
|
1712
|
-
|
|
1713
|
-
let result = server.extract_bytes(Parameters(params)).await;
|
|
1714
|
-
|
|
1715
|
-
assert!(result.is_ok() || result.is_err());
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
#[tokio::test]
|
|
1719
|
-
async fn test_batch_extract_preserves_file_order() {
|
|
1720
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1721
|
-
|
|
1722
|
-
let file1 = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1723
|
-
let file2 = get_test_path("pdfs_with_tables/medium.pdf");
|
|
1724
|
-
|
|
1725
|
-
if std::path::Path::new(&file1).exists() && std::path::Path::new(&file2).exists() {
|
|
1726
|
-
let params = BatchExtractFilesParams {
|
|
1727
|
-
paths: vec![file1.to_string(), file2.to_string()],
|
|
1728
|
-
enable_ocr: false,
|
|
1729
|
-
force_ocr: false,
|
|
1730
|
-
r#async: true,
|
|
1731
|
-
};
|
|
1732
|
-
|
|
1733
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1734
|
-
|
|
1735
|
-
if let Ok(call_result) = result
|
|
1736
|
-
&& let Some(content) = call_result.content.first()
|
|
1737
|
-
&& let RawContent::Text(text) = &content.raw
|
|
1738
|
-
{
|
|
1739
|
-
assert!(text.text.contains("Document 1"));
|
|
1740
|
-
assert!(text.text.contains("Document 2"));
|
|
1741
|
-
|
|
1742
|
-
let doc1_pos = text.text.find("Document 1");
|
|
1743
|
-
let doc2_pos = text.text.find("Document 2");
|
|
1744
|
-
if let (Some(pos1), Some(pos2)) = (doc1_pos, doc2_pos) {
|
|
1745
|
-
assert!(pos1 < pos2, "Documents should be in order");
|
|
1746
|
-
}
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
}
|
|
1750
|
-
|
|
1751
|
-
#[tokio::test]
|
|
1752
|
-
async fn test_cache_clear_is_idempotent() {
|
|
1753
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1754
|
-
|
|
1755
|
-
let result1 = server.cache_clear(Parameters(()));
|
|
1756
|
-
assert!(result1.is_ok());
|
|
1757
|
-
|
|
1758
|
-
let result2 = server.cache_clear(Parameters(()));
|
|
1759
|
-
assert!(result2.is_ok());
|
|
1760
|
-
}
|
|
1761
|
-
|
|
1762
|
-
#[tokio::test]
|
|
1763
|
-
async fn test_cache_clear_returns_metrics() {
|
|
1764
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1765
|
-
|
|
1766
|
-
let result = server.cache_clear(Parameters(()));
|
|
1767
|
-
|
|
1768
|
-
assert!(result.is_ok());
|
|
1769
|
-
let call_result = result.unwrap();
|
|
1770
|
-
if let Some(content) = call_result.content.first()
|
|
1771
|
-
&& let RawContent::Text(text) = &content.raw
|
|
1772
|
-
{
|
|
1773
|
-
assert!(text.text.contains("Removed files:"));
|
|
1774
|
-
assert!(text.text.contains("Freed space:"));
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
1777
|
-
|
|
1778
|
-
#[test]
|
|
1779
|
-
fn test_error_mapping_preserves_error_context() {
|
|
1780
|
-
let validation_error = KreuzbergError::validation("invalid file path");
|
|
1781
|
-
let mcp_error = map_kreuzberg_error_to_mcp(validation_error);
|
|
1782
|
-
|
|
1783
|
-
assert!(mcp_error.message.contains("invalid file path"));
|
|
1784
|
-
}
|
|
1785
|
-
|
|
1786
|
-
#[test]
|
|
1787
|
-
fn test_io_errors_bubble_up_as_internal() {
|
|
1788
|
-
// OSError/RuntimeError must bubble up - system errors need user reports ~keep
|
|
1789
|
-
let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
|
|
1790
|
-
let kreuzberg_error = KreuzbergError::Io(io_error);
|
|
1791
|
-
let mcp_error = map_kreuzberg_error_to_mcp(kreuzberg_error);
|
|
1792
|
-
|
|
1793
|
-
assert_eq!(mcp_error.code.0, -32603);
|
|
1794
|
-
assert!(mcp_error.message.contains("System I/O error"));
|
|
1795
|
-
}
|
|
1796
|
-
|
|
1797
|
-
#[test]
|
|
1798
|
-
fn test_all_error_variants_have_mappings() {
|
|
1799
|
-
let errors = vec![
|
|
1800
|
-
KreuzbergError::validation("test"),
|
|
1801
|
-
KreuzbergError::UnsupportedFormat("test/unknown".to_string()),
|
|
1802
|
-
KreuzbergError::MissingDependency("test-dep".to_string()),
|
|
1803
|
-
KreuzbergError::parsing("test"),
|
|
1804
|
-
KreuzbergError::Io(std::io::Error::other("test")),
|
|
1805
|
-
KreuzbergError::ocr("test"),
|
|
1806
|
-
KreuzbergError::cache("test"),
|
|
1807
|
-
KreuzbergError::image_processing("test"),
|
|
1808
|
-
KreuzbergError::serialization("test"),
|
|
1809
|
-
KreuzbergError::Plugin {
|
|
1810
|
-
message: "test".to_string(),
|
|
1811
|
-
plugin_name: "test-plugin".to_string(),
|
|
1812
|
-
},
|
|
1813
|
-
KreuzbergError::LockPoisoned("test".to_string()),
|
|
1814
|
-
KreuzbergError::Other("test".to_string()),
|
|
1815
|
-
];
|
|
1816
|
-
|
|
1817
|
-
for error in errors {
|
|
1818
|
-
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
1819
|
-
|
|
1820
|
-
assert!(mcp_error.code.0 < 0, "Error code should be negative");
|
|
1821
|
-
|
|
1822
|
-
assert!(!mcp_error.message.is_empty());
|
|
1823
|
-
}
|
|
1824
|
-
}
|
|
1825
|
-
|
|
1826
|
-
#[tokio::test]
|
|
1827
|
-
async fn test_response_includes_metadata() {
|
|
1828
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1829
|
-
|
|
1830
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1831
|
-
|
|
1832
|
-
if std::path::Path::new(&test_file).exists() {
|
|
1833
|
-
let params = ExtractFileParams {
|
|
1834
|
-
path: test_file.to_string(),
|
|
1835
|
-
mime_type: None,
|
|
1836
|
-
enable_ocr: false,
|
|
1837
|
-
force_ocr: false,
|
|
1838
|
-
r#async: true,
|
|
1839
|
-
};
|
|
1840
|
-
|
|
1841
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1842
|
-
|
|
1843
|
-
assert!(result.is_ok());
|
|
1844
|
-
let call_result = result.unwrap();
|
|
1845
|
-
|
|
1846
|
-
if let Some(content) = call_result.content.first()
|
|
1847
|
-
&& let RawContent::Text(text) = &content.raw
|
|
1848
|
-
{
|
|
1849
|
-
assert!(text.text.contains("Metadata:"));
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1852
|
-
}
|
|
1853
|
-
|
|
1854
|
-
#[tokio::test]
|
|
1855
|
-
async fn test_response_includes_content_length() {
|
|
1856
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1857
|
-
|
|
1858
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1859
|
-
|
|
1860
|
-
if std::path::Path::new(&test_file).exists() {
|
|
1861
|
-
let params = ExtractFileParams {
|
|
1862
|
-
path: test_file.to_string(),
|
|
1863
|
-
mime_type: None,
|
|
1864
|
-
enable_ocr: false,
|
|
1865
|
-
force_ocr: false,
|
|
1866
|
-
r#async: true,
|
|
1867
|
-
};
|
|
1868
|
-
|
|
1869
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1870
|
-
|
|
1871
|
-
assert!(result.is_ok());
|
|
1872
|
-
let call_result = result.unwrap();
|
|
1873
|
-
|
|
1874
|
-
if let Some(content) = call_result.content.first()
|
|
1875
|
-
&& let RawContent::Text(text) = &content.raw
|
|
1876
|
-
{
|
|
1877
|
-
assert!(text.text.contains("characters"));
|
|
1878
|
-
assert!(text.text.contains("Content"));
|
|
1879
|
-
}
|
|
1880
|
-
}
|
|
1881
|
-
}
|
|
1882
|
-
|
|
1883
|
-
#[tokio::test]
|
|
1884
|
-
async fn test_server_is_thread_safe() {
|
|
1885
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1886
|
-
|
|
1887
|
-
let server1 = server.clone();
|
|
1888
|
-
let server2 = server.clone();
|
|
1889
|
-
|
|
1890
|
-
let handle1 = tokio::spawn(async move { server1.get_info() });
|
|
1891
|
-
|
|
1892
|
-
let handle2 = tokio::spawn(async move { server2.get_info() });
|
|
1893
|
-
|
|
1894
|
-
let info1 = handle1.await.unwrap();
|
|
1895
|
-
let info2 = handle2.await.unwrap();
|
|
1896
|
-
|
|
1897
|
-
assert_eq!(info1.server_info.name, info2.server_info.name);
|
|
1898
|
-
}
|
|
1899
|
-
|
|
1900
|
-
#[test]
|
|
1901
|
-
fn test_extract_file_params_serialization() {
|
|
1902
|
-
let params = ExtractFileParams {
|
|
1903
|
-
path: "/test.pdf".to_string(),
|
|
1904
|
-
mime_type: Some("application/pdf".to_string()),
|
|
1905
|
-
enable_ocr: true,
|
|
1906
|
-
force_ocr: false,
|
|
1907
|
-
r#async: true,
|
|
1908
|
-
};
|
|
1909
|
-
|
|
1910
|
-
let json = serde_json::to_string(¶ms).unwrap();
|
|
1911
|
-
let deserialized: ExtractFileParams = serde_json::from_str(&json).unwrap();
|
|
1912
|
-
|
|
1913
|
-
assert_eq!(params.path, deserialized.path);
|
|
1914
|
-
assert_eq!(params.mime_type, deserialized.mime_type);
|
|
1915
|
-
assert_eq!(params.enable_ocr, deserialized.enable_ocr);
|
|
1916
|
-
assert_eq!(params.force_ocr, deserialized.force_ocr);
|
|
1917
|
-
assert_eq!(params.r#async, deserialized.r#async);
|
|
1918
|
-
}
|
|
1919
|
-
|
|
1920
|
-
#[test]
|
|
1921
|
-
fn test_extract_bytes_params_serialization() {
|
|
1922
|
-
let params = ExtractBytesParams {
|
|
1923
|
-
data: "SGVsbG8=".to_string(),
|
|
1924
|
-
mime_type: None,
|
|
1925
|
-
enable_ocr: false,
|
|
1926
|
-
force_ocr: false,
|
|
1927
|
-
r#async: false,
|
|
1928
|
-
};
|
|
1929
|
-
|
|
1930
|
-
let json = serde_json::to_string(¶ms).unwrap();
|
|
1931
|
-
let deserialized: ExtractBytesParams = serde_json::from_str(&json).unwrap();
|
|
1932
|
-
|
|
1933
|
-
assert_eq!(params.data, deserialized.data);
|
|
1934
|
-
}
|
|
1935
|
-
|
|
1936
|
-
#[test]
|
|
1937
|
-
fn test_batch_extract_params_serialization() {
|
|
1938
|
-
let params = BatchExtractFilesParams {
|
|
1939
|
-
paths: vec!["/a.pdf".to_string(), "/b.pdf".to_string()],
|
|
1940
|
-
enable_ocr: true,
|
|
1941
|
-
force_ocr: true,
|
|
1942
|
-
r#async: true,
|
|
1943
|
-
};
|
|
1944
|
-
|
|
1945
|
-
let json = serde_json::to_string(¶ms).unwrap();
|
|
1946
|
-
let deserialized: BatchExtractFilesParams = serde_json::from_str(&json).unwrap();
|
|
1947
|
-
|
|
1948
|
-
assert_eq!(params.paths, deserialized.paths);
|
|
1949
|
-
assert_eq!(params.enable_ocr, deserialized.enable_ocr);
|
|
1950
|
-
}
|
|
1951
|
-
|
|
1952
|
-
#[test]
|
|
1953
|
-
fn test_detect_mime_type_params_serialization() {
|
|
1954
|
-
let params = DetectMimeTypeParams {
|
|
1955
|
-
path: "/test.pdf".to_string(),
|
|
1956
|
-
use_content: false,
|
|
1957
|
-
};
|
|
1958
|
-
|
|
1959
|
-
let json = serde_json::to_string(¶ms).unwrap();
|
|
1960
|
-
let deserialized: DetectMimeTypeParams = serde_json::from_str(&json).unwrap();
|
|
1961
|
-
|
|
1962
|
-
assert_eq!(params.path, deserialized.path);
|
|
1963
|
-
assert_eq!(params.use_content, deserialized.use_content);
|
|
1964
|
-
}
|
|
1965
|
-
|
|
1966
|
-
#[tokio::test]
|
|
1967
|
-
async fn test_extract_file_respects_custom_default_config() {
|
|
1968
|
-
let custom_config = ExtractionConfig {
|
|
1969
|
-
use_cache: false,
|
|
1970
|
-
..Default::default()
|
|
1971
|
-
};
|
|
1972
|
-
|
|
1973
|
-
let server = KreuzbergMcp::with_config(custom_config);
|
|
1974
|
-
|
|
1975
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1976
|
-
|
|
1977
|
-
if std::path::Path::new(&test_file).exists() {
|
|
1978
|
-
let params = ExtractFileParams {
|
|
1979
|
-
path: test_file.to_string(),
|
|
1980
|
-
mime_type: None,
|
|
1981
|
-
enable_ocr: false,
|
|
1982
|
-
force_ocr: false,
|
|
1983
|
-
r#async: true,
|
|
1984
|
-
};
|
|
1985
|
-
|
|
1986
|
-
let result = server.extract_file(Parameters(params)).await;
|
|
1987
|
-
|
|
1988
|
-
assert!(result.is_ok() || result.is_err());
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
|
|
1992
|
-
#[tokio::test]
|
|
1993
|
-
async fn test_batch_extract_with_single_file() {
|
|
1994
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1995
|
-
|
|
1996
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1997
|
-
|
|
1998
|
-
if std::path::Path::new(&test_file).exists() {
|
|
1999
|
-
let params = BatchExtractFilesParams {
|
|
2000
|
-
paths: vec![test_file.to_string()],
|
|
2001
|
-
enable_ocr: false,
|
|
2002
|
-
force_ocr: false,
|
|
2003
|
-
r#async: true,
|
|
2004
|
-
};
|
|
2005
|
-
|
|
2006
|
-
let result = server.batch_extract_files(Parameters(params)).await;
|
|
2007
|
-
|
|
2008
|
-
assert!(result.is_ok());
|
|
2009
|
-
let call_result = result.unwrap();
|
|
2010
|
-
assert!(!call_result.content.is_empty());
|
|
2011
|
-
}
|
|
2012
|
-
}
|
|
2013
|
-
|
|
2014
|
-
#[tokio::test]
|
|
2015
|
-
async fn test_detect_mime_type_with_extension_only() {
|
|
2016
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2017
|
-
|
|
2018
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
2019
|
-
|
|
2020
|
-
if std::path::Path::new(&test_file).exists() {
|
|
2021
|
-
let params = DetectMimeTypeParams {
|
|
2022
|
-
path: test_file.to_string(),
|
|
2023
|
-
use_content: false,
|
|
2024
|
-
};
|
|
2025
|
-
|
|
2026
|
-
let result = server.detect_mime_type(Parameters(params));
|
|
2027
|
-
|
|
2028
|
-
assert!(result.is_ok());
|
|
2029
|
-
let call_result = result.unwrap();
|
|
2030
|
-
if let Some(content) = call_result.content.first()
|
|
2031
|
-
&& let RawContent::Text(text) = &content.raw
|
|
2032
|
-
{
|
|
2033
|
-
assert!(text.text.contains("pdf") || text.text.contains("PDF"));
|
|
2034
|
-
}
|
|
2035
|
-
}
|
|
2036
|
-
}
|
|
2037
|
-
|
|
2038
|
-
#[tokio::test]
|
|
2039
|
-
async fn test_detect_mime_type_with_content_analysis() {
|
|
2040
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2041
|
-
|
|
2042
|
-
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
2043
|
-
|
|
2044
|
-
if std::path::Path::new(&test_file).exists() {
|
|
2045
|
-
let params = DetectMimeTypeParams {
|
|
2046
|
-
path: test_file.to_string(),
|
|
2047
|
-
use_content: true,
|
|
2048
|
-
};
|
|
2049
|
-
|
|
2050
|
-
let result = server.detect_mime_type(Parameters(params));
|
|
2051
|
-
|
|
2052
|
-
assert!(result.is_ok());
|
|
2053
|
-
}
|
|
2054
|
-
}
|
|
2055
|
-
|
|
2056
|
-
#[tokio::test]
|
|
2057
|
-
async fn test_cache_stats_returns_valid_data() {
|
|
2058
|
-
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2059
|
-
|
|
2060
|
-
let result = server.cache_stats(Parameters(()));
|
|
2061
|
-
|
|
2062
|
-
assert!(result.is_ok());
|
|
2063
|
-
let call_result = result.unwrap();
|
|
2064
|
-
if let Some(content) = call_result.content.first()
|
|
2065
|
-
&& let RawContent::Text(text) = &content.raw
|
|
2066
|
-
{
|
|
2067
|
-
assert!(text.text.contains("Cache Statistics"));
|
|
2068
|
-
assert!(text.text.contains("Directory:"));
|
|
2069
|
-
assert!(text.text.contains("Total files:"));
|
|
2070
|
-
assert!(text.text.contains("Total size:"));
|
|
2071
|
-
assert!(text.text.contains("Available space:"));
|
|
2072
|
-
assert!(text.text.contains("Oldest file age:"));
|
|
2073
|
-
assert!(text.text.contains("Newest file age:"));
|
|
2074
|
-
}
|
|
2075
|
-
}
|
|
2076
|
-
}
|
|
1
|
+
//! MCP server implementation for Kreuzberg.
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides the core MCP server that exposes document extraction
|
|
4
|
+
//! as tools for AI assistants via the Model Context Protocol.
|
|
5
|
+
|
|
6
|
+
use base64::prelude::*;
|
|
7
|
+
use rmcp::{
|
|
8
|
+
ErrorData as McpError, ServerHandler, ServiceExt,
|
|
9
|
+
handler::server::{router::tool::ToolRouter, wrapper::Parameters},
|
|
10
|
+
model::*,
|
|
11
|
+
schemars, tool, tool_handler, tool_router,
|
|
12
|
+
transport::stdio,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
#[cfg(feature = "mcp-http")]
|
|
16
|
+
use rmcp::transport::streamable_http_server::{StreamableHttpService, session::local::LocalSessionManager};
|
|
17
|
+
|
|
18
|
+
use crate::{
|
|
19
|
+
ExtractionConfig, ExtractionResult as KreuzbergResult, KreuzbergError, batch_extract_file, batch_extract_file_sync,
|
|
20
|
+
cache, detect_mime_type, extract_bytes, extract_bytes_sync, extract_file, extract_file_sync,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/// Request parameters for file extraction.
|
|
24
|
+
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
25
|
+
pub struct ExtractFileParams {
|
|
26
|
+
/// Path to the file to extract
|
|
27
|
+
pub path: String,
|
|
28
|
+
/// Optional MIME type hint (auto-detected if not provided)
|
|
29
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
30
|
+
pub mime_type: Option<String>,
|
|
31
|
+
/// Enable OCR for scanned documents
|
|
32
|
+
#[serde(default)]
|
|
33
|
+
pub enable_ocr: bool,
|
|
34
|
+
/// Force OCR even if text extraction succeeds
|
|
35
|
+
#[serde(default)]
|
|
36
|
+
pub force_ocr: bool,
|
|
37
|
+
/// Use async extraction (default: false for sync)
|
|
38
|
+
#[serde(default)]
|
|
39
|
+
pub r#async: bool,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/// Request parameters for bytes extraction.
|
|
43
|
+
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
44
|
+
pub struct ExtractBytesParams {
|
|
45
|
+
/// Base64-encoded file content
|
|
46
|
+
pub data: String,
|
|
47
|
+
/// Optional MIME type hint (auto-detected if not provided)
|
|
48
|
+
#[serde(skip_serializing_if = "Option::is_none")]
|
|
49
|
+
pub mime_type: Option<String>,
|
|
50
|
+
/// Enable OCR for scanned documents
|
|
51
|
+
#[serde(default)]
|
|
52
|
+
pub enable_ocr: bool,
|
|
53
|
+
/// Force OCR even if text extraction succeeds
|
|
54
|
+
#[serde(default)]
|
|
55
|
+
pub force_ocr: bool,
|
|
56
|
+
/// Use async extraction (default: false for sync)
|
|
57
|
+
#[serde(default)]
|
|
58
|
+
pub r#async: bool,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Request parameters for batch file extraction.
|
|
62
|
+
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
63
|
+
pub struct BatchExtractFilesParams {
|
|
64
|
+
/// Paths to files to extract
|
|
65
|
+
pub paths: Vec<String>,
|
|
66
|
+
/// Enable OCR for scanned documents
|
|
67
|
+
#[serde(default)]
|
|
68
|
+
pub enable_ocr: bool,
|
|
69
|
+
/// Force OCR even if text extraction succeeds
|
|
70
|
+
#[serde(default)]
|
|
71
|
+
pub force_ocr: bool,
|
|
72
|
+
/// Use async extraction (default: false for sync)
|
|
73
|
+
#[serde(default)]
|
|
74
|
+
pub r#async: bool,
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/// Request parameters for MIME type detection.
|
|
78
|
+
#[derive(Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
|
|
79
|
+
pub struct DetectMimeTypeParams {
|
|
80
|
+
/// Path to the file
|
|
81
|
+
pub path: String,
|
|
82
|
+
/// Use content-based detection (default: true)
|
|
83
|
+
#[serde(default = "default_use_content")]
|
|
84
|
+
pub use_content: bool,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
fn default_use_content() -> bool {
|
|
88
|
+
true
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Map Kreuzberg errors to MCP error responses with appropriate error codes.
|
|
92
|
+
///
|
|
93
|
+
/// This function ensures different error types are properly differentiated in MCP responses:
|
|
94
|
+
/// - `Validation` errors → `INVALID_PARAMS` (-32602)
|
|
95
|
+
/// - `UnsupportedFormat` errors → `INVALID_PARAMS` (-32602)
|
|
96
|
+
/// - `Parsing` errors → `PARSE_ERROR` (-32700)
|
|
97
|
+
/// - `Io` errors → `INTERNAL_ERROR` (-32603) with context preserved
|
|
98
|
+
/// - All other errors → `INTERNAL_ERROR` (-32603)
|
|
99
|
+
///
|
|
100
|
+
/// The error message and source chain are preserved to aid debugging.
|
|
101
|
+
#[doc(hidden)]
|
|
102
|
+
pub fn map_kreuzberg_error_to_mcp(error: KreuzbergError) -> McpError {
|
|
103
|
+
match error {
|
|
104
|
+
KreuzbergError::Validation { message, source } => {
|
|
105
|
+
let mut error_message = format!("Validation error: {}", message);
|
|
106
|
+
if let Some(src) = source {
|
|
107
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
108
|
+
}
|
|
109
|
+
McpError::invalid_params(error_message, None)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
KreuzbergError::UnsupportedFormat(mime_type) => {
|
|
113
|
+
McpError::invalid_params(format!("Unsupported format: {}", mime_type), None)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
KreuzbergError::MissingDependency(dep) => McpError::invalid_params(
|
|
117
|
+
format!(
|
|
118
|
+
"Missing required dependency: {}. Please install it to use this feature.",
|
|
119
|
+
dep
|
|
120
|
+
),
|
|
121
|
+
None,
|
|
122
|
+
),
|
|
123
|
+
|
|
124
|
+
KreuzbergError::Parsing { message, source } => {
|
|
125
|
+
let mut error_message = format!("Parsing error: {}", message);
|
|
126
|
+
if let Some(src) = source {
|
|
127
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
128
|
+
}
|
|
129
|
+
McpError::parse_error(error_message, None)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// OSError/RuntimeError must bubble up - system errors need user reports ~keep
|
|
133
|
+
KreuzbergError::Io(io_err) => McpError::internal_error(format!("System I/O error: {}", io_err), None),
|
|
134
|
+
|
|
135
|
+
KreuzbergError::Ocr { message, source } => {
|
|
136
|
+
let mut error_message = format!("OCR processing error: {}", message);
|
|
137
|
+
if let Some(src) = source {
|
|
138
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
139
|
+
}
|
|
140
|
+
McpError::internal_error(error_message, None)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
KreuzbergError::Cache { message, source } => {
|
|
144
|
+
let mut error_message = format!("Cache error: {}", message);
|
|
145
|
+
if let Some(src) = source {
|
|
146
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
147
|
+
}
|
|
148
|
+
McpError::internal_error(error_message, None)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
KreuzbergError::ImageProcessing { message, source } => {
|
|
152
|
+
let mut error_message = format!("Image processing error: {}", message);
|
|
153
|
+
if let Some(src) = source {
|
|
154
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
155
|
+
}
|
|
156
|
+
McpError::internal_error(error_message, None)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
KreuzbergError::Serialization { message, source } => {
|
|
160
|
+
let mut error_message = format!("Serialization error: {}", message);
|
|
161
|
+
if let Some(src) = source {
|
|
162
|
+
error_message.push_str(&format!(" (caused by: {})", src));
|
|
163
|
+
}
|
|
164
|
+
McpError::internal_error(error_message, None)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
KreuzbergError::Plugin { message, plugin_name } => {
|
|
168
|
+
McpError::internal_error(format!("Plugin '{}' error: {}", plugin_name, message), None)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
KreuzbergError::LockPoisoned(msg) => McpError::internal_error(format!("Internal lock poisoned: {}", msg), None),
|
|
172
|
+
|
|
173
|
+
KreuzbergError::Other(msg) => McpError::internal_error(msg, None),
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// Kreuzberg MCP server.
|
|
178
|
+
///
|
|
179
|
+
/// Provides document extraction capabilities via MCP tools.
|
|
180
|
+
///
|
|
181
|
+
/// The server loads a default extraction configuration from kreuzberg.toml/yaml/json
|
|
182
|
+
/// via discovery. Per-request OCR settings override the defaults.
|
|
183
|
+
#[derive(Clone)]
|
|
184
|
+
pub struct KreuzbergMcp {
|
|
185
|
+
tool_router: ToolRouter<KreuzbergMcp>,
|
|
186
|
+
/// Default extraction configuration loaded from config file via discovery
|
|
187
|
+
default_config: std::sync::Arc<ExtractionConfig>,
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
#[tool_router]
|
|
191
|
+
impl KreuzbergMcp {
|
|
192
|
+
/// Create a new Kreuzberg MCP server instance with default config.
|
|
193
|
+
///
|
|
194
|
+
/// Uses `ExtractionConfig::discover()` to search for kreuzberg.toml/yaml/json
|
|
195
|
+
/// in current and parent directories. Falls back to default configuration if
|
|
196
|
+
/// no config file is found.
|
|
197
|
+
#[allow(clippy::manual_unwrap_or_default)]
|
|
198
|
+
pub fn new() -> crate::Result<Self> {
|
|
199
|
+
let config = match ExtractionConfig::discover()? {
|
|
200
|
+
Some(config) => {
|
|
201
|
+
#[cfg(feature = "api")]
|
|
202
|
+
tracing::info!("Loaded extraction config from discovered file");
|
|
203
|
+
config
|
|
204
|
+
}
|
|
205
|
+
None => {
|
|
206
|
+
#[cfg(feature = "api")]
|
|
207
|
+
tracing::info!("No config file found, using default configuration");
|
|
208
|
+
ExtractionConfig::default()
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
Ok(Self::with_config(config))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Create a new Kreuzberg MCP server instance with explicit config.
|
|
216
|
+
///
|
|
217
|
+
/// # Arguments
|
|
218
|
+
///
|
|
219
|
+
/// * `config` - Default extraction configuration for all tool calls
|
|
220
|
+
pub fn with_config(config: ExtractionConfig) -> Self {
|
|
221
|
+
Self {
|
|
222
|
+
tool_router: Self::tool_router(),
|
|
223
|
+
default_config: std::sync::Arc::new(config),
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// Extract content from a file.
|
|
228
|
+
///
|
|
229
|
+
/// This tool extracts text, metadata, and tables from documents in various formats
|
|
230
|
+
/// including PDFs, Word documents, Excel spreadsheets, images (with OCR), and more.
|
|
231
|
+
#[tool(
|
|
232
|
+
description = "Extract content from a file by path. Supports PDFs, Word, Excel, images (with OCR), HTML, and more."
|
|
233
|
+
)]
|
|
234
|
+
async fn extract_file(
|
|
235
|
+
&self,
|
|
236
|
+
Parameters(params): Parameters<ExtractFileParams>,
|
|
237
|
+
) -> Result<CallToolResult, McpError> {
|
|
238
|
+
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
239
|
+
|
|
240
|
+
let result = if params.r#async {
|
|
241
|
+
extract_file(¶ms.path, params.mime_type.as_deref(), &config)
|
|
242
|
+
.await
|
|
243
|
+
.map_err(map_kreuzberg_error_to_mcp)?
|
|
244
|
+
} else {
|
|
245
|
+
extract_file_sync(¶ms.path, params.mime_type.as_deref(), &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
let response = format_extraction_result(&result);
|
|
249
|
+
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// Extract content from base64-encoded bytes.
|
|
253
|
+
///
|
|
254
|
+
/// This tool extracts text, metadata, and tables from base64-encoded document data.
|
|
255
|
+
#[tool(
|
|
256
|
+
description = "Extract content from base64-encoded file data. Returns extracted text, metadata, and tables."
|
|
257
|
+
)]
|
|
258
|
+
async fn extract_bytes(
|
|
259
|
+
&self,
|
|
260
|
+
Parameters(params): Parameters<ExtractBytesParams>,
|
|
261
|
+
) -> Result<CallToolResult, McpError> {
|
|
262
|
+
let bytes = BASE64_STANDARD
|
|
263
|
+
.decode(¶ms.data)
|
|
264
|
+
.map_err(|e| McpError::invalid_params(format!("Invalid base64: {}", e), None))?;
|
|
265
|
+
|
|
266
|
+
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
267
|
+
|
|
268
|
+
let mime_type = params.mime_type.as_deref().unwrap_or("");
|
|
269
|
+
|
|
270
|
+
let result = if params.r#async {
|
|
271
|
+
extract_bytes(&bytes, mime_type, &config)
|
|
272
|
+
.await
|
|
273
|
+
.map_err(map_kreuzberg_error_to_mcp)?
|
|
274
|
+
} else {
|
|
275
|
+
extract_bytes_sync(&bytes, mime_type, &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
let response = format_extraction_result(&result);
|
|
279
|
+
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// Extract content from multiple files in parallel.
|
|
283
|
+
///
|
|
284
|
+
/// This tool efficiently processes multiple documents simultaneously, useful for batch operations.
|
|
285
|
+
#[tool(description = "Extract content from multiple files in parallel. Returns results for all files.")]
|
|
286
|
+
async fn batch_extract_files(
|
|
287
|
+
&self,
|
|
288
|
+
Parameters(params): Parameters<BatchExtractFilesParams>,
|
|
289
|
+
) -> Result<CallToolResult, McpError> {
|
|
290
|
+
let config = build_config(&self.default_config, params.enable_ocr, params.force_ocr);
|
|
291
|
+
|
|
292
|
+
let results = if params.r#async {
|
|
293
|
+
batch_extract_file(params.paths.clone(), &config)
|
|
294
|
+
.await
|
|
295
|
+
.map_err(map_kreuzberg_error_to_mcp)?
|
|
296
|
+
} else {
|
|
297
|
+
batch_extract_file_sync(params.paths.clone(), &config).map_err(map_kreuzberg_error_to_mcp)?
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
let mut response = String::new();
|
|
301
|
+
for (i, result) in results.iter().enumerate() {
|
|
302
|
+
response.push_str(&format!("=== Document {}: {} ===\n", i + 1, params.paths[i]));
|
|
303
|
+
response.push_str(&format_extraction_result(result));
|
|
304
|
+
response.push_str("\n\n");
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/// Detect the MIME type of a file.
|
|
311
|
+
///
|
|
312
|
+
/// This tool identifies the file format, useful for determining which extractor to use.
|
|
313
|
+
#[tool(description = "Detect the MIME type of a file. Returns the detected MIME type string.")]
|
|
314
|
+
fn detect_mime_type(
|
|
315
|
+
&self,
|
|
316
|
+
Parameters(params): Parameters<DetectMimeTypeParams>,
|
|
317
|
+
) -> Result<CallToolResult, McpError> {
|
|
318
|
+
let mime_type = detect_mime_type(¶ms.path, params.use_content).map_err(map_kreuzberg_error_to_mcp)?;
|
|
319
|
+
|
|
320
|
+
Ok(CallToolResult::success(vec![Content::text(mime_type)]))
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/// Get cache statistics.
|
|
324
|
+
///
|
|
325
|
+
/// This tool returns statistics about the cache including total files, size, and disk space.
|
|
326
|
+
#[tool(description = "Get cache statistics including total files, size, and available disk space.")]
|
|
327
|
+
fn cache_stats(&self, Parameters(_): Parameters<()>) -> Result<CallToolResult, McpError> {
|
|
328
|
+
let cache_dir = std::env::current_dir()
|
|
329
|
+
.unwrap_or_else(|_| std::path::PathBuf::from("."))
|
|
330
|
+
.join(".kreuzberg");
|
|
331
|
+
|
|
332
|
+
let stats = cache::get_cache_metadata(cache_dir.to_str().unwrap_or(".")).map_err(map_kreuzberg_error_to_mcp)?;
|
|
333
|
+
|
|
334
|
+
let response = format!(
|
|
335
|
+
"Cache Statistics\n\
|
|
336
|
+
================\n\
|
|
337
|
+
Directory: {}\n\
|
|
338
|
+
Total files: {}\n\
|
|
339
|
+
Total size: {:.2} MB\n\
|
|
340
|
+
Available space: {:.2} MB\n\
|
|
341
|
+
Oldest file age: {:.2} days\n\
|
|
342
|
+
Newest file age: {:.2} days",
|
|
343
|
+
cache_dir.to_string_lossy(),
|
|
344
|
+
stats.total_files,
|
|
345
|
+
stats.total_size_mb,
|
|
346
|
+
stats.available_space_mb,
|
|
347
|
+
stats.oldest_file_age_days,
|
|
348
|
+
stats.newest_file_age_days
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/// Clear the cache.
|
|
355
|
+
///
|
|
356
|
+
/// This tool removes all cached files and returns the number of files removed and space freed.
|
|
357
|
+
#[tool(description = "Clear all cached files. Returns the number of files removed and space freed in MB.")]
|
|
358
|
+
fn cache_clear(&self, Parameters(_): Parameters<()>) -> Result<CallToolResult, McpError> {
|
|
359
|
+
let cache_dir = std::env::current_dir()
|
|
360
|
+
.unwrap_or_else(|_| std::path::PathBuf::from("."))
|
|
361
|
+
.join(".kreuzberg");
|
|
362
|
+
|
|
363
|
+
let (removed_files, freed_mb) =
|
|
364
|
+
cache::clear_cache_directory(cache_dir.to_str().unwrap_or(".")).map_err(map_kreuzberg_error_to_mcp)?;
|
|
365
|
+
|
|
366
|
+
let response = format!(
|
|
367
|
+
"Cache cleared successfully\n\
|
|
368
|
+
Directory: {}\n\
|
|
369
|
+
Removed files: {}\n\
|
|
370
|
+
Freed space: {:.2} MB",
|
|
371
|
+
cache_dir.to_string_lossy(),
|
|
372
|
+
removed_files,
|
|
373
|
+
freed_mb
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
Ok(CallToolResult::success(vec![Content::text(response)]))
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
#[tool_handler]
|
|
381
|
+
impl ServerHandler for KreuzbergMcp {
|
|
382
|
+
fn get_info(&self) -> ServerInfo {
|
|
383
|
+
ServerInfo {
|
|
384
|
+
protocol_version: ProtocolVersion::default(),
|
|
385
|
+
capabilities: ServerCapabilities {
|
|
386
|
+
tools: Some(ToolsCapability::default()),
|
|
387
|
+
..Default::default()
|
|
388
|
+
},
|
|
389
|
+
server_info: Implementation {
|
|
390
|
+
name: "kreuzberg-mcp".to_string(),
|
|
391
|
+
title: Some("Kreuzberg Document Intelligence MCP Server".to_string()),
|
|
392
|
+
version: env!("CARGO_PKG_VERSION").to_string(),
|
|
393
|
+
icons: None,
|
|
394
|
+
website_url: Some("https://goldziher.github.io/kreuzberg/".to_string()),
|
|
395
|
+
},
|
|
396
|
+
instructions: Some(
|
|
397
|
+
"Extract content from documents in various formats. Supports PDFs, Word documents, \
|
|
398
|
+
Excel spreadsheets, images (with OCR), HTML, emails, and more. Use enable_ocr=true \
|
|
399
|
+
for scanned documents, force_ocr=true to always use OCR even if text extraction \
|
|
400
|
+
succeeds."
|
|
401
|
+
.to_string(),
|
|
402
|
+
),
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
impl Default for KreuzbergMcp {
|
|
408
|
+
fn default() -> Self {
|
|
409
|
+
Self::new().unwrap_or_else(|e| {
|
|
410
|
+
#[cfg(feature = "api")]
|
|
411
|
+
tracing::warn!("Failed to discover config, using default: {}", e);
|
|
412
|
+
#[cfg(not(feature = "api"))]
|
|
413
|
+
tracing::debug!("Warning: Failed to discover config, using default: {}", e);
|
|
414
|
+
Self::with_config(ExtractionConfig::default())
|
|
415
|
+
})
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/// Start the Kreuzberg MCP server.
|
|
420
|
+
///
|
|
421
|
+
/// This function initializes and runs the MCP server using stdio transport.
|
|
422
|
+
/// It will block until the server is shut down.
|
|
423
|
+
///
|
|
424
|
+
/// # Errors
|
|
425
|
+
///
|
|
426
|
+
/// Returns an error if the server fails to start or encounters a fatal error.
|
|
427
|
+
///
|
|
428
|
+
/// # Example
|
|
429
|
+
///
|
|
430
|
+
/// ```rust,no_run
|
|
431
|
+
/// use kreuzberg::mcp::start_mcp_server;
|
|
432
|
+
///
|
|
433
|
+
/// #[tokio::main]
|
|
434
|
+
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
435
|
+
/// start_mcp_server().await?;
|
|
436
|
+
/// Ok(())
|
|
437
|
+
/// }
|
|
438
|
+
/// ```
|
|
439
|
+
pub async fn start_mcp_server() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
440
|
+
let service = KreuzbergMcp::new()?.serve(stdio()).await?;
|
|
441
|
+
|
|
442
|
+
service.waiting().await?;
|
|
443
|
+
Ok(())
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/// Start MCP server with custom extraction config.
|
|
447
|
+
///
|
|
448
|
+
/// This variant allows specifying a custom extraction configuration
|
|
449
|
+
/// (e.g., loaded from a file) instead of using defaults.
|
|
450
|
+
pub async fn start_mcp_server_with_config(
|
|
451
|
+
config: ExtractionConfig,
|
|
452
|
+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
453
|
+
let service = KreuzbergMcp::with_config(config).serve(stdio()).await?;
|
|
454
|
+
|
|
455
|
+
service.waiting().await?;
|
|
456
|
+
Ok(())
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/// Start MCP server with HTTP Stream transport.
|
|
460
|
+
///
|
|
461
|
+
/// Uses rmcp's built-in StreamableHttpService for HTTP/SSE support per MCP spec.
|
|
462
|
+
///
|
|
463
|
+
/// # Arguments
|
|
464
|
+
///
|
|
465
|
+
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
|
|
466
|
+
/// * `port` - Port number (e.g., 8001)
|
|
467
|
+
///
|
|
468
|
+
/// # Example
|
|
469
|
+
///
|
|
470
|
+
/// ```no_run
|
|
471
|
+
/// use kreuzberg::mcp::start_mcp_server_http;
|
|
472
|
+
///
|
|
473
|
+
/// #[tokio::main]
|
|
474
|
+
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
475
|
+
/// start_mcp_server_http("127.0.0.1", 8001).await?;
|
|
476
|
+
/// Ok(())
|
|
477
|
+
/// }
|
|
478
|
+
/// ```
|
|
479
|
+
#[cfg(feature = "mcp-http")]
|
|
480
|
+
pub async fn start_mcp_server_http(
|
|
481
|
+
host: impl AsRef<str>,
|
|
482
|
+
port: u16,
|
|
483
|
+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
484
|
+
use axum::Router;
|
|
485
|
+
use std::net::SocketAddr;
|
|
486
|
+
|
|
487
|
+
let http_service = StreamableHttpService::new(
|
|
488
|
+
|| KreuzbergMcp::new().map_err(|e| std::io::Error::other(e.to_string())),
|
|
489
|
+
LocalSessionManager::default().into(),
|
|
490
|
+
Default::default(),
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
let router = Router::new().nest_service("/mcp", http_service);
|
|
494
|
+
|
|
495
|
+
let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
|
|
496
|
+
.parse()
|
|
497
|
+
.map_err(|e| format!("Invalid address: {}", e))?;
|
|
498
|
+
|
|
499
|
+
#[cfg(feature = "api")]
|
|
500
|
+
tracing::info!("Starting MCP HTTP server on http://{}", addr);
|
|
501
|
+
|
|
502
|
+
let listener = tokio::net::TcpListener::bind(addr).await?;
|
|
503
|
+
axum::serve(listener, router).await?;
|
|
504
|
+
|
|
505
|
+
Ok(())
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/// Start MCP HTTP server with custom extraction config.
|
|
509
|
+
///
|
|
510
|
+
/// This variant allows specifying a custom extraction configuration
|
|
511
|
+
/// while using HTTP Stream transport.
|
|
512
|
+
///
|
|
513
|
+
/// # Arguments
|
|
514
|
+
///
|
|
515
|
+
/// * `host` - Host to bind to (e.g., "127.0.0.1" or "0.0.0.0")
|
|
516
|
+
/// * `port` - Port number (e.g., 8001)
|
|
517
|
+
/// * `config` - Custom extraction configuration
|
|
518
|
+
///
|
|
519
|
+
/// # Example
|
|
520
|
+
///
|
|
521
|
+
/// ```no_run
|
|
522
|
+
/// use kreuzberg::mcp::start_mcp_server_http_with_config;
|
|
523
|
+
/// use kreuzberg::ExtractionConfig;
|
|
524
|
+
///
|
|
525
|
+
/// #[tokio::main]
|
|
526
|
+
/// async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
527
|
+
/// let config = ExtractionConfig::default();
|
|
528
|
+
/// start_mcp_server_http_with_config("127.0.0.1", 8001, config).await?;
|
|
529
|
+
/// Ok(())
|
|
530
|
+
/// }
|
|
531
|
+
/// ```
|
|
532
|
+
#[cfg(feature = "mcp-http")]
|
|
533
|
+
pub async fn start_mcp_server_http_with_config(
|
|
534
|
+
host: impl AsRef<str>,
|
|
535
|
+
port: u16,
|
|
536
|
+
config: ExtractionConfig,
|
|
537
|
+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
538
|
+
use axum::Router;
|
|
539
|
+
use std::net::SocketAddr;
|
|
540
|
+
|
|
541
|
+
let http_service = StreamableHttpService::new(
|
|
542
|
+
move || Ok(KreuzbergMcp::with_config(config.clone())),
|
|
543
|
+
LocalSessionManager::default().into(),
|
|
544
|
+
Default::default(),
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
let router = Router::new().nest_service("/mcp", http_service);
|
|
548
|
+
|
|
549
|
+
let addr: SocketAddr = format!("{}:{}", host.as_ref(), port)
|
|
550
|
+
.parse()
|
|
551
|
+
.map_err(|e| format!("Invalid address: {}", e))?;
|
|
552
|
+
|
|
553
|
+
#[cfg(feature = "api")]
|
|
554
|
+
tracing::info!("Starting MCP HTTP server on http://{}", addr);
|
|
555
|
+
|
|
556
|
+
let listener = tokio::net::TcpListener::bind(addr).await?;
|
|
557
|
+
axum::serve(listener, router).await?;
|
|
558
|
+
|
|
559
|
+
Ok(())
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/// Build extraction config from MCP parameters.
|
|
563
|
+
///
|
|
564
|
+
/// Starts with the default config and overlays OCR settings from request parameters.
|
|
565
|
+
fn build_config(default_config: &ExtractionConfig, enable_ocr: bool, force_ocr: bool) -> ExtractionConfig {
|
|
566
|
+
let mut config = default_config.clone();
|
|
567
|
+
|
|
568
|
+
config.ocr = if enable_ocr {
|
|
569
|
+
Some(crate::OcrConfig {
|
|
570
|
+
backend: "tesseract".to_string(),
|
|
571
|
+
language: "eng".to_string(),
|
|
572
|
+
tesseract_config: None,
|
|
573
|
+
})
|
|
574
|
+
} else {
|
|
575
|
+
None
|
|
576
|
+
};
|
|
577
|
+
config.force_ocr = force_ocr;
|
|
578
|
+
|
|
579
|
+
config
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/// Format extraction result as human-readable text.
|
|
583
|
+
fn format_extraction_result(result: &KreuzbergResult) -> String {
|
|
584
|
+
let mut response = String::new();
|
|
585
|
+
|
|
586
|
+
response.push_str(&format!("Content ({} characters):\n", result.content.len()));
|
|
587
|
+
response.push_str(&result.content);
|
|
588
|
+
response.push_str("\n\n");
|
|
589
|
+
|
|
590
|
+
response.push_str("Metadata:\n");
|
|
591
|
+
response.push_str(&serde_json::to_string_pretty(&result.metadata).unwrap_or_default());
|
|
592
|
+
response.push_str("\n\n");
|
|
593
|
+
|
|
594
|
+
if !result.tables.is_empty() {
|
|
595
|
+
response.push_str(&format!("Tables ({}):\n", result.tables.len()));
|
|
596
|
+
for (i, table) in result.tables.iter().enumerate() {
|
|
597
|
+
response.push_str(&format!("\nTable {} (page {}):\n", i + 1, table.page_number));
|
|
598
|
+
response.push_str(&table.markdown);
|
|
599
|
+
response.push('\n');
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
response
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
#[cfg(test)]
|
|
607
|
+
mod tests {
|
|
608
|
+
use super::*;
|
|
609
|
+
use std::path::PathBuf;
|
|
610
|
+
|
|
611
|
+
/// Get the path to a test document relative to workspace root.
|
|
612
|
+
fn get_test_path(relative_path: &str) -> String {
|
|
613
|
+
let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
614
|
+
.parent()
|
|
615
|
+
.unwrap()
|
|
616
|
+
.parent()
|
|
617
|
+
.unwrap()
|
|
618
|
+
.to_path_buf();
|
|
619
|
+
|
|
620
|
+
workspace_root
|
|
621
|
+
.join("test_documents")
|
|
622
|
+
.join(relative_path)
|
|
623
|
+
.to_string_lossy()
|
|
624
|
+
.to_string()
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
#[tokio::test]
|
|
628
|
+
async fn test_tool_router_has_routes() {
|
|
629
|
+
let router = KreuzbergMcp::tool_router();
|
|
630
|
+
assert!(router.has_route("extract_file"));
|
|
631
|
+
assert!(router.has_route("extract_bytes"));
|
|
632
|
+
assert!(router.has_route("batch_extract_files"));
|
|
633
|
+
assert!(router.has_route("detect_mime_type"));
|
|
634
|
+
assert!(router.has_route("cache_stats"));
|
|
635
|
+
assert!(router.has_route("cache_clear"));
|
|
636
|
+
|
|
637
|
+
let tools = router.list_all();
|
|
638
|
+
assert_eq!(tools.len(), 6);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
#[test]
|
|
642
|
+
fn test_server_info() {
|
|
643
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
644
|
+
let info = server.get_info();
|
|
645
|
+
|
|
646
|
+
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
647
|
+
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
648
|
+
assert!(info.capabilities.tools.is_some());
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
#[test]
|
|
652
|
+
fn test_build_config() {
|
|
653
|
+
let default_config = ExtractionConfig::default();
|
|
654
|
+
|
|
655
|
+
let config = build_config(&default_config, false, false);
|
|
656
|
+
assert!(config.ocr.is_none());
|
|
657
|
+
assert!(!config.force_ocr);
|
|
658
|
+
|
|
659
|
+
let config = build_config(&default_config, true, false);
|
|
660
|
+
assert!(config.ocr.is_some());
|
|
661
|
+
assert!(!config.force_ocr);
|
|
662
|
+
|
|
663
|
+
let config = build_config(&default_config, true, true);
|
|
664
|
+
assert!(config.ocr.is_some());
|
|
665
|
+
assert!(config.force_ocr);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
#[test]
|
|
669
|
+
fn test_with_config_stores_provided_config() {
|
|
670
|
+
let custom_config = ExtractionConfig {
|
|
671
|
+
force_ocr: true,
|
|
672
|
+
use_cache: false,
|
|
673
|
+
..Default::default()
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
let server = KreuzbergMcp::with_config(custom_config);
|
|
677
|
+
|
|
678
|
+
assert!(server.default_config.force_ocr);
|
|
679
|
+
assert!(!server.default_config.use_cache);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
#[test]
|
|
683
|
+
fn test_map_validation_error_to_invalid_params() {
|
|
684
|
+
let error = KreuzbergError::validation("invalid file path");
|
|
685
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
686
|
+
|
|
687
|
+
assert_eq!(mcp_error.code.0, -32602);
|
|
688
|
+
assert!(mcp_error.message.contains("Validation error"));
|
|
689
|
+
assert!(mcp_error.message.contains("invalid file path"));
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
#[test]
|
|
693
|
+
fn test_map_validation_error_with_source_preserves_chain() {
|
|
694
|
+
let source = std::io::Error::new(std::io::ErrorKind::InvalidInput, "bad param");
|
|
695
|
+
let error = KreuzbergError::validation_with_source("invalid configuration", source);
|
|
696
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
697
|
+
|
|
698
|
+
assert_eq!(mcp_error.code.0, -32602);
|
|
699
|
+
assert!(mcp_error.message.contains("Validation error"));
|
|
700
|
+
assert!(mcp_error.message.contains("invalid configuration"));
|
|
701
|
+
assert!(mcp_error.message.contains("caused by"));
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
#[test]
|
|
705
|
+
fn test_map_unsupported_format_to_invalid_params() {
|
|
706
|
+
let error = KreuzbergError::UnsupportedFormat("application/unknown".to_string());
|
|
707
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
708
|
+
|
|
709
|
+
assert_eq!(mcp_error.code.0, -32602);
|
|
710
|
+
assert!(mcp_error.message.contains("Unsupported format"));
|
|
711
|
+
assert!(mcp_error.message.contains("application/unknown"));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
#[test]
|
|
715
|
+
fn test_map_missing_dependency_to_invalid_params() {
|
|
716
|
+
let error = KreuzbergError::MissingDependency("tesseract".to_string());
|
|
717
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
718
|
+
|
|
719
|
+
assert_eq!(mcp_error.code.0, -32602);
|
|
720
|
+
assert!(mcp_error.message.contains("Missing required dependency"));
|
|
721
|
+
assert!(mcp_error.message.contains("tesseract"));
|
|
722
|
+
assert!(mcp_error.message.contains("Please install"));
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
#[test]
|
|
726
|
+
fn test_map_parsing_error_to_parse_error() {
|
|
727
|
+
let error = KreuzbergError::parsing("corrupt PDF file");
|
|
728
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
729
|
+
|
|
730
|
+
assert_eq!(mcp_error.code.0, -32700);
|
|
731
|
+
assert!(mcp_error.message.contains("Parsing error"));
|
|
732
|
+
assert!(mcp_error.message.contains("corrupt PDF file"));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
#[test]
|
|
736
|
+
fn test_map_parsing_error_with_source_preserves_chain() {
|
|
737
|
+
let source = std::io::Error::new(std::io::ErrorKind::InvalidData, "malformed data");
|
|
738
|
+
let error = KreuzbergError::parsing_with_source("failed to parse document", source);
|
|
739
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
740
|
+
|
|
741
|
+
assert_eq!(mcp_error.code.0, -32700);
|
|
742
|
+
assert!(mcp_error.message.contains("Parsing error"));
|
|
743
|
+
assert!(mcp_error.message.contains("failed to parse document"));
|
|
744
|
+
assert!(mcp_error.message.contains("caused by"));
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
#[test]
|
|
748
|
+
fn test_map_io_error_to_internal_error() {
|
|
749
|
+
let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
|
|
750
|
+
let error = KreuzbergError::Io(io_error);
|
|
751
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
752
|
+
|
|
753
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
754
|
+
assert!(mcp_error.message.contains("System I/O error"));
|
|
755
|
+
assert!(mcp_error.message.contains("file not found"));
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
#[test]
|
|
759
|
+
fn test_map_ocr_error_to_internal_error() {
|
|
760
|
+
let error = KreuzbergError::ocr("tesseract failed");
|
|
761
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
762
|
+
|
|
763
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
764
|
+
assert!(mcp_error.message.contains("OCR processing error"));
|
|
765
|
+
assert!(mcp_error.message.contains("tesseract failed"));
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
#[test]
|
|
769
|
+
fn test_map_cache_error_to_internal_error() {
|
|
770
|
+
let error = KreuzbergError::cache("cache write failed");
|
|
771
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
772
|
+
|
|
773
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
774
|
+
assert!(mcp_error.message.contains("Cache error"));
|
|
775
|
+
assert!(mcp_error.message.contains("cache write failed"));
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
#[test]
|
|
779
|
+
fn test_map_image_processing_error_to_internal_error() {
|
|
780
|
+
let error = KreuzbergError::image_processing("resize failed");
|
|
781
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
782
|
+
|
|
783
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
784
|
+
assert!(mcp_error.message.contains("Image processing error"));
|
|
785
|
+
assert!(mcp_error.message.contains("resize failed"));
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
#[test]
|
|
789
|
+
fn test_map_serialization_error_to_internal_error() {
|
|
790
|
+
let error = KreuzbergError::serialization("JSON encode failed");
|
|
791
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
792
|
+
|
|
793
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
794
|
+
assert!(mcp_error.message.contains("Serialization error"));
|
|
795
|
+
assert!(mcp_error.message.contains("JSON encode failed"));
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
#[test]
|
|
799
|
+
fn test_map_plugin_error_to_internal_error() {
|
|
800
|
+
let error = KreuzbergError::Plugin {
|
|
801
|
+
message: "extraction failed".to_string(),
|
|
802
|
+
plugin_name: "pdf-extractor".to_string(),
|
|
803
|
+
};
|
|
804
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
805
|
+
|
|
806
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
807
|
+
assert!(mcp_error.message.contains("Plugin 'pdf-extractor' error"));
|
|
808
|
+
assert!(mcp_error.message.contains("extraction failed"));
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
#[test]
|
|
812
|
+
fn test_map_lock_poisoned_error_to_internal_error() {
|
|
813
|
+
let error = KreuzbergError::LockPoisoned("registry lock poisoned".to_string());
|
|
814
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
815
|
+
|
|
816
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
817
|
+
assert!(mcp_error.message.contains("Internal lock poisoned"));
|
|
818
|
+
assert!(mcp_error.message.contains("registry lock poisoned"));
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
#[test]
|
|
822
|
+
fn test_map_other_error_to_internal_error() {
|
|
823
|
+
let error = KreuzbergError::Other("unexpected error".to_string());
|
|
824
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
825
|
+
|
|
826
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
827
|
+
assert!(mcp_error.message.contains("unexpected error"));
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
#[test]
|
|
831
|
+
fn test_error_type_differentiation() {
|
|
832
|
+
let validation = KreuzbergError::validation("test");
|
|
833
|
+
let parsing = KreuzbergError::parsing("test");
|
|
834
|
+
let io = KreuzbergError::Io(std::io::Error::other("test"));
|
|
835
|
+
|
|
836
|
+
let val_mcp = map_kreuzberg_error_to_mcp(validation);
|
|
837
|
+
let parse_mcp = map_kreuzberg_error_to_mcp(parsing);
|
|
838
|
+
let io_mcp = map_kreuzberg_error_to_mcp(io);
|
|
839
|
+
|
|
840
|
+
assert_eq!(val_mcp.code.0, -32602);
|
|
841
|
+
assert_eq!(parse_mcp.code.0, -32700);
|
|
842
|
+
assert_eq!(io_mcp.code.0, -32603);
|
|
843
|
+
|
|
844
|
+
assert_ne!(val_mcp.code.0, parse_mcp.code.0);
|
|
845
|
+
assert_ne!(val_mcp.code.0, io_mcp.code.0);
|
|
846
|
+
assert_ne!(parse_mcp.code.0, io_mcp.code.0);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
#[test]
|
|
850
|
+
fn test_format_extraction_result_with_content() {
|
|
851
|
+
let result = KreuzbergResult {
|
|
852
|
+
content: "Sample extracted text".to_string(),
|
|
853
|
+
mime_type: "text/plain".to_string(),
|
|
854
|
+
metadata: crate::Metadata::default(),
|
|
855
|
+
tables: vec![],
|
|
856
|
+
detected_languages: None,
|
|
857
|
+
chunks: None,
|
|
858
|
+
images: None,
|
|
859
|
+
pages: None,
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
let formatted = format_extraction_result(&result);
|
|
863
|
+
|
|
864
|
+
assert!(formatted.contains("Content (21 characters)"));
|
|
865
|
+
assert!(formatted.contains("Sample extracted text"));
|
|
866
|
+
assert!(formatted.contains("Metadata:"));
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
#[test]
|
|
870
|
+
fn test_format_extraction_result_with_tables() {
|
|
871
|
+
let result = KreuzbergResult {
|
|
872
|
+
content: "Document with tables".to_string(),
|
|
873
|
+
mime_type: "application/pdf".to_string(),
|
|
874
|
+
metadata: crate::Metadata::default(),
|
|
875
|
+
tables: vec![
|
|
876
|
+
crate::Table {
|
|
877
|
+
cells: vec![
|
|
878
|
+
vec!["Col1".to_string(), "Col2".to_string()],
|
|
879
|
+
vec!["A".to_string(), "B".to_string()],
|
|
880
|
+
],
|
|
881
|
+
page_number: 1,
|
|
882
|
+
markdown: "| Col1 | Col2 |\n|------|------|\n| A | B |".to_string(),
|
|
883
|
+
},
|
|
884
|
+
crate::Table {
|
|
885
|
+
cells: vec![
|
|
886
|
+
vec!["X".to_string(), "Y".to_string()],
|
|
887
|
+
vec!["1".to_string(), "2".to_string()],
|
|
888
|
+
],
|
|
889
|
+
page_number: 2,
|
|
890
|
+
markdown: "| X | Y |\n|---|---|\n| 1 | 2 |".to_string(),
|
|
891
|
+
},
|
|
892
|
+
],
|
|
893
|
+
detected_languages: None,
|
|
894
|
+
chunks: None,
|
|
895
|
+
images: None,
|
|
896
|
+
pages: None,
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
let formatted = format_extraction_result(&result);
|
|
900
|
+
|
|
901
|
+
assert!(formatted.contains("Tables (2)"));
|
|
902
|
+
assert!(formatted.contains("Table 1 (page 1)"));
|
|
903
|
+
assert!(formatted.contains("Table 2 (page 2)"));
|
|
904
|
+
assert!(formatted.contains("| Col1 | Col2 |"));
|
|
905
|
+
assert!(formatted.contains("| X | Y |"));
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
#[test]
|
|
909
|
+
fn test_format_extraction_result_empty_content() {
|
|
910
|
+
let result = KreuzbergResult {
|
|
911
|
+
content: String::new(),
|
|
912
|
+
mime_type: "text/plain".to_string(),
|
|
913
|
+
metadata: crate::Metadata::default(),
|
|
914
|
+
tables: vec![],
|
|
915
|
+
detected_languages: None,
|
|
916
|
+
chunks: None,
|
|
917
|
+
images: None,
|
|
918
|
+
pages: None,
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
let formatted = format_extraction_result(&result);
|
|
922
|
+
|
|
923
|
+
assert!(formatted.contains("Content (0 characters)"));
|
|
924
|
+
assert!(formatted.contains("Metadata:"));
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
#[test]
|
|
928
|
+
fn test_format_extraction_result_no_tables() {
|
|
929
|
+
let result = KreuzbergResult {
|
|
930
|
+
content: "Simple text".to_string(),
|
|
931
|
+
mime_type: "text/plain".to_string(),
|
|
932
|
+
metadata: crate::Metadata::default(),
|
|
933
|
+
tables: vec![],
|
|
934
|
+
detected_languages: None,
|
|
935
|
+
chunks: None,
|
|
936
|
+
images: None,
|
|
937
|
+
pages: None,
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
let formatted = format_extraction_result(&result);
|
|
941
|
+
|
|
942
|
+
assert!(formatted.contains("Simple text"));
|
|
943
|
+
assert!(!formatted.contains("Tables"));
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
#[tokio::test]
|
|
947
|
+
async fn test_extract_file_sync_with_valid_pdf() {
|
|
948
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
949
|
+
let params = ExtractFileParams {
|
|
950
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
951
|
+
mime_type: None,
|
|
952
|
+
enable_ocr: false,
|
|
953
|
+
force_ocr: false,
|
|
954
|
+
r#async: true,
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
958
|
+
|
|
959
|
+
assert!(result.is_ok());
|
|
960
|
+
let call_result = result.unwrap();
|
|
961
|
+
if let Some(content) = call_result.content.first() {
|
|
962
|
+
match &content.raw {
|
|
963
|
+
RawContent::Text(text) => {
|
|
964
|
+
assert!(!text.text.is_empty());
|
|
965
|
+
assert!(text.text.contains("Content"));
|
|
966
|
+
}
|
|
967
|
+
_ => panic!("Expected text content"),
|
|
968
|
+
}
|
|
969
|
+
} else {
|
|
970
|
+
panic!("Expected content in result");
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
#[tokio::test]
|
|
975
|
+
async fn test_extract_file_async_with_valid_pdf() {
|
|
976
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
977
|
+
let params = ExtractFileParams {
|
|
978
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
979
|
+
mime_type: None,
|
|
980
|
+
enable_ocr: false,
|
|
981
|
+
force_ocr: false,
|
|
982
|
+
r#async: true,
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
986
|
+
|
|
987
|
+
assert!(result.is_ok());
|
|
988
|
+
let call_result = result.unwrap();
|
|
989
|
+
if let Some(content) = call_result.content.first() {
|
|
990
|
+
match &content.raw {
|
|
991
|
+
RawContent::Text(text) => {
|
|
992
|
+
assert!(!text.text.is_empty());
|
|
993
|
+
}
|
|
994
|
+
_ => panic!("Expected text content"),
|
|
995
|
+
}
|
|
996
|
+
} else {
|
|
997
|
+
panic!("Expected content in result");
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
#[tokio::test]
|
|
1002
|
+
async fn test_extract_file_with_invalid_path() {
|
|
1003
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1004
|
+
let params = ExtractFileParams {
|
|
1005
|
+
path: "/nonexistent/file.pdf".to_string(),
|
|
1006
|
+
mime_type: None,
|
|
1007
|
+
enable_ocr: false,
|
|
1008
|
+
force_ocr: false,
|
|
1009
|
+
r#async: true,
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1013
|
+
|
|
1014
|
+
assert!(result.is_err());
|
|
1015
|
+
let error = result.unwrap_err();
|
|
1016
|
+
assert!(error.code.0 == -32602 || error.code.0 == -32603);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
#[tokio::test]
|
|
1020
|
+
async fn test_extract_file_with_mime_type_hint() {
|
|
1021
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1022
|
+
let params = ExtractFileParams {
|
|
1023
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1024
|
+
mime_type: Some("application/pdf".to_string()),
|
|
1025
|
+
enable_ocr: false,
|
|
1026
|
+
force_ocr: false,
|
|
1027
|
+
r#async: true,
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1031
|
+
|
|
1032
|
+
assert!(result.is_ok());
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
#[tokio::test]
|
|
1036
|
+
async fn test_extract_file_with_ocr_enabled() {
|
|
1037
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1038
|
+
let params = ExtractFileParams {
|
|
1039
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1040
|
+
mime_type: None,
|
|
1041
|
+
enable_ocr: true,
|
|
1042
|
+
force_ocr: false,
|
|
1043
|
+
r#async: true,
|
|
1044
|
+
};
|
|
1045
|
+
|
|
1046
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1047
|
+
|
|
1048
|
+
assert!(result.is_ok() || result.is_err());
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
#[tokio::test]
|
|
1052
|
+
async fn test_extract_file_with_force_ocr() {
|
|
1053
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1054
|
+
let params = ExtractFileParams {
|
|
1055
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1056
|
+
mime_type: None,
|
|
1057
|
+
enable_ocr: true,
|
|
1058
|
+
force_ocr: true,
|
|
1059
|
+
r#async: true,
|
|
1060
|
+
};
|
|
1061
|
+
|
|
1062
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1063
|
+
|
|
1064
|
+
assert!(result.is_ok() || result.is_err());
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
#[tokio::test]
|
|
1068
|
+
async fn test_extract_bytes_sync_with_valid_data() {
|
|
1069
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1070
|
+
|
|
1071
|
+
let text_content = b"Hello, world!";
|
|
1072
|
+
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1073
|
+
|
|
1074
|
+
let params = ExtractBytesParams {
|
|
1075
|
+
data: encoded,
|
|
1076
|
+
mime_type: Some("text/plain".to_string()),
|
|
1077
|
+
enable_ocr: false,
|
|
1078
|
+
force_ocr: false,
|
|
1079
|
+
r#async: true,
|
|
1080
|
+
};
|
|
1081
|
+
|
|
1082
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1083
|
+
|
|
1084
|
+
assert!(result.is_ok());
|
|
1085
|
+
let call_result = result.unwrap();
|
|
1086
|
+
if let Some(content) = call_result.content.first() {
|
|
1087
|
+
match &content.raw {
|
|
1088
|
+
RawContent::Text(text) => {
|
|
1089
|
+
assert!(text.text.contains("Hello, world!"));
|
|
1090
|
+
}
|
|
1091
|
+
_ => panic!("Expected text content"),
|
|
1092
|
+
}
|
|
1093
|
+
} else {
|
|
1094
|
+
panic!("Expected content in result");
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
#[tokio::test]
|
|
1099
|
+
async fn test_extract_bytes_async_with_valid_data() {
|
|
1100
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1101
|
+
|
|
1102
|
+
let text_content = b"Async extraction test";
|
|
1103
|
+
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1104
|
+
|
|
1105
|
+
let params = ExtractBytesParams {
|
|
1106
|
+
data: encoded,
|
|
1107
|
+
mime_type: Some("text/plain".to_string()),
|
|
1108
|
+
enable_ocr: false,
|
|
1109
|
+
force_ocr: false,
|
|
1110
|
+
r#async: true,
|
|
1111
|
+
};
|
|
1112
|
+
|
|
1113
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1114
|
+
|
|
1115
|
+
assert!(result.is_ok());
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
#[tokio::test]
|
|
1119
|
+
async fn test_extract_bytes_with_invalid_base64() {
|
|
1120
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1121
|
+
|
|
1122
|
+
let params = ExtractBytesParams {
|
|
1123
|
+
data: "not-valid-base64!!!".to_string(),
|
|
1124
|
+
mime_type: None,
|
|
1125
|
+
enable_ocr: false,
|
|
1126
|
+
force_ocr: false,
|
|
1127
|
+
r#async: true,
|
|
1128
|
+
};
|
|
1129
|
+
|
|
1130
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1131
|
+
|
|
1132
|
+
assert!(result.is_err());
|
|
1133
|
+
let error = result.unwrap_err();
|
|
1134
|
+
assert_eq!(error.code.0, -32602);
|
|
1135
|
+
assert!(error.message.contains("Invalid base64"));
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
#[tokio::test]
|
|
1139
|
+
async fn test_extract_bytes_without_mime_type() {
|
|
1140
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1141
|
+
|
|
1142
|
+
let text_content = b"Test content";
|
|
1143
|
+
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1144
|
+
|
|
1145
|
+
let params = ExtractBytesParams {
|
|
1146
|
+
data: encoded,
|
|
1147
|
+
mime_type: None,
|
|
1148
|
+
enable_ocr: false,
|
|
1149
|
+
force_ocr: false,
|
|
1150
|
+
r#async: true,
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1154
|
+
|
|
1155
|
+
assert!(result.is_ok() || result.is_err());
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
#[tokio::test]
|
|
1159
|
+
async fn test_extract_bytes_with_ocr_enabled() {
|
|
1160
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1161
|
+
|
|
1162
|
+
let text_content = b"OCR test content";
|
|
1163
|
+
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1164
|
+
|
|
1165
|
+
let params = ExtractBytesParams {
|
|
1166
|
+
data: encoded,
|
|
1167
|
+
mime_type: Some("text/plain".to_string()),
|
|
1168
|
+
enable_ocr: true,
|
|
1169
|
+
force_ocr: false,
|
|
1170
|
+
r#async: true,
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1174
|
+
|
|
1175
|
+
assert!(result.is_ok() || result.is_err());
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
#[tokio::test]
|
|
1179
|
+
async fn test_batch_extract_files_sync_with_valid_files() {
|
|
1180
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1181
|
+
let params = BatchExtractFilesParams {
|
|
1182
|
+
paths: vec![get_test_path("pdfs_with_tables/tiny.pdf").to_string()],
|
|
1183
|
+
enable_ocr: false,
|
|
1184
|
+
force_ocr: false,
|
|
1185
|
+
r#async: true,
|
|
1186
|
+
};
|
|
1187
|
+
|
|
1188
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1189
|
+
|
|
1190
|
+
assert!(result.is_ok());
|
|
1191
|
+
let call_result = result.unwrap();
|
|
1192
|
+
if let Some(content) = call_result.content.first() {
|
|
1193
|
+
match &content.raw {
|
|
1194
|
+
RawContent::Text(text) => {
|
|
1195
|
+
assert!(text.text.contains("Document 1"));
|
|
1196
|
+
assert!(text.text.contains("tiny.pdf"));
|
|
1197
|
+
}
|
|
1198
|
+
_ => panic!("Expected text content"),
|
|
1199
|
+
}
|
|
1200
|
+
} else {
|
|
1201
|
+
panic!("Expected content in result");
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
#[tokio::test]
|
|
1206
|
+
async fn test_batch_extract_files_async_with_multiple_files() {
|
|
1207
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1208
|
+
let params = BatchExtractFilesParams {
|
|
1209
|
+
paths: vec![
|
|
1210
|
+
get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1211
|
+
get_test_path("pdfs_with_tables/medium.pdf").to_string(),
|
|
1212
|
+
],
|
|
1213
|
+
enable_ocr: false,
|
|
1214
|
+
force_ocr: false,
|
|
1215
|
+
r#async: true,
|
|
1216
|
+
};
|
|
1217
|
+
|
|
1218
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1219
|
+
|
|
1220
|
+
assert!(result.is_ok());
|
|
1221
|
+
let call_result = result.unwrap();
|
|
1222
|
+
if let Some(content) = call_result.content.first() {
|
|
1223
|
+
match &content.raw {
|
|
1224
|
+
RawContent::Text(text) => {
|
|
1225
|
+
assert!(text.text.contains("Document 1"));
|
|
1226
|
+
assert!(text.text.contains("Document 2"));
|
|
1227
|
+
}
|
|
1228
|
+
_ => panic!("Expected text content"),
|
|
1229
|
+
}
|
|
1230
|
+
} else {
|
|
1231
|
+
panic!("Expected content in result");
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
#[tokio::test]
|
|
1236
|
+
async fn test_batch_extract_files_with_empty_list() {
|
|
1237
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1238
|
+
let params = BatchExtractFilesParams {
|
|
1239
|
+
paths: vec![],
|
|
1240
|
+
enable_ocr: false,
|
|
1241
|
+
force_ocr: false,
|
|
1242
|
+
r#async: true,
|
|
1243
|
+
};
|
|
1244
|
+
|
|
1245
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1246
|
+
|
|
1247
|
+
assert!(result.is_ok());
|
|
1248
|
+
let call_result = result.unwrap();
|
|
1249
|
+
if let Some(content) = call_result.content.first() {
|
|
1250
|
+
match &content.raw {
|
|
1251
|
+
RawContent::Text(text) => {
|
|
1252
|
+
assert!(text.text.is_empty() || text.text.trim().is_empty());
|
|
1253
|
+
}
|
|
1254
|
+
_ => panic!("Expected text content"),
|
|
1255
|
+
}
|
|
1256
|
+
} else {
|
|
1257
|
+
panic!("Expected content in result");
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
#[tokio::test]
|
|
1262
|
+
async fn test_batch_extract_files_with_invalid_file() {
|
|
1263
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1264
|
+
let params = BatchExtractFilesParams {
|
|
1265
|
+
paths: vec!["/nonexistent/file.pdf".to_string()],
|
|
1266
|
+
enable_ocr: false,
|
|
1267
|
+
force_ocr: false,
|
|
1268
|
+
r#async: true,
|
|
1269
|
+
};
|
|
1270
|
+
|
|
1271
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1272
|
+
|
|
1273
|
+
assert!(result.is_ok() || result.is_err());
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
#[tokio::test]
|
|
1277
|
+
async fn test_detect_mime_type_with_valid_file() {
|
|
1278
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1279
|
+
let params = DetectMimeTypeParams {
|
|
1280
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1281
|
+
use_content: true,
|
|
1282
|
+
};
|
|
1283
|
+
|
|
1284
|
+
let result = server.detect_mime_type(Parameters(params));
|
|
1285
|
+
|
|
1286
|
+
assert!(result.is_ok());
|
|
1287
|
+
let call_result = result.unwrap();
|
|
1288
|
+
if let Some(content) = call_result.content.first() {
|
|
1289
|
+
match &content.raw {
|
|
1290
|
+
RawContent::Text(text) => {
|
|
1291
|
+
assert!(text.text.contains("application/pdf") || text.text.contains("pdf"));
|
|
1292
|
+
}
|
|
1293
|
+
_ => panic!("Expected text content"),
|
|
1294
|
+
}
|
|
1295
|
+
} else {
|
|
1296
|
+
panic!("Expected content in result");
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
#[tokio::test]
|
|
1301
|
+
async fn test_detect_mime_type_without_content_detection() {
|
|
1302
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1303
|
+
let params = DetectMimeTypeParams {
|
|
1304
|
+
path: get_test_path("pdfs_with_tables/tiny.pdf").to_string(),
|
|
1305
|
+
use_content: false,
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
let result = server.detect_mime_type(Parameters(params));
|
|
1309
|
+
|
|
1310
|
+
assert!(result.is_ok());
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
#[tokio::test]
|
|
1314
|
+
async fn test_detect_mime_type_with_invalid_file() {
|
|
1315
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1316
|
+
let params = DetectMimeTypeParams {
|
|
1317
|
+
path: "/nonexistent/file.pdf".to_string(),
|
|
1318
|
+
use_content: true,
|
|
1319
|
+
};
|
|
1320
|
+
|
|
1321
|
+
let result = server.detect_mime_type(Parameters(params));
|
|
1322
|
+
|
|
1323
|
+
assert!(result.is_err());
|
|
1324
|
+
let error = result.unwrap_err();
|
|
1325
|
+
assert!(error.code.0 == -32602 || error.code.0 == -32603);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
#[tokio::test]
|
|
1329
|
+
async fn test_cache_stats_returns_statistics() {
|
|
1330
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1331
|
+
|
|
1332
|
+
let result = server.cache_stats(Parameters(()));
|
|
1333
|
+
|
|
1334
|
+
assert!(result.is_ok());
|
|
1335
|
+
let call_result = result.unwrap();
|
|
1336
|
+
if let Some(content) = call_result.content.first() {
|
|
1337
|
+
match &content.raw {
|
|
1338
|
+
RawContent::Text(text) => {
|
|
1339
|
+
assert!(text.text.contains("Cache Statistics"));
|
|
1340
|
+
assert!(text.text.contains("Directory:"));
|
|
1341
|
+
assert!(text.text.contains("Total files:"));
|
|
1342
|
+
assert!(text.text.contains("Total size:"));
|
|
1343
|
+
assert!(text.text.contains("Available space:"));
|
|
1344
|
+
}
|
|
1345
|
+
_ => panic!("Expected text content"),
|
|
1346
|
+
}
|
|
1347
|
+
} else {
|
|
1348
|
+
panic!("Expected content in result");
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
#[tokio::test]
|
|
1353
|
+
async fn test_cache_clear_returns_result() {
|
|
1354
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1355
|
+
|
|
1356
|
+
let result = server.cache_clear(Parameters(()));
|
|
1357
|
+
|
|
1358
|
+
assert!(result.is_ok());
|
|
1359
|
+
let call_result = result.unwrap();
|
|
1360
|
+
if let Some(content) = call_result.content.first() {
|
|
1361
|
+
match &content.raw {
|
|
1362
|
+
RawContent::Text(text) => {
|
|
1363
|
+
assert!(text.text.contains("Cache cleared"));
|
|
1364
|
+
assert!(text.text.contains("Directory:"));
|
|
1365
|
+
assert!(text.text.contains("Removed files:"));
|
|
1366
|
+
assert!(text.text.contains("Freed space:"));
|
|
1367
|
+
}
|
|
1368
|
+
_ => panic!("Expected text content"),
|
|
1369
|
+
}
|
|
1370
|
+
} else {
|
|
1371
|
+
panic!("Expected content in result");
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
#[test]
|
|
1376
|
+
fn test_new_creates_server_with_default_config() {
|
|
1377
|
+
let server = KreuzbergMcp::new();
|
|
1378
|
+
assert!(server.is_ok());
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
#[test]
|
|
1382
|
+
fn test_default_creates_server_without_panic() {
|
|
1383
|
+
let server = KreuzbergMcp::default();
|
|
1384
|
+
let info = server.get_info();
|
|
1385
|
+
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
#[test]
|
|
1389
|
+
fn test_server_info_has_correct_fields() {
|
|
1390
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1391
|
+
let info = server.get_info();
|
|
1392
|
+
|
|
1393
|
+
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1394
|
+
assert_eq!(
|
|
1395
|
+
info.server_info.title,
|
|
1396
|
+
Some("Kreuzberg Document Intelligence MCP Server".to_string())
|
|
1397
|
+
);
|
|
1398
|
+
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
1399
|
+
assert_eq!(
|
|
1400
|
+
info.server_info.website_url,
|
|
1401
|
+
Some("https://goldziher.github.io/kreuzberg/".to_string())
|
|
1402
|
+
);
|
|
1403
|
+
assert!(info.instructions.is_some());
|
|
1404
|
+
assert!(info.capabilities.tools.is_some());
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
#[test]
|
|
1408
|
+
fn test_build_config_preserves_default_config_settings() {
|
|
1409
|
+
let default_config = ExtractionConfig {
|
|
1410
|
+
use_cache: false,
|
|
1411
|
+
..Default::default()
|
|
1412
|
+
};
|
|
1413
|
+
|
|
1414
|
+
let config = build_config(&default_config, false, false);
|
|
1415
|
+
|
|
1416
|
+
assert!(!config.use_cache);
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
#[test]
|
|
1420
|
+
fn test_build_config_ocr_disabled_by_default() {
|
|
1421
|
+
let default_config = ExtractionConfig::default();
|
|
1422
|
+
|
|
1423
|
+
let config = build_config(&default_config, false, false);
|
|
1424
|
+
|
|
1425
|
+
assert!(config.ocr.is_none());
|
|
1426
|
+
assert!(!config.force_ocr);
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
#[test]
|
|
1430
|
+
fn test_build_config_ocr_enabled_creates_tesseract_config() {
|
|
1431
|
+
let default_config = ExtractionConfig::default();
|
|
1432
|
+
|
|
1433
|
+
let config = build_config(&default_config, true, false);
|
|
1434
|
+
|
|
1435
|
+
assert!(config.ocr.is_some());
|
|
1436
|
+
let ocr_config = config.ocr.unwrap();
|
|
1437
|
+
assert_eq!(ocr_config.backend, "tesseract");
|
|
1438
|
+
assert_eq!(ocr_config.language, "eng");
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
#[test]
|
|
1442
|
+
fn test_extract_file_params_defaults() {
|
|
1443
|
+
let json = r#"{"path": "/test.pdf"}"#;
|
|
1444
|
+
let params: ExtractFileParams = serde_json::from_str(json).unwrap();
|
|
1445
|
+
|
|
1446
|
+
assert_eq!(params.path, "/test.pdf");
|
|
1447
|
+
assert_eq!(params.mime_type, None);
|
|
1448
|
+
assert!(!params.enable_ocr);
|
|
1449
|
+
assert!(!params.force_ocr);
|
|
1450
|
+
assert!(!params.r#async);
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
#[test]
|
|
1454
|
+
fn test_extract_bytes_params_defaults() {
|
|
1455
|
+
let json = r#"{"data": "SGVsbG8="}"#;
|
|
1456
|
+
let params: ExtractBytesParams = serde_json::from_str(json).unwrap();
|
|
1457
|
+
|
|
1458
|
+
assert_eq!(params.data, "SGVsbG8=");
|
|
1459
|
+
assert_eq!(params.mime_type, None);
|
|
1460
|
+
assert!(!params.enable_ocr);
|
|
1461
|
+
assert!(!params.force_ocr);
|
|
1462
|
+
assert!(!params.r#async);
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
#[test]
|
|
1466
|
+
fn test_batch_extract_files_params_defaults() {
|
|
1467
|
+
let json = r#"{"paths": ["/a.pdf", "/b.pdf"]}"#;
|
|
1468
|
+
let params: BatchExtractFilesParams = serde_json::from_str(json).unwrap();
|
|
1469
|
+
|
|
1470
|
+
assert_eq!(params.paths.len(), 2);
|
|
1471
|
+
assert!(!params.enable_ocr);
|
|
1472
|
+
assert!(!params.force_ocr);
|
|
1473
|
+
assert!(!params.r#async);
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
#[test]
|
|
1477
|
+
fn test_detect_mime_type_params_defaults() {
|
|
1478
|
+
let json = r#"{"path": "/test.pdf"}"#;
|
|
1479
|
+
let params: DetectMimeTypeParams = serde_json::from_str(json).unwrap();
|
|
1480
|
+
|
|
1481
|
+
assert_eq!(params.path, "/test.pdf");
|
|
1482
|
+
assert!(params.use_content);
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
#[test]
|
|
1486
|
+
fn test_detect_mime_type_params_use_content_false() {
|
|
1487
|
+
let json = r#"{"path": "/test.pdf", "use_content": false}"#;
|
|
1488
|
+
let params: DetectMimeTypeParams = serde_json::from_str(json).unwrap();
|
|
1489
|
+
|
|
1490
|
+
assert!(!params.use_content);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
#[test]
|
|
1494
|
+
fn test_mcp_server_info_protocol_version() {
|
|
1495
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1496
|
+
let info = server.get_info();
|
|
1497
|
+
|
|
1498
|
+
assert_eq!(info.protocol_version, ProtocolVersion::default());
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
#[test]
|
|
1502
|
+
fn test_mcp_server_info_has_all_required_fields() {
|
|
1503
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1504
|
+
let info = server.get_info();
|
|
1505
|
+
|
|
1506
|
+
assert!(!info.server_info.name.is_empty());
|
|
1507
|
+
assert!(!info.server_info.version.is_empty());
|
|
1508
|
+
|
|
1509
|
+
assert!(info.server_info.title.is_some());
|
|
1510
|
+
assert!(info.server_info.website_url.is_some());
|
|
1511
|
+
assert!(info.instructions.is_some());
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
#[test]
|
|
1515
|
+
fn test_mcp_server_capabilities_declares_tools() {
|
|
1516
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1517
|
+
let info = server.get_info();
|
|
1518
|
+
|
|
1519
|
+
assert!(info.capabilities.tools.is_some());
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
#[test]
|
|
1523
|
+
fn test_mcp_server_name_follows_convention() {
|
|
1524
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1525
|
+
let info = server.get_info();
|
|
1526
|
+
|
|
1527
|
+
assert_eq!(info.server_info.name, "kreuzberg-mcp");
|
|
1528
|
+
assert!(!info.server_info.name.contains('_'));
|
|
1529
|
+
assert!(!info.server_info.name.contains(' '));
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
#[test]
|
|
1533
|
+
fn test_mcp_version_matches_cargo_version() {
|
|
1534
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1535
|
+
let info = server.get_info();
|
|
1536
|
+
|
|
1537
|
+
assert_eq!(info.server_info.version, env!("CARGO_PKG_VERSION"));
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
#[test]
|
|
1541
|
+
fn test_mcp_instructions_are_helpful() {
|
|
1542
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1543
|
+
let info = server.get_info();
|
|
1544
|
+
|
|
1545
|
+
let instructions = info.instructions.expect("Instructions should be present");
|
|
1546
|
+
|
|
1547
|
+
assert!(instructions.contains("extract") || instructions.contains("Extract"));
|
|
1548
|
+
assert!(instructions.contains("OCR") || instructions.contains("ocr"));
|
|
1549
|
+
assert!(instructions.contains("document"));
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
#[tokio::test]
|
|
1553
|
+
async fn test_all_tools_are_registered() {
|
|
1554
|
+
let router = KreuzbergMcp::tool_router();
|
|
1555
|
+
|
|
1556
|
+
let expected_tools = vec![
|
|
1557
|
+
"extract_file",
|
|
1558
|
+
"extract_bytes",
|
|
1559
|
+
"batch_extract_files",
|
|
1560
|
+
"detect_mime_type",
|
|
1561
|
+
"cache_stats",
|
|
1562
|
+
"cache_clear",
|
|
1563
|
+
];
|
|
1564
|
+
|
|
1565
|
+
for tool_name in expected_tools {
|
|
1566
|
+
assert!(router.has_route(tool_name), "Tool '{}' should be registered", tool_name);
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
#[tokio::test]
|
|
1571
|
+
async fn test_tool_count_is_correct() {
|
|
1572
|
+
let router = KreuzbergMcp::tool_router();
|
|
1573
|
+
let tools = router.list_all();
|
|
1574
|
+
|
|
1575
|
+
assert_eq!(tools.len(), 6, "Expected 6 tools, found {}", tools.len());
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
#[tokio::test]
|
|
1579
|
+
async fn test_tools_have_descriptions() {
|
|
1580
|
+
let router = KreuzbergMcp::tool_router();
|
|
1581
|
+
let tools = router.list_all();
|
|
1582
|
+
|
|
1583
|
+
for tool in tools {
|
|
1584
|
+
assert!(
|
|
1585
|
+
tool.description.is_some(),
|
|
1586
|
+
"Tool '{}' should have a description",
|
|
1587
|
+
tool.name
|
|
1588
|
+
);
|
|
1589
|
+
let desc = tool.description.as_ref().unwrap();
|
|
1590
|
+
assert!(!desc.is_empty(), "Tool '{}' description should not be empty", tool.name);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
#[tokio::test]
|
|
1595
|
+
async fn test_extract_file_tool_has_correct_schema() {
|
|
1596
|
+
let router = KreuzbergMcp::tool_router();
|
|
1597
|
+
let tools = router.list_all();
|
|
1598
|
+
|
|
1599
|
+
let extract_file_tool = tools
|
|
1600
|
+
.iter()
|
|
1601
|
+
.find(|t| t.name == "extract_file")
|
|
1602
|
+
.expect("extract_file tool should exist");
|
|
1603
|
+
|
|
1604
|
+
assert!(extract_file_tool.description.is_some());
|
|
1605
|
+
|
|
1606
|
+
assert!(!extract_file_tool.input_schema.is_empty());
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
#[tokio::test]
|
|
1610
|
+
async fn test_all_tools_have_input_schemas() {
|
|
1611
|
+
let router = KreuzbergMcp::tool_router();
|
|
1612
|
+
let tools = router.list_all();
|
|
1613
|
+
|
|
1614
|
+
for tool in tools {
|
|
1615
|
+
assert!(
|
|
1616
|
+
!tool.input_schema.is_empty(),
|
|
1617
|
+
"Tool '{}' should have an input schema with fields",
|
|
1618
|
+
tool.name
|
|
1619
|
+
);
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
#[test]
|
|
1624
|
+
fn test_server_creation_with_custom_config() {
|
|
1625
|
+
let custom_config = ExtractionConfig {
|
|
1626
|
+
force_ocr: true,
|
|
1627
|
+
use_cache: false,
|
|
1628
|
+
ocr: Some(crate::OcrConfig {
|
|
1629
|
+
backend: "tesseract".to_string(),
|
|
1630
|
+
language: "spa".to_string(),
|
|
1631
|
+
tesseract_config: None,
|
|
1632
|
+
}),
|
|
1633
|
+
..Default::default()
|
|
1634
|
+
};
|
|
1635
|
+
|
|
1636
|
+
let server = KreuzbergMcp::with_config(custom_config.clone());
|
|
1637
|
+
|
|
1638
|
+
assert_eq!(server.default_config.force_ocr, custom_config.force_ocr);
|
|
1639
|
+
assert_eq!(server.default_config.use_cache, custom_config.use_cache);
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
#[test]
|
|
1643
|
+
fn test_server_clone_preserves_config() {
|
|
1644
|
+
let custom_config = ExtractionConfig {
|
|
1645
|
+
force_ocr: true,
|
|
1646
|
+
..Default::default()
|
|
1647
|
+
};
|
|
1648
|
+
|
|
1649
|
+
let server1 = KreuzbergMcp::with_config(custom_config);
|
|
1650
|
+
let server2 = server1.clone();
|
|
1651
|
+
|
|
1652
|
+
assert_eq!(server1.default_config.force_ocr, server2.default_config.force_ocr);
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
#[tokio::test]
|
|
1656
|
+
async fn test_extract_bytes_with_empty_data() {
|
|
1657
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1658
|
+
|
|
1659
|
+
let params = ExtractBytesParams {
|
|
1660
|
+
data: String::new(),
|
|
1661
|
+
mime_type: Some("text/plain".to_string()),
|
|
1662
|
+
enable_ocr: false,
|
|
1663
|
+
force_ocr: false,
|
|
1664
|
+
r#async: true,
|
|
1665
|
+
};
|
|
1666
|
+
|
|
1667
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1668
|
+
|
|
1669
|
+
assert!(result.is_ok() || result.is_err());
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
#[tokio::test]
|
|
1673
|
+
async fn test_extract_bytes_with_valid_pdf_bytes() {
|
|
1674
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1675
|
+
|
|
1676
|
+
let pdf_path = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1677
|
+
|
|
1678
|
+
if std::path::Path::new(&pdf_path).exists() {
|
|
1679
|
+
let pdf_bytes = std::fs::read(&pdf_path).unwrap();
|
|
1680
|
+
let encoded = BASE64_STANDARD.encode(&pdf_bytes);
|
|
1681
|
+
|
|
1682
|
+
let params = ExtractBytesParams {
|
|
1683
|
+
data: encoded,
|
|
1684
|
+
mime_type: Some("application/pdf".to_string()),
|
|
1685
|
+
enable_ocr: false,
|
|
1686
|
+
force_ocr: false,
|
|
1687
|
+
r#async: true,
|
|
1688
|
+
};
|
|
1689
|
+
|
|
1690
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1691
|
+
|
|
1692
|
+
assert!(result.is_ok(), "PDF bytes extraction should succeed");
|
|
1693
|
+
let call_result = result.unwrap();
|
|
1694
|
+
assert!(!call_result.content.is_empty());
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1698
|
+
#[tokio::test]
|
|
1699
|
+
async fn test_extract_bytes_mime_type_auto_detection() {
|
|
1700
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1701
|
+
|
|
1702
|
+
let text_content = b"Plain text content for testing";
|
|
1703
|
+
let encoded = BASE64_STANDARD.encode(text_content);
|
|
1704
|
+
|
|
1705
|
+
let params = ExtractBytesParams {
|
|
1706
|
+
data: encoded,
|
|
1707
|
+
mime_type: None,
|
|
1708
|
+
enable_ocr: false,
|
|
1709
|
+
force_ocr: false,
|
|
1710
|
+
r#async: true,
|
|
1711
|
+
};
|
|
1712
|
+
|
|
1713
|
+
let result = server.extract_bytes(Parameters(params)).await;
|
|
1714
|
+
|
|
1715
|
+
assert!(result.is_ok() || result.is_err());
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
#[tokio::test]
|
|
1719
|
+
async fn test_batch_extract_preserves_file_order() {
|
|
1720
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1721
|
+
|
|
1722
|
+
let file1 = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1723
|
+
let file2 = get_test_path("pdfs_with_tables/medium.pdf");
|
|
1724
|
+
|
|
1725
|
+
if std::path::Path::new(&file1).exists() && std::path::Path::new(&file2).exists() {
|
|
1726
|
+
let params = BatchExtractFilesParams {
|
|
1727
|
+
paths: vec![file1.to_string(), file2.to_string()],
|
|
1728
|
+
enable_ocr: false,
|
|
1729
|
+
force_ocr: false,
|
|
1730
|
+
r#async: true,
|
|
1731
|
+
};
|
|
1732
|
+
|
|
1733
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
1734
|
+
|
|
1735
|
+
if let Ok(call_result) = result
|
|
1736
|
+
&& let Some(content) = call_result.content.first()
|
|
1737
|
+
&& let RawContent::Text(text) = &content.raw
|
|
1738
|
+
{
|
|
1739
|
+
assert!(text.text.contains("Document 1"));
|
|
1740
|
+
assert!(text.text.contains("Document 2"));
|
|
1741
|
+
|
|
1742
|
+
let doc1_pos = text.text.find("Document 1");
|
|
1743
|
+
let doc2_pos = text.text.find("Document 2");
|
|
1744
|
+
if let (Some(pos1), Some(pos2)) = (doc1_pos, doc2_pos) {
|
|
1745
|
+
assert!(pos1 < pos2, "Documents should be in order");
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
#[tokio::test]
|
|
1752
|
+
async fn test_cache_clear_is_idempotent() {
|
|
1753
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1754
|
+
|
|
1755
|
+
let result1 = server.cache_clear(Parameters(()));
|
|
1756
|
+
assert!(result1.is_ok());
|
|
1757
|
+
|
|
1758
|
+
let result2 = server.cache_clear(Parameters(()));
|
|
1759
|
+
assert!(result2.is_ok());
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
#[tokio::test]
|
|
1763
|
+
async fn test_cache_clear_returns_metrics() {
|
|
1764
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1765
|
+
|
|
1766
|
+
let result = server.cache_clear(Parameters(()));
|
|
1767
|
+
|
|
1768
|
+
assert!(result.is_ok());
|
|
1769
|
+
let call_result = result.unwrap();
|
|
1770
|
+
if let Some(content) = call_result.content.first()
|
|
1771
|
+
&& let RawContent::Text(text) = &content.raw
|
|
1772
|
+
{
|
|
1773
|
+
assert!(text.text.contains("Removed files:"));
|
|
1774
|
+
assert!(text.text.contains("Freed space:"));
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
#[test]
|
|
1779
|
+
fn test_error_mapping_preserves_error_context() {
|
|
1780
|
+
let validation_error = KreuzbergError::validation("invalid file path");
|
|
1781
|
+
let mcp_error = map_kreuzberg_error_to_mcp(validation_error);
|
|
1782
|
+
|
|
1783
|
+
assert!(mcp_error.message.contains("invalid file path"));
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
#[test]
|
|
1787
|
+
fn test_io_errors_bubble_up_as_internal() {
|
|
1788
|
+
// OSError/RuntimeError must bubble up - system errors need user reports ~keep
|
|
1789
|
+
let io_error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
|
|
1790
|
+
let kreuzberg_error = KreuzbergError::Io(io_error);
|
|
1791
|
+
let mcp_error = map_kreuzberg_error_to_mcp(kreuzberg_error);
|
|
1792
|
+
|
|
1793
|
+
assert_eq!(mcp_error.code.0, -32603);
|
|
1794
|
+
assert!(mcp_error.message.contains("System I/O error"));
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
#[test]
|
|
1798
|
+
fn test_all_error_variants_have_mappings() {
|
|
1799
|
+
let errors = vec![
|
|
1800
|
+
KreuzbergError::validation("test"),
|
|
1801
|
+
KreuzbergError::UnsupportedFormat("test/unknown".to_string()),
|
|
1802
|
+
KreuzbergError::MissingDependency("test-dep".to_string()),
|
|
1803
|
+
KreuzbergError::parsing("test"),
|
|
1804
|
+
KreuzbergError::Io(std::io::Error::other("test")),
|
|
1805
|
+
KreuzbergError::ocr("test"),
|
|
1806
|
+
KreuzbergError::cache("test"),
|
|
1807
|
+
KreuzbergError::image_processing("test"),
|
|
1808
|
+
KreuzbergError::serialization("test"),
|
|
1809
|
+
KreuzbergError::Plugin {
|
|
1810
|
+
message: "test".to_string(),
|
|
1811
|
+
plugin_name: "test-plugin".to_string(),
|
|
1812
|
+
},
|
|
1813
|
+
KreuzbergError::LockPoisoned("test".to_string()),
|
|
1814
|
+
KreuzbergError::Other("test".to_string()),
|
|
1815
|
+
];
|
|
1816
|
+
|
|
1817
|
+
for error in errors {
|
|
1818
|
+
let mcp_error = map_kreuzberg_error_to_mcp(error);
|
|
1819
|
+
|
|
1820
|
+
assert!(mcp_error.code.0 < 0, "Error code should be negative");
|
|
1821
|
+
|
|
1822
|
+
assert!(!mcp_error.message.is_empty());
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
#[tokio::test]
|
|
1827
|
+
async fn test_response_includes_metadata() {
|
|
1828
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1829
|
+
|
|
1830
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1831
|
+
|
|
1832
|
+
if std::path::Path::new(&test_file).exists() {
|
|
1833
|
+
let params = ExtractFileParams {
|
|
1834
|
+
path: test_file.to_string(),
|
|
1835
|
+
mime_type: None,
|
|
1836
|
+
enable_ocr: false,
|
|
1837
|
+
force_ocr: false,
|
|
1838
|
+
r#async: true,
|
|
1839
|
+
};
|
|
1840
|
+
|
|
1841
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1842
|
+
|
|
1843
|
+
assert!(result.is_ok());
|
|
1844
|
+
let call_result = result.unwrap();
|
|
1845
|
+
|
|
1846
|
+
if let Some(content) = call_result.content.first()
|
|
1847
|
+
&& let RawContent::Text(text) = &content.raw
|
|
1848
|
+
{
|
|
1849
|
+
assert!(text.text.contains("Metadata:"));
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
#[tokio::test]
|
|
1855
|
+
async fn test_response_includes_content_length() {
|
|
1856
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1857
|
+
|
|
1858
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1859
|
+
|
|
1860
|
+
if std::path::Path::new(&test_file).exists() {
|
|
1861
|
+
let params = ExtractFileParams {
|
|
1862
|
+
path: test_file.to_string(),
|
|
1863
|
+
mime_type: None,
|
|
1864
|
+
enable_ocr: false,
|
|
1865
|
+
force_ocr: false,
|
|
1866
|
+
r#async: true,
|
|
1867
|
+
};
|
|
1868
|
+
|
|
1869
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1870
|
+
|
|
1871
|
+
assert!(result.is_ok());
|
|
1872
|
+
let call_result = result.unwrap();
|
|
1873
|
+
|
|
1874
|
+
if let Some(content) = call_result.content.first()
|
|
1875
|
+
&& let RawContent::Text(text) = &content.raw
|
|
1876
|
+
{
|
|
1877
|
+
assert!(text.text.contains("characters"));
|
|
1878
|
+
assert!(text.text.contains("Content"));
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
#[tokio::test]
|
|
1884
|
+
async fn test_server_is_thread_safe() {
|
|
1885
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1886
|
+
|
|
1887
|
+
let server1 = server.clone();
|
|
1888
|
+
let server2 = server.clone();
|
|
1889
|
+
|
|
1890
|
+
let handle1 = tokio::spawn(async move { server1.get_info() });
|
|
1891
|
+
|
|
1892
|
+
let handle2 = tokio::spawn(async move { server2.get_info() });
|
|
1893
|
+
|
|
1894
|
+
let info1 = handle1.await.unwrap();
|
|
1895
|
+
let info2 = handle2.await.unwrap();
|
|
1896
|
+
|
|
1897
|
+
assert_eq!(info1.server_info.name, info2.server_info.name);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
#[test]
|
|
1901
|
+
fn test_extract_file_params_serialization() {
|
|
1902
|
+
let params = ExtractFileParams {
|
|
1903
|
+
path: "/test.pdf".to_string(),
|
|
1904
|
+
mime_type: Some("application/pdf".to_string()),
|
|
1905
|
+
enable_ocr: true,
|
|
1906
|
+
force_ocr: false,
|
|
1907
|
+
r#async: true,
|
|
1908
|
+
};
|
|
1909
|
+
|
|
1910
|
+
let json = serde_json::to_string(¶ms).unwrap();
|
|
1911
|
+
let deserialized: ExtractFileParams = serde_json::from_str(&json).unwrap();
|
|
1912
|
+
|
|
1913
|
+
assert_eq!(params.path, deserialized.path);
|
|
1914
|
+
assert_eq!(params.mime_type, deserialized.mime_type);
|
|
1915
|
+
assert_eq!(params.enable_ocr, deserialized.enable_ocr);
|
|
1916
|
+
assert_eq!(params.force_ocr, deserialized.force_ocr);
|
|
1917
|
+
assert_eq!(params.r#async, deserialized.r#async);
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
#[test]
|
|
1921
|
+
fn test_extract_bytes_params_serialization() {
|
|
1922
|
+
let params = ExtractBytesParams {
|
|
1923
|
+
data: "SGVsbG8=".to_string(),
|
|
1924
|
+
mime_type: None,
|
|
1925
|
+
enable_ocr: false,
|
|
1926
|
+
force_ocr: false,
|
|
1927
|
+
r#async: false,
|
|
1928
|
+
};
|
|
1929
|
+
|
|
1930
|
+
let json = serde_json::to_string(¶ms).unwrap();
|
|
1931
|
+
let deserialized: ExtractBytesParams = serde_json::from_str(&json).unwrap();
|
|
1932
|
+
|
|
1933
|
+
assert_eq!(params.data, deserialized.data);
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
#[test]
|
|
1937
|
+
fn test_batch_extract_params_serialization() {
|
|
1938
|
+
let params = BatchExtractFilesParams {
|
|
1939
|
+
paths: vec!["/a.pdf".to_string(), "/b.pdf".to_string()],
|
|
1940
|
+
enable_ocr: true,
|
|
1941
|
+
force_ocr: true,
|
|
1942
|
+
r#async: true,
|
|
1943
|
+
};
|
|
1944
|
+
|
|
1945
|
+
let json = serde_json::to_string(¶ms).unwrap();
|
|
1946
|
+
let deserialized: BatchExtractFilesParams = serde_json::from_str(&json).unwrap();
|
|
1947
|
+
|
|
1948
|
+
assert_eq!(params.paths, deserialized.paths);
|
|
1949
|
+
assert_eq!(params.enable_ocr, deserialized.enable_ocr);
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
#[test]
|
|
1953
|
+
fn test_detect_mime_type_params_serialization() {
|
|
1954
|
+
let params = DetectMimeTypeParams {
|
|
1955
|
+
path: "/test.pdf".to_string(),
|
|
1956
|
+
use_content: false,
|
|
1957
|
+
};
|
|
1958
|
+
|
|
1959
|
+
let json = serde_json::to_string(¶ms).unwrap();
|
|
1960
|
+
let deserialized: DetectMimeTypeParams = serde_json::from_str(&json).unwrap();
|
|
1961
|
+
|
|
1962
|
+
assert_eq!(params.path, deserialized.path);
|
|
1963
|
+
assert_eq!(params.use_content, deserialized.use_content);
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
#[tokio::test]
|
|
1967
|
+
async fn test_extract_file_respects_custom_default_config() {
|
|
1968
|
+
let custom_config = ExtractionConfig {
|
|
1969
|
+
use_cache: false,
|
|
1970
|
+
..Default::default()
|
|
1971
|
+
};
|
|
1972
|
+
|
|
1973
|
+
let server = KreuzbergMcp::with_config(custom_config);
|
|
1974
|
+
|
|
1975
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1976
|
+
|
|
1977
|
+
if std::path::Path::new(&test_file).exists() {
|
|
1978
|
+
let params = ExtractFileParams {
|
|
1979
|
+
path: test_file.to_string(),
|
|
1980
|
+
mime_type: None,
|
|
1981
|
+
enable_ocr: false,
|
|
1982
|
+
force_ocr: false,
|
|
1983
|
+
r#async: true,
|
|
1984
|
+
};
|
|
1985
|
+
|
|
1986
|
+
let result = server.extract_file(Parameters(params)).await;
|
|
1987
|
+
|
|
1988
|
+
assert!(result.is_ok() || result.is_err());
|
|
1989
|
+
}
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
#[tokio::test]
|
|
1993
|
+
async fn test_batch_extract_with_single_file() {
|
|
1994
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
1995
|
+
|
|
1996
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
1997
|
+
|
|
1998
|
+
if std::path::Path::new(&test_file).exists() {
|
|
1999
|
+
let params = BatchExtractFilesParams {
|
|
2000
|
+
paths: vec![test_file.to_string()],
|
|
2001
|
+
enable_ocr: false,
|
|
2002
|
+
force_ocr: false,
|
|
2003
|
+
r#async: true,
|
|
2004
|
+
};
|
|
2005
|
+
|
|
2006
|
+
let result = server.batch_extract_files(Parameters(params)).await;
|
|
2007
|
+
|
|
2008
|
+
assert!(result.is_ok());
|
|
2009
|
+
let call_result = result.unwrap();
|
|
2010
|
+
assert!(!call_result.content.is_empty());
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
|
|
2014
|
+
#[tokio::test]
|
|
2015
|
+
async fn test_detect_mime_type_with_extension_only() {
|
|
2016
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2017
|
+
|
|
2018
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
2019
|
+
|
|
2020
|
+
if std::path::Path::new(&test_file).exists() {
|
|
2021
|
+
let params = DetectMimeTypeParams {
|
|
2022
|
+
path: test_file.to_string(),
|
|
2023
|
+
use_content: false,
|
|
2024
|
+
};
|
|
2025
|
+
|
|
2026
|
+
let result = server.detect_mime_type(Parameters(params));
|
|
2027
|
+
|
|
2028
|
+
assert!(result.is_ok());
|
|
2029
|
+
let call_result = result.unwrap();
|
|
2030
|
+
if let Some(content) = call_result.content.first()
|
|
2031
|
+
&& let RawContent::Text(text) = &content.raw
|
|
2032
|
+
{
|
|
2033
|
+
assert!(text.text.contains("pdf") || text.text.contains("PDF"));
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
#[tokio::test]
|
|
2039
|
+
async fn test_detect_mime_type_with_content_analysis() {
|
|
2040
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2041
|
+
|
|
2042
|
+
let test_file = get_test_path("pdfs_with_tables/tiny.pdf");
|
|
2043
|
+
|
|
2044
|
+
if std::path::Path::new(&test_file).exists() {
|
|
2045
|
+
let params = DetectMimeTypeParams {
|
|
2046
|
+
path: test_file.to_string(),
|
|
2047
|
+
use_content: true,
|
|
2048
|
+
};
|
|
2049
|
+
|
|
2050
|
+
let result = server.detect_mime_type(Parameters(params));
|
|
2051
|
+
|
|
2052
|
+
assert!(result.is_ok());
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
#[tokio::test]
|
|
2057
|
+
async fn test_cache_stats_returns_valid_data() {
|
|
2058
|
+
let server = KreuzbergMcp::with_config(ExtractionConfig::default());
|
|
2059
|
+
|
|
2060
|
+
let result = server.cache_stats(Parameters(()));
|
|
2061
|
+
|
|
2062
|
+
assert!(result.is_ok());
|
|
2063
|
+
let call_result = result.unwrap();
|
|
2064
|
+
if let Some(content) = call_result.content.first()
|
|
2065
|
+
&& let RawContent::Text(text) = &content.raw
|
|
2066
|
+
{
|
|
2067
|
+
assert!(text.text.contains("Cache Statistics"));
|
|
2068
|
+
assert!(text.text.contains("Directory:"));
|
|
2069
|
+
assert!(text.text.contains("Total files:"));
|
|
2070
|
+
assert!(text.text.contains("Total size:"));
|
|
2071
|
+
assert!(text.text.contains("Available space:"));
|
|
2072
|
+
assert!(text.text.contains("Oldest file age:"));
|
|
2073
|
+
assert!(text.text.contains("Newest file age:"));
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
}
|