Skip to main content

Overview

AgentClient is a WebSocket client for connecting to Agents from browsers and Node.js. It provides RPC method calls, state synchronization, and identity management.

Constructor

AgentClientOptions<State>
required
string
required
WebSocket host (e.g., “localhost:1999” or “agent.example.com”)
string
required
Name of the agent class (kebab-case, e.g., “my-agent”)
string
default:"default"
Name of the specific Agent instance
string
Full URL path - bypasses agent/name URL construction. When set, connects to this path directly. Server must handle routing manually (e.g., with getAgentByName).
string
Additional path to append to the URL. Works with both standard routing and basePath.
(state: State, source: 'server' | 'client') => void
Called when the Agent’s state is updated
(error: string) => void
Called when a state update fails (e.g., connection is readonly)
(name: string, agent: string) => void
Called when the server sends the agent’s identity on connect
(oldName: string, newName: string, oldAgent: string, newAgent: string) => void
Called when identity changes on reconnect (different instance than before)

Standard Routing

Custom Routing with basePath

With Path Suffix

Properties

agent

string
required
The agent class name (kebab-case). Updated when identity message is received.

name

string
required
The agent instance name. Updated when identity message is received.

identified

boolean
required
Whether the client has received identity from the server. Becomes true after the first identity message, resets to false on connection close.

ready

Promise<void>
required
Promise that resolves when identity has been received from the server. Useful for waiting before making calls that depend on knowing the instance. Resets on connection close.

Methods

call()

Call a method on the Agent.
string
required
Name of the method to call
unknown[]
Arguments to pass to the method
CallOptions
number
Timeout in milliseconds. If the call doesn’t complete within this time, it will be rejected.
StreamOptions
(chunk: unknown) => void
Called when a chunk of data is received
(finalChunk: unknown) => void
Called when the stream ends
(error: string) => void
Called when an error occurs
Returns: Promise<T> - Promise that resolves with the method’s return value

Basic Call

With Arguments

With Timeout

Streaming Call

setState()

Update the Agent’s state from the client.
State
required
New state to set
If the connection is readonly, the server will respond with a state update error.

close()

Close the connection and immediately reject all pending RPC calls.
number
WebSocket close code
string
Close reason

Events

AgentClient extends PartySocket, so all PartySocket events are available:

onopen

onmessage

Internal protocol messages are handled automatically. Override to handle custom messages:

onclose

onerror

Identity Management

The server sends an identity message on connect:
This is useful when using basePath for custom routing:

Identity Changes

If the server routes to a different instance on reconnect:

State Synchronization

The client automatically receives state updates:

Readonly Connections

If the connection is readonly, state updates will fail:

Best Practices

Wait for Ready

Handle Errors

Clean Up