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
@type request_id() :: String.t()
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec notify(GenServer.server(), String.t(), map()) :: :ok
Send a JSON-RPC notification (no response expected).
Send a JSON-RPC request and wait for the response.
Returns {:ok, result} or {:error, %{code: integer, message: String.t()}}.
@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.
@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.
@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.
@spec stop(GenServer.server()) :: :ok
Stop the client and close the port.