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 val log : level -> string -> unit 16 val debug : string -> unit 17 val info : string -> unit 18 val warning : string -> unit 19 val error : string -> unit 20end 21 22(** Context for tools and resources *) 23module Context : sig 24 type t = { 25 request_id: RequestId.t option; 26 lifespan_context: (string * Json.t) list; 27 mutable progress_token: ProgressToken.t option; 28 } 29 30 val create : ?request_id:RequestId.t -> ?lifespan_context:(string * Json.t) list -> unit -> t 31 val get_context_value : t -> string -> Json.t option 32 val report_progress : t -> float -> float -> JSONRPCMessage.t option 33end 34 35(** Tools for the MCP server *) 36module Tool : sig 37 type handler = Context.t -> Json.t -> (Json.t, string) result 38 39 type t = { 40 name: string; 41 description: string option; 42 input_schema: Json.t; 43 handler: handler; 44 } 45 46 val create : name:string -> ?description:string -> input_schema:Json.t -> handler:handler -> unit -> t 47 val to_json : t -> Json.t 48end 49 50(** Resources for the MCP server *) 51module Resource : sig 52 type handler = Context.t -> string list -> (string, string) result 53 54 type t = { 55 uri_template: string; 56 description: string option; 57 mime_type: string option; 58 handler: handler; 59 } 60 61 val create : uri_template:string -> ?description:string -> ?mime_type:string -> handler:handler -> unit -> t 62 val to_json : t -> Json.t 63end 64 65(** Prompts for the MCP server *) 66module Prompt : sig 67 type argument = { 68 name: string; 69 description: string option; 70 required: bool; 71 } 72 73 type message = { 74 role: Role.t; 75 content: content; 76 } 77 78 type handler = Context.t -> (string * string) list -> (message list, string) result 79 80 type t = { 81 name: string; 82 description: string option; 83 arguments: argument list; 84 handler: handler; 85 } 86 87 val create : name:string -> ?description:string -> ?arguments:argument list -> handler:handler -> unit -> t 88 val create_argument : name:string -> ?description:string -> ?required:bool -> unit -> argument 89 val to_json : t -> Json.t 90end 91 92(** Server implementation *) 93module Server : sig 94 type startup_hook = unit -> unit 95 type shutdown_hook = unit -> unit 96 97 type t = { 98 name: string; 99 version: string; 100 protocol_version: string; 101 mutable capabilities: Json.t; 102 mutable tools: Tool.t list; 103 mutable resources: Resource.t list; 104 mutable prompts: Prompt.t list; 105 mutable lifespan_context: (string * Json.t) list; 106 startup_hook: startup_hook option; 107 shutdown_hook: shutdown_hook option; 108 } 109 110 val create : name:string -> ?version:string -> ?protocol_version:string -> ?startup_hook:startup_hook -> ?shutdown_hook:shutdown_hook -> unit -> t 111 val register_tool : t -> Tool.t -> unit 112 val register_resource : t -> Resource.t -> unit 113 val register_prompt : t -> Prompt.t -> unit 114 val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t 115 val update_capabilities : t -> Json.t -> unit 116 117 val process_message : t -> Json.t -> JSONRPCMessage.t option 118 val run : t -> unit 119end 120 121(** Helper functions for creating common objects *) 122val make_text_content : string -> content 123val make_tool_schema : (string * string * string) list -> string list -> Json.t 124 125(** Syntax sugar for creating an MCP server *) 126module MakeServer : functor (S : sig 127 val name : string 128 val version : string option 129end) -> sig 130 val _config : string * string option (* Used to prevent unused parameter warning *) 131 val server : Server.t 132 133 val tool : ?name:string option -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t 134 val resource : ?uri_template:string option -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t 135 val prompt : ?name:string option -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t 136 val run : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> unit 137end