MacpStream provides a bidirectional gRPC stream for real-time session participation. Use it when you want to receive accepted envelopes as they arrive rather than polling.
const stream = client.openStream({ auth: Auth.devAgent('observer') }); // local dev only; see Security guide for production auth// Send an envelope through the streamawait stream.send(envelope);// Consume accepted envelopes as an async iteratorfor await (const received of stream.responses()) { console.log(received.messageType, received.sender); if (received.messageType === 'Commitment') { break; // session resolved }}// Always close when donestream.close();
responses() yields accepted envelopes from the server
close() terminates the stream
Instead of iterating responses(), you can pull one envelope at a time with
read(timeoutMs?): it resolves the next envelope, returns null once the
stream has ended, and throws MacpTimeoutError if timeoutMs elapses first
(omit the timeout to wait indefinitely). End-of-stream is sticky — after
read() returns null, subsequent read()/responses() calls observe the
end too.
Errors on the stream surface as thrown exceptions from the async iterator:
try { for await (const envelope of stream.responses()) { // process }} catch (err) { if (err instanceof MacpTransportError) { console.log('stream disconnected:', err.message); }}
A freshly opened stream only broadcasts envelopes accepted after you connect. To also receive envelopes that were accepted earlier in the session, send a subscribe-only frame. The subscribe/replay contract is specified in RFC-MACP-0006 (Transport Bindings) §3.2 (StreamSession) and documented server-side in the runtime API reference › StreamSession.
const stream = client.openStream({ auth });// Replay everything the runtime has accepted so far, then continue live.await stream.sendSubscribe(sessionId);// Or resume after a known cursor (e.g. after a reconnect):await stream.sendSubscribe(sessionId, lastSeenSequence);for await (const envelope of stream.responses()) { // first frames are the replay; then live broadcast follows seamlessly.}
sendSubscribe(sessionId, afterSequence?) is how non-initiator agents observe the SessionStart and earlier Proposal / Vote envelopes when they join a session that is already in flight. The GrpcTransportAdapter in the agent framework calls this automatically, passing its own resume cursor on every start() — including a second start() after stop(), which is the only reachable "reconnect" in this SDK — so it does not replay the whole session again. You only need to call sendSubscribe yourself when driving a raw MacpStream.
afterSequence is the 1-based accepted-envelope ordinal, exclusive (0 = from the start): the Nth accepted envelope has ordinal N. Derive lastSeenSequence by counting distinct delivered envelopes, keyed on message_id (GrpcTransportAdapter.lastSequence does this for you) — a redelivered envelope MUST NOT advance the count (RFC-MACP-0006 §3.2 Redelivery). Ordinals are stable across compaction and restart; resuming below a compacted base is refused. On runtime ≥ 0.12.0 the stream ends with FAILED_PRECONDITION and macpErrorCodeHISTORY_UNAVAILABLE; the runtime's details carry lowest_available_sequence. On 0.9.x–0.11.x it was an inline INVALID_ENVELOPE error frame and the stream stayed open. On 0.5.0–0.8.x it was FAILED_PRECONDITION ending the stream, with no code. See sendSubscribe for the full contract.
StreamSession is a server-advertised capability (sessions.stream). Check Initialize response capabilities before using.
The stream delivers envelopes in authoritative acceptance order, matching the runtime's ordering.
Late-attach is supported via sendSubscribe(sessionId, afterSequence?) — the runtime replays accepted envelopes from that cursor before switching to live broadcast.
For durable observation, use getSession() to fetch current metadata and sendSubscribe() to replay the envelope history over the stream.
Lag recovery (broadcast buffer holds 256 envelopes per session): consumer lag terminates the stream with a coded MacpTransportError (error.code === 'RESOURCE_EXHAUSTED'). Reconnect and call sendSubscribe(sessionId, lastSeenSequence) to resume without missing envelopes. Do not reconnect on UNAUTHENTICATED — fix auth first. See the runtime's SDK guide — Handling stream lag for the full recovery contract.
RootsWatcher monitors changes to coordination roots/boundaries.
Runtime 0.5.0 advertises roots.list_changed: false in its Initialize
capabilities: ListRoots always returns an empty root list (the runtime does
not populate roots yet) and WatchRoots idles, so RootsWatcher yields
nothing against this runtime. Check capabilities.roots.listChanged from
initialize() before relying on it.
import { RootsWatcher } from 'macp-sdk-typescript';const watcher = new RootsWatcher(client, { auth });for await (const change of watcher.changes()) { console.log('roots changed at', change.observedAtUnixMs); // Refresh root list const roots = await client.listRoots(); console.log('current roots:', roots.roots);}
The API is identical to ModeRegistryWatcher — changes(signal?), watch(handler), and nextChange().
SignalWatcher subscribes to the ambient signal plane — Signal envelopes
sent via client.sendSignal() outside any session. Its iterator method is
signals(signal?) (not changes()), with watch(handler) and
nextSignal() conveniences:
import { SignalWatcher } from 'macp-sdk-typescript';const watcher = new SignalWatcher(client, { auth });for await (const envelope of watcher.signals()) { console.log('signal:', envelope.messageType, 'from', envelope.sender);}
Authenticated senders (runtime 0.9.0).signals() yields bare envelopes
and cannot tell you whether the runtime authenticated a Signal's sender.
Wherever a Signal can drive a decision, iterate events() instead: it
yields SignalEvent ({ envelope, senderAuthenticated }).
senderAuthenticated is true only under JWT or static-token auth. It is
false in dev mode and on runtimes older than 0.9.0, and per RFC-MACP-0004
§4.1 such a Signal MUST NOT influence operational decisions.
for await (const { envelope, senderAuthenticated } of watcher.events()) { if (!senderAuthenticated) continue; // informational only act(envelope);}
Auth required (runtime 0.5.0).WatchSignals is an authenticated RPC:
if neither the watcher nor the client has a credential, iterating
signals() throws immediately client-side. A rejected credential surfaces
as a MacpTransportError with code === 'UNAUTHENTICATED' — fix auth
rather than reconnecting. Consumer lag terminates the stream with
code === 'RESOURCE_EXHAUSTED'; there the correct response is to
reconnect.
Session lifecycle events — created / resolved / expired / suspended / resumed /
cancelled — have their own guide: see Session Discovery. The API
is identical to ModeRegistryWatcher and the other watchers above (changes(signal?),
watch(handler), nextChange()).