BroadcastChannel to make earlier instances disable themselves.
The snippets on this page assume you have registered a UI resource and tool and created an App instance from @modelcontextprotocol/ext-apps. See the SDK Quickstart if you haven’t.
How it works
All widget iframes from a single connector are served from the same sandbox origin on*.claudemcpcontent.com (the iframe sandbox includes allow-same-origin). That means a BroadcastChannel opened in one instance reaches every other instance from the same connector in the current conversation. See Channel scope and ui.domain for how a fixed domain widens this.
The pattern has three parts:
- The server stamps each tool result with an election key. It returns a
{createdAt, seq}pair (server wall-clock time and a monotonic counter) instructuredContent, the typed JSON payload slot of an MCP tool result. Tool results are stored in the conversation transcript, so every device and every remount of the widget sees the same key. - Each widget announces its key on a shared channel. Shortly after
connect()resolves, the host delivers the tool result that mounted this widget (including itsstructuredContent) via the SDK’stoolresultevent. The widget reads its key from that event, opens aBroadcastChannel, and broadcasts the key. - Any widget that sees a younger sibling marks itself superseded. It greys out its UI, disables its buttons, and short-circuits all calls that mutate model context or inject messages.
Mint the election key on the server
UseregisterAppTool to register the tool, and return the key in structuredContent alongside your normal tool output. A per-process counter works for a demo; a production server should derive the key from something durable, such as a database row ID or a version number on the underlying record.
Why not use client-side Date.now()?
Client mount time does not reflect tool-call order. When a stored conversation is reopened, Claude lazy-mounts widget cells as they scroll into view, so an older widget can mount after a newer one and would win an election based on client timestamps. The server-minted key is written into the transcript at tool-call time and is identical everywhere.
Run the election in the widget
The four snippets in this section form a single module; paste them in order into your widget entry file.Read the key from the toolresult event
Connect and read the values you need from the host: your instance ID from hostContext.toolInfo, and the server-minted key from the toolresult event. The event’s structuredContent is typed Record<string, unknown>, so cast it to the shape your server returns.
Broadcast and compare on a shared channel
Broadcast the key and compare against every sibling you hear from. The comparison iscreatedAt, tie-broken by seq, then by instance ID for determinism. Ignore inbound messages until your own key is finalized so you never reply with an undefined key.
Gate host-mutating calls on !superseded
The election only matters if superseded instances actually stop talking to Claude. Guard every call to updateModelContext or sendMessage:
Reflect the state in the UI
In your render function, disable buttons and show a banner that points the user to the newest instance:Special considerations
The election above covers the common case. A production widget should also handle the following.Channel scope and ui.domain
BroadcastChannel is same-origin only. How far that origin extends depends on whether you set _meta.ui.domain on your resource:
- Without
ui.domain(the default), Claude derives the iframe origin from the conversation and connector, so the broadcast is scoped to a single conversation. - With a fixed
ui.domain, the origin is shared across every conversation and tab for your connector. A fixed channel name would let a widget in one conversation supersede a widget in another. NeitherhostContextnor the tool-call arguments include a Claude-provided conversation ID, so if you need both a fixed domain and per-conversation elections, generate your own scope key on the server (for example, a UUID minted once per client connection) and return it instructuredContentfor the widget to append to the channel name.
Fall back if the server key is delayed
The main snippet above waits for thetoolresult event before announcing. If you want the widget to participate in the election even when that event is slow to arrive, replace that listener with one that resolves a promise, and race the promise against a short timeout after connect():
superseded against the peers you have already heard from, and re-announce so siblings update their view of you. The recomputed result may flip the instance back to live.
Fallback caveat: don’t compare server and client timestamps
This applies only if you implemented the fallback above. If you fall back to a client-sideDate.now() while waiting for the server key, tag the key with its source and refuse to compare a client value against a server value. A server createdAt from a tool call made hours ago will always be smaller than a fresh client timestamp, which would wrongly hand “live” to whichever instance happened to fall back. Include keySource in the broadcast payload (announce() and the born reply) and in the peers Map value type so siblings can read it:
Caching the key across remounts
On Claude.ai web,hostContext.toolInfo.id is the stable tool-use ID, so you can persist the resolved server key to localStorage keyed by that ID and reuse it on the next mount without waiting for the toolresult event again.
Treat this as an optimization rather than a correctness guarantee. On Claude iOS, toolInfo.id is undefined when a stored conversation is rehydrated, so there is no stable per-instance cache key. Detect that case and skip the cache; the server key from the toolresult event is the only ordering source that works on every platform.
If you bypass the SDK App class
The snippets on this page use the SDK’s App class. If you instead hand-roll a minimal postMessage bridge, it will silently drop requests sent from the host to the widget, such as ping (a liveness check) and ui/resource-teardown (the host asking the widget to clean up before unmount). Claude.ai web does not currently send either to widgets, and Claude iOS sends ui/resource-teardown only when the user navigates away from the conversation, so ignoring them is harmless today. The App class handles the full request surface and is recommended for production.
Related topics
- Cross-platform compatibility for how
_meta.ui.domainis computed on Claude. - SDK API reference for
registerAppTool,App, andMcpUiResourceMeta.