Module Spindle_mcp_client

Calling another server's tools, and reading and watching its resources, over the Model Context Protocol, 2026-07-28, on Spindle_client alone: a program that only calls another server installs and links no framework.

let weather =
  Spindle_mcp_client.create client ~url:"https://weather.example.com/mcp"

let* tools = Spindle_mcp_client.tools weather in
let* forecast =
  Spindle_mcp_client.call weather "get_forecast" query_json
    { city = "Paris" } forecast_json
in
...

Each call is one POST, answered on its own, as 2026-07-28 has it: a value of this module holds the server's address, what this client says of itself, and the tools it last listed, and nothing of a session. Every request carries the headers and the _meta the revision asks for, and reads its answer as one JSON object or an event stream, whichever the server sent.

type t

A server to call.

type reply =
  1. | Accept of Wiretype.Value.t
    (*

    the content, as the request's schema asks

    *)
  2. | Decline
  3. | Cancel
    (*

    dismissed, with no choice made

    *)
type elicit = message:string -> schema:Wiretype.Value.t -> reply

How a question a server asks in the middle of a call is answered -- by a person, a form, a policy: its message, and the schema of what it asks for.

val create : ?name:string -> ?version:string -> ?headers:(string * string) list -> ?elicit:elicit -> ?rounds:int -> ?oauth:Spindle_client.Oauth.oauth -> Spindle_client.t -> url:string -> t

create client ~url: the MCP endpoint at url, called through client. Where name and version are both given, every request names this client by them (clientInfo). headers go with every request: a credential, say.

elicit answers a server's questions, and is how this client says it can (elicitation among its capabilities): without it, a server may not ask. A call a server asks of is retried with the answers, as a new request, at most rounds (3) times.

oauth signs a person in where the server needs a token, as MCP's authorization has it: the token kept for url goes with every request; a 401 signs in at the authorization server its challenge's metadata names, and a 403 insufficient_scope steps up to the scope it names, keeping those granted, each retried once. Does no I/O.

Answers

type refusal = {
  1. status : Spindle_http.Status.t;
  2. code : int option;
    (*

    the JSON-RPC error's, where one was sent

    *)
  3. message : string;
    (*

    the server's words, for a log

    *)
}
type error = [
  1. | Spindle_client.error
  2. | `Refused of refusal
    (*

    the server answered no result: an HTTP refusal -- a 401, a 403, a 429 -- or a JSON-RPC error

    *)
  3. | `Unreadable of string
    (*

    an answer that is no MCP answer, or a result its description does not read; in words

    *)
  4. | `Unanswered of string
    (*

    a server that asked questions this client cannot answer -- it has no elicit, or the rounds ran out

    *)
  5. | `Oauth of string
    (*

    signing in failed, as Spindle_client.Oauth says

    *)
]
val error_to_string : [< error | `Tool_failed of string ] -> string

Calling

type server = {
  1. versions : string list;
    (*

    supportedVersions

    *)
  2. instructions : string option;
    (*

    for a model, on using the server

    *)
  3. name : string option;
    (*

    serverInfo's, as it says of itself

    *)
  4. version : string option;
}
val discover : t -> (server, [> error ]) Stdlib.result

server/discover.

type tool = {
  1. name : string;
  2. title : string option;
  3. description : string option;
  4. input_schema : Wiretype.Value.t;
  5. output_schema : Wiretype.Value.t option;
}
val tools : t -> (tool list, [> error ]) Stdlib.result

tools/list, every page, in the server's order. A tool whose x-mcp-header annotations break the rules of 2026-07-28 is left out, as the revision asks of a client, with a warn line naming it and why. The list is kept, for the headers each call mirrors.

type progress = {
  1. progress : float;
    (*

    how far the call has come

    *)
  2. total : float option;
  3. message : string option;
}

A report a server sent while a call ran.

type answer = {
  1. content : Wiretype.Value.t list;
    (*

    for a model: text, images, links

    *)
  2. structured : Wiretype.Value.t option;
    (*

    structuredContent

    *)
}
val call : ?progress:(progress -> unit) -> t -> string -> 'a Wiretype.t -> 'a -> 'r Wiretype.t -> ('r, [> error | `Tool_failed of string ]) Stdlib.result

call t name arguments value result: the tool name, its arguments written by arguments and its structuredContent read by result. A result the tool marked isError is `Tool_failed, its text. progress is told each report the server sends while the tool runs, as it arrives, and asks the server for them (progressToken); without it, none is asked for.

val call_value : ?progress:(progress -> unit) -> t -> string -> Wiretype.Value.t -> (answer, [> error | `Tool_failed of string ]) Stdlib.result

call_value t name arguments: as call, for a program that passes a model's JSON through without knowing the tool.

Resources

type resource = {
  1. uri : string;
  2. name : string;
  3. title : string option;
    (*

    for a person

    *)
  4. description : string option;
    (*

    for a model

    *)
  5. mime : string option;
}
type template = {
  1. uri_template : string;
    (*

    RFC 6570

    *)
  2. name : string;
  3. title : string option;
  4. description : string option;
  5. mime : string option;
}
type data =
  1. | Text of string
  2. | Blob of string
    (*

    bytes, decoded from base64

    *)
type content = {
  1. uri : string;
  2. mime : string option;
  3. data : data;
}

One of what a read answers: a resource may answer several.

val resources : t -> (resource list, [> error ]) Stdlib.result

resources/list, every page, in the server's order.

val templates : t -> (template list, [> error ]) Stdlib.result

resources/templates/list, every page, in the server's order.

val read : t -> string -> (content list, [> error ]) Stdlib.result

resources/read of a URI, mirrored in Mcp-Name. A resource the server has not is `Refused with the code -32602.

Watching

type notification =
  1. | Acknowledged of string list
    (*

    the URIs the server watches for this listen, always first: one it left out it will never tell of

    *)
  2. | Updated of string
    (*

    a URI that changed: read it again

    *)

What a listen hands its caller.

type ended =
  1. | Stopped
    (*

    the function answered `Stop

    *)
  2. | Completed
    (*

    the server ended it, with the graceful result: it has nothing more to tell this listen

    *)
  3. | Dropped of string
    (*

    it ended without that result -- the connection lost, the server dropping a listen that fell behind, no word within read_timeout -- in words: changes may have been missed, and the caller listens again and reads from where it was

    *)

How a listen ended, once acknowledged.

val listen : ?read_timeout:Mtime.Span.t -> t -> string list -> (notification -> [ `Continue | `Stop ]) -> (ended, [> error ]) Stdlib.result

listen t uris f: subscriptions/listen for uris, each notification handed to f as it arrives, until the listen ends or f answers `Stop. It runs in the caller's fiber and keeps nothing of the stream; a program reads beside it, in a fiber of its own or from f.

A change made after Acknowledged is told, so a caller that reads from its own place once acknowledged, and again on each Updated, misses nothing. An error is a listen refused or never acknowledged; once acknowledged, a stream cut off is Dropped, since the caller's answer to either is to listen again. It never listens again by itself: whether to, and how long to wait first, are the caller's.

let rec watch () =
  match
    Spindle_mcp_client.listen mcp [ uri ] (fun _ ->
        catch_up ();
        `Continue)
  with
  | Ok (Dropped _) ->
      Eio.Time.Mono.sleep_span clock Mtime.Span.(1 * s);
      watch ()
  | Ok (Completed | Stopped) -> Ok ()
  | Error _ as e -> e

read_timeout (60 s) bounds each wait for more of the stream, never the whole: four of a Spindle server's keep-alives. A notification of another listen, or of a kind not asked for, is dropped with a warn line, as a server never sends one.