Using with UI SDK
Run Bits UI and Vercel UI SDK on the same page under one WebMCP toolset or as scoped islands.
bits-ui/experimental/agent follows the conventions pioneered by Vercel's UI SDK (vercel-labs/ui): the same three tools (snapshot, act, query), WebMCP registration via registerTools(), and the same data-agent-* HTML attributes.
Each package maintains its own component registry, so calling registerTools() from both would collide on tool names and double-count plain HTML controls (includeNative: true). Use one of two patterns to run both on the same page:
- Unified toolset with
composeAgents(): merge both bridges into one WebMCP toolset with namespaced refs (bits:c1,ui:c1) and passincludeNative: falseon one bridge. - Scoped islands with
rootandprefix: keep each library scoped to its own DOM subtree and tool prefix during an incremental migration.
Unified toolset with composeAgents()
If Bits UI and UI SDK components live side-by-side on the same screen, merge both bridges with composeAgents() and register one WebMCP toolset.
<script lang="ts">
import { composeAgents, createAgent, registerTools } from "bits-ui/experimental/agent";
import { createAgent as createUiSdkAgent } from "@ui-sdk/svelte/agent";
let { children } = $props();
$effect(() => {
// Turn off native fallback on one bridge so plain inputs aren't counted twice
const bitsAgent = createAgent({ includeNative: false });
const uiSdkAgent = createUiSdkAgent();
const agent = composeAgents({
bits: bitsAgent,
ui: uiSdkAgent,
});
const unregister = registerTools(agent, {
allow: (step) => !step.confirm,
});
return () => unregister();
});
</script>
{@render children()}
What composeAgents() handles for you
- Namespaced refs:
snapshot()andquery()prefix every ref with its bridge key (bits:c1,ui:c1) so refs never collide. - Label-first routing: Steps like
{ ref: "Region", action: "setValue", args: ["EU Central"] }route automatically to whichever bridge owns"Region". If both libraries expose a"Save"button,act()fails with the exact prefixed refs (bits:c4,ui:c2) instead of guessing. - One
allowgate:registerTools(agent, { allow })forwards the callback into Bits UI child bridges (preserving closed-overlay and per-optiondata-agent-confirminspection) and runs it before calling external bridges. - Zero coupling:
composeAgents()only expects{ snapshot, act, query? }, so it works with@ui-sdk/svelteor any custom bridge without adding dependencies.
Scoped islands with root and prefix
During an incremental migration where distinct parts of the page belong to each library, you can keep the bridges completely separate by scoping root and prefixing Bits UI's WebMCP tool names:
<script lang="ts">
import { createAgent, registerTools } from "bits-ui/experimental/agent";
let bitsRegion = $state<HTMLElement | null>(null);
$effect(() => {
if (!bitsRegion) return;
const agent = createAgent({ root: () => bitsRegion });
// Registers "bits_snapshot", "bits_act", and "bits_query"
const unregister = registerTools(agent, {
prefix: "bits_",
allow: (step) => !step.confirm,
});
return () => unregister();
});
</script>
<section bind:this={bitsRegion}>
<!-- Bits UI components (portaled dialogs, selects, and popovers inside are included) -->
</section>
Shared attributes and cross-library boundaries
- Core
data-agent-*marks work across both libraries. Puttingdata-agent-hidden,data-agent-secret, ordata-agent-confirmon a shared wrapper applies to both Bits UI and UI SDK controls inside it. Bits UI also supportsdata-agent-rowanddata-agent-sectionfor extra targeting context. - Closed container reveals stay per-bridge. Auto-revealing behind closed
Tabs,Collapsible, orAccordionpanels only discovers controls registered on that same bridge. If a Bits UI tab wraps a UI SDK input, activate the tab in step 1 and update the UI SDK input in step 2 of the sameact()batch.