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(** Main server type *)
93type server
94
95val name : server -> string
96val version : server -> string
97val protocol_version : server -> string
98val capabilities : server -> Json.t
99val tools : server -> Tool.t list
100val resources : server -> Resource.t list
101val prompts : server -> Prompt.t list
102
103(** Create a new server *)
104val create_server : name:string -> ?version:string -> ?protocol_version:string -> unit -> server
105
106(** Default capabilities for the server *)
107val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t
108
109(** Register a tool with the server *)
110val register_tool : server -> Tool.t -> Tool.t
111
112(** Create and register a tool in one step *)
113val add_tool : server -> name:string -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t
114
115(** Register a resource with the server *)
116val register_resource : server -> Resource.t -> Resource.t
117
118(** Create and register a resource in one step *)
119val add_resource : server -> uri_template:string -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t
120
121(** Register a prompt with the server *)
122val register_prompt : server -> Prompt.t -> Prompt.t
123
124(** Create and register a prompt in one step *)
125val add_prompt : server -> name:string -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t
126
127(** Set server capabilities *)
128val set_capabilities : server -> Json.t -> unit
129
130(** Configure server with default capabilities based on registered components *)
131val configure_server : server -> ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> server
132
133(** Content type constructors *)
134val make_text_content : string -> content
135val make_image_content : string -> string -> content
136val make_audio_content : string -> string -> content
137val make_resource_text_content : string -> string -> string option -> content
138val make_resource_blob_content : string -> string -> string option -> content
139
140(** Error types with standard JSON-RPC error codes *)
141type error_code =
142 | ParseError (** -32700 *)
143 | InvalidRequest (** -32600 *)
144 | MethodNotFound (** -32601 *)
145 | InvalidParams (** -32602 *)
146 | InternalError (** -32603 *)
147 | ResourceNotFound (** -32002 Custom code for MCP *)
148 | AuthenticationRequired (** -32001 Custom code for MCP *)
149 | CustomError of int
150
151val error_code_to_int : error_code -> int
152
153(** Tool result handling *)
154type tool_content =
155 | TextContent of string
156 | ImageContent of { data: string; mime_type: string }
157 | AudioContent of { data: string; mime_type: string }
158 | ResourceContent of { uri: string; data: string; is_blob: bool; mime_type: string option }
159
160val create_tool_result : tool_content list -> is_error:bool -> Json.t
161
162(** Create a rich tool result with multiple content types *)
163val create_rich_tool_result :
164 ?text:string option ->
165 ?image:(string * string) option ->
166 ?audio:(string * string) option ->
167 ?resource:(string * string * bool * string option) option ->
168 is_error:bool ->
169 unit -> Json.t
170
171(** Helper functions for creating common objects *)
172val make_tool_schema : (string * string * string) list -> string list -> Json.t