moonbitlang/openseek/jsonrpc does not have a README file

    Transport

    pub(open) trait Transport {
    async fn send(Self,
    Json
    ) -> Unit
    async fn recv(Self) ->
    Json
    ?
    fn close(Self) -> Unit
    }

    The message transport a Client speaks over. It owns framing and delivery, exchanging whole JSON-RPC messages. recv yields None at end of stream. send is called only by the writer task (a single sender). close unblocks any in-progress recv/send and is idempotent.

    ResponseError

    pub suberror ResponseError {
    Failed(code~ : Int, message~ : String, data~ :
    Json
    ?)
    Closed
    }

    Why a request did not yield a result: the peer answered with a JSON-RPC error object, or the transport ended before any reply arrived.

    ResponseError::output

    fn ResponseError::output(self : ResponseError, logger : &Logger) -> Unit

    ResponseError::to_string

    fn ResponseError::to_string(self : ResponseError) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    Client

    type Client

    A JSON-RPC client. Build it with Client::new, spawn run_message_pump and run_writer, then issue request/notify. Requests may be concurrent; replies are matched by id.

    Client::Client

    fn[T : Transport] Client::Client(transport : T) -> Client

    Build a client over transport. Pure setup — no I/O and no background task. The caller must spawn run_message_pump and run_writer before issuing requests (see the module comment). Closing the transport makes in-flight and later requests fail with Closed.

    Client::close

    fn Client::close(self : Client) -> Unit

    Mark the client closed, close the transport (unblocking a parked reader/ writer) and the outbox, and fail every outstanding request with Closed. Idempotent and synchronous (transport and queue close never suspend), so it is safe to run during cancellation unwind.

    Client::is_closed

    fn Client::is_closed(self : Client) -> Bool

    Whether the client has closed. A closed client never answers again — every later request fails fast with Closed — so a pool holding one should discard it and connect afresh.

    Client::notify

    async fn Client::notify(self : Client, method_~ : String, params~ :
    Json
    ) -> Unit

    Send a notification (no reply expected). Like request, it enqueues on the outbox and so may block if the outbox is full (the writer is backpressured).

    Client::request

    async fn Client::request(self : Client, method_~ : String, params~ :
    Json
    , cancel_on_exit? : Bool) ->
    Json

    Issue a request and await its result. Concurrency-safe and order-independent: many requests may be outstanding at once, and each reply is routed to its request by id by the reader. Requires running run_message_pump and run_writer. Raises ResponseError::Failed on a JSON-RPC error reply, or ResponseError::Closed if the client closes before the reply arrives. With cancel_on_exit, abandoning an outstanding request queues an MCP-style cancellation notification. Transports may translate it into stream closure.

    Client::respond

    async fn Client::respond(self : Client, request : PeerRequest, result :
    Json
    ) -> Unit

    Answer a peer-initiated request with a successful result. This uses the regular bounded outbox, so it may wait for writer backpressure but never writes concurrently with another client task.

    Client::respond_error

    async fn Client::respond_error(self : Client, request : PeerRequest, code~ : Int, message~ : String) -> Unit

    Answer a peer-initiated request with a JSON-RPC error.

    Client::run_message_pump

    async fn Client::run_message_pump(self : Client) -> Unit

    The reader task: owns transport.recv and routes each inbound message until the stream ends or the client closes. Spawn exactly one, on a task that outlives the requests. Closes the client on every exit — end of stream, a transport error, or cancellation — so is_closed is always truthful and no request is left hanging.

    Client::run_writer

    async fn Client::run_writer(self : Client) -> Unit

    The writer task: the sole caller of transport.send, draining the outbox until the client closes. A failed send closes the client (so a request never waits on a reply to a message that could not be sent). Spawn exactly one.

    Client::set_close_handler

    fn Client::set_close_handler(self : Client, handler : () -> Unit) -> Unit

    Install the handler run once when the client closes — the peer ending the stream, a transport failure, or a local close. Replaces any previous handler; it runs synchronously inside close, so it must not block.

    Client::set_notification_handler

    fn Client::set_notification_handler(self : Client, handler : (String,
    Json
    ) -> Unit) -> Unit

    Install the handler run for each peer-initiated notification (method, params). Replaces any previous handler; it runs on the reader task, so it must not block on I/O.

    Client::set_request_handler

    fn Client::set_request_handler(self : Client, handler : (PeerRequest) -> Bool raise) -> Unit

    Install the receiver for non-ping requests initiated by the peer. The receiver runs on the reader task and therefore must not block. Return true only after transferring the request into caller-owned bounded state; that caller must later answer it with respond or respond_error. Returning false immediately answers with a server-busy error. If the receiver raises, the client answers that request with an internal error and keeps routing unrelated traffic.

    PeerRequest

    type PeerRequest

    One request initiated by the peer. Callers may read the id for correlation, but the opaque type keeps construction inside this package; answers still go through Client::respond or Client::respond_error, preserving the single-writer transport rule.

    PeerRequest::id

    The exact JSON-RPC id to carry as an opaque correlation value in a host UI. Answer the original PeerRequest; do not construct a reply from this value.

    PeerRequest::method_name

    fn PeerRequest::method_name(self : PeerRequest) -> String

    The peer method name for this request.

    PeerRequest::params

    The peer parameters, or JSON null when the request omitted params.