Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing

WebMCP: Give the Browser Agent Real Buttons

Enable experimental WebMCP in Nuxt 4.7, register tools with @tool, turn forms into agent-callable actions, define custom tools, and show the agent's activity without inventing a second UI.

WebMCP is a proposed browser API that lets a page offer tools to an AI agent running in the browser. Nuxt 4.7 ships an experimental implementation that starts from the UI you already have.

What's new#

Turn it on:

nuxt.config.ts
export default defineNuxtConfig({
  experimental: {
    webmcp: true,
  },
});

Tag a function with @tool:

app/pages/index.vue
<script setup lang="ts">
const { data: products } = await useFetch('/api/products')
const cart = useCart()

/**
 * Add one of a product to the cart. Use when the user wants to buy something.
 * @tool
 */
function addToCart (sku: string) {
  cart.value.push(sku)
}
</script>

<template>
  <ul>
    <li v-for="product in products" :key="product.sku">
      {{ product.name }}
      <button :disabled="!product.inStock" @click="addToCart(product.sku)">
        Add
      </button>
    </li>
  </ul>
</template>

That registers an add_to_cart tool. The arguments come from the template, so the agent can only do what a user could click, and it cannot run while the button is disabled.

Why you'd care#

Most "AI in the app" features bolt a second interface onto the product: a chat box, a tool schema, a prompt. The agent then guesses which button to press, or the team duplicates the UI.

WebMCP inverts that. The UI is the tool surface. Inside a v-for, the :key becomes a target the agent picks, and a shared list_targets tool lists what is on screen. The agent sees the same affordances a person does.

That is a better contract for everyone: your validation runs, your permissions apply, and the agent cannot invent a button that does not exist.

Before#

An agent integration needs a hand-written schema, a permission model, and a second set of actions that drift from the UI.

After#

A form tool is still a form. A @tool bound to @submit takes the form's v-model bindings as input, fills the form, and submits it for real:

app/pages/contact.vue
<script setup lang="ts">
import { z } from 'zod'

const contactSchema = z.object({ email: z.email(), message: z.string().min(5) })
const form = reactive({ email: '', message: '' })

/**
 * Send a message to the shop. Use when the user has a question about an order.
 * @tool
 * @input contactSchema
 */
async function sendMessage () {
  await $fetch('/api/contact', { method: 'POST', body: form })
  return 'Sent.'
}
</script>

<template>
  <form @submit.prevent="sendMessage">
    <label>Email <input v-model="form.email" type="email"></label>
    <label>Message <textarea v-model="form.message" /></label>
    <button type="submit">Send</button>
  </form>
</template>

For actions that do not map to one control, use defineAgentTool() and useAgentTool():

app/pages/products.vue
const searchProducts = defineAgentTool({
  name: 'search_products',
  description: 'Find products by name. Use when the user asks what is for sale.',
  input: z.object({ query: z.string() }),
  annotations: { readOnlyHint: true },
  execute: ({ query }, { signal }) => $fetch('/api/products', {
    query: { q: query },
    signal,
  }),
})

useAgentTool(searchProducts)

On this site, the article page registers a copy_article_link tool with this runtime API; the Save button also carries the @tool tag. The runtime tool is the easier one to verify: visit the page in development and .nuxt/webmcp/schema.json lists it for that route.

Show the agent's activity with useAgentActivity():

app/components/AgentStatus.vue
<script setup lang="ts">
const { current, stop } = useAgentActivity()
</script>

<template>
  <p v-if="current">
    {{ current.message }} <button @click="stop">Stop</button>
  </p>
</template>

Do it yourself#

  1. Enable experimental.webmcp.
  2. Add @tool to one function bound in a template.
  3. Run nuxt dev and visit the page in Chrome with the WebMCP flag enabled.
  4. Inspect .nuxt/webmcp/schema.json to see the generated tools.
  5. Feed the schema to webmcp-evals if you want to test the agent path.
  6. Add a defineAgentTool() for one action that has no single control.

The live activity panel#

useAgentActivity() is the part users see. It returns the current tool call, the target, and a stop() function. Build a small status bar, not a modal. The agent is doing something on the page; the page should say so without taking over.

Gotchas#

  • The spec is moving and Chrome ships it behind a flag. In browsers without WebMCP, nothing registers and the app behaves exactly as before.
  • The template is the permission boundary. A disabled button cannot be clicked by the agent. A hidden field cannot be filled. That is the feature; do not bypass it with a custom tool.
  • Descriptions matter. Chrome's tool-design guidance is enforced at build time. Write the description for a model, not for a changelog.
  • @input takes a schema. Use it when the form's shape is not obvious from the bindings.
  • Do not expose destructive actions without confirm. Flags such as consequential and confirm exist for a reason.
  • The schema is a dev artifact. .nuxt/webmcp/schema.json is for evaluation, not a production API.

Recap#

WebMCP turns your existing template into an agent surface: @tool for functions, @submit for forms, defineAgentTool() for everything else, and useAgentActivity() for the human in the loop. The agent can only do what the UI already allows.

Next: One Problems Drawer.