···
3
+
(* Standard error codes *)
4
+
module ErrorCode = struct
5
+
let parse_error = -32700
6
+
let invalid_request = -32600
7
+
let method_not_found = -32601
8
+
let invalid_params = -32602
9
+
let internal_error = -32603
10
+
let resource_not_found = -32002
11
+
let server_error_start = -32000
12
+
let server_error_end = -32099
···
| j -> raise (Json.Of_json ("Expected object for ImageContent", j))
172
+
module AudioContent = struct
176
+
annotations: Annotated.annotation option;
179
+
let yojson_of_t { data; mime_type; annotations } =
181
+
("data", `String data);
182
+
("mimeType", `String mime_type);
183
+
("type", `String "audio");
185
+
let assoc = match annotations with
186
+
| Some annotations -> ("annotations", Annotated.yojson_of_annotation annotations) :: assoc
191
+
let t_of_yojson = function
193
+
let data = match List.assoc_opt "data" fields with
194
+
| Some (`String s) -> s
195
+
| _ -> raise (Json.Of_json ("Missing or invalid 'data' field", `Assoc fields))
197
+
let mime_type = match List.assoc_opt "mimeType" fields with
198
+
| Some (`String s) -> s
199
+
| _ -> raise (Json.Of_json ("Missing or invalid 'mimeType' field", `Assoc fields))
201
+
let _ = match List.assoc_opt "type" fields with
202
+
| Some (`String "audio") -> ()
203
+
| _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields))
205
+
let annotations = List.assoc_opt "annotations" fields |> Option.map Annotated.annotation_of_yojson in
206
+
{ data; mime_type; annotations }
207
+
| j -> raise (Json.Of_json ("Expected object for AudioContent", j))
module ResourceContents = struct
···
| j -> raise (Json.Of_json ("Expected object for EmbeddedResource", j))
354
+
(** Tool definition *)
355
+
module Tool = struct
358
+
description: string option;
359
+
input_schema: Json.t;
362
+
let yojson_of_t { name; description; input_schema } =
364
+
("name", `String name);
365
+
("inputSchema", input_schema);
367
+
let assoc = match description with
368
+
| Some desc -> ("description", `String desc) :: assoc
373
+
let t_of_yojson = function
375
+
let name = match List.assoc_opt "name" fields with
376
+
| Some (`String s) -> s
377
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
379
+
let description = match List.assoc_opt "description" fields with
380
+
| Some (`String s) -> Some s
383
+
let input_schema = match List.assoc_opt "inputSchema" fields with
384
+
| Some json -> json
385
+
| _ -> raise (Json.Of_json ("Missing 'inputSchema' field", `Assoc fields))
387
+
{ name; description; input_schema }
388
+
| j -> raise (Json.Of_json ("Expected object for Tool", j))
| Image of ImageContent.t
394
+
| Audio of AudioContent.t
| Resource of EmbeddedResource.t
let yojson_of_content = function
| Text t -> TextContent.yojson_of_t t
| Image i -> ImageContent.yojson_of_t i
400
+
| Audio a -> AudioContent.yojson_of_t a
| Resource r -> EmbeddedResource.yojson_of_t r
let content_of_yojson = function
···
(match List.assoc_opt "type" fields with
| Some (`String "text") -> Text (TextContent.t_of_yojson (`Assoc fields))
| Some (`String "image") -> Image (ImageContent.t_of_yojson (`Assoc fields))
408
+
| Some (`String "audio") -> Audio (AudioContent.t_of_yojson (`Assoc fields))
| Some (`String "resource") -> Resource (EmbeddedResource.t_of_yojson (`Assoc fields))
| _ -> raise (Json.Of_json ("Invalid or missing content type", `Assoc fields)))
| j -> raise (Json.Of_json ("Expected object for content", j))
414
+
module CallToolResult = struct
416
+
content: content list;
418
+
meta: Json.t option;
421
+
let yojson_of_t { content; is_error; meta } =
423
+
("content", `List (List.map yojson_of_content content));
424
+
("isError", `Bool is_error);
426
+
let assoc = match meta with
427
+
| Some meta_json -> ("_meta", meta_json) :: assoc
432
+
let t_of_yojson = function
434
+
let content = match List.assoc_opt "content" fields with
435
+
| Some (`List items) -> List.map content_of_yojson items
436
+
| _ -> raise (Json.Of_json ("Missing or invalid 'content' field", `Assoc fields))
438
+
let is_error = match List.assoc_opt "isError" fields with
439
+
| Some (`Bool b) -> b
440
+
| None -> false (* Default to false if not specified *)
441
+
| _ -> raise (Json.Of_json ("Invalid 'isError' field", `Assoc fields))
443
+
let meta = List.assoc_opt "_meta" fields in
444
+
{ content; is_error; meta }
445
+
| j -> raise (Json.Of_json ("Expected object for CallToolResult", j))
···
(* JSONRPC Message types *)
module JSONRPCMessage = struct