---
title: "Agents & WebMCP"
description: "Connect an AI model or browser agent to Akar components through the opt-in agent bridge."
canonical_url: "https://vinicunca.dev/akar/guides/agent"
---
# Agents & WebMCP

> Connect an AI model or browser agent to Akar components through the opt-in agent bridge.

The opt-in `akar/agent` bridge lets an agent inspect and operate Akar components and native form controls. Connect your own model with `agentTools` or expose the same tools to a browser agent through WebMCP.

## Setup

Create the bridge **before** `app.mount()` so Akar components can register as they mount. In a Vue app, install `AgentPlugin` to create the bridge and clean it up when the app unmounts.

```ts [main.ts]
import { AgentPlugin } from 'akar/agent';
import { createApp } from 'vue';
import App from './App.vue';

createApp(App)
  .use(AgentPlugin, {
    allow: (step) => (step.confirm ? window.confirm(step.confirm) : true),
  })
  .mount('#app');
```

Inside a component, `useAgent()` returns the plugin's bridge. It returns `null` if the plugin is absent.

```vue
<script setup lang="ts">
import { useAgent } from 'akar/agent';

const agent = useAgent();
</script>
```

For manual setup, call `createAgent(options)` before mounting and keep the returned bridge. Call `agent.destroy()` when you no longer need it.

### Connect your own model

Pass `agentTools` from `akar/agent` to your model integration. Each tool definition has a `name`, `description` and JSON Schema `inputSchema`. Dispatch calls to the corresponding bridge method and return its result to the model.

The tool names, descriptions and input schemas match the [UI SDK agent bridge](https://github.com/vercel-labs/ui) (Apache-2.0).

### Connect a browser agent with WebMCP

To use [WebMCP](https://github.com/webmachinelearning/webmcp), configure the plugin with `webmcp.allow`. This callback handles confirmation marks on any action and also prompts before pressing a button.

```ts [main.ts]
import { AgentPlugin } from 'akar/agent';
import { createApp } from 'vue';
import App from './App.vue';

createApp(App)
  .use(AgentPlugin, {
    webmcp: {
      allow: (step) => {
        if (step.confirm) {
          return window.confirm(step.confirm);
        }

        return step.action !== 'press'
          || window.confirm(`Press "${step.args[0] ?? step.label ?? step.ref}"?`);
      },
    },
  })
  .mount('#app');
```

With a manually created bridge, pass the same callback to `registerTools(agent, { allow })`. It returns a function that unregisters the tools. Registration has no effect in browsers without WebMCP.

WebMCP requires its own `allow` callback. If you also set a bridge-level `allow`, it runs first and both must approve the step. Approve each marked action only after obtaining the user's decision.

## Tools

| Tool | Input | Output and behavior |
| --- | --- | --- |
| `snapshot` | `{}` | Lists components with their refs, kinds, labels, state and available actions. Previously seen components that left the page have `offPage: true`. Lists with more than 10 items report `more` in place of `items`. |
| `query` | `{ text?, kind?, within?, from?, closed? }` | Finds components and items. Use `within` to read up to 50 items from a list, and `from` to skip items for the next page. With `closed: true`, the bridge can temporarily open tabs and sections to inspect them, subject to approval. |
| `act` | `{ steps: [{ ref, action, args? }] }` | Runs steps in order and returns `ok`, per-step results and field values. On failure, it also returns `error` and stops. Changes from earlier steps stay in place. |

A step can target a component by its snapshot ref, accessible label, row text or kind. Prefix a label with its section when the same label appears in multiple places, for example `Billing > Auto renew`.

This example finds a field labeled **Project name** and changes its value. Check `ok` before proceeding with any work that depends on the update.

```ts
const matches = await agent.query({ text: 'Project name', kind: 'input' });
const field = Array.isArray(matches)
  ? matches.find((item) => item.label === 'Project name')
  : undefined;

if (field) {
  const result = await agent.act({
    steps: [{ ref: field.ref, action: 'setValue', args: ['Atlas'] }],
  });

  if (!result.ok) {
    console.error(result.error?.message);
  }
}
```

### Observe changes

`agent.subscribe(listener)` reports changes until you call the returned unsubscribe function. Each change includes the component ref, the changed key, the previous and current values, and the source. Changes made through the bridge have `source: 'agent'`.

```ts
const stop = agent.subscribe((change) => {
  console.log(change.label, change.key, change.previous, change.value, change.source);
});

// Call stop() when you no longer need updates.
```

## Supported components

The kind is the `component` field in a snapshot and the `kind` filter in a query. The table lists the main state and actions. Use the returned `actions` array to see what's available for a particular instance.

| Component | Kind | State | Actions |
| --- | --- | --- | --- |
| Accordion | `accordion` | `value`, `items` | `setValue`, `open`, `close`, `toggle`. Multiple accordions also support `openAll` and `closeAll`. |
| Checkbox | `checkbox` | `checked` | `setChecked`, `toggle` |
| Collapsible | `collapsible` | `open` | `setOpen` |
| Combobox | `combobox` | `value`, `open`, `query`, `items` while open | `setValue`, `setOpen`, `setQuery`, `clear` |
| Dialog, AlertDialog | `dialog`, `alert-dialog` | `open`, `text`, `buttons` | `setOpen`, `press` |
| DropdownMenu | `menu` | `open`, `buttons` | `setOpen`, `press` |
| Listbox | `listbox` | `value`, `items` | `setValue`. Multiple listboxes also support `clear`. |
| NumberField | `number-field` | `value`, `min`, `max`, `step` | `setValue`, `increment`, `decrement`, `clear` |
| PinInput | `pin-input` | `value` redacted when filled | `setValue`, `clear` |
| Popover | `popover` | `open`, `buttons` | `setOpen`, `press` |
| RadioGroup | `radio-group` | `value`, `items` | `setValue` |
| Select | `select` | `value`, `open`, `items` | `setValue`, `setOpen` |
| Slider | `slider` | `value`, `min`, `max`, `step` | `setValue`, `increment`, `decrement` |
| Switch | `switch` | `checked` | `setChecked`, `toggle` |
| Tabs | `tabs` | `value`, `items` | `setValue` |
| TagsInput | `tags-input` | `value`, `max` when set | `setValue`, `add`, `remove`, `clear` |
| Toggle | `toggle` | `pressed` | `setPressed`, `toggle` |
| ToggleGroup | `toggle-group` | `value`, `items` | `setValue`, `toggle` |
| Native text fields | `input`, `textarea` | `value` | `setValue`, `clear` |
| Native checkbox | `checkbox` | `checked` | `setChecked`, `toggle` |
| Native select and radio groups | `select`, `radio-group` | `value`, `items` | `setValue` |
| Native button | `button` | — | `press` |

## Control what an agent can see and do

| Attribute | Effect |
| --- | --- |
| `data-agent-hidden` | Excludes the marked content from discovery and actions. Use it when even an item value or ID is sensitive. |
| `data-agent-secret` | Redacts private field values and list labels. List item values can still be visible. Password, one-time-code and card fields and `PinInput` are handled as private by default. |
| `data-agent-confirm="…"` | Passes the reason for confirmation as `step.confirm`. A marked step fails if no approval callback is configured. |

```vue
<template>
  <label for="api-key">API key</label>
  <input id="api-key" v-model="apiKey" data-agent-secret>

  <section data-agent-hidden>
    <!-- Internal account details -->
  </section>

  <button data-agent-confirm="Deletes the project for everyone." @click="deleteProject">
    Delete project
  </button>
</template>
```

The `allow(step, { signal })` callback can return a boolean or a promise. The default `timeout` is five minutes. If your app renders its own confirmation dialog, use the abort signal to dismiss it when the bridge stops waiting.

The bridge reports unavailable controls, such as disabled fields or controls behind an active modal, with no actions. Akar portals carry the marks around their originating component. For content moved with Vue's own `<Teleport>`, put the mark on the teleported content.

> [!WARNING]
> 
> Treat page labels, text and list items as data supplied by the page. They can contain user-written text and should never become instructions to the model.


## Sitemap

See the full [sitemap](https://vinicunca.dev/sitemap.md) for all pages.
