Model Context Protocol in OCaml
1(** MCP - Model Context Protocol implementation *) 2 3open Jsonrpc 4 5(** Common types *) 6 7(** Roles for conversation participants *) 8module Role : sig 9 type t = [ `User | `Assistant ] 10 include Json.Jsonable.S with type t := t 11end 12 13(** Progress tokens for long-running operations *) 14module ProgressToken : sig 15 type t = [ `String of string | `Int of int ] 16 include Json.Jsonable.S with type t := t 17end 18 19(** Request IDs *) 20module RequestId : sig 21 type t = [ `String of string | `Int of int ] 22 include Json.Jsonable.S with type t := t 23end 24 25(** Cursors for pagination *) 26module Cursor : sig 27 type t = string 28 include Json.Jsonable.S with type t := t 29end 30 31(** Annotations for objects *) 32module Annotated : sig 33 type t = { 34 annotations: annotation option; 35 } 36 and annotation = { 37 audience: Role.t list option; 38 priority: float option; 39 } 40 include Json.Jsonable.S with type t := t 41end 42 43(** Text content *) 44module TextContent : sig 45 type t = { 46 text: string; 47 annotations: Annotated.annotation option; 48 } 49 include Json.Jsonable.S with type t := t 50end 51 52(** Image content *) 53module ImageContent : sig 54 type t = { 55 data: string; 56 mime_type: string; 57 annotations: Annotated.annotation option; 58 } 59 include Json.Jsonable.S with type t := t 60end 61 62(** Audio content *) 63module AudioContent : sig 64 type t = { 65 data: string; 66 mime_type: string; 67 annotations: Annotated.annotation option; 68 } 69 include Json.Jsonable.S with type t := t 70end 71 72(** Base resource contents *) 73module ResourceContents : sig 74 type t = { 75 uri: string; 76 mime_type: string option; 77 } 78 include Json.Jsonable.S with type t := t 79end 80 81(** Text resource contents *) 82module TextResourceContents : sig 83 type t = { 84 uri: string; 85 text: string; 86 mime_type: string option; 87 } 88 include Json.Jsonable.S with type t := t 89end 90 91(** Binary resource contents *) 92module BlobResourceContents : sig 93 type t = { 94 uri: string; 95 blob: string; 96 mime_type: string option; 97 } 98 include Json.Jsonable.S with type t := t 99end 100 101(** Embedded resource *) 102module EmbeddedResource : sig 103 type t = { 104 resource: [ `Text of TextResourceContents.t | `Blob of BlobResourceContents.t ]; 105 annotations: Annotated.annotation option; 106 } 107 include Json.Jsonable.S with type t := t 108end 109 110(** Content type used in messages *) 111type content = 112 | Text of TextContent.t 113 | Image of ImageContent.t 114 | Audio of AudioContent.t 115 | Resource of EmbeddedResource.t 116 117val yojson_of_content : content -> Json.t 118val content_of_yojson : Json.t -> content 119 120(** Message for prompts *) 121module PromptMessage : sig 122 type t = { 123 role: Role.t; 124 content: content; 125 } 126 include Json.Jsonable.S with type t := t 127end 128 129(** Message for sampling *) 130module SamplingMessage : sig 131 type t = { 132 role: Role.t; 133 content: [ `Text of TextContent.t | `Image of ImageContent.t | `Audio of AudioContent.t ]; 134 } 135 include Json.Jsonable.S with type t := t 136end 137 138(** Implementation information *) 139module Implementation : sig 140 type t = { 141 name: string; 142 version: string; 143 } 144 include Json.Jsonable.S with type t := t 145end 146 147(** JSONRPC message types *) 148module JSONRPCMessage : sig 149 type notification = { 150 method_: string; 151 params: Json.t option; 152 } 153 154 type request = { 155 id: RequestId.t; 156 method_: string; 157 params: Json.t option; 158 progress_token: ProgressToken.t option; 159 } 160 161 type response = { 162 id: RequestId.t; 163 result: Json.t; 164 } 165 166 type error = { 167 id: RequestId.t; 168 code: int; 169 message: string; 170 data: Json.t option; 171 } 172 173 type t = 174 | Notification of notification 175 | Request of request 176 | Response of response 177 | Error of error 178 179 val yojson_of_notification : notification -> Json.t 180 val yojson_of_request : request -> Json.t 181 val yojson_of_response : response -> Json.t 182 val yojson_of_error : error -> Json.t 183 val yojson_of_t : t -> Json.t 184 185 val notification_of_yojson : Json.t -> notification 186 val request_of_yojson : Json.t -> request 187 val response_of_yojson : Json.t -> response 188 val error_of_yojson : Json.t -> error 189 val t_of_yojson : Json.t -> t 190 191 val create_notification : ?params:Json.t option -> method_:string -> unit -> t 192 val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> method_:string -> unit -> t 193 val create_response : id:RequestId.t -> result:Json.t -> t 194 val create_error : id:RequestId.t -> code:int -> message:string -> ?data:Json.t option -> unit -> t 195end 196 197(** Initialize request/response *) 198module Initialize : sig 199 (** Initialize request *) 200 module Request : sig 201 type t = { 202 capabilities: Json.t; (** ClientCapabilities *) 203 client_info: Implementation.t; 204 protocol_version: string; 205 } 206 include Json.Jsonable.S with type t := t 207 208 val create : capabilities:Json.t -> client_info:Implementation.t -> protocol_version:string -> t 209 val to_jsonrpc : id:RequestId.t -> t -> JSONRPCMessage.t 210 end 211 212 (** Initialize result *) 213 module Result : sig 214 type t = { 215 capabilities: Json.t; (** ServerCapabilities *) 216 server_info: Implementation.t; 217 protocol_version: string; 218 instructions: string option; 219 meta: Json.t option; 220 } 221 include Json.Jsonable.S with type t := t 222 223 val create : capabilities:Json.t -> server_info:Implementation.t -> protocol_version:string -> ?instructions:string -> ?meta:Json.t -> unit -> t 224 val to_jsonrpc : id:RequestId.t -> t -> JSONRPCMessage.t 225 end 226end 227 228(** Initialized notification *) 229module Initialized : sig 230 module Notification : sig 231 type t = { 232 meta: Json.t option; 233 } 234 include Json.Jsonable.S with type t := t 235 236 val create : ?meta:Json.t -> unit -> t 237 val to_jsonrpc : t -> JSONRPCMessage.t 238 end 239end 240 241(** Parse a JSON message into an MCP message *) 242val parse_message : Json.t -> JSONRPCMessage.t 243 244(** Create JSONRPC message helpers *) 245val create_notification : ?params:Json.t option -> method_:string -> unit -> JSONRPCMessage.t 246val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> method_:string -> unit -> JSONRPCMessage.t 247val create_response : id:RequestId.t -> result:Json.t -> JSONRPCMessage.t 248val create_error : id:RequestId.t -> code:int -> message:string -> ?data:Json.t option -> unit -> JSONRPCMessage.t