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
26 val create : ?request_id:RequestId.t -> ?progress_token:ProgressToken.t -> ?lifespan_context:(string * Json.t) list -> unit -> t
27 val get_context_value : t -> string -> Json.t option
28 val report_progress : t -> float -> float -> JSONRPCMessage.t option
29end
30
31(** Tools for the MCP server *)
32module Tool : sig
33 type handler = Context.t -> Json.t -> (Json.t, string) result
34
35 type t = {
36 name: string;
37 description: string option;
38 input_schema: Json.t;
39 handler: handler;
40 }
41
42 val create : name:string -> ?description:string -> input_schema:Json.t -> handler:handler -> unit -> t
43 val to_json : t -> Json.t
44end
45
46(** Resources for the MCP server *)
47module Resource : sig
48 type handler = Context.t -> string list -> (string, string) result
49
50 type t = {
51 uri_template: string;
52 description: string option;
53 mime_type: string option;
54 handler: handler;
55 }
56
57 val create : uri_template:string -> ?description:string -> ?mime_type:string -> handler:handler -> unit -> t
58 val to_json : t -> Json.t
59end
60
61(** Prompts for the MCP server *)
62module Prompt : sig
63 type argument = {
64 name: string;
65 description: string option;
66 required: bool;
67 }
68
69 type message = {
70 role: Role.t;
71 content: content;
72 }
73
74 type handler = Context.t -> (string * string) list -> (message list, string) result
75
76 type t = {
77 name: string;
78 description: string option;
79 arguments: argument list;
80 handler: handler;
81 }
82
83 val create : name:string -> ?description:string -> ?arguments:argument list -> handler:handler -> unit -> t
84 val create_argument : name:string -> ?description:string -> ?required:bool -> unit -> argument
85 val to_json : t -> Json.t
86end
87
88(** Main server type *)
89type server
90
91val name : server -> string
92val version : server -> string
93val protocol_version : server -> string
94val capabilities : server -> Json.t
95val tools : server -> Tool.t list
96val resources : server -> Resource.t list
97val prompts : server -> Prompt.t list
98
99(** Create a new server *)
100val create_server : name:string -> ?version:string -> ?protocol_version:string -> unit -> server
101
102(** Default capabilities for the server *)
103val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t
104
105(** Create and register a tool in one step *)
106val add_tool : server -> name:string -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t
107
108(** Create and register a resource in one step *)
109val add_resource : server -> uri_template:string -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t
110
111(** Create and register a prompt in one step *)
112val add_prompt : server -> name:string -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t
113
114(** Configure server with default capabilities based on registered components *)
115val configure_server : server -> ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> server
116
117(** Content type constructors *)
118val make_text_content : string -> content
119val make_image_content : string -> string -> content
120val make_audio_content : string -> string -> content
121val make_resource_text_content : string -> string -> string option -> content
122val make_resource_blob_content : string -> string -> string option -> content
123
124(** Error types with standard JSON-RPC error codes *)
125type error_code =
126 | ParseError (** -32700 *)
127 | InvalidRequest (** -32600 *)
128 | MethodNotFound (** -32601 *)
129 | InvalidParams (** -32602 *)
130 | InternalError (** -32603 *)
131 | ResourceNotFound (** -32002 Custom code for MCP *)
132 | AuthenticationRequired (** -32001 Custom code for MCP *)
133 | CustomError of int
134
135val error_code_to_int : error_code -> int
136
137(** Tool result handling *)
138type tool_content =
139 | TextContent of string
140 | ImageContent of { data: string; mime_type: string }
141 | AudioContent of { data: string; mime_type: string }
142 | ResourceContent of { uri: string; data: string; is_blob: bool; mime_type: string option }
143
144val create_tool_result : tool_content list -> is_error:bool -> Json.t
145
146(** Create a rich tool result with multiple content types *)
147val create_rich_tool_result :
148 ?text:string option ->
149 ?image:(string * string) option ->
150 ?audio:(string * string) option ->
151 ?resource:(string * string * bool * string option) option ->
152 is_error:bool ->
153 unit -> Json.t
154
155(** Helper functions for creating common objects *)
156val make_tool_schema : (string * string * string) list -> string list -> Json.t