WebSocket Protocol

The daemon accepts WebSocket connections at /ws for real-time, bidirectional communication. Remote agents connect via WebSocket to send messages and receive streaming events.

Connecting

const ws = new WebSocket("ws://127.0.0.1:7741/ws");

ws.onopen = () => {
  // First message must be an auth message
  ws.send(JSON.stringify({
    type: "auth",
    token: process.env.NODE_AUTH_SECRET,
    name: "my-agent",  // optional display name
  }));
};

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  console.log(msg.type, msg);
};

Authentication

The first message after connecting must be an auth message. Until authenticated, only auth and ping messages are accepted.

After 3 failed auth attempts, the connection is closed with code 1008.

Inbound Messages (Client → Server)

TypeFieldsDescription
authtoken, name?Authenticate with Bearer token
chatmessage, sessionId?Send a message to the agent
pingKeep-alive ping
tool_resulttoolCallId, content, isError?Return result of a tool call

Outbound Messages (Server → Client)

TypeFieldsDescription
auth_okagentIdAuthentication succeeded
auth_failerrorAuthentication failed
text_deltacontentStreaming text chunk from agent
tool_callid, name, argumentsAgent wants to call a tool
turn_completetokensIn, tokensOutAgent finished its turn
errormessageError occurred
pongResponse to ping

Connection Lifecycle

  1. Client opens WebSocket to ws://host:port/ws
  2. Server assigns a unique agentId
  3. Client sends auth message with token
  4. Server responds with auth_ok or auth_fail
  5. Client sends chat messages, receives streaming events
  6. Either side can close the connection at any time

Close Codes

CodeMeaning
1000Normal disconnection
1008Too many failed auth attempts