package mcp-kit

All checked builds pass
  1. Overview
  2. Docs
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_array.ml.html

Source file kw_array.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
(** Keywords: items, additionalItems, maxItems, minItems, uniqueItems, contains
    (spec §6.4). *)

open Jsonschema_core

let parent_path path =
  match List.rev path with [] -> [] | _ :: rest -> List.rev rest

let compile_items : Compiler.keyword_compiler =
  {
    keyword = "items";
    compile =
      (fun ctx value ->
        match value with
        | `Assoc _ | `Bool _ ->
            (* items is a single schema — applies to all elements *)
            let sub_v = ctx.compile_sub value in
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    List.concat_map
                      (fun (i, item) ->
                        let p = Json_pointer.append path (string_of_int i) in
                        sub_v item p)
                      (List.mapi (fun i x -> (i, x)) items)
                | _ -> [])
        | `List schemas ->
            (* items is tuple — each schema applies to corresponding element *)
            let sub_vs =
              List.mapi
                (fun index schema ->
                  ctx.compile_sub
                    ~schema_path:
                      (Json_pointer.append ctx.schema_path (string_of_int index))
                    schema)
                schemas
            in
            (* Also check additionalItems *)
            let additional_v =
              match
                Schema.keyword "additionalItems" (Schema.Obj ctx.schema_obj)
              with
              | Some (`Bool false) ->
                  let additional_items_path =
                    Json_pointer.append
                      (parent_path ctx.schema_path)
                      "additionalItems"
                  in
                  Some
                    (fun _instance path i ->
                      [
                        Error.make
                          ~instance_path:
                            (Json_pointer.append path (string_of_int i))
                          ~schema_path:additional_items_path
                          ~keyword:"additionalItems"
                          "additional items not allowed";
                      ])
              | Some (`Bool true) | None -> None
              | Some additional_schema ->
                  let v =
                    ctx.compile_sub
                      ~schema_path:
                        (Json_pointer.append
                           (parent_path ctx.schema_path)
                           "additionalItems")
                      additional_schema
                  in
                  Some
                    (fun instance path i ->
                      v instance (Json_pointer.append path (string_of_int i)))
            in
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    let indexed = List.mapi (fun i x -> (i, x)) items in
                    let tuple_errors =
                      List.concat_map
                        (fun (i, item) ->
                          if i < List.length sub_vs then
                            let p =
                              Json_pointer.append path (string_of_int i)
                            in
                            (List.nth sub_vs i) item p
                          else [])
                        indexed
                    in
                    let additional_errors =
                      match additional_v with
                      | None -> []
                      | Some check ->
                          List.concat_map
                            (fun (i, item) ->
                              if i >= List.length sub_vs then check item path i
                              else [])
                            indexed
                    in
                    tuple_errors @ additional_errors
                | _ -> [])
        | _ -> None);
  }

let compile_prefix_items : Compiler.keyword_compiler =
  {
    keyword = "prefixItems";
    compile =
      (fun ctx value ->
        match value with
        | `List schemas ->
            let sub_vs =
              List.mapi
                (fun index schema ->
                  ctx.compile_sub
                    ~schema_path:
                      (Json_pointer.append ctx.schema_path (string_of_int index))
                    schema)
                schemas
            in
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    List.mapi
                      (fun i item ->
                        if i < List.length sub_vs then
                          (List.nth sub_vs i) item
                            (Json_pointer.append path (string_of_int i))
                        else [])
                      items
                    |> List.concat
                | _ -> [])
        | _ -> None);
  }

let compile_items_draft2020_12 : Compiler.keyword_compiler =
  {
    keyword = "items";
    compile =
      (fun ctx value ->
        match value with
        | `Assoc _ | `Bool _ ->
            let prefix_len =
              match
                Schema.keyword "prefixItems" (Schema.Obj ctx.schema_obj)
              with
              | Some (`List schemas) -> List.length schemas
              | _ -> 0
            in
            let sub_v = ctx.compile_sub value in
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    List.concat_map
                      (fun (i, item) ->
                        if i >= prefix_len then
                          let p = Json_pointer.append path (string_of_int i) in
                          sub_v item p
                        else [])
                      (List.mapi (fun i x -> (i, x)) items)
                | _ -> [])
        | _ -> None);
  }

let compile_additional_items : Compiler.keyword_compiler =
  {
    keyword = "additionalItems";
    compile =
      (fun ctx _value ->
        (* Only meaningful when items is an array — handled by items compiler *)
        match Schema.keyword "items" (Schema.Obj ctx.schema_obj) with
        | Some (`List _) -> None (* handled by items *)
        | _ -> None)
      (* ignored when items is schema or absent *);
  }

let compile_max_items : Compiler.keyword_compiler =
  {
    keyword = "maxItems";
    compile =
      (fun ctx value ->
        match Util.to_nonneg_int_bound value with
        | Some bound ->
            Some
              (fun instance path ->
                match instance with
                | `List items
                  when Util.actual_greater_than_bound (List.length items) bound
                  ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"maxItems"
                        (Printf.sprintf "too many items (max %s)"
                           (Util.bound_to_string bound));
                    ]
                | _ -> [])
        | None -> None);
  }

let compile_min_items : Compiler.keyword_compiler =
  {
    keyword = "minItems";
    compile =
      (fun ctx value ->
        match Util.to_nonneg_int_bound value with
        | Some bound ->
            Some
              (fun instance path ->
                match instance with
                | `List items
                  when Util.actual_less_than_bound (List.length items) bound ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"minItems"
                        (Printf.sprintf "too few items (min %s)"
                           (Util.bound_to_string bound));
                    ]
                | _ -> [])
        | None -> None);
  }

let compile_unique_items : Compiler.keyword_compiler =
  {
    keyword = "uniqueItems";
    compile =
      (fun ctx value ->
        match value with
        | `Bool true ->
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    let rec has_dup = function
                      | [] -> false
                      | x :: rest ->
                          if List.exists (Instance.json_equal x) rest then true
                          else has_dup rest
                    in
                    if has_dup items then
                      [
                        Error.make ~instance_path:path
                          ~schema_path:ctx.schema_path ~keyword:"uniqueItems"
                          "items are not unique";
                      ]
                    else []
                | _ -> [])
        | _ -> None);
  }

let compile_contains : Compiler.keyword_compiler =
  {
    keyword = "contains";
    compile =
      (fun ctx value ->
        let sub_v = ctx.compile_sub value in
        Some
          (fun instance path ->
            match instance with
            | `List items ->
                if List.exists (fun item -> sub_v item path = []) items then []
                else
                  [
                    Error.make ~instance_path:path ~schema_path:ctx.schema_path
                      ~keyword:"contains" "no item matches contains schema";
                  ]
            | _ -> []));
  }

let compile_contains_bounded : Compiler.keyword_compiler =
  {
    keyword = "contains";
    compile =
      (fun ctx value ->
        let sub_v = ctx.compile_sub value in
        let explicit_min_contains =
          match Schema.keyword "minContains" (Schema.Obj ctx.schema_obj) with
          | Some value -> Some value
          | None -> None
        in
        let min_contains =
          match explicit_min_contains with
          | Some value -> Util.to_nonneg_int_bound value
          | None -> Some (Util.Within_int 1)
        in
        let explicit_max_contains =
          match Schema.keyword "maxContains" (Schema.Obj ctx.schema_obj) with
          | Some value -> Some value
          | None -> None
        in
        let max_contains =
          match explicit_max_contains with
          | Some value -> Util.to_nonneg_int_bound value
          | None -> None
        in
        let parent_schema_path = parent_path ctx.schema_path in
        Some
          (fun instance path ->
            match instance with
            | `List items ->
                let matches =
                  List.mapi
                    (fun i item ->
                      let item_path =
                        Json_pointer.append path (string_of_int i)
                      in
                      sub_v item item_path = [])
                    items
                  |> List.filter Fun.id |> List.length
                in
                let min_errors =
                  match min_contains with
                  | Some bound when Util.actual_less_than_bound matches bound ->
                      let schema_path, keyword, message =
                        match explicit_min_contains with
                        | Some _ ->
                            ( Json_pointer.append parent_schema_path
                                "minContains",
                              "minContains",
                              Printf.sprintf "too few matching items (min %s)"
                                (Util.bound_to_string bound) )
                        | None ->
                            ( ctx.schema_path,
                              "contains",
                              "no item matches contains schema" )
                      in
                      [
                        Error.make ~instance_path:path ~schema_path ~keyword
                          message;
                      ]
                  | _ -> []
                in
                let max_errors =
                  match max_contains with
                  | Some bound when Util.actual_greater_than_bound matches bound
                    ->
                      [
                        Error.make ~instance_path:path
                          ~schema_path:
                            (Json_pointer.append parent_schema_path
                               "maxContains")
                          ~keyword:"maxContains"
                          (Printf.sprintf "too many matching items (max %s)"
                             (Util.bound_to_string bound));
                      ]
                  | _ -> []
                in
                min_errors @ max_errors
            | _ -> []));
  }

let add_unique_int x xs = if List.mem x xs then xs else x :: xs

let union_indexes xs ys =
  List.fold_left (fun acc x -> add_unique_int 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_items_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, `List items ->
        let base_uri = schema_local_base ctx base_uri schema_pairs in
        let indexed = List.mapi (fun i item -> (i, item)) items 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_indexes acc
                    (evaluated_items_of_schema ~include_unevaluated:true
                       ~depth:(depth - 1) ctx target_base target_schema instance
                       path)
              | _ -> acc)
          | _ -> acc
        in
        let item_annotations, tuple_len =
          let prefix_annotations, prefix_len =
            match List.assoc_opt "prefixItems" schema_pairs with
            | Some (`List schemas) ->
                let annotations =
                  List.fold_left
                    (fun acc (i, item) ->
                      if i < List.length schemas then
                        let item_schema = List.nth schemas i in
                        if
                          schema_valid_at ctx base_uri item_schema item
                            (Json_pointer.append path (string_of_int i))
                        then add_unique_int i acc
                        else acc
                      else acc)
                    [] indexed
                in
                (annotations, Some (List.length schemas))
            | _ -> ([], None)
          in
          match List.assoc_opt "items" schema_pairs with
          | Some (`List schemas) ->
              let annotations =
                List.fold_left
                  (fun acc (i, item) ->
                    if i < List.length schemas then
                      let item_schema = List.nth schemas i in
                      if
                        schema_valid_at ctx base_uri item_schema item
                          (Json_pointer.append path (string_of_int i))
                      then add_unique_int i acc
                      else acc
                    else acc)
                  [] indexed
              in
              ( union_indexes prefix_annotations annotations,
                Some (List.length schemas) )
          | Some ((`Assoc _ | `Bool _) as item_schema) ->
              let start = match prefix_len with Some n -> n | None -> 0 in
              let annotations =
                List.fold_left
                  (fun acc (i, item) ->
                    if
                      i >= start
                      && schema_valid_at ctx base_uri item_schema item
                           (Json_pointer.append path (string_of_int i))
                    then add_unique_int i acc
                    else acc)
                  [] indexed
              in
              (union_indexes prefix_annotations annotations, prefix_len)
          | _ -> (prefix_annotations, prefix_len)
        in
        let additional_annotations =
          match (tuple_len, List.assoc_opt "additionalItems" schema_pairs) with
          | Some n, Some (`Bool true) ->
              List.fold_left
                (fun acc (i, _) -> if i >= n then add_unique_int i acc else acc)
                [] indexed
          | Some _n, Some (`Bool false) -> []
          | Some n, Some additional_schema ->
              List.fold_left
                (fun acc (i, item) ->
                  if
                    i >= n
                    && schema_valid_at ctx base_uri additional_schema item
                         (Json_pointer.append path (string_of_int i))
                  then add_unique_int i acc
                  else acc)
                [] indexed
          | _ -> []
        in
        let contains_annotations =
          match List.assoc_opt "contains" schema_pairs with
          | Some contains_schema ->
              List.fold_left
                (fun acc (i, item) ->
                  if
                    schema_valid_at ctx base_uri contains_schema item
                      (Json_pointer.append path (string_of_int i))
                  then add_unique_int i acc
                  else acc)
                [] indexed
          | _ -> []
        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_indexes acc
                      (evaluated_items_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_indexes acc
                    (evaluated_items_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_indexes acc
                    (evaluated_items_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_indexes acc
                      (evaluated_items_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_indexes acc
                      (evaluated_items_of_schema ~include_unevaluated:true
                         ~depth:(depth - 1) ctx base_uri else_schema instance
                         path)
                | _ -> acc)
          | None -> acc
        in
        let annotations =
          [] |> collect_from_ref "$ref"
          |> collect_from_ref "$recursiveRef"
          |> collect_from_ref "$dynamicRef"
          |> union_indexes item_annotations
          |> union_indexes additional_annotations
          |> union_indexes contains_annotations
          |> collect_schema_array "allOf"
          |> collect_schema_array "anyOf"
          |> collect_one_of |> collect_conditionals
        in
        if include_unevaluated then
          match List.assoc_opt "unevaluatedItems" schema_pairs with
          | Some (`Bool true) ->
              List.fold_left
                (fun acc (i, _) -> add_unique_int i acc)
                annotations indexed
          | Some (`Bool false) -> annotations
          | Some unevaluated_schema ->
              List.fold_left
                (fun acc (i, item) ->
                  if List.mem i annotations then acc
                  else if
                    schema_valid_at ctx base_uri unevaluated_schema item
                      (Json_pointer.append path (string_of_int i))
                  then add_unique_int i acc
                  else acc)
                annotations indexed
          | None -> annotations
        else annotations
    | _ -> []

let compile_unevaluated_items_simple : Compiler.keyword_compiler =
  {
    keyword = "unevaluatedItems";
    compile =
      (fun ctx value ->
        match value with
        | `Bool true -> None
        | `Bool false ->
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    let evaluated =
                      evaluated_items_of_schema ~include_unevaluated:false ctx
                        ctx.base_uri ctx.schema_obj instance path
                    in
                    List.mapi
                      (fun i _item ->
                        if not (List.mem i evaluated) then
                          [
                            Error.make
                              ~instance_path:
                                (Json_pointer.append path (string_of_int i))
                              ~schema_path:ctx.schema_path
                              ~keyword:"unevaluatedItems"
                              "unevaluated item not allowed";
                          ]
                        else [])
                      items
                    |> List.concat
                | _ -> [])
        | _ ->
            let sub_v = ctx.compile_sub value in
            Some
              (fun instance path ->
                match instance with
                | `List items ->
                    let evaluated =
                      evaluated_items_of_schema ~include_unevaluated:false ctx
                        ctx.base_uri ctx.schema_obj instance path
                    in
                    List.mapi
                      (fun i item ->
                        if not (List.mem i evaluated) then
                          sub_v item
                            (Json_pointer.append path (string_of_int i))
                        else [])
                      items
                    |> List.concat
                | _ -> []));
  }

let compilers =
  [
    compile_items;
    compile_additional_items;
    compile_max_items;
    compile_min_items;
    compile_unique_items;
    compile_contains;
  ]

let draft04_compilers =
  [
    compile_items;
    compile_additional_items;
    compile_max_items;
    compile_min_items;
    compile_unique_items;
  ]

let draft2019_09_compilers =
  [
    compile_items;
    compile_additional_items;
    compile_max_items;
    compile_min_items;
    compile_unique_items;
    compile_contains_bounded;
    compile_unevaluated_items_simple;
  ]

let draft2020_12_compilers =
  [
    compile_prefix_items;
    compile_items_draft2020_12;
    compile_max_items;
    compile_min_items;
    compile_unique_items;
    compile_contains_bounded;
    compile_unevaluated_items_simple;
  ]