package mcp-kit
All checked builds pass sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
OCaml library for building MCP servers
Install
dune-project
Dependency
Authors
Maintainers
Sources
ocaml-mcp-v0.2.0.tar.gz
sha256=43c8319a13be91e323ae952a71ab673533fb3295886029eed9d81e5828de5ef7
doc/src/mcp-kit.jsonschema-validation/kw_object.ml.html
Source file kw_object.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831(** Keywords: properties, patternProperties, additionalProperties, maxProperties, minProperties, required, dependencies, propertyNames (spec §6.5). *) open Jsonschema_core let compile_properties : Compiler.keyword_compiler = { keyword = "properties"; compile = (fun ctx value -> match value with | `Assoc prop_schemas -> let compiled = List.map (fun (name, schema) -> ( name, ctx.compile_sub ~schema_path:(Json_pointer.append ctx.schema_path name) schema )) prop_schemas in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, v) -> match List.assoc_opt name pairs with | Some value -> v value (Json_pointer.append path name) | None -> []) compiled | _ -> []) | _ -> None); } let compile_draft03_properties : Compiler.keyword_compiler = { keyword = "properties"; compile = (fun ctx value -> match value with | `Assoc prop_schemas -> let compiled = List.map (fun (name, schema) -> let required = match schema with | `Assoc pairs -> ( match List.assoc_opt "required" pairs with | Some (`Bool true) -> true | _ -> false) | _ -> false in ( name, required, ctx.compile_sub ~schema_path:(Json_pointer.append ctx.schema_path name) schema )) prop_schemas in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, required, v) -> match List.assoc_opt name pairs with | Some value -> v value (Json_pointer.append path name) | None when required -> [ Error.make ~instance_path:path ~schema_path: (Json_pointer.append ctx.schema_path name) ~keyword:"required" (Printf.sprintf "missing required property: %s" name); ] | None -> []) compiled | _ -> []) | _ -> None); } let compile_pattern_properties : Compiler.keyword_compiler = { keyword = "patternProperties"; compile = (fun ctx value -> match value with | `Assoc pat_schemas -> let compiled = List.filter_map (fun (pattern, schema) -> match Regex.compile pattern with | Ok re -> Some ( re, ctx.compile_sub ~schema_path: (Json_pointer.append ctx.schema_path pattern) schema ) | Error _ -> None) pat_schemas in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, value) -> List.concat_map (fun (re, v) -> if Regex.matches re name then v value (Json_pointer.append path name) else []) compiled) pairs | _ -> []) | _ -> None); } let compile_additional_properties : Compiler.keyword_compiler = { keyword = "additionalProperties"; compile = (fun ctx value -> (* Collect property names from "properties" *) let prop_names = match Schema.keyword "properties" (Schema.Obj ctx.schema_obj) with | Some (`Assoc pairs) -> List.map fst pairs | _ -> [] in (* Collect patterns from "patternProperties" *) let patterns = match Schema.keyword "patternProperties" (Schema.Obj ctx.schema_obj) with | Some (`Assoc pairs) -> List.filter_map (fun (pat, _) -> match Regex.compile pat with | Ok re -> Some re | Error _ -> None) pairs | _ -> [] in let is_additional name = let matched_by_props = List.mem name prop_names in let matched_by_patterns = List.exists (fun re -> Regex.matches re name) patterns in not (matched_by_props || matched_by_patterns) in match value with | `Bool false -> Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, _) -> if is_additional name then [ Error.make ~instance_path:(Json_pointer.append path name) ~schema_path:ctx.schema_path ~keyword:"additionalProperties" "additional property not allowed"; ] else []) pairs | _ -> []) | `Bool true -> None | _ -> let sub_v = ctx.compile_sub value in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, v) -> if is_additional name then sub_v v (Json_pointer.append path name) else []) pairs | _ -> [])); } let compile_max_properties : Compiler.keyword_compiler = { keyword = "maxProperties"; compile = (fun ctx value -> match Util.to_nonneg_int_bound value with | Some bound -> Some (fun instance path -> match instance with | `Assoc pairs when Util.actual_greater_than_bound (List.length pairs) bound -> [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"maxProperties" "too many properties"; ] | _ -> []) | None -> None); } let compile_min_properties : Compiler.keyword_compiler = { keyword = "minProperties"; compile = (fun ctx value -> match Util.to_nonneg_int_bound value with | Some bound -> Some (fun instance path -> match instance with | `Assoc pairs when Util.actual_less_than_bound (List.length pairs) bound -> [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"minProperties" "too few properties"; ] | _ -> []) | None -> None); } let compile_required : Compiler.keyword_compiler = { keyword = "required"; compile = (fun ctx value -> match value with | `List names -> let required = List.filter_map (function `String s -> Some s | _ -> None) names in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun name -> if List.mem_assoc name pairs then [] else [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"required" (Printf.sprintf "missing required property: %s" name); ]) required | _ -> []) | _ -> None); } let compile_dependencies : Compiler.keyword_compiler = { keyword = "dependencies"; compile = (fun ctx value -> match value with | `Assoc deps -> let compiled = List.map (fun (prop, dep) -> match dep with | `List required_props -> (* Property dependency *) let names = List.filter_map (function `String s -> Some s | _ -> None) required_props in (prop, `Props names) | _ -> (* Schema dependency *) ( prop, `Schema (ctx.compile_sub ~schema_path: (Json_pointer.append ctx.schema_path prop) dep) )) deps in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (prop, dep) -> if not (List.mem_assoc prop pairs) then [] else match dep with | `Props required -> List.concat_map (fun name -> if List.mem_assoc name pairs then [] else [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"dependencies" (Printf.sprintf "%s requires %s" prop name); ]) required | `Schema v -> v instance path) compiled | _ -> []) | _ -> None); } let compile_draft03_dependencies : Compiler.keyword_compiler = { keyword = "dependencies"; compile = (fun ctx value -> match value with | `Assoc deps -> let compiled = List.map (fun (prop, dep) -> match dep with | `String name -> (prop, `Props [ name ]) | `List required_props -> let names = List.filter_map (function `String s -> Some s | _ -> None) required_props in (prop, `Props names) | _ -> ( prop, `Schema (ctx.compile_sub ~schema_path: (Json_pointer.append ctx.schema_path prop) dep) )) deps in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (prop, dep) -> if not (List.mem_assoc prop pairs) then [] else match dep with | `Props required -> List.concat_map (fun name -> if List.mem_assoc name pairs then [] else [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"dependencies" (Printf.sprintf "%s requires %s" prop name); ]) required | `Schema v -> v instance path) compiled | _ -> []) | _ -> None); } let compile_property_names : Compiler.keyword_compiler = { keyword = "propertyNames"; compile = (fun ctx value -> let sub_v = ctx.compile_sub value in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (name, _) -> sub_v (`String name) (Json_pointer.append path name)) pairs | _ -> [])); } let compile_dependent_required : Compiler.keyword_compiler = { keyword = "dependentRequired"; compile = (fun ctx value -> match value with | `Assoc deps -> let compiled = List.filter_map (fun (prop, dep) -> match dep with | `List required_props -> let names = List.filter_map (function `String s -> Some s | _ -> None) required_props in Some (prop, names) | _ -> None) deps in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (prop, required) -> if not (List.mem_assoc prop pairs) then [] else List.concat_map (fun name -> if List.mem_assoc name pairs then [] else [ Error.make ~instance_path:path ~schema_path:ctx.schema_path ~keyword:"dependentRequired" (Printf.sprintf "%s requires %s" prop name); ]) required) compiled | _ -> []) | _ -> None); } let compile_dependent_schemas : Compiler.keyword_compiler = { keyword = "dependentSchemas"; compile = (fun ctx value -> match value with | `Assoc deps -> let compiled = List.map (fun (prop, schema) -> ( prop, ctx.compile_sub ~schema_path:(Json_pointer.append ctx.schema_path prop) schema )) deps in Some (fun instance path -> match instance with | `Assoc pairs -> List.concat_map (fun (prop, sub_v) -> if List.mem_assoc prop pairs then sub_v instance path else []) compiled | _ -> []) | _ -> None); } let add_unique x xs = if List.mem x xs then xs else x :: xs let union_names xs ys = List.fold_left (fun acc x -> add_unique x acc) xs ys let schema_local_base (ctx : Compiler.context) base_uri pairs = match List.assoc_opt ctx.config.id_keyword pairs with | Some (`String id_str) -> let id_uri = Uri.of_string id_str in let resolved = Uri_util.resolve ~base:base_uri id_uri in if Uri_util.fragment id_uri <> None && Uri.path id_uri = "" then base_uri else Uri_util.without_fragment resolved | _ -> base_uri let compile_at (ctx : Compiler.context) base_uri schema = Compiler.compile ctx.config ctx.registry ~base_uri schema let schema_valid_at (ctx : Compiler.context) base_uri schema instance path = let v = compile_at ctx base_uri schema in v instance path = [] let dynamic_ref_plain_name ref_str = let uri = Uri.of_string ref_str in match Uri_util.fragment uri with | Some name when name <> "" && name.[0] <> '/' -> Some name | _ -> None let resolve_ref_uri (ctx : Compiler.context) base_uri keyword ref_str = match (keyword, ref_str, ctx.config.recursive_ref_target) with | "$recursiveRef", "#", Some target -> Uri.with_fragment target (Some "") | "$dynamicRef", _, _ when ctx.config.dynamic_ref_enabled -> ( let ref_uri = Uri.of_string ref_str in let resolved = Uri_util.resolve ~base:base_uri ref_uri in match dynamic_ref_plain_name ref_str with | Some name when Registry.is_dynamic_anchor_uri ctx.registry resolved -> ( match List.assoc_opt name ctx.config.dynamic_ref_targets with | Some target -> target | None -> resolved) | _ -> resolved) | _ -> let ref_uri = Uri.of_string ref_str in Uri_util.resolve ~base:base_uri ref_uri let rec evaluated_properties_of_schema ?(include_unevaluated = true) ?(depth = 32) (ctx : Compiler.context) base_uri schema instance path = if depth <= 0 then [] else match (schema, instance) with | `Assoc schema_pairs, `Assoc instance_pairs -> let base_uri = schema_local_base ctx base_uri schema_pairs in let collect_from_ref keyword acc = match List.assoc_opt keyword schema_pairs with | Some (`String ref_str) -> ( let resolved = resolve_ref_uri ctx base_uri keyword ref_str in match Registry.resolve_with_base ctx.registry resolved with | Some (target_schema, target_base) when schema_valid_at ctx target_base target_schema instance path -> union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx target_base target_schema instance path) | _ -> acc) | _ -> acc in let from_refs = [] |> collect_from_ref "$ref" |> collect_from_ref "$recursiveRef" |> collect_from_ref "$dynamicRef" in let property_names, property_annotations = match List.assoc_opt "properties" schema_pairs with | Some (`Assoc props) -> let names = List.map fst props in let annotations = List.fold_left (fun acc (name, prop_schema) -> match List.assoc_opt name instance_pairs with | Some prop_value when schema_valid_at ctx base_uri prop_schema prop_value (Json_pointer.append path name) -> add_unique name acc | _ -> acc) [] props in (names, annotations) | _ -> ([], []) in let pattern_entries = match List.assoc_opt "patternProperties" schema_pairs with | Some (`Assoc entries) -> List.filter_map (fun (pattern, prop_schema) -> match Regex.compile pattern with | Ok re -> Some (re, prop_schema) | Error _ -> None) entries | _ -> [] in let pattern_annotations = List.fold_left (fun acc (name, prop_value) -> List.fold_left (fun acc (re, prop_schema) -> if Regex.matches re name && schema_valid_at ctx base_uri prop_schema prop_value (Json_pointer.append path name) then add_unique name acc else acc) acc pattern_entries) [] instance_pairs in let matched_by_known_applicators name = List.mem name property_names || List.exists (fun (re, _) -> Regex.matches re name) pattern_entries in let additional_annotations = match List.assoc_opt "additionalProperties" schema_pairs with | Some (`Bool true) -> List.fold_left (fun acc (name, _) -> if matched_by_known_applicators name then acc else add_unique name acc) [] instance_pairs | Some (`Bool false) -> [] | Some addl_schema -> List.fold_left (fun acc (name, prop_value) -> if matched_by_known_applicators name then acc else if schema_valid_at ctx base_uri addl_schema prop_value (Json_pointer.append path name) then add_unique name acc else acc) [] instance_pairs | _ -> [] in let collect_schema_map keyword acc = match List.assoc_opt keyword schema_pairs with | Some (`Assoc deps) -> List.fold_left (fun acc (trigger, sub_schema) -> if List.mem_assoc trigger instance_pairs && schema_valid_at ctx base_uri sub_schema instance path then union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri sub_schema instance path) else acc) acc deps | _ -> acc in let collect_schema_array keyword acc = match List.assoc_opt keyword schema_pairs with | Some (`List schemas) -> List.fold_left (fun acc sub_schema -> if schema_valid_at ctx base_uri sub_schema instance path then union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri sub_schema instance path) else acc) acc schemas | _ -> acc in let collect_one_of acc = match List.assoc_opt "oneOf" schema_pairs with | Some (`List schemas) -> ( let passing = List.filter (fun sub_schema -> schema_valid_at ctx base_uri sub_schema instance path) schemas in match passing with | [ sub_schema ] -> union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri sub_schema instance path) | _ -> acc) | _ -> acc in let collect_conditionals acc = match List.assoc_opt "if" schema_pairs with | Some if_schema -> ( if schema_valid_at ctx base_uri if_schema instance path then let acc = union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri if_schema instance path) in match List.assoc_opt "then" schema_pairs with | Some then_schema when schema_valid_at ctx base_uri then_schema instance path -> union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri then_schema instance path) | _ -> acc else match List.assoc_opt "else" schema_pairs with | Some else_schema when schema_valid_at ctx base_uri else_schema instance path -> union_names acc (evaluated_properties_of_schema ~include_unevaluated:true ~depth:(depth - 1) ctx base_uri else_schema instance path) | _ -> acc) | None -> acc in let annotations = [] |> union_names from_refs |> union_names property_annotations |> union_names pattern_annotations |> union_names additional_annotations |> collect_schema_map "dependentSchemas" |> collect_schema_array "allOf" |> collect_schema_array "anyOf" |> collect_one_of |> collect_conditionals in if include_unevaluated then match List.assoc_opt "unevaluatedProperties" schema_pairs with | Some (`Bool true) -> List.fold_left (fun acc (name, _) -> add_unique name acc) annotations instance_pairs | Some (`Assoc _) as unevaluated -> ( match unevaluated with | Some unevaluated_schema -> List.fold_left (fun acc (name, prop_value) -> if List.mem name annotations then acc else if schema_valid_at ctx base_uri unevaluated_schema prop_value (Json_pointer.append path name) then add_unique name acc else acc) annotations instance_pairs | _ -> annotations) | _ -> annotations else annotations | _ -> [] let compile_unevaluated_properties_simple : Compiler.keyword_compiler = { keyword = "unevaluatedProperties"; compile = (fun ctx value -> match value with | `Bool true -> None | `Bool false -> Some (fun instance path -> match instance with | `Assoc pairs -> let evaluated = evaluated_properties_of_schema ~include_unevaluated:false ctx ctx.base_uri ctx.schema_obj instance path in List.concat_map (fun (name, _) -> if List.mem name evaluated then [] else [ Error.make ~instance_path:(Json_pointer.append path name) ~schema_path:ctx.schema_path ~keyword:"unevaluatedProperties" "unevaluated property not allowed"; ]) pairs | _ -> []) | _ -> let sub_v = ctx.compile_sub value in Some (fun instance path -> match instance with | `Assoc pairs -> let evaluated = evaluated_properties_of_schema ~include_unevaluated:false ctx ctx.base_uri ctx.schema_obj instance path in List.concat_map (fun (name, prop_value) -> if List.mem name evaluated then [] else sub_v prop_value (Json_pointer.append path name)) pairs | _ -> [])); } let compilers = [ compile_properties; compile_pattern_properties; compile_additional_properties; compile_max_properties; compile_min_properties; compile_required; compile_dependencies; compile_property_names; compile_dependent_required; ] let draft07_compilers = [ compile_properties; compile_pattern_properties; compile_additional_properties; compile_max_properties; compile_min_properties; compile_required; compile_dependencies; compile_property_names; ] let draft04_compilers = [ compile_properties; compile_pattern_properties; compile_additional_properties; compile_max_properties; compile_min_properties; compile_required; compile_dependencies; ] let draft03_compilers = [ compile_draft03_properties; compile_pattern_properties; compile_additional_properties; compile_draft03_dependencies; ] let draft2019_09_compilers = [ compile_properties; compile_pattern_properties; compile_additional_properties; compile_max_properties; compile_min_properties; compile_required; compile_dependencies; compile_property_names; compile_dependent_required; compile_dependent_schemas; compile_unevaluated_properties_simple; ] let draft2020_12_compilers = [ compile_properties; compile_pattern_properties; compile_additional_properties; compile_max_properties; compile_min_properties; compile_required; compile_dependencies; compile_property_names; compile_dependent_required; compile_dependent_schemas; compile_unevaluated_properties_simple; ]
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>