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 (** Convert to Mcp_rpc.ToolsList.Tool.t *)
54 val to_rpc_tool_list_tool : t -> Mcp_rpc.ToolsList.Tool.t
55
56 (** Convert a list of Tool.t to the format needed for tools/list response *)
57 val to_rpc_tools_list : t list -> Mcp_rpc.ToolsList.Tool.t list
58
59 (** Convert Mcp_rpc.ToolsCall response content to Mcp.content list *)
60 val rpc_content_to_mcp_content : Mcp_rpc.ToolsCall.ToolContent.t list -> Mcp.content list
61
62 (** Convert Mcp.content list to Mcp_rpc.ToolsCall.ToolContent.t list *)
63 val mcp_content_to_rpc_content : Mcp.content list -> Mcp_rpc.ToolsCall.ToolContent.t list
64
65 (** Create a tool result with content *)
66 val create_tool_result : Mcp.content list -> is_error:bool -> Json.t
67
68 (** Create a tool error result with structured content *)
69 val create_error_result : string -> Json.t
70
71 (** Handle tool execution errors *)
72 val handle_execution_error : string -> Json.t
73
74 (** Handle unknown tool error *)
75 val handle_unknown_tool_error : string -> Json.t
76
77 (** Handle general tool execution exception *)
78 val handle_execution_exception : exn -> Json.t
79end
80
81(** Resources for the MCP server *)
82module Resource : sig
83 type handler = Context.t -> string list -> (string, string) result
84
85 type t = {
86 uri_template: string;
87 description: string option;
88 mime_type: string option;
89 handler: handler;
90 }
91
92 val create : uri_template:string -> ?description:string -> ?mime_type:string -> handler:handler -> unit -> t
93 val to_json : t -> Json.t
94
95 (** Convert to Mcp_rpc.ResourcesList.Resource.t *)
96 val to_rpc_resource_list_resource : t -> Mcp_rpc.ResourcesList.Resource.t
97
98 (** Convert a list of Resource.t to the format needed for resources/list response *)
99 val to_rpc_resources_list : t list -> Mcp_rpc.ResourcesList.Resource.t list
100end
101
102(** Prompts for the MCP server *)
103module Prompt : sig
104 type argument = {
105 name: string;
106 description: string option;
107 required: bool;
108 }
109
110 type message = {
111 role: Role.t;
112 content: content;
113 }
114
115 type handler = Context.t -> (string * string) list -> (message list, string) result
116
117 type t = {
118 name: string;
119 description: string option;
120 arguments: argument list;
121 handler: handler;
122 }
123
124 val create : name:string -> ?description:string -> ?arguments:argument list -> handler:handler -> unit -> t
125 val create_argument : name:string -> ?description:string -> ?required:bool -> unit -> argument
126 val to_json : t -> Json.t
127
128 (** Convert argument to Mcp_rpc.PromptsList.PromptArgument.t *)
129 val argument_to_rpc_prompt_argument : argument -> Mcp_rpc.PromptsList.PromptArgument.t
130
131 (** Convert to Mcp_rpc.PromptsList.Prompt.t *)
132 val to_rpc_prompt_list_prompt : t -> Mcp_rpc.PromptsList.Prompt.t
133
134 (** Convert a list of Prompt.t to the format needed for prompts/list response *)
135 val to_rpc_prompts_list : t list -> Mcp_rpc.PromptsList.Prompt.t list
136
137 (** Convert message to Mcp_rpc.PromptMessage.t *)
138 val message_to_rpc_prompt_message : message -> PromptMessage.t
139
140 (** Convert a list of messages to the format needed for prompts/get response *)
141 val messages_to_rpc_prompt_messages : message list -> PromptMessage.t list
142end
143
144(** Main server type *)
145type server
146
147val name : server -> string
148val version : server -> string
149val protocol_version : server -> string
150val capabilities : server -> Json.t
151val tools : server -> Tool.t list
152val resources : server -> Resource.t list
153val prompts : server -> Prompt.t list
154
155(** Create a new server *)
156val create_server : name:string -> ?version:string -> ?protocol_version:string -> unit -> server
157
158(** Default capabilities for the server *)
159val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t
160
161(** Create and register a tool in one step *)
162val add_tool : server -> name:string -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t
163
164(** Create and register a resource in one step *)
165val add_resource : server -> uri_template:string -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t
166
167(** Create and register a prompt in one step *)
168val add_prompt : server -> name:string -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t
169
170(** Configure server with default capabilities based on registered components *)
171val configure_server : server -> ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> server
172
173val make_tool_schema : (string * string * string) list -> string list -> Json.t