Available Clients
The client SDK offers two ways to connect with a websocket connection, and one way to make HTTP requests.useAgent
React hook with automatic reconnection and state management
AgentClient
Vanilla JavaScript/TypeScript class for any environment
agentFetch
HTTP requests when WebSocket is not needed
Core Features
All clients provide:- Bidirectional state sync - Push and receive state updates in real-time
- RPC calls - Call agent methods with typed arguments and return values
- Streaming - Handle chunked responses for AI completions
- Auto-reconnection - Built on PartySocket for reliable connections
Quick Start
- React
- Vanilla JS
Connecting to Agents
Agent Naming
Theagent parameter is your agent class name. It is automatically converted from camelCase to kebab-case for the URL:
Instance Names
Thename parameter identifies a specific agent instance. If omitted, defaults to "default":
Connection Options
BothuseAgent and AgentClient accept PartySocket options:
Async Query Parameters
For authentication tokens or other async data, pass a function that returns a Promise:The query function is cached and only re-called when
queryDeps change, cacheTtl expires, or the component remounts.State Synchronization
Agents can maintain state that syncs bidirectionally with all connected clients.Receiving State Updates
Pushing State Updates
setState():
1
Send to Agent
The state is sent to the agent over WebSocket
2
Agent Processes
The agent’s
onStateChanged() method is called3
Broadcast to Clients
The agent broadcasts the new state to all connected clients
4
Callback Fires
Your
onStateUpdate callback fires with source: "client"Calling Agent Methods (RPC)
Call methods on your agent that are decorated with@callable().
The
@callable() decorator is only required for methods called from external runtimes (browsers, other services). When calling from within the same Worker, you can use standard Durable Object RPC directly on the stub without the decorator.Using call()
Using the Stub Proxy
Thestub property provides a cleaner syntax for method calls:
TypeScript Integration
For full type safety, pass your Agent class as a type parameter:Streaming Responses
For methods that returnStreamingResponse, handle chunks as they arrive:
MCP Server Integration
If your agent uses MCP (Model Context Protocol) servers, you can receive updates about their state:Error Handling
Connection Errors
RPC Errors
Streaming Errors
Best Practices
Use Typed Stubs
Reconnection is Automatic
The client auto-reconnects and the agent automatically sends the current state on each connection. YouronStateUpdate callback will fire with the latest state — no manual re-sync needed.
Optimize Query Caching
Clean Up Connections
In vanilla JS, close connections when done:React’s
useAgent handles cleanup automatically on unmount.Next Steps
React Hooks
Learn about the useAgent hook and React-specific features
Vanilla JS
Explore AgentClient and agentFetch for non-React environments