Model Context Protocol in OCaml
1(** MCP SDK - Model Context Protocol SDK for OCaml *) 2 3open Mcp 4open Jsonrpc 5 6(** SDK version *) 7val version : string 8 9(** Logging utilities *) 10module Log : sig 11 type level = Debug | Info | Warning | Error 12 13 val string_of_level : level -> string 14 15 (** Format-string based logging functions *) 16 val logf : level -> ('a, out_channel, unit) format -> 'a 17 val debugf : ('a, out_channel, unit) format -> 'a 18 val infof : ('a, out_channel, unit) format -> 'a 19 val warningf : ('a, out_channel, unit) format -> 'a 20 val errorf : ('a, out_channel, unit) format -> 'a 21 22 (** Simple string logging functions (for backward compatibility) *) 23 val log : level -> string -> unit 24 val debug : string -> unit 25 val info : string -> unit 26 val warning : string -> unit 27 val error : string -> unit 28end 29 30(** Context for tools and resources *) 31module Context : sig 32 type t 33 34 val create : ?request_id:RequestId.t -> ?progress_token:ProgressToken.t -> ?lifespan_context:(string * Json.t) list -> unit -> t 35 val get_context_value : t -> string -> Json.t option 36 val report_progress : t -> float -> float -> JSONRPCMessage.t option 37end 38 39(** Tools for the MCP server *) 40module Tool : sig 41 type handler = Context.t -> Json.t -> (Json.t, string) result 42 43 type t = { 44 name: string; 45 description: string option; 46 input_schema: Json.t; 47 handler: handler; 48 } 49 50 val create : name:string -> ?description:string -> input_schema:Json.t -> handler:handler -> unit -> t 51 val to_json : t -> Json.t 52 53 (** Create a tool result with content *) 54 val create_tool_result : Mcp.content list -> is_error:bool -> Json.t 55 56 (** Create a tool error result with structured content *) 57 val create_error_result : string -> Json.t 58 59 (** Handle tool execution errors *) 60 val handle_execution_error : string -> Json.t 61 62 (** Handle unknown tool error *) 63 val handle_unknown_tool_error : string -> Json.t 64 65 (** Handle general tool execution exception *) 66 val handle_execution_exception : exn -> Json.t 67end 68 69(** Resources for the MCP server *) 70module Resource : sig 71 type handler = Context.t -> string list -> (string, string) result 72 73 type t = { 74 uri_template: string; 75 description: string option; 76 mime_type: string option; 77 handler: handler; 78 } 79 80 val create : uri_template:string -> ?description:string -> ?mime_type:string -> handler:handler -> unit -> t 81 val to_json : t -> Json.t 82end 83 84(** Prompts for the MCP server *) 85module Prompt : sig 86 type argument = { 87 name: string; 88 description: string option; 89 required: bool; 90 } 91 92 type message = { 93 role: Role.t; 94 content: content; 95 } 96 97 type handler = Context.t -> (string * string) list -> (message list, string) result 98 99 type t = { 100 name: string; 101 description: string option; 102 arguments: argument list; 103 handler: handler; 104 } 105 106 val create : name:string -> ?description:string -> ?arguments:argument list -> handler:handler -> unit -> t 107 val create_argument : name:string -> ?description:string -> ?required:bool -> unit -> argument 108 val to_json : t -> Json.t 109end 110 111(** Main server type *) 112type server 113 114val name : server -> string 115val version : server -> string 116val protocol_version : server -> string 117val capabilities : server -> Json.t 118val tools : server -> Tool.t list 119val resources : server -> Resource.t list 120val prompts : server -> Prompt.t list 121 122(** Create a new server *) 123val create_server : name:string -> ?version:string -> ?protocol_version:string -> unit -> server 124 125(** Default capabilities for the server *) 126val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t 127 128(** Create and register a tool in one step *) 129val add_tool : server -> name:string -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t 130 131(** Create and register a resource in one step *) 132val add_resource : server -> uri_template:string -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t 133 134(** Create and register a prompt in one step *) 135val add_prompt : server -> name:string -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t 136 137(** Configure server with default capabilities based on registered components *) 138val configure_server : server -> ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> server 139 140val make_tool_schema : (string * string * string) list -> string list -> Json.t