···
| j -> raise (Json.Of_json ("Expected object for CallToolResult", j))
448
+
(** Resource definition *)
449
+
module Resource = struct
453
+
description: string option;
454
+
mime_type: string option;
456
+
annotations: Annotated.annotation option;
459
+
let yojson_of_t { name; uri; description; mime_type; size; annotations } =
461
+
("name", `String name);
462
+
("uri", `String uri);
464
+
let assoc = match description with
465
+
| Some desc -> ("description", `String desc) :: assoc
468
+
let assoc = match mime_type with
469
+
| Some mime -> ("mimeType", `String mime) :: assoc
472
+
let assoc = match size with
473
+
| Some s -> ("size", `Int s) :: assoc
476
+
let assoc = match annotations with
477
+
| Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc
482
+
let t_of_yojson = function
484
+
let name = match List.assoc_opt "name" fields with
485
+
| Some (`String s) -> s
486
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
488
+
let uri = match List.assoc_opt "uri" fields with
489
+
| Some (`String s) -> s
490
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields))
492
+
let description = match List.assoc_opt "description" fields with
493
+
| Some (`String s) -> Some s
496
+
let mime_type = match List.assoc_opt "mimeType" fields with
497
+
| Some (`String s) -> Some s
500
+
let size = match List.assoc_opt "size" fields with
501
+
| Some (`Int s) -> Some s
504
+
let annotations = match List.assoc_opt "annotations" fields with
505
+
| Some json -> Some (Annotated.annotation_of_yojson json)
508
+
{ name; uri; description; mime_type; size; annotations }
509
+
| j -> raise (Json.Of_json ("Expected object for Resource", j))
512
+
(** Resource Template definition *)
513
+
module ResourceTemplate = struct
516
+
uri_template: string;
517
+
description: string option;
518
+
mime_type: string option;
519
+
annotations: Annotated.annotation option;
522
+
let yojson_of_t { name; uri_template; description; mime_type; annotations } =
524
+
("name", `String name);
525
+
("uriTemplate", `String uri_template);
527
+
let assoc = match description with
528
+
| Some desc -> ("description", `String desc) :: assoc
531
+
let assoc = match mime_type with
532
+
| Some mime -> ("mimeType", `String mime) :: assoc
535
+
let assoc = match annotations with
536
+
| Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc
541
+
let t_of_yojson = function
543
+
let name = match List.assoc_opt "name" fields with
544
+
| Some (`String s) -> s
545
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
547
+
let uri_template = match List.assoc_opt "uriTemplate" fields with
548
+
| Some (`String s) -> s
549
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uriTemplate' field", `Assoc fields))
551
+
let description = match List.assoc_opt "description" fields with
552
+
| Some (`String s) -> Some s
555
+
let mime_type = match List.assoc_opt "mimeType" fields with
556
+
| Some (`String s) -> Some s
559
+
let annotations = match List.assoc_opt "annotations" fields with
560
+
| Some json -> Some (Annotated.annotation_of_yojson json)
563
+
{ name; uri_template; description; mime_type; annotations }
564
+
| j -> raise (Json.Of_json ("Expected object for ResourceTemplate", j))
567
+
(** Resource Reference *)
568
+
module ResourceReference = struct
573
+
let yojson_of_t { uri } =
575
+
("type", `String "ref/resource");
576
+
("uri", `String uri);
579
+
let t_of_yojson = function
581
+
let _ = match List.assoc_opt "type" fields with
582
+
| Some (`String "ref/resource") -> ()
583
+
| _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields))
585
+
let uri = match List.assoc_opt "uri" fields with
586
+
| Some (`String s) -> s
587
+
| _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields))
590
+
| j -> raise (Json.Of_json ("Expected object for ResourceReference", j))
593
+
(** Prompt Reference *)
594
+
module PromptReference = struct
599
+
let yojson_of_t { name } =
601
+
("type", `String "ref/prompt");
602
+
("name", `String name);
605
+
let t_of_yojson = function
607
+
let _ = match List.assoc_opt "type" fields with
608
+
| Some (`String "ref/prompt") -> ()
609
+
| _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields))
611
+
let name = match List.assoc_opt "name" fields with
612
+
| Some (`String s) -> s
613
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
616
+
| j -> raise (Json.Of_json ("Expected object for PromptReference", j))
619
+
(** Completion support *)
620
+
module Completion = struct
622
+
module Argument = struct
628
+
let yojson_of_t { name; value } =
630
+
("name", `String name);
631
+
("value", `String value);
634
+
let t_of_yojson = function
636
+
let name = match List.assoc_opt "name" fields with
637
+
| Some (`String s) -> s
638
+
| _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields))
640
+
let value = match List.assoc_opt "value" fields with
641
+
| Some (`String s) -> s
642
+
| _ -> raise (Json.Of_json ("Missing or invalid 'value' field", `Assoc fields))
645
+
| j -> raise (Json.Of_json ("Expected object for Completion.Argument", j))
648
+
module Request = struct
649
+
type reference = [ `Prompt of PromptReference.t | `Resource of ResourceReference.t ]
652
+
argument: Argument.t;
656
+
let yojson_of_reference = function
657
+
| `Prompt p -> PromptReference.yojson_of_t p
658
+
| `Resource r -> ResourceReference.yojson_of_t r
660
+
let reference_of_yojson = function
662
+
(match List.assoc_opt "type" fields with
663
+
| Some (`String "ref/prompt") -> `Prompt (PromptReference.t_of_yojson (`Assoc fields))
664
+
| Some (`String "ref/resource") -> `Resource (ResourceReference.t_of_yojson (`Assoc fields))
665
+
| _ -> raise (Json.Of_json ("Invalid or missing reference type", `Assoc fields)))
666
+
| j -> raise (Json.Of_json ("Expected object for reference", j))
668
+
let yojson_of_t { argument; ref } =
670
+
("argument", Argument.yojson_of_t argument);
671
+
("ref", yojson_of_reference ref);
674
+
let t_of_yojson = function
676
+
let argument = match List.assoc_opt "argument" fields with
677
+
| Some json -> Argument.t_of_yojson json
678
+
| _ -> raise (Json.Of_json ("Missing argument field", `Assoc fields))
680
+
let ref = match List.assoc_opt "ref" fields with
681
+
| Some json -> reference_of_yojson json
682
+
| _ -> raise (Json.Of_json ("Missing ref field", `Assoc fields))
685
+
| j -> raise (Json.Of_json ("Expected object for Completion.Request", j))
687
+
let create ~argument ~ref =
694
+
module Result = struct
695
+
type completion = {
696
+
values: string list;
697
+
has_more: bool option;
702
+
completion: completion;
703
+
meta: Json.t option;
706
+
let yojson_of_completion { values; has_more; total } =
708
+
("values", `List (List.map (fun s -> `String s) values));
710
+
let assoc = match has_more with
711
+
| Some b -> ("hasMore", `Bool b) :: assoc
714
+
let assoc = match total with
715
+
| Some n -> ("total", `Int n) :: assoc
720
+
let completion_of_yojson = function
722
+
let values = match List.assoc_opt "values" fields with
723
+
| Some (`List items) ->
726
+
| _ -> raise (Json.Of_json ("Expected string in values array", `List items))
728
+
| _ -> raise (Json.Of_json ("Missing or invalid 'values' field", `Assoc fields))
730
+
let has_more = match List.assoc_opt "hasMore" fields with
731
+
| Some (`Bool b) -> Some b
733
+
| _ -> raise (Json.Of_json ("Invalid 'hasMore' field", `Assoc fields))
735
+
let total = match List.assoc_opt "total" fields with
736
+
| Some (`Int n) -> Some n
738
+
| _ -> raise (Json.Of_json ("Invalid 'total' field", `Assoc fields))
740
+
{ values; has_more; total }
741
+
| j -> raise (Json.Of_json ("Expected object for completion", j))
743
+
let yojson_of_t { completion; meta } =
745
+
("completion", yojson_of_completion completion);
747
+
let assoc = match meta with
748
+
| Some meta_json -> ("_meta", meta_json) :: assoc
753
+
let t_of_yojson = function
755
+
let completion = match List.assoc_opt "completion" fields with
756
+
| Some json -> completion_of_yojson json
757
+
| _ -> raise (Json.Of_json ("Missing completion field", `Assoc fields))
759
+
let meta = List.assoc_opt "_meta" fields in
760
+
{ completion; meta }
761
+
| j -> raise (Json.Of_json ("Expected object for Completion.Result", j))
763
+
let create ~completion ?meta () =
764
+
{ completion; meta }
module PromptMessage = struct
···
let create_notification = JSONRPCMessage.create_notification
let create_request = JSONRPCMessage.create_request
let create_response = JSONRPCMessage.create_response
874
-
let create_error = JSONRPCMessage.create_error
1197
+
let create_error = JSONRPCMessage.create_error
1199
+
(* Helper functions *)
1200
+
let create_completion_request ~id ~argument ~ref =
1201
+
let params = Completion.Request.to_params { argument; ref } in
1202
+
create_request ~id ~method_:"completion/complete" ~params:(Some params) ()
1204
+
let create_completion_response ~id ~values ?(has_more=None) ?(total=None) ?(meta=None) () =
1205
+
let completion = { Completion.Result.values; has_more; total } in
1206
+
let result = Completion.Result.to_result { completion; meta } in
1207
+
create_response ~id ~result