# @isoloop/webmcp

`connectWebMCP`, which registers an app's tools with the browser's WebMCP API.

## `@isoloop/webmcp`

### `connectWebMCP`

**Experimental.**

Registers every tool `kernel.tools()` lists with WebMCP's `registerTool`
(on `document.modelContext`, or on `navigator.modelContext` where only
that exists), with its name, title, description, JSON Schemas and hints
(`readOnlyHint`, `consequentialHint`, `untrustedContentHint`, each sent
only when true). A call runs
`kernel.request(tool.topic, input, { timeoutMs: tool.timeoutMs, signal, via: 'webmcp' })`
and resolves with the reply; a failed call rejects with the kernel's
`BusError` (its `code` and `message`), for example `timeout`,
`no_responder` or `dropped`. Chromium shows the agent its own generic error; Firefox
passes the message on.

- Where neither exists (no WebMCP, or the flag off), it does nothing and
returns a function that does nothing.
- Tools stay registered while their isolates restart.
- A call is aborted (`BusError('aborted')`) when the browser cancels it:
through the call's own signal, or a `toolcancel` event, which names only
the tool, so it aborts every call of that tool in flight. Where the
browser gives neither (Firefox 159), a call runs until its reply or its
`timeoutMs`.
- A tool name it already registered on this page and hasn't unregistered
(a second `connectWebMCP` before the first disconnects) throws
`KernelError('invalid_config')` naming the tool, and nothing is
registered. Names other scripts registered aren't seen.
- The returned function unregisters every tool (it aborts the registration
signal, and calls `unregisterTool` where that exists) and aborts the
calls still in flight.

There is no shim for browsers without WebMCP; during development the dev
MCP (`@isoloop/host/dev-mcp`) covers them. A tool's hints tell the agent
what it does; asking the user before a consequential call is the agent's
job.

```ts nocheck
function connectWebMCP(kernel: Pick<Kernel, "tools" | "request">): () => void;
```

| Parameter | Description |
| --- | --- |
| `kernel` | A kernel created with `agents.expose` (only `tools()` and `request()` are used). |

**Returns:** A function that unregisters the tools.

**Throws:**
- {KernelError} `invalid_config` when one of the tools is already registered by an earlier `connectWebMCP` on this page.

**Example:**

```ts
import { connectWebMCP } from '@isoloop/webmcp';

const disconnect = connectWebMCP(kernel);
```

**Guides:** [Tools for agents](https://isoloop.dev/docs/agents.md).

