Model Context Protocol in OCaml

more

Changed files
+9 -21
lib
+3 -3
lib/mcp_sdk.ml
···
type t = {
request_id: RequestId.t option;
lifespan_context: (string * Json.t) list;
-
mutable progress_token: ProgressToken.t option;
+
progress_token: ProgressToken.t option;
}
-
let create ?request_id ?(lifespan_context=[]) () =
-
{ request_id; lifespan_context; progress_token = None }
+
let create ?request_id ?progress_token ?(lifespan_context=[]) () =
+
{ request_id; lifespan_context; progress_token }
let get_context_value ctx key =
List.assoc_opt key ctx.lifespan_context
+2 -6
lib/mcp_sdk.mli
···
(** Context for tools and resources *)
module Context : sig
-
type t = {
-
request_id: RequestId.t option;
-
lifespan_context: (string * Json.t) list;
-
mutable progress_token: ProgressToken.t option;
-
}
+
type t
-
val create : ?request_id:RequestId.t -> ?lifespan_context:(string * Json.t) list -> unit -> t
+
val create : ?request_id:RequestId.t -> ?progress_token:ProgressToken.t -> ?lifespan_context:(string * Json.t) list -> unit -> t
val get_context_value : t -> string -> Json.t option
val report_progress : t -> float -> float -> JSONRPCMessage.t option
end
+4 -12
lib/mcp_server.ml
···
(* Create context for this request *)
let ctx = Context.create
-
?request_id:(Some req.id) (* Store request ID for progress reporting *)
-
~lifespan_context:[("tools/call", `Assoc params)] (* Store params for reference *)
+
?request_id:(Some req.id)
+
?progress_token:req.progress_token
+
~lifespan_context:[("tools/call", `Assoc params)]
()
in
-
-
(* Set progress token if present *)
-
ctx.progress_token <- req.progress_token;
-
+
(* Execute the tool *)
let result = execute_tool server ctx name args in
-
-
(* Process progress messages if any *)
-
let progress_msg = Context.report_progress ctx 1.0 1.0 in
-
(match progress_msg with
-
| Some _msg -> Log.debug "Progress complete notification would be sent here";
-
| None -> ());
Some (create_response ~id:req.id ~result)
| None ->