···
| j -> raise (Json.Of_json ("Expected object for CallToolResult", j))
+
(** Resource definition *)
+
module Resource = struct
+
description: string option;
+
mime_type: string option;
+
annotations: Annotated.annotation option;
+
let yojson_of_t { name; uri; description; mime_type; size; annotations } =
+
("name", `String name);
+
let assoc = match description with
+
| Some desc -> ("description", `String desc) :: assoc
+
let assoc = match mime_type with
+
| Some mime -> ("mimeType", `String mime) :: assoc
+
let assoc = match size with
+
| Some s -> ("size", `Int s) :: assoc
+
let assoc = match annotations with
+
| Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc
+
let t_of_yojson = function
+
let name = match List.assoc_opt "name" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
+
let uri = match List.assoc_opt "uri" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields))
+
let description = match List.assoc_opt "description" fields with
+
| Some (`String s) -> Some s
+
let mime_type = match List.assoc_opt "mimeType" fields with
+
| Some (`String s) -> Some s
+
let size = match List.assoc_opt "size" fields with
+
| Some (`Int s) -> Some s
+
let annotations = match List.assoc_opt "annotations" fields with
+
| Some json -> Some (Annotated.annotation_of_yojson json)
+
{ name; uri; description; mime_type; size; annotations }
+
| j -> raise (Json.Of_json ("Expected object for Resource", j))
+
(** Resource Template definition *)
+
module ResourceTemplate = struct
+
description: string option;
+
mime_type: string option;
+
annotations: Annotated.annotation option;
+
let yojson_of_t { name; uri_template; description; mime_type; annotations } =
+
("name", `String name);
+
("uriTemplate", `String uri_template);
+
let assoc = match description with
+
| Some desc -> ("description", `String desc) :: assoc
+
let assoc = match mime_type with
+
| Some mime -> ("mimeType", `String mime) :: assoc
+
let assoc = match annotations with
+
| Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc
+
let t_of_yojson = function
+
let name = match List.assoc_opt "name" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
+
let uri_template = match List.assoc_opt "uriTemplate" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uriTemplate' field", `Assoc fields))
+
let description = match List.assoc_opt "description" fields with
+
| Some (`String s) -> Some s
+
let mime_type = match List.assoc_opt "mimeType" fields with
+
| Some (`String s) -> Some s
+
let annotations = match List.assoc_opt "annotations" fields with
+
| Some json -> Some (Annotated.annotation_of_yojson json)
+
{ name; uri_template; description; mime_type; annotations }
+
| j -> raise (Json.Of_json ("Expected object for ResourceTemplate", j))
+
(** Resource Reference *)
+
module ResourceReference = struct
+
let yojson_of_t { uri } =
+
("type", `String "ref/resource");
+
let t_of_yojson = function
+
let _ = match List.assoc_opt "type" fields with
+
| Some (`String "ref/resource") -> ()
+
| _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields))
+
let uri = match List.assoc_opt "uri" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields))
+
| j -> raise (Json.Of_json ("Expected object for ResourceReference", j))
+
(** Prompt Reference *)
+
module PromptReference = struct
+
let yojson_of_t { name } =
+
("type", `String "ref/prompt");
+
("name", `String name);
+
let t_of_yojson = function
+
let _ = match List.assoc_opt "type" fields with
+
| Some (`String "ref/prompt") -> ()
+
| _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields))
+
let name = match List.assoc_opt "name" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
+
| j -> raise (Json.Of_json ("Expected object for PromptReference", j))
+
(** Completion support *)
+
module Completion = struct
+
module Argument = struct
+
let yojson_of_t { name; value } =
+
("name", `String name);
+
("value", `String value);
+
let t_of_yojson = function
+
let name = match List.assoc_opt "name" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
+
let value = match List.assoc_opt "value" fields with
+
| Some (`String s) -> s
+
| _ -> raise (Json.Of_json ("Missing or invalid 'value' field", `Assoc fields))
+
| j -> raise (Json.Of_json ("Expected object for Completion.Argument", j))
+
module Request = struct
+
type reference = [ `Prompt of PromptReference.t | `Resource of ResourceReference.t ]
+
let yojson_of_reference = function
+
| `Prompt p -> PromptReference.yojson_of_t p
+
| `Resource r -> ResourceReference.yojson_of_t r
+
let reference_of_yojson = function
+
(match List.assoc_opt "type" fields with
+
| Some (`String "ref/prompt") -> `Prompt (PromptReference.t_of_yojson (`Assoc fields))
+
| Some (`String "ref/resource") -> `Resource (ResourceReference.t_of_yojson (`Assoc fields))
+
| _ -> raise (Json.Of_json ("Invalid or missing reference type", `Assoc fields)))
+
| j -> raise (Json.Of_json ("Expected object for reference", j))
+
let yojson_of_t { argument; ref } =
+
("argument", Argument.yojson_of_t argument);
+
("ref", yojson_of_reference ref);
+
let t_of_yojson = function
+
let argument = match List.assoc_opt "argument" fields with
+
| Some json -> Argument.t_of_yojson json
+
| _ -> raise (Json.Of_json ("Missing argument field", `Assoc fields))
+
let ref = match List.assoc_opt "ref" fields with
+
| Some json -> reference_of_yojson json
+
| _ -> raise (Json.Of_json ("Missing ref field", `Assoc fields))
+
| j -> raise (Json.Of_json ("Expected object for Completion.Request", j))
+
let create ~argument ~ref =
+
completion: completion;
+
let yojson_of_completion { values; has_more; total } =
+
("values", `List (List.map (fun s -> `String s) values));
+
let assoc = match has_more with
+
| Some b -> ("hasMore", `Bool b) :: assoc
+
let assoc = match total with
+
| Some n -> ("total", `Int n) :: assoc
+
let completion_of_yojson = function
+
let values = match List.assoc_opt "values" fields with
+
| Some (`List items) ->
+
| _ -> raise (Json.Of_json ("Expected string in values array", `List items))
+
| _ -> raise (Json.Of_json ("Missing or invalid 'values' field", `Assoc fields))
+
let has_more = match List.assoc_opt "hasMore" fields with
+
| Some (`Bool b) -> Some b
+
| _ -> raise (Json.Of_json ("Invalid 'hasMore' field", `Assoc fields))
+
let total = match List.assoc_opt "total" fields with
+
| Some (`Int n) -> Some n
+
| _ -> raise (Json.Of_json ("Invalid 'total' field", `Assoc fields))
+
{ values; has_more; total }
+
| j -> raise (Json.Of_json ("Expected object for completion", j))
+
let yojson_of_t { completion; meta } =
+
("completion", yojson_of_completion completion);
+
let assoc = match meta with
+
| Some meta_json -> ("_meta", meta_json) :: assoc
+
let t_of_yojson = function
+
let completion = match List.assoc_opt "completion" fields with
+
| Some json -> completion_of_yojson json
+
| _ -> raise (Json.Of_json ("Missing completion field", `Assoc fields))
+
let meta = List.assoc_opt "_meta" fields in
+
| j -> raise (Json.Of_json ("Expected object for Completion.Result", j))
+
let create ~completion ?meta () =
module PromptMessage = struct
···
let create_notification = JSONRPCMessage.create_notification
let create_request = JSONRPCMessage.create_request
let create_response = JSONRPCMessage.create_response
+
let create_error = JSONRPCMessage.create_error
+
let create_completion_request ~id ~argument ~ref =
+
let params = Completion.Request.to_params { argument; ref } in
+
create_request ~id ~method_:"completion/complete" ~params:(Some params) ()
+
let create_completion_response ~id ~values ?(has_more=None) ?(total=None) ?(meta=None) () =
+
let completion = { Completion.Result.values; has_more; total } in
+
let result = Completion.Result.to_result { completion; meta } in
+
create_response ~id ~result