My agentic slop goes here. Not intended for anyone else!
at main 3.9 kB view raw
1(** JSON-RPC 2.0 protocol implementation using jsont. 2 3 Based on the JSON-RPC 2.0 specification: https://www.jsonrpc.org/ 4 5 This module provides type-safe encoding/decoding of JSON-RPC messages 6 with forward-compatible unknown field preservation. *) 7 8(** {1 Protocol Version} *) 9 10type jsonrpc = [ `V2 ] 11(** JSON-RPC protocol version *) 12 13val jsonrpc_jsont : jsonrpc Jsont.t 14(** Codec for protocol version *) 15 16(** {1 Request/Response Identifiers} *) 17 18module Id : sig 19 type t = [ `String of string | `Number of float | `Null ] 20 (** Request/response correlation ID. 21 Can be a string, number, or null. *) 22 23 val jsont : t Jsont.t 24 (** Codec for IDs *) 25 26 val to_string : t -> string 27 (** Convert ID to string representation *) 28 29 val compare : t -> t -> int 30 (** Compare IDs for ordering *) 31 32 val pp : Format.formatter -> t -> unit 33 (** Pretty-print an ID *) 34end 35 36(** {1 Error Codes} *) 37 38module Error_code : sig 39 type t = 40 | Parse_error (** -32700: Invalid JSON *) 41 | Invalid_request (** -32600: Invalid Request object *) 42 | Method_not_found (** -32601: Method does not exist *) 43 | Invalid_params (** -32602: Invalid method parameters *) 44 | Internal_error (** -32603: Internal JSON-RPC error *) 45 | Connection_closed (** -32000: MCP-specific: connection closed *) 46 | Server_error of int (** -32099 to -32000: Server error *) 47 | Other of int (** Implementation-defined error *) 48 49 val jsont : t Jsont.t 50 (** Codec for error codes *) 51 52 val to_int : t -> int 53 (** Convert error code to integer *) 54 55 val of_int : int -> t 56 (** Convert integer to error code *) 57 58 val pp : Format.formatter -> t -> unit 59 (** Pretty-print an error code *) 60end 61 62(** {1 Error Data} *) 63 64module Error_data : sig 65 type t = { 66 code : Error_code.t; 67 message : string; 68 data : Jsont.json option; 69 unknown : Jsont.json; 70 } 71 (** Error information *) 72 73 val make : code:Error_code.t -> message:string -> ?data:Jsont.json -> unit -> t 74 (** Create error data *) 75 76 val jsont : t Jsont.t 77 (** Codec for error data *) 78 79 val pp : Format.formatter -> t -> unit 80 (** Pretty-print error data *) 81end 82 83(** {1 Params} *) 84 85type params = Jsont.json 86(** Parameters for requests (must be Array or Object) *) 87 88val params_jsont : params Jsont.t 89(** Codec for params (validates array or object) *) 90 91(** {1 Request Message} *) 92 93module Request : sig 94 type t = { 95 jsonrpc : jsonrpc; 96 method_ : string; 97 params : params option; 98 id : Id.t option; 99 unknown : Jsont.json; 100 } 101 (** JSON-RPC request. 102 - If [id] is [Some _], expects a response 103 - If [id] is [None], it's a notification (no response) *) 104 105 val make : 106 method_:string -> 107 ?params:params -> 108 ?id:Id.t -> 109 unit -> 110 t 111 (** Create a request *) 112 113 val jsont : t Jsont.t 114 (** Codec for requests *) 115 116 val pp : Format.formatter -> t -> unit 117 (** Pretty-print a request *) 118end 119 120(** {1 Response Message} *) 121 122module Response : sig 123 type t = { 124 jsonrpc : jsonrpc; 125 value : (Jsont.json, Error_data.t) result; 126 id : Id.t; 127 unknown : Jsont.json; 128 } 129 (** JSON-RPC response. 130 Either contains [Ok result] or [Error error]. *) 131 132 val make_result : id:Id.t -> result:Jsont.json -> t 133 (** Create a successful response *) 134 135 val make_error : id:Id.t -> error:Error_data.t -> t 136 (** Create an error response *) 137 138 val jsont : t Jsont.t 139 (** Codec for responses *) 140 141 val pp : Format.formatter -> t -> unit 142 (** Pretty-print a response *) 143end 144 145(** {1 Message Union} *) 146 147module Message : sig 148 type t = 149 | Request of Request.t 150 | Response of Response.t 151 (** Union of all JSON-RPC message types *) 152 153 val jsont : t Jsont.t 154 (** Codec for messages *) 155 156 val classify : Jsont.json -> t 157 (** Classify a JSON value as a specific message type *) 158 159 val pp : Format.formatter -> t -> unit 160 (** Pretty-print a message *) 161end