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

Source file kw_numeric.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
(** Keywords: multipleOf, maximum, exclusiveMaximum, minimum, exclusiveMinimum
    (spec §6.2). *)

open Jsonschema_core

let get_number j = Instance.to_float j

type exact_int = { sign : int; digits : string }

let normalize_digits s =
  let len = String.length s in
  let rec first_nonzero i =
    if i = len then len else if s.[i] = '0' then first_nonzero (i + 1) else i
  in
  let i = first_nonzero 0 in
  if i = len then "0" else String.sub s i (len - i)

let exact_int_of_string s =
  let len = String.length s in
  if len = 0 then None
  else
    let sign, start =
      if s.[0] = '-' then (-1, 1) else if s.[0] = '+' then (1, 1) else (1, 0)
    in
    if start = len then None
    else
      let rec all_digits i =
        if i = len then true
        else match s.[i] with '0' .. '9' -> all_digits (i + 1) | _ -> false
      in
      if not (all_digits start) then None
      else
        let digits = normalize_digits (String.sub s start (len - start)) in
        let sign = if digits = "0" then 0 else sign in
        Some { sign; digits }

let exact_int_of_json = function
  | `Int n -> exact_int_of_string (string_of_int n)
  | `Intlit s -> exact_int_of_string s
  | _ -> None

let compare_abs_digits a b =
  let la = String.length a and lb = String.length b in
  if la <> lb then compare la lb else String.compare a b

let compare_exact_int a b =
  match compare a.sign b.sign with
  | 0 when a.sign = 0 -> 0
  | 0 when a.sign > 0 -> compare_abs_digits a.digits b.digits
  | 0 -> -compare_abs_digits a.digits b.digits
  | c -> c

let subtract_abs_digits a b =
  let la = String.length a and lb = String.length b in
  let out = Bytes.make la '0' in
  let borrow = ref 0 in
  for offset = 0 to la - 1 do
    let ia = la - 1 - offset in
    let ib = lb - 1 - offset in
    let da = Char.code a.[ia] - Char.code '0' - !borrow in
    let db = if ib >= 0 then Char.code b.[ib] - Char.code '0' else 0 in
    let digit, next_borrow =
      if da < db then (da + 10 - db, 1) else (da - db, 0)
    in
    Bytes.set out ia (Char.chr (Char.code '0' + digit));
    borrow := next_borrow
  done;
  normalize_digits (Bytes.to_string out)

let append_digit digits c =
  if digits = "0" then String.make 1 c else digits ^ String.make 1 c

let mod_abs_digits value divisor =
  if divisor = "0" then None
  else
    let rem = ref "0" in
    String.iter
      (fun c ->
        rem := normalize_digits (append_digit !rem c);
        while compare_abs_digits !rem divisor >= 0 do
          rem := subtract_abs_digits !rem divisor
        done)
      value;
    Some !rem

let exact_int_multiple_of ~divisor value =
  if divisor.sign <= 0 then None
  else
    match mod_abs_digits value.digits divisor.digits with
    | Some "0" -> Some true
    | Some _ -> Some false
    | None -> None

let compare_json_numbers a b =
  match (exact_int_of_json a, exact_int_of_json b) with
  | Some ea, Some eb -> Some (compare_exact_int ea eb)
  | _ -> (
      match (get_number a, get_number b) with
      | Some fa, Some fb -> Some (Float.compare fa fb)
      | _ -> None)

let number_to_string j =
  match j with
  | `Int n -> string_of_int n
  | `Intlit s -> s
  | `Float f -> Printf.sprintf "%g" f
  | _ -> "number"

let multiple_of_float_errors ~(ctx : Compiler.context) ~keyword ~divisor
    instance path =
  match get_number instance with
  | Some v -> (
      if v = 0.0 then []
      else
        let r = v /. divisor in
        match Float.classify_float r with
        | FP_infinite ->
            (* Overflow: if value is integer and divisor divides 1.0, it's valid *)
            if Float.is_integer divisor && mod_float v divisor = 0.0 then []
            else if mod_float (1.0 /. divisor) 1.0 = 0.0 && Float.is_integer v
            then []
            else
              [
                Error.make ~instance_path:path ~schema_path:ctx.schema_path
                  ~keyword "not a multiple";
              ]
        | FP_nan ->
            [
              Error.make ~instance_path:path ~schema_path:ctx.schema_path
                ~keyword "not a multiple";
            ]
        | _ ->
            if Float.abs (r -. Float.round r) < 1e-9 then []
            else
              [
                Error.make ~instance_path:path ~schema_path:ctx.schema_path
                  ~keyword "not a multiple";
              ])
  | None -> []

let compile_multiple_of_keyword keyword : Compiler.keyword_compiler =
  {
    keyword;
    compile =
      (fun ctx value ->
        let exact_divisor = exact_int_of_json value in
        let float_divisor = get_number value in
        match (exact_divisor, float_divisor) with
        | Some divisor, _ when divisor.sign > 0 ->
            Some
              (fun instance path ->
                let valid =
                  match exact_int_of_json instance with
                  | Some exact_instance ->
                      exact_int_multiple_of ~divisor exact_instance
                  | None -> None
                in
                match valid with
                | Some true -> []
                | Some false ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword "not a multiple";
                    ]
                | None -> (
                    match float_divisor with
                    | Some divisor ->
                        multiple_of_float_errors ~ctx ~keyword ~divisor instance
                          path
                    | _ -> []))
        | _, Some divisor when divisor > 0.0 ->
            Some
              (fun instance path ->
                multiple_of_float_errors ~ctx ~keyword ~divisor instance path)
        | _ -> None);
  }

let compile_multiple_of = compile_multiple_of_keyword "multipleOf"
let compile_divisible_by = compile_multiple_of_keyword "divisibleBy"

let make_compare keyword op : Compiler.keyword_compiler =
  {
    keyword;
    compile =
      (fun ctx value ->
        match compare_json_numbers value value with
        | Some _ ->
            Some
              (fun instance path ->
                match compare_json_numbers instance value with
                | Some c ->
                    if op c then []
                    else
                      [
                        Error.make ~instance_path:path
                          ~schema_path:ctx.schema_path ~keyword
                          (Printf.sprintf "expected %s %s" keyword
                             (number_to_string value));
                      ]
                | None -> [])
        | _ -> None);
  }

let compile_maximum = make_compare "maximum" (fun c -> c <= 0)
let compile_exclusive_maximum = make_compare "exclusiveMaximum" (fun c -> c < 0)
let compile_minimum = make_compare "minimum" (fun c -> c >= 0)
let compile_exclusive_minimum = make_compare "exclusiveMinimum" (fun c -> c > 0)

let compile_draft04_exclusive_maximum : Compiler.keyword_compiler =
  {
    keyword = "exclusiveMaximum";
    compile =
      (fun ctx value ->
        match (value, Schema.keyword "maximum" (Schema.Obj ctx.schema_obj)) with
        | `Bool true, Some maximum -> (
            match compare_json_numbers maximum maximum with
            | Some _ ->
                Some
                  (fun instance path ->
                    match compare_json_numbers instance maximum with
                    | Some c when c < 0 -> []
                    | Some _ ->
                        [
                          Error.make ~instance_path:path
                            ~schema_path:ctx.schema_path
                            ~keyword:"exclusiveMaximum"
                            (Printf.sprintf "expected exclusiveMaximum %s"
                               (number_to_string maximum));
                        ]
                    | None -> [])
            | None -> None)
        | _ -> None);
  }

let compile_draft04_exclusive_minimum : Compiler.keyword_compiler =
  {
    keyword = "exclusiveMinimum";
    compile =
      (fun ctx value ->
        match (value, Schema.keyword "minimum" (Schema.Obj ctx.schema_obj)) with
        | `Bool true, Some minimum -> (
            match compare_json_numbers minimum minimum with
            | Some _ ->
                Some
                  (fun instance path ->
                    match compare_json_numbers instance minimum with
                    | Some c when c > 0 -> []
                    | Some _ ->
                        [
                          Error.make ~instance_path:path
                            ~schema_path:ctx.schema_path
                            ~keyword:"exclusiveMinimum"
                            (Printf.sprintf "expected exclusiveMinimum %s"
                               (number_to_string minimum));
                        ]
                    | None -> [])
            | None -> None)
        | _ -> None);
  }

let compilers =
  [
    compile_multiple_of;
    compile_maximum;
    compile_exclusive_maximum;
    compile_minimum;
    compile_exclusive_minimum;
  ]

let draft04_compilers =
  [
    compile_multiple_of;
    compile_maximum;
    compile_draft04_exclusive_maximum;
    compile_minimum;
    compile_draft04_exclusive_minimum;
  ]

let draft03_compilers =
  [
    compile_divisible_by;
    compile_maximum;
    compile_draft04_exclusive_maximum;
    compile_minimum;
    compile_draft04_exclusive_minimum;
  ]