Copilot.JsonRpcClient (Copilot SDK v2.3.0)

Copy Markdown

GenServer-based JSON-RPC 2.0 client using Port for stdio communication.

Messages are framed with Content-Length headers following the LSP base protocol:

Content-Length: <byte-length>



<JSON body>

The client supports:

  • Outgoing requests (with responses matched by id)
  • Outgoing notifications (no response expected)
  • Incoming notifications from the server
  • Incoming requests from the server (tool.call, permission.request, etc.)

Summary

Functions

Returns a specification to start this module under a supervisor.

Send a JSON-RPC notification (no response expected).

Send a JSON-RPC request and wait for the response.

Register a handler for incoming notifications from the server.

Register a handler for an incoming request method (e.g. "tool.call").

Start the JSON-RPC client linked to the calling process.

Stop the client and close the port.

Types

request_id()

@type request_id() :: String.t()

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

notify(client, method, params \\ %{})

@spec notify(GenServer.server(), String.t(), map()) :: :ok

Send a JSON-RPC notification (no response expected).

request(client, method, params \\ %{}, timeout \\ 30000)

@spec request(GenServer.server(), String.t(), map(), timeout()) ::
  {:ok, any()} | {:error, map()}

Send a JSON-RPC request and wait for the response.

Returns {:ok, result} or {:error, %{code: integer, message: String.t()}}.

set_notification_handler(client, handler)

@spec set_notification_handler(GenServer.server(), (String.t(), map() -> :ok) | nil) ::
  :ok

Register a handler for incoming notifications from the server.

The handler receives (method, params) and is called in the GenServer process.

set_request_handler(client, method, handler)

@spec set_request_handler(GenServer.server(), String.t(), (map() -> map()) | nil) ::
  :ok

Register a handler for an incoming request method (e.g. "tool.call").

The handler receives params and must return a result map.

start_link(port, opts \\ [])

@spec start_link(
  port(),
  keyword()
) :: GenServer.on_start()

Start the JSON-RPC client linked to the calling process.

port must be an already-opened Erlang port connected to the CLI subprocess via stdin/stdout.

stop(client)

@spec stop(GenServer.server()) :: :ok

Stop the client and close the port.