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