Model Context Protocol in OCaml

method_ -> meth

+18 -18
lib/mcp.ml
···
module JSONRPCMessage = struct
type notification = {
-
method_: Method.t;
+
meth: Method.t;
params: Json.t option;
}
type request = {
id: RequestId.t;
-
method_: Method.t;
+
meth: Method.t;
params: Json.t option;
progress_token: ProgressToken.t option;
}
···
let yojson_of_notification (n: notification) =
let assoc = [
("jsonrpc", `String "2.0");
-
("method", `String (Method.to_string n.method_));
+
("method", `String (Method.to_string n.meth));
] in
let assoc = match n.params with
| Some params -> ("params", params) :: assoc
···
let assoc = [
("jsonrpc", `String "2.0");
("id", Id.yojson_of_t r.id);
-
("method", `String (Method.to_string r.method_));
+
("method", `String (Method.to_string r.meth));
] in
let assoc = match r.params with
| Some params ->
···
let notification_of_yojson = function
| `Assoc fields ->
-
let method_ = match List.assoc_opt "method" fields with
+
let meth = match List.assoc_opt "method" fields with
| Some (`String s) ->
(try Method.of_string s
with Failure msg -> raise (Json.Of_json (msg, `String s)))
| _ -> raise (Json.Of_json ("Missing or invalid 'method' field", `Assoc fields))
in
let params = List.assoc_opt "params" fields in
-
{ method_; params }
+
{ meth; params }
| j -> raise (Json.Of_json ("Expected object for notification", j))
let request_of_yojson = function
···
| Some id_json -> Id.t_of_yojson id_json
| _ -> raise (Json.Of_json ("Missing or invalid 'id' field", `Assoc fields))
in
-
let method_ = match List.assoc_opt "method" fields with
+
let meth = match List.assoc_opt "method" fields with
| Some (`String s) ->
(try Method.of_string s
with Failure msg -> raise (Json.Of_json (msg, `String s)))
···
| _ -> None)
| _ -> None
in
-
{ id; method_; params; progress_token }
+
{ id; meth; params; progress_token }
| j -> raise (Json.Of_json ("Expected object for request", j))
let response_of_yojson = function
···
raise (Json.Of_json ("Invalid JSONRPC message format", json))
| j -> raise (Json.Of_json ("Expected object for JSONRPC message", j))
-
let create_notification ?(params=None) ~method_ () =
-
Notification { method_; params }
+
let create_notification ?(params=None) ~meth () =
+
Notification { meth; params }
-
let create_request ?(params=None) ?(progress_token=None) ~id ~method_ () =
-
Request { id; method_; params; progress_token }
+
let create_request ?(params=None) ?(progress_token=None) ~id ~meth () =
+
Request { id; meth; params; progress_token }
let create_response ~id ~result =
Response { id; result }
···
let to_jsonrpc ~id t =
let params = yojson_of_t t in
-
JSONRPCMessage.create_request ~id ~method_:Method.Initialize ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.Initialize ~params:(Some params) ()
end
module Result = struct
···
| `Assoc [] -> None
| json -> Some json
in
-
JSONRPCMessage.create_notification ~method_:Method.Initialized ~params ()
+
JSONRPCMessage.create_notification ~meth:Method.Initialized ~params ()
end
end
···
let parse_message json =
JSONRPCMessage.t_of_yojson json
-
let create_notification ?(params=None) ~method_ () =
-
JSONRPCMessage.create_notification ~params ~method_ ()
+
let create_notification ?(params=None) ~meth () =
+
JSONRPCMessage.create_notification ~params ~meth ()
-
let create_request ?(params=None) ?(progress_token=None) ~id ~method_ () =
-
JSONRPCMessage.create_request ~params ~progress_token ~id ~method_ ()
+
let create_request ?(params=None) ?(progress_token=None) ~id ~meth () =
+
JSONRPCMessage.create_request ~params ~progress_token ~id ~meth ()
let create_response = JSONRPCMessage.create_response
let create_error = JSONRPCMessage.create_error
+11 -11
lib/mcp.mli
···
| Progress (** Progress update for long-running operations *)
(** Convert method type to string representation
-
@param method_ The method to convert
+
@param meth The method to convert
@return The string representation of the method (e.g., "initialize", "resources/list")
*)
val to_string : t -> string
···
v}
*)
type notification = {
-
method_: Method.t;
+
meth: Method.t;
(** Method for the notification, using the Method.t type to ensure type safety.
Examples: Method.Initialized, Method.ResourcesUpdated *)
params: Json.t option;
···
id: RequestId.t;
(** Unique identifier for the request, which will be echoed in the response.
This can be a string or integer and should be unique within the session. *)
-
method_: Method.t;
+
meth: Method.t;
(** Method for the request, using the Method.t type to ensure type safety.
Examples: Method.Initialize, Method.ResourcesRead, Method.ToolsCall *)
params: Json.t option;
···
(** Create a new notification message
@param params Optional parameters for the notification
-
@param method_ Method name for the notification
+
@param meth Method name for the notification
@return A new JSON-RPC notification message
*)
-
val create_notification : ?params:Json.t option -> method_:Method.t -> unit -> t
+
val create_notification : ?params:Json.t option -> meth:Method.t -> unit -> t
(** Create a new request message
@param params Optional parameters for the request
@param progress_token Optional progress token for long-running operations
@param id Unique identifier for the request
-
@param method_ Method name for the request
+
@param meth Method name for the request
@return A new JSON-RPC request message
*)
-
val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> method_:Method.t -> unit -> t
+
val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> meth:Method.t -> unit -> t
(** Create a new response message
@param id ID matching the original request
···
- "notifications/tools/list_changed" - Available tools changed
@param params Optional parameters for the notification as a JSON value
-
@param method_ Method type for the notification
+
@param meth Method type for the notification
@return A new JSON-RPC notification message
*)
-
val create_notification : ?params:Json.t option -> method_:Method.t -> unit -> JSONRPCMessage.t
+
val create_notification : ?params:Json.t option -> meth:Method.t -> unit -> JSONRPCMessage.t
(** Create a new request message
···
@param progress_token Optional progress token for long-running operations
that can report progress updates
@param id Unique identifier for the request, used to correlate with the response
-
@param method_ Method type for the request
+
@param meth Method type for the request
@return A new JSON-RPC request message
*)
-
val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> method_:Method.t -> unit -> JSONRPCMessage.t
+
val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> meth:Method.t -> unit -> JSONRPCMessage.t
(** Create a new response message
+11 -11
lib/mcp_message.ml
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { cursor } in
-
JSONRPCMessage.create_request ~id ~method_:Method.ResourcesList ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.ResourcesList ~params:(Some params) ()
let create_response ~id ~resources ?next_cursor () =
let result = Response.yojson_of_t { resources; next_cursor } in
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { uri } in
-
JSONRPCMessage.create_request ~id ~method_:Method.ResourcesRead ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.ResourcesRead ~params:(Some params) ()
let create_response ~id ~contents () =
let result = Response.yojson_of_t { contents } in
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { cursor } in
-
JSONRPCMessage.create_request ~id ~method_:Method.ToolsList ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.ToolsList ~params:(Some params) ()
let create_response ~id ~tools ?next_cursor () =
let result = Response.yojson_of_t { tools; next_cursor } in
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { name; arguments } in
-
JSONRPCMessage.create_request ~id ~method_:Method.ToolsCall ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.ToolsCall ~params:(Some params) ()
let create_response ~id ~content ~is_error () =
let result = Response.yojson_of_t { content; is_error } in
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { cursor } in
-
JSONRPCMessage.create_request ~id ~method_:Method.PromptsList ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.PromptsList ~params:(Some params) ()
let create_response ~id ~prompts ?next_cursor () =
let result = Response.yojson_of_t { prompts; next_cursor } in
···
| None -> `Int (Random.int 10000)
in
let params = Request.yojson_of_t { name; arguments } in
-
JSONRPCMessage.create_request ~id ~method_:Method.PromptsGet ~params:(Some params) ()
+
JSONRPCMessage.create_request ~id ~meth:Method.PromptsGet ~params:(Some params) ()
let create_response ~id ?description ~messages () =
let result = Response.yojson_of_t { description; messages } in
···
(* No parameters for these notifications *)
let create_resources_notification () =
-
JSONRPCMessage.create_notification ~method_:Method.ResourcesListChanged ()
+
JSONRPCMessage.create_notification ~meth:Method.ResourcesListChanged ()
let create_tools_notification () =
-
JSONRPCMessage.create_notification ~method_:Method.ToolsListChanged ()
+
JSONRPCMessage.create_notification ~meth:Method.ToolsListChanged ()
let create_prompts_notification () =
-
JSONRPCMessage.create_notification ~method_:Method.PromptsListChanged ()
+
JSONRPCMessage.create_notification ~meth:Method.PromptsListChanged ()
end
(* Resource Updated Notification *)
···
let create_notification ~uri () =
let params = Notification.yojson_of_t { uri } in
-
JSONRPCMessage.create_notification ~method_:Method.ResourcesUpdated ~params:(Some params) ()
+
JSONRPCMessage.create_notification ~meth:Method.ResourcesUpdated ~params:(Some params) ()
end
(* Progress Notification *)
···
let create_notification ~progress ~total ~progress_token () =
let params = Notification.yojson_of_t { progress; total; progress_token } in
-
JSONRPCMessage.create_notification ~method_:Method.Progress ~params:(Some params) ()
+
JSONRPCMessage.create_notification ~meth:Method.Progress ~params:(Some params) ()
end
(* Type aliases for backward compatibility *)
+1 -1
lib/mcp_sdk.ml
···
("total", `Float total);
("progressToken", ProgressToken.yojson_of_t token)
] in
-
Some (create_notification ~method_:Method.Progress ~params:(Some params) ())
+
Some (create_notification ~meth:Method.Progress ~params:(Some params) ())
| _ -> None
end
+7 -7
lib/mcp_server.ml
···
Log.debug (Printf.sprintf "Processing message: %s" (Yojson.Safe.to_string message));
match JSONRPCMessage.t_of_yojson message with
| JSONRPCMessage.Request req ->
-
Log.debug (Printf.sprintf "Received request with method: %s" (Method.to_string req.method_));
-
(match req.method_ with
+
Log.debug (Printf.sprintf "Received request with method: %s" (Method.to_string req.meth));
+
(match req.meth with
| Method.Initialize -> handle_initialize server req
| Method.ToolsList -> handle_tools_list server req
| Method.ToolsCall -> handle_tools_call server req
| Method.PromptsList -> handle_prompts_list server req
| Method.ResourcesList -> handle_resources_list server req
| _ ->
-
Log.error (Printf.sprintf "Unknown method received: %s" (Method.to_string req.method_));
-
Some (create_jsonrpc_error req.id MethodNotFound ("Method not found: " ^ (Method.to_string req.method_)) ()))
+
Log.error (Printf.sprintf "Unknown method received: %s" (Method.to_string req.meth));
+
Some (create_jsonrpc_error req.id MethodNotFound ("Method not found: " ^ (Method.to_string req.meth)) ()))
| JSONRPCMessage.Notification notif ->
-
Log.debug (Printf.sprintf "Received notification with method: %s" (Method.to_string notif.method_));
-
(match notif.method_ with
+
Log.debug (Printf.sprintf "Received notification with method: %s" (Method.to_string notif.meth));
+
(match notif.meth with
| Method.Initialized -> handle_initialized notif
| _ ->
-
Log.debug (Printf.sprintf "Ignoring notification: %s" (Method.to_string notif.method_));
+
Log.debug (Printf.sprintf "Ignoring notification: %s" (Method.to_string notif.meth));
None)
| JSONRPCMessage.Response _ ->
Log.error "Unexpected response message received";