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_logic.ml.html

Source file kw_logic.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
(** Keywords: allOf, anyOf, oneOf, not (spec §6.7). *)

open Jsonschema_core

let compile_all_of : Compiler.keyword_compiler =
  {
    keyword = "allOf";
    compile =
      (fun ctx value ->
        match value with
        | `List schemas when 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 ->
                let errors =
                  List.concat_map (fun v -> v instance path) sub_vs
                in
                if errors = [] then []
                else
                  [
                    Error.make_with_context ~instance_path:path
                      ~schema_path:ctx.schema_path ~keyword:"allOf"
                      ~context:errors "not valid against all schemas";
                  ])
        | _ -> None);
  }

let compile_any_of : Compiler.keyword_compiler =
  {
    keyword = "anyOf";
    compile =
      (fun ctx value ->
        match value with
        | `List schemas when 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 ->
                let results = List.map (fun v -> v instance path) sub_vs in
                if List.exists (( = ) []) results then []
                else
                  [
                    Error.make_with_context ~instance_path:path
                      ~schema_path:ctx.schema_path ~keyword:"anyOf"
                      ~context:(List.concat results)
                      "not valid against any schema";
                  ])
        | _ -> None);
  }

let compile_one_of : Compiler.keyword_compiler =
  {
    keyword = "oneOf";
    compile =
      (fun ctx value ->
        match value with
        | `List schemas when 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 ->
                let results = List.map (fun v -> v instance path) sub_vs in
                let passing = List.filter (( = ) []) results in
                match List.length passing with
                | 1 -> []
                | 0 ->
                    [
                      Error.make_with_context ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"oneOf"
                        ~context:(List.concat results)
                        "not valid against any schema";
                    ]
                | _ ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"oneOf"
                        "valid against more than one schema";
                    ])
        | _ -> None);
  }

let compile_not : Compiler.keyword_compiler =
  {
    keyword = "not";
    compile =
      (fun ctx value ->
        let sub_v = ctx.compile_sub value in
        Some
          (fun instance path ->
            if sub_v instance path <> [] then []
            else
              [
                Error.make ~instance_path:path ~schema_path:ctx.schema_path
                  ~keyword:"not" "should not be valid";
              ]));
  }

let compilers = [ compile_all_of; compile_any_of; compile_one_of; compile_not ]

let compile_extends : Compiler.keyword_compiler =
  {
    keyword = "extends";
    compile =
      (fun ctx value ->
        let schemas =
          match value with
          | `List schemas -> schemas
          | `Assoc _ -> [ value ]
          | _ -> []
        in
        match schemas with
        | [] -> None
        | _ ->
            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 ->
                let errors =
                  List.concat_map (fun v -> v instance path) sub_vs
                in
                if errors = [] then []
                else
                  [
                    Error.make_with_context ~instance_path:path
                      ~schema_path:ctx.schema_path ~keyword:"extends"
                      ~context:errors "not valid against extended schema";
                  ]));
  }

let draft03_compilers = [ compile_extends ]