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 yojson_of_message : message -> Json.t
90 val message_of_yojson : Json.t -> message
91 val to_json : t -> Json.t
92end
93
94(** Main server type *)
95type server = {
96 name: string;
97 version: string;
98 protocol_version: string;
99 mutable capabilities: Json.t;
100 mutable tools: Tool.t list;
101 mutable resources: Resource.t list;
102 mutable prompts: Prompt.t list;
103 mutable lifespan_context: (string * Json.t) list;
104 mutable startup_hook: (unit -> unit) option;
105 mutable shutdown_hook: (unit -> unit) option;
106}
107
108(** Create a new server *)
109val create_server : name:string -> ?version:string -> ?protocol_version:string -> unit -> server
110
111(** Default capabilities for the server *)
112val default_capabilities : ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> Json.t
113
114(** Register a tool with the server *)
115val register_tool : server -> Tool.t -> Tool.t
116
117(** Create and register a tool in one step *)
118val add_tool : server -> name:string -> ?description:string -> ?schema_properties:(string * string * string) list -> ?schema_required:string list -> (Json.t -> Json.t) -> Tool.t
119
120(** Register a resource with the server *)
121val register_resource : server -> Resource.t -> Resource.t
122
123(** Create and register a resource in one step *)
124val add_resource : server -> uri_template:string -> ?description:string -> ?mime_type:string -> (string list -> string) -> Resource.t
125
126(** Register a prompt with the server *)
127val register_prompt : server -> Prompt.t -> Prompt.t
128
129(** Create and register a prompt in one step *)
130val add_prompt : server -> name:string -> ?description:string -> ?arguments:(string * string option * bool) list -> ((string * string) list -> Prompt.message list) -> Prompt.t
131
132(** Set server capabilities *)
133val set_capabilities : server -> Json.t -> unit
134
135(** Configure server with default capabilities based on registered components *)
136val configure_server : server -> ?with_tools:bool -> ?with_resources:bool -> ?with_prompts:bool -> unit -> server
137
138(** Set startup hook *)
139val set_startup_hook : server -> (unit -> unit) -> unit
140
141(** Set shutdown hook *)
142val set_shutdown_hook : server -> (unit -> unit) -> unit
143
144(** Run the server using stdio transport (legacy method) *)
145val run_server : server -> unit
146
147(** Transport type for the server *)
148type transport_type =
149 | Stdio (** Read/write to stdin/stdout *)
150 | Http (** HTTP server - to be implemented *)
151
152(** Create and start a server with the specified transport *)
153val run_server_with_transport : server -> transport_type -> unit
154
155(** Helper functions for creating common objects *)
156val make_text_content : string -> content
157val make_text_content_with_annotations : string -> Annotated.annotation -> content
158val make_image_content : string -> string -> content
159val make_image_content_with_annotations : string -> string -> Annotated.annotation -> content
160val make_audio_content : string -> string -> content
161val make_audio_content_with_annotations : string -> string -> Annotated.annotation -> content
162val make_text_resource_content : string -> string -> ?mime_type:string -> unit -> content
163val make_blob_resource_content : string -> string -> ?mime_type:string -> unit -> content
164val make_tool_schema : (string * string * string) list -> string list -> Json.t