@ractive-ch/tab-mcp-client 0.15.0 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +481 -148
- package/always-allowed.d.ts +25 -6
- package/cdn/confirm-y30cx7e1.min.js +4 -0
- package/cdn/confirm-y30cx7e1.min.js.map +10 -0
- package/cdn/connect-bbhj2y7w.min.js +6 -0
- package/cdn/connect-bbhj2y7w.min.js.map +12 -0
- package/cdn/connection-w97tf22a.min.js +17 -0
- package/cdn/connection-w97tf22a.min.js.map +22 -0
- package/cdn/index-vt5zk9me.min.js +91 -0
- package/cdn/index-vt5zk9me.min.js.map +13 -0
- package/cdn/tab-mcp-client.min.js +3 -3
- package/cdn/tab-mcp-client.min.js.map +6 -7
- package/cdn/webmcp-khy4f5rj.min.js +4 -0
- package/cdn/webmcp-khy4f5rj.min.js.map +10 -0
- package/confirm-fgnjgd08.js +263 -0
- package/confirm-fgnjgd08.js.map +10 -0
- package/confirm.d.ts +54 -0
- package/connect-8jpb0k29.js +484 -0
- package/connect-8jpb0k29.js.map +10 -0
- package/connect.d.ts +40 -0
- package/{connection-g6wk37yd.js → connection-g91xjjgm.js} +252 -126
- package/connection-g91xjjgm.js.map +16 -0
- package/connection.d.ts +7 -4
- package/dialog.d.ts +83 -0
- package/{index-nmn1j689.js → index-5jyqehpk.js} +14 -10
- package/index-5jyqehpk.js.map +11 -0
- package/index-jnz7w9xa.js +392 -0
- package/index-jnz7w9xa.js.map +12 -0
- package/index.d.ts +1 -1
- package/index.js +234 -263
- package/index.js.map +6 -7
- package/internal.d.ts +2 -0
- package/mapping.d.ts +37 -4
- package/mcp.d.ts +28 -8
- package/package.json +1 -1
- package/protocol.d.ts +8 -0
- package/react.d.ts +8 -10
- package/react.js +18 -14
- package/react.js.map +3 -3
- package/registry.d.ts +12 -5
- package/secret.d.ts +5 -2
- package/serve.d.ts +26 -4
- package/setup.d.ts +15 -25
- package/setup.js +1 -1
- package/strings.d.ts +107 -0
- package/tab-mcp.d.ts +63 -35
- package/types.d.ts +146 -68
- package/webmcp-zq1enyz0.js +57 -0
- package/webmcp-zq1enyz0.js.map +10 -0
- package/webmcp.d.ts +13 -24
- package/cdn/connection-hgt931xk.min.js +0 -12
- package/cdn/connection-hgt931xk.min.js.map +0 -22
- package/cdn/ui-2nv84ry7.min.js +0 -86
- package/cdn/ui-2nv84ry7.min.js.map +0 -14
- package/connection-g6wk37yd.js.map +0 -16
- package/index-nmn1j689.js.map +0 -11
- package/ui-04mwfxm9.js +0 -783
- package/ui-04mwfxm9.js.map +0 -11
- package/ui.d.ts +0 -83
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ tools with the browser's own [WebMCP](#webmcp), for agents in the browser. A sit
|
|
|
10
10
|
assistant can call the same tools over a port, without a relay:
|
|
11
11
|
[Your own assistant on the page](#your-own-assistant-on-the-page).
|
|
12
12
|
|
|
13
|
-
**Status: 0.
|
|
13
|
+
**Status: 0.16.1.** Until 1.0, a new minor version can still change the API. Which AI apps
|
|
14
14
|
work is listed in [compatibility](https://github.com/ractive/tab-mcp/blob/main/docs/compatibility.md).
|
|
15
15
|
|
|
16
16
|
## You need a relay
|
|
@@ -26,7 +26,7 @@ docker run --rm -p 8080:8080 \
|
|
|
26
26
|
-e TAB_MCP_PUBLIC_URL=http://localhost:8080 \
|
|
27
27
|
-e TAB_MCP_ALLOWED_ORIGINS=http://localhost:3000 \
|
|
28
28
|
-e TAB_MCP_STORE=memory \
|
|
29
|
-
ractive/tab-mcp:0.
|
|
29
|
+
ractive/tab-mcp:0.10.2
|
|
30
30
|
```
|
|
31
31
|
|
|
32
32
|
For production, see
|
|
@@ -45,7 +45,7 @@ Or without a bundler, from a CDN as an ES module:
|
|
|
45
45
|
|
|
46
46
|
```html
|
|
47
47
|
<script type="module">
|
|
48
|
-
import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.
|
|
48
|
+
import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.16.1/cdn/tab-mcp-client.min.js";
|
|
49
49
|
</script>
|
|
50
50
|
```
|
|
51
51
|
|
|
@@ -76,7 +76,8 @@ await mcp.registerTool({
|
|
|
76
76
|
button.addEventListener("click", () => mcp.showConnectDialog());
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
Importing the library loads only a small core; `connect()` loads the rest
|
|
79
|
+
Importing the library loads only a small core; `connect()` loads the rest, and
|
|
80
|
+
`webmcp: true` loads the WebMCP mirror where the browser has WebMCP.
|
|
80
81
|
|
|
81
82
|
`server` is what the AI app sees: `name`, `version`, optional `title` and
|
|
82
83
|
`instructions`, and `icons`. Without `icons`, the page's own `<link rel="icon">` and
|
|
@@ -88,26 +89,65 @@ PNG, JPEG, GIF, WebP or ICO `data:` URIs up to 16 KiB (no inline SVG). They go o
|
|
|
88
89
|
|
|
89
90
|
### Tools
|
|
90
91
|
|
|
91
|
-
`registerTool(tool, { signal,
|
|
92
|
-
`registerTool` (`
|
|
92
|
+
`registerTool(tool, { signal, callers, exposedTo })` takes a tool in the shape of the
|
|
93
|
+
WebMCP draft's `registerTool` (`callers`: [which callers see a tool](#which-callers-see-a-tool);
|
|
94
|
+
`exposedTo`: see [WebMCP](#webmcp)):
|
|
93
95
|
|
|
94
96
|
- `name` (1-128 characters of `A-Z a-z 0-9 _ - .`), `title`, `description` (not empty);
|
|
95
97
|
- `inputSchema`: a JSON Schema with `"type": "object"`. Arguments are checked against it
|
|
96
98
|
before `execute` runs. `registerTool` rejects a schema the validator cannot use (a
|
|
97
|
-
pattern that is not a regular expression, a `$ref` that does not resolve
|
|
99
|
+
pattern that is not a regular expression, a `$ref` that does not resolve; values of
|
|
100
|
+
`default`, `examples`, `const` and `enum` are data and are not read as schema). The
|
|
98
101
|
validator itself is loaded by `connect()`: from then on it checks each schema at
|
|
99
102
|
registration too, and a tool registered earlier whose schema it refuses (for example a
|
|
100
103
|
`$ref` next to a nested `$id`) is unregistered, with the reason in the event `error`;
|
|
104
|
+
- `outputSchema` (optional): a JSON Schema with `"type": "object"` for the result, listed
|
|
105
|
+
to AI apps (`tools/list`) and passed on to WebMCP. `registerTool` checks it as it checks
|
|
106
|
+
`inputSchema` (an AI app compiles it when it lists the tools, and one that does not
|
|
107
|
+
compile can cost the whole list), but results are not checked against it. An AI app
|
|
108
|
+
expects every result of such a tool to carry
|
|
109
|
+
`structuredContent` that matches it (the MCP SDK's client refuses one without), so
|
|
110
|
+
`execute` returns a plain object, not a string;
|
|
101
111
|
- `execute(input, { signal, client })`: runs the tool. `signal` aborts when the caller
|
|
102
|
-
gives up
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
gives up (or a served call takes too long); the call ends then, also if `execute` does
|
|
113
|
+
not stop, and what it returns later is dropped. `client` names the caller: `{ kind: "relay", grantId, id, host, name? }` for
|
|
114
|
+
an AI app through the relay (`id`, verified `host`, declared `name`),
|
|
115
|
+
`{ kind: "local", id, name? }` for the site's own assistant on a served port, with the
|
|
116
|
+
`id` and `name` the page gave `serve` ([Your own assistant](#your-own-assistant-on-the-page)),
|
|
117
|
+
or `{ kind: "webmcp" }` for an agent in the browser ([WebMCP](#webmcp)), of which nothing
|
|
105
118
|
more is known. Check `client.kind` before you read `host`. Return a
|
|
106
|
-
string (one text block), an MCP result with
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
119
|
+
string (one text block), an MCP result (passed through with its `_meta`: an object
|
|
120
|
+
whose `content` is an array of MCP content blocks, each with `type` `text`, `image`,
|
|
121
|
+
`audio`, `resource_link` or `resource` and the fields that type needs), or any other JSON value (sent as JSON text,
|
|
122
|
+
and as `structuredContent` when that JSON is an object; a `Date` becomes a string, so
|
|
123
|
+
it has none). Data that only looks like a result, such as a ProseMirror document with
|
|
124
|
+
its `content` array, is sent as JSON. A thrown error becomes an error result with its
|
|
125
|
+
message;
|
|
126
|
+
- `annotations`: the WebMCP hints `readOnlyHint`, `consequentialHint`,
|
|
127
|
+
`untrustedContentHint` and `debugging` (a tool meant for the page's developers), and
|
|
128
|
+
the MCP hints `destructiveHint`, `idempotentHint`, `openWorldHint`. A hint must be
|
|
129
|
+
`true` or `false`: `registerTool` rejects any other value with a `TypeError`, since a
|
|
130
|
+
`consequentialHint: "true"` that was dropped would run the tool without asking. MCP has
|
|
131
|
+
no `untrustedContentHint`: every result of such a tool carries a marker in its `_meta`
|
|
132
|
+
([untrusted results](#untrusted-results)).
|
|
133
|
+
|
|
134
|
+
Where `registerTool` differs from the WebMCP draft's (of 12 November 2026), so that code
|
|
135
|
+
written for the native API behaves differently here:
|
|
136
|
+
|
|
137
|
+
- An invalid name, an empty description or an invalid schema is a `TypeError`; the
|
|
138
|
+
draft rejects the first two with an `InvalidStateError`. A duplicate name is an
|
|
139
|
+
`InvalidStateError` in both.
|
|
140
|
+
- `inputSchema` and `outputSchema` need `"type": "object"`, as MCP does; the draft takes
|
|
141
|
+
any object. The library also checks `inputSchema`'s patterns and `$ref`s (above).
|
|
142
|
+
- The library checks the arguments against `inputSchema` before `execute` runs; the
|
|
143
|
+
draft leaves that to the page.
|
|
144
|
+
- `execute` may return an MCP result; through WebMCP it goes out as its
|
|
145
|
+
`structuredContent` or its text ([WebMCP](#webmcp)).
|
|
146
|
+
- `exposedTo` matters only for WebMCP: it is passed on to the native registration of a
|
|
147
|
+
mirrored tool, and AI apps never see it.
|
|
148
|
+
|
|
149
|
+
An already aborted `signal` rejects with its reason and registers nothing, as in the
|
|
150
|
+
draft.
|
|
111
151
|
|
|
112
152
|
In TypeScript, `Tool<Input>` types the `input` of `execute`, and `registerTool` and
|
|
113
153
|
`useTool` infer it from the tool or take it explicitly
|
|
@@ -128,26 +168,42 @@ where the browser has it, so that agents in the browser can call them: an agent
|
|
|
128
168
|
into the browser, or an extension. Off by default. Turning it on is a change in what the
|
|
129
169
|
page exposes: any agent in the browser can then call the mirrored tools without pairing.
|
|
130
170
|
Chrome has WebMCP for sites in its
|
|
131
|
-
[origin trial](https://developer.chrome.com/blog/ai-webmcp-origin-trial) (
|
|
132
|
-
|
|
133
|
-
([Chrome's WebMCP docs](https://developer.chrome.com/docs/ai/webmcp)).
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
171
|
+
[origin trial](https://developer.chrome.com/blog/ai-webmcp-origin-trial) (Chrome 149 to
|
|
172
|
+
162; a site needs a trial token) and with `chrome://flags/#enable-webmcp-testing`
|
|
173
|
+
([Chrome's WebMCP docs](https://developer.chrome.com/docs/ai/webmcp)). Without either,
|
|
174
|
+
`webmcp: true` does nothing. Each `new TabMcp` looks for WebMCP when it is made, so a
|
|
175
|
+
trial token that a script adds later counts for a `TabMcp` made after it.
|
|
176
|
+
|
|
177
|
+
- Tools marked `consequentialHint`, or MCP's `destructiveHint`, are left out unless
|
|
178
|
+
their registration lists `"webmcp"` in `callers` (`{ callers: ["relay", "webmcp"] }`):
|
|
179
|
+
an agent in the browser often drives the very tab it calls into, so it can click Allow
|
|
180
|
+
in the page's confirmation itself. `callers` without `"webmcp"` keeps any tool out of
|
|
181
|
+
WebMCP ([which callers see a tool](#which-callers-see-a-tool)). A tool marked only
|
|
182
|
+
`destructiveHint: true` is registered natively with `consequentialHint: true`, since
|
|
183
|
+
WebMCP has no `destructiveHint` (DEC-022 maps the other way).
|
|
184
|
+
- The native registration gets the WebMCP fields: `name`, `title`, `description`,
|
|
185
|
+
`inputSchema`, `outputSchema` and the WebMCP hints, and `exposedTo` from
|
|
186
|
+
`registerTool`'s options (origins of other documents in the tab that may see the
|
|
187
|
+
tool; the browser checks it, and an invalid list only warns, as below).
|
|
139
188
|
- A WebMCP call takes the same path as an AI app's: the arguments are checked against
|
|
140
189
|
`inputSchema` (Chrome does not check them), a consequential tool asks (`onConfirm`, or
|
|
141
190
|
the library's dialog, which names the caller "An agent in this browser"), and
|
|
142
|
-
`toolcall` and `toolcallend` fire with `client: { kind: "webmcp" }`. "
|
|
191
|
+
`toolcall` and `toolcallend` fire with `client: { kind: "webmcp" }`. "Allow until reload" is
|
|
143
192
|
never offered or applied for these calls: there is no identity to remember it for.
|
|
144
|
-
- The call resolves with what `execute` returned
|
|
193
|
+
- The call resolves with what `execute` returned, which the browser hands the agent as
|
|
194
|
+
JSON text. An MCP result goes out as its `structuredContent` if it has one, else as
|
|
195
|
+
its text blocks, one per line, each other block (an image, a resource) as a line that
|
|
196
|
+
says it was left out: WebMCP results are text, and an image as base64 inside JSON
|
|
197
|
+
costs the agent tokens for nothing. Nothing (`undefined`), which the browser cannot
|
|
198
|
+
serialize, and an empty MCP result resolve as `"Done."`.
|
|
199
|
+
- A failure resolves too, as
|
|
145
200
|
`{ error: "<message>" }`, so that the agent can read it: invalid arguments (the same
|
|
146
201
|
message as for an AI app), "Don't allow" (`The user did not allow <tool> to run.`), a
|
|
147
202
|
question not answered (`The user did not answer whether <tool> may run.`), a tool that
|
|
148
203
|
is gone, an error thrown by `execute` (its message) and an MCP result with
|
|
149
|
-
`isError: true` (its text). A rejection would not help:
|
|
150
|
-
every rejection as
|
|
204
|
+
`isError: true` (its text). A rejection would not help: the draft hands the agent
|
|
205
|
+
every rejection as an `OperationError` without its message, on purpose (a failure the
|
|
206
|
+
agent should read belongs in the result), and Chrome 156 as `UnknownError`. Two cases still
|
|
151
207
|
reject: a call the browser cancelled (`AbortError`), since its caller no longer waits
|
|
152
208
|
for an answer, and a call made when the library's lazily loaded part cannot be loaded
|
|
153
209
|
(a new deploy with other chunk names while the page stays open, the network, a
|
|
@@ -163,14 +219,16 @@ Chrome has WebMCP for sites in its
|
|
|
163
219
|
schema.
|
|
164
220
|
- A failed native registration only warns in the console, once per tool, and the tool
|
|
165
221
|
stays offered through the relay; it is tried again only when the tool is registered
|
|
166
|
-
again
|
|
222
|
+
again (a browser that unregisters a tool some time after its abort would refuse a
|
|
223
|
+
registration of the same name at once after it, as a duplicate; Chrome unregisters at
|
|
224
|
+
once): a name the page or another script already registered
|
|
167
225
|
natively keeps its owner, and a frame without `allow="tools"` refuses all. The other
|
|
168
226
|
way round, once a name is mirrored, the page's own native `registerTool()` of that name
|
|
169
227
|
fails: a page that registers tools natively itself leaves `webmcp` off or uses other
|
|
170
228
|
names.
|
|
171
|
-
-
|
|
172
|
-
|
|
173
|
-
the extension.
|
|
229
|
+
- tab-mcp for Chrome offers mirrored tools like any page tool, since it cannot know
|
|
230
|
+
whether the AI app is also paired with the page, so an AI app connected to the page and
|
|
231
|
+
to the extension sees such a tool twice.
|
|
174
232
|
|
|
175
233
|
### Your own assistant on the page
|
|
176
234
|
|
|
@@ -199,7 +257,11 @@ addEventListener("message", async (event) => {
|
|
|
199
257
|
if (event.data?.type !== "tools-wanted") return;
|
|
200
258
|
const { port1, port2 } = new MessageChannel();
|
|
201
259
|
served?.close(); // the chat asked again (it reloaded): the old port is dead
|
|
202
|
-
served = await mcp.serve(port1, {
|
|
260
|
+
served = await mcp.serve(port1, {
|
|
261
|
+
client: { id: "assistant", name: "Example assistant" },
|
|
262
|
+
// Consequential tools ask in the chat, in the user's language (below).
|
|
263
|
+
onConfirm: (request) => askInChat(request),
|
|
264
|
+
});
|
|
203
265
|
chat.contentWindow!.postMessage({ type: "tools-port" }, "https://chat.example", [port2]);
|
|
204
266
|
});
|
|
205
267
|
```
|
|
@@ -219,11 +281,10 @@ addEventListener("message", (event) => {
|
|
|
219
281
|
});
|
|
220
282
|
```
|
|
221
283
|
|
|
222
|
-
The backend (Bun, Node or anything with `fetch`'s `Request` and `Response`)
|
|
223
|
-
|
|
284
|
+
The backend (Bun, Node or anything with `fetch`'s `Request` and `Response`) makes the
|
|
285
|
+
tunnel's end on its WebSocket:
|
|
224
286
|
|
|
225
287
|
```ts
|
|
226
|
-
import { Client, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
227
288
|
import { createTunnelFetch } from "@ractive-ch/tab-mcp-client/tunnel";
|
|
228
289
|
|
|
229
290
|
const tunnel = createTunnelFetch({
|
|
@@ -234,35 +295,161 @@ socket.on("message", (data) => {
|
|
|
234
295
|
if (message.tunnel) tunnel.receive(message.tunnel);
|
|
235
296
|
});
|
|
236
297
|
socket.on("close", () => tunnel.close());
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
#### The backend and an agent framework
|
|
301
|
+
|
|
302
|
+
An agent framework's own MCP client may not speak MCP 2026-07-28, the only version the
|
|
303
|
+
page speaks. Use the MCP SDK's client (`@modelcontextprotocol/client`), pinned to that
|
|
304
|
+
version, next to the framework, and hand the framework the page's tools as plain function
|
|
305
|
+
tools: most frameworks take a function tool as a name, a description, a JSON Schema and a
|
|
306
|
+
function. This recipe is tested in the repository
|
|
307
|
+
([`packages/tab-mcp-client/test/assistant-backend.ts`](https://github.com/ractive/tab-mcp/blob/main/packages/tab-mcp-client/test/assistant-backend.ts)):
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
import {
|
|
311
|
+
Client,
|
|
312
|
+
StreamableHTTPClientTransport,
|
|
313
|
+
} from "@modelcontextprotocol/client";
|
|
314
|
+
import type { TunnelFetch } from "@ractive-ch/tab-mcp-client/tunnel";
|
|
315
|
+
|
|
316
|
+
/** A function tool as agent frameworks take one: a name, a description, a JSON Schema and a function. */
|
|
317
|
+
export interface PageTool {
|
|
318
|
+
name: string;
|
|
319
|
+
description: string;
|
|
320
|
+
inputSchema: Record<string, unknown>;
|
|
321
|
+
run(args: Record<string, unknown>): Promise<string>;
|
|
322
|
+
}
|
|
237
323
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
);
|
|
243
|
-
// The
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
324
|
+
const ANNOTATIONS = "ch.ractive.tab-mcp/annotations";
|
|
325
|
+
|
|
326
|
+
export async function pageTools(
|
|
327
|
+
tunnel: TunnelFetch,
|
|
328
|
+
): Promise<{ tools: PageTool[]; close(): Promise<void> }> {
|
|
329
|
+
// The page speaks MCP 2026-07-28 only: pin it, or the SDK starts with an older version.
|
|
330
|
+
const client = new Client(
|
|
331
|
+
{ name: "assistant", version: "1.0.0" },
|
|
332
|
+
{ versionNegotiation: { mode: { pin: "2026-07-28" } } },
|
|
333
|
+
);
|
|
334
|
+
// The URL is never fetched; only its path reaches the page.
|
|
335
|
+
await client.connect(
|
|
336
|
+
new StreamableHTTPClientTransport(new URL("https://tab.invalid/mcp"), {
|
|
337
|
+
fetch: tunnel.fetch,
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
340
|
+
const { tools } = await client.listTools();
|
|
341
|
+
return {
|
|
342
|
+
tools: tools.map((tool) => ({
|
|
343
|
+
name: tool.name,
|
|
344
|
+
description: tool.description ?? "",
|
|
345
|
+
inputSchema: tool.inputSchema,
|
|
346
|
+
async run(args) {
|
|
347
|
+
const result = await client.callTool({
|
|
348
|
+
name: tool.name,
|
|
349
|
+
arguments: args,
|
|
350
|
+
});
|
|
351
|
+
const text = result.content
|
|
352
|
+
.map((block) =>
|
|
353
|
+
block.type === "text" ? block.text : `[${block.type}]`,
|
|
354
|
+
)
|
|
355
|
+
.join("\n");
|
|
356
|
+
if (result.isError === true) return `The tool failed: ${text}`;
|
|
357
|
+
// Text from third parties (reviews, messages): say so to the model.
|
|
358
|
+
const hints = result._meta?.[ANNOTATIONS] as
|
|
359
|
+
| { untrustedContentHint?: boolean }
|
|
360
|
+
| undefined;
|
|
361
|
+
return hints?.untrustedContentHint === true
|
|
362
|
+
? `<untrusted-content>\n${text}\n</untrusted-content>`
|
|
363
|
+
: text;
|
|
364
|
+
},
|
|
365
|
+
})),
|
|
366
|
+
close: () => client.close(),
|
|
367
|
+
};
|
|
368
|
+
}
|
|
248
369
|
```
|
|
249
370
|
|
|
371
|
+
`const { tools, close } = await pageTools(tunnel)` lists the tools once; give each to the
|
|
372
|
+
framework with `inputSchema` as its parameters and `run` as its function. List them again
|
|
373
|
+
for a new conversation, or when the page's tools change
|
|
374
|
+
(`notifications/tools/list_changed` on a `subscriptions/listen` stream).
|
|
375
|
+
|
|
376
|
+
**Page context.** The server's `instructions` reach the assistant once, when its client
|
|
377
|
+
connects (`server/discover`), and MCP has no notification for instructions that changed.
|
|
378
|
+
What changes with the page (the product shown, the cart, the user's selection) belongs in
|
|
379
|
+
a read-only tool such as `get_page_context` (`readOnlyHint: true`, perhaps
|
|
380
|
+
`callers: ["local"]`) that the model calls when it needs it.
|
|
381
|
+
|
|
382
|
+
#### Asking in your chat
|
|
383
|
+
|
|
384
|
+
A consequential tool asks before every call. For the calls through one port, `serve`'s
|
|
385
|
+
`onConfirm` asks instead of the page's `onConfirm` and the library's dialog; calls through
|
|
386
|
+
the relay and WebMCP keep those. That is the recommended way for a site's own assistant:
|
|
387
|
+
the question appears where the user talks to the assistant, in the user's language. It
|
|
388
|
+
answers as the page's `onConfirm` does: `true`, `false` or `"unanswered"`; anything else,
|
|
389
|
+
and a throw, is a refusal. `request.signal` aborts when the call is cancelled, takes longer
|
|
390
|
+
than `requestTimeoutMs` or the serve closes: take the question away then.
|
|
391
|
+
|
|
392
|
+
The answer has to come from the user's click, in UI that the assistant cannot answer for
|
|
393
|
+
it. The library does not send the question over the port, because the other end of the
|
|
394
|
+
port is the caller that wants the tool to run: a model that injected text told to say yes
|
|
395
|
+
would answer it. If the chat in the iframe shows the question, give it the question on a
|
|
396
|
+
channel of its own, and let only a click on its buttons answer, never a message from the
|
|
397
|
+
backend.
|
|
398
|
+
|
|
399
|
+
Do not answer `true` for `client.kind === "local"` to skip the question. Tool results carry
|
|
400
|
+
text the site did not write (reviews, messages, other users' posts) into the model, and
|
|
401
|
+
that text can tell the model to call a consequential tool. The question is the one check
|
|
402
|
+
between that text and the tool.
|
|
403
|
+
|
|
404
|
+
#### Which callers see a tool
|
|
405
|
+
|
|
406
|
+
`registerTool(tool, { callers })` lists the callers that see a tool: `"relay"` (AI apps
|
|
407
|
+
through the relay), `"local"` (a served port) and `"webmcp"` (agents in the browser). By
|
|
408
|
+
default all of them, except that a consequential tool goes to WebMCP only when `"webmcp"`
|
|
409
|
+
is listed ([WebMCP](#webmcp)). For a caller that is not listed the tool does not exist:
|
|
410
|
+
`tools/list` leaves it out, `notifications/tools/list_changed` does not fire for it, and a
|
|
411
|
+
call gets the same error as a call of an unknown tool. `{ callers: ["local"] }` keeps a
|
|
412
|
+
tool for the site's own assistant, `{ callers: ["relay", "webmcp"] }` keeps it from the
|
|
413
|
+
port.
|
|
414
|
+
|
|
415
|
+
#### Untrusted results
|
|
416
|
+
|
|
417
|
+
A tool marked `untrustedContentHint` returns text the page did not write. MCP has no such
|
|
418
|
+
hint: in `tools/list` the tool has `annotations.openWorldHint: true` and the original hints
|
|
419
|
+
in `_meta["ch.ractive.tab-mcp/annotations"]` (for example
|
|
420
|
+
`{ "readOnlyHint": true, "untrustedContentHint": true }`). Every result of such a tool
|
|
421
|
+
carries `_meta["ch.ractive.tab-mcp/annotations"]: { "untrustedContentHint": true }` too,
|
|
422
|
+
through the relay and through a port, next to the tool's own `_meta`. A consumer reads the
|
|
423
|
+
marker on the result, so it needs no list at hand, and keeps such text apart from its
|
|
424
|
+
instructions, as the recipe does with a delimited block.
|
|
425
|
+
|
|
426
|
+
#### More about `serve`
|
|
427
|
+
|
|
250
428
|
- **A port is a key.** Whoever holds the other end calls the tools without pairing, and
|
|
251
429
|
the library cannot check who that is. Hand a port only to a frame whose origin you
|
|
252
430
|
checked, as above, and send it with that origin as the target, never `"*"`.
|
|
253
431
|
- Calls arrive with `client: { kind: "local", id, name }`, as given to `serve`. A `grant`
|
|
254
432
|
in a message is ignored, so the other end cannot pass for an AI app of the relay.
|
|
255
|
-
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
433
|
+
- Without `serve`'s `onConfirm`, a consequential call through the port asks the page's
|
|
434
|
+
`onConfirm` or the library's dialog, which names the caller "Example assistant (this
|
|
435
|
+
site's assistant)" and never offers "Allow until reload".
|
|
436
|
+
- `requestTimeoutMs` (300 s by default, as `createTunnelFetch`; below 2^31 ms) limits each
|
|
437
|
+
request on the page's side: on expiry the call is aborted (`execute`'s signal and the
|
|
438
|
+
question's), `toolcallend` says `cancelled`, a later answer of the tool is dropped, and
|
|
439
|
+
the other end gets the JSON-RPC error `-31002`, as from `createTunnelFetch`'s own limit.
|
|
440
|
+
A `subscriptions/listen` stream is not limited.
|
|
259
441
|
- `serve` resolves with `{ close() }`; `options.signal` does the same. Closing aborts the
|
|
260
442
|
calls in flight and tells the other end (`bye`), whose requests then end at once with
|
|
261
443
|
`-31003`. Close a serve whose port is dead, as above: its calls, listen streams and an
|
|
262
|
-
open confirmation would stay otherwise.
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
444
|
+
open confirmation would stay otherwise. Where the port fires `close` when its other end
|
|
445
|
+
goes away (Bun does; Chrome only with the `MessagePortCloseEvent` Blink feature), the
|
|
446
|
+
serve closes itself then. `serve` may be called for several ports (each port once at a
|
|
447
|
+
time), and next to a relay connection and `webmcp: true`: one registration reaches all
|
|
448
|
+
three.
|
|
449
|
+
- A port has none of the relay's limits: no cap on requests in flight, listen streams,
|
|
450
|
+
body size or rate. The other end is the site's own code (threat model A15). The
|
|
451
|
+
confirmations of consequential calls are shown one at a time, however many arrive.
|
|
452
|
+
- `createTunnelFetch` answers a request that takes longer than its `requestTimeoutMs`
|
|
266
453
|
(300 s by default; a `subscriptions/listen` stream is not limited) with the JSON-RPC
|
|
267
454
|
error `-31002`, and every request after `close()` with `-31003`, at HTTP 200 like the
|
|
268
455
|
relay: the MCP client reads an error, not a network failure. Only an aborted `signal`
|
|
@@ -279,22 +466,42 @@ asks the user before every call ("Don't allow" or "Allow"), showing the AI app a
|
|
|
279
466
|
arguments. A refusal reaches the AI app as an error result, and so does a call whose tool
|
|
280
467
|
was removed while the user was asked.
|
|
281
468
|
|
|
282
|
-
`alwaysAllow: true` adds "
|
|
283
|
-
AI app until the page is reloaded or the app is disconnected.
|
|
469
|
+
`alwaysAllow: true` adds "Allow until reload", which lets that tool run without asking for
|
|
470
|
+
that AI app, with any arguments, until the page is reloaded or the app is disconnected.
|
|
471
|
+
The connect dialog lists such tools under the app ("Runs without asking: …") with "Ask
|
|
472
|
+
again", and a tool registered again unchanged (React's `useTool` on a remount) keeps its
|
|
473
|
+
entry; one whose title or description changed is asked again. It is off by default: it
|
|
284
474
|
lifts the only brake at run time, which is what a prompt injection needs, and the relay
|
|
285
475
|
cannot help, since its one OAuth scope (`tools`) covers every tool of the page. Consent
|
|
286
476
|
per tool is the page's. Turn it on only for tools whose calls the user need not check one
|
|
287
477
|
by one.
|
|
288
478
|
|
|
289
479
|
Tools are called while the user is in the AI app, so the dialog often opens in a tab in
|
|
290
|
-
the background. "Allow" and "
|
|
291
|
-
visible and focused for
|
|
480
|
+
the background. "Allow" and "Allow until reload" can be clicked only once the page has
|
|
481
|
+
been visible and focused for 750 ms with the dialog open; leaving the page
|
|
292
482
|
disables them and coming back starts the wait again, so the click that brings the page
|
|
293
|
-
forward cannot approve.
|
|
483
|
+
forward cannot approve. One confirmation is shown at a time; the others wait, the shown
|
|
484
|
+
one says how many, and each starts its own wait when it is shown (and again when another
|
|
485
|
+
dialog of the page closes over it), so the second click of a double click cannot approve
|
|
486
|
+
the next call. A call cancelled while it waits is never shown. When the arguments do not
|
|
487
|
+
fit their box (`min(50vh, 24rem)`), the approving buttons also wait until the box has been
|
|
488
|
+
scrolled to its end, and the dialog says so: the AI app chooses the order of the keys, so
|
|
489
|
+
what matters may come last. Before the wait has run out, Enter or Space on a button and
|
|
490
|
+
Escape do nothing, so typing meant for the page cannot refuse a call the user has not
|
|
491
|
+
seen; a click on "Don't allow" works at once. Characters that are invisible, blank (such as U+00A0, U+3000 or
|
|
294
492
|
U+2800) or change the text direction (such as U+202E) are shown in the arguments and the
|
|
295
493
|
AI app's name as `\uXXXX`, so the AI app cannot make them look like something else; the
|
|
296
494
|
tool gets the arguments unchanged.
|
|
297
495
|
|
|
496
|
+
While a confirmation is open in a hidden tab, the tab's title starts with "Allow? – ", so
|
|
497
|
+
the user can find the tab that asks; the title is put back when the tab is shown or the
|
|
498
|
+
dialog closes (unless the page changed it meanwhile). In `tools/list`, the description of
|
|
499
|
+
a consequential tool ends with "The page may ask the user to allow
|
|
500
|
+
each call, usually in the page's browser tab.", so the AI app can tell the user where to look while it waits.
|
|
501
|
+
Not when an `onConfirm` answers (the page's, or for a served port its own): it may ask
|
|
502
|
+
anywhere, such as a side panel or the site's chat. Say where in the tool's own
|
|
503
|
+
description if the AI app should know.
|
|
504
|
+
|
|
298
505
|
Pass `onConfirm` to ask in your own UI (it is asked every time; remember answers there if
|
|
299
506
|
you want to). Show the verified `client.host` next to the name the AI app declares, as the
|
|
300
507
|
library's dialog does, since anyone can declare any name. Escape the arguments and the
|
|
@@ -309,7 +516,8 @@ const mcp = new TabMcp({
|
|
|
309
516
|
server: { name: "calculator", version: "1.0.0" },
|
|
310
517
|
onConfirm: (request) => {
|
|
311
518
|
const { tool, arguments: args, client, signal } = request;
|
|
312
|
-
// An agent in the browser (`webmcp`)
|
|
519
|
+
// An agent in the browser (`webmcp`), or the site's own assistant (`local`) on a
|
|
520
|
+
// port served without its own onConfirm: the library's own dialog.
|
|
313
521
|
if (client.kind !== "relay") return mcp.confirmInPage(request);
|
|
314
522
|
const name = client.name ? escapeInvisible(client.name) : undefined;
|
|
315
523
|
const app = name ? `${name} (${client.host})` : client.host;
|
|
@@ -324,8 +532,8 @@ asked and did not answer, e.g. your question timed out. The AI app then reads "T
|
|
|
324
532
|
asked and did not answer." instead of "The user declined", and `toolcallend` has the
|
|
325
533
|
outcome `unanswered`. Any other value, and a throw, is a refusal.
|
|
326
534
|
|
|
327
|
-
`mcp.confirmInPage(request)` is the library's own dialog, for an
|
|
328
|
-
handles only some calls itself.
|
|
535
|
+
`mcp.confirmInPage(request, { document })` is the library's own dialog, for an
|
|
536
|
+
`onConfirm` that handles only some calls itself (`document` defaults to the global one).
|
|
329
537
|
|
|
330
538
|
### Connecting and pairing
|
|
331
539
|
|
|
@@ -334,25 +542,40 @@ handles only some calls itself.
|
|
|
334
542
|
opens the "Connect your AI app" dialog. A script's `element.click()` has no user
|
|
335
543
|
activation, so tests and browser automation see "connect() must be called from a
|
|
336
544
|
user action"; a click through Playwright or the DevTools protocol has one, and
|
|
337
|
-
`connect({ resume: true })` skips the check.
|
|
338
|
-
|
|
339
|
-
|
|
545
|
+
`connect({ resume: true })` skips the check. When it cannot connect, the dialog says
|
|
546
|
+
why in plain words ("Could not connect. Check your internet connection.", "Too many
|
|
547
|
+
attempts…", "The connection was refused.", or "Not connected." when the click had no
|
|
548
|
+
user activation) and offers "Connect again", which connects from its own click; the
|
|
549
|
+
error itself goes to the console (`console.warn`). After a disconnect it says why
|
|
550
|
+
("The session ended." for `expired` and `idle`) and offers the same button. While the
|
|
551
|
+
library reconnects by itself, it says "Connection lost. Reconnecting…", and "Connect
|
|
552
|
+
again" tries at once.
|
|
553
|
+
2. The dialog shows the address with Copy and, right below it, "How do I add it?" (closed;
|
|
554
|
+
open, its steps would push the code out of view): short instructions for Claude,
|
|
555
|
+
ChatGPT, Claude Code and Codex. The commands for Claude Code and Codex each have a
|
|
556
|
+
Copy. They name the
|
|
340
557
|
server by a label made from `server.name` that every shell reads as one plain word:
|
|
341
558
|
lower case, letters without marks, each run of other characters as `-`, at most 40
|
|
342
559
|
characters, `tools` if nothing is left ("Bücher" gives `bucher`). The address goes
|
|
343
560
|
into a command only if it has nothing but plain URL characters; otherwise the help
|
|
344
|
-
shows the steps without commands.
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
561
|
+
shows the steps without commands. Each entry ends with the step that starts the
|
|
562
|
+
sign-in: `/mcp` and Authenticate in Claude Code, `codex mcp login <name>` as the third
|
|
563
|
+
line of Codex's command. Codex 0.163 needs its feature `mcp_2026_07_28` for the relay's MCP
|
|
564
|
+
revision, so its command turns that on first. The steps are the minimum; the link at
|
|
565
|
+
the end of the help leads to the guide with the rest (ChatGPT's Mac app, turning the
|
|
566
|
+
Codex feature off, what to do when it fails). A page can link its own guide and give
|
|
567
|
+
its own steps (`ui.setup`, see [Texts and help of the dialogs](#texts-and-help-of-the-dialogs)).
|
|
568
|
+
The user adds the address to their AI app.
|
|
348
569
|
3. The AI app opens the relay's sign-in page in the browser. The dialog shows a 6-digit
|
|
349
570
|
code with a Copy button, which the user types or pastes there. The code changes every
|
|
350
571
|
2 minutes and after each use.
|
|
351
572
|
4. Once the AI app connects, the dialog says so at the top: a check mark, "Claude
|
|
352
|
-
(claude.ai) is connected" and what to do next (close it and ask the app to use the
|
|
573
|
+
(claude.ai) is connected" (every connected app is named) and what to do next (close it and ask the app to use the
|
|
353
574
|
page's tools); screen readers hear the same. The setup (address, code, help) moves
|
|
354
575
|
behind "Connect another app", closed, so the next code does not appear in view; the
|
|
355
|
-
connected app is listed with a Disconnect button
|
|
576
|
+
connected app is listed with a Disconnect button (and the tools it may run without
|
|
577
|
+
asking, with "Ask again"), and "Done" closes the dialog. A Disconnect the relay could
|
|
578
|
+
not save says so in the list ("The relay could not save this. Try again.").
|
|
356
579
|
Opened again while an app is connected, the dialog starts like this, with the focus
|
|
357
580
|
on Done.
|
|
358
581
|
|
|
@@ -378,35 +601,50 @@ key yourself; the name is URI-encoded, and renaming it changes the address). A s
|
|
|
378
601
|
existing page keeps its address; on a site with several pages the first one to connect
|
|
379
602
|
keeps it, the others get new ones. The old key stays for pages still on 0.4.
|
|
380
603
|
|
|
381
|
-
"New address" in the dialog
|
|
382
|
-
|
|
604
|
+
"New address" in the dialog (at the end of the setup, below the code, with a line on what
|
|
605
|
+
it does; not next to Copy, since with no app connected it changes the address at once)
|
|
606
|
+
calls `newAddress()`, which disconnects every
|
|
607
|
+
app and makes a new address; when apps are connected, the dialog asks first. It first
|
|
608
|
+
takes the old address back: it connects with the stored secret, and the relay revokes
|
|
609
|
+
every grant of the address in
|
|
610
|
+
the same step (`hello.revokeAll`), whether the page was connected or not. Then it makes
|
|
611
|
+
the new secret, clears `grants`, and connects with it if the page was connected,
|
|
612
|
+
connecting or reconnecting (or taken over). A page that never had a secret makes one
|
|
613
|
+
without connecting. If the old address cannot be taken back (the relay is unreachable,
|
|
614
|
+
or someone takes it over again first), it rejects and keeps the old secret, so calling
|
|
615
|
+
it again tries again. A second call while it runs returns the same promise; `connect()`
|
|
616
|
+
meanwhile waits for it and then connects with the new address. The relay's
|
|
383
617
|
sign-in page points users to it when someone else may be guessing codes. A page with its
|
|
384
618
|
own UI calls `newAddress()` from a click. Connected apps keep working after a reload once the page connects
|
|
385
619
|
again; while the page is closed, they get "the page is not open".
|
|
386
620
|
|
|
387
621
|
When another tab or program connects with the same address, it takes the address over:
|
|
388
|
-
the page gets `disconnected` with reason `replaced` at once, then the
|
|
622
|
+
the page gets `statechange` to `disconnected` with reason `replaced` at once, then the
|
|
623
|
+
event `replaced`,
|
|
389
624
|
and does not reconnect by itself. Tabs of the same page say on a `BroadcastChannel` (per
|
|
390
|
-
relay and storage key; it carries
|
|
391
|
-
accepted their session, so `replaced` says which it probably
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
625
|
+
relay and storage key; it carries the address id, which is part of the address, never
|
|
626
|
+
the secret) when the relay accepted their session, so `replaced` says which it probably
|
|
627
|
+
was:
|
|
628
|
+
|
|
629
|
+
- `event.detail.sibling` is `true`: a tab of the same page in this browser said within
|
|
630
|
+
the last 10 seconds that it claimed this tab's address, after this tab's own session
|
|
631
|
+
started (a claim of another address, or one heard while this tab was still connecting,
|
|
632
|
+
does not count; a claim without an address, from a tab still on 0.15 or older, does). That message usually comes just after the takeover, so the
|
|
395
633
|
library waits up to a second for it, and `replaced` may come that much after
|
|
396
|
-
`
|
|
634
|
+
the `statechange`. The usual cause, and harmless. Once the page has opened the default
|
|
397
635
|
dialog, the library shows "Opened in another tab".
|
|
398
636
|
- `false`: none did (or the browser has no `BroadcastChannel`). It may be a program with
|
|
399
637
|
a copy of the address secret, and the AI apps paired with the address now talk to it.
|
|
400
638
|
The library shows "Another tab or program took over this page" with "New address".
|
|
401
639
|
|
|
402
|
-
After a takeover, `newAddress()` (from the `replaced` listener, before it, or later
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
the
|
|
640
|
+
After a takeover, `newAddress()` (from the `replaced` listener, before it, or later, also
|
|
641
|
+
after a reload) takes the address back as above and makes the new address only once the
|
|
642
|
+
relay confirmed that no grant is left. If the address is taken over again before that,
|
|
643
|
+
it rejects with an error whose `reason` is `"replaced"` and keeps the old secret, so
|
|
644
|
+
calling it again takes it back again. A relay of 0.4 or older cannot take an address
|
|
645
|
+
back: `newAddress()` rejects there with `reason` `"policy"`. When the takeover ended a
|
|
646
|
+
session of this page, it then connects with the new address; the default notice opens
|
|
647
|
+
the connect dialog to add it to the AI apps.
|
|
410
648
|
|
|
411
649
|
The default dialog closes when the page is replaced, and the notice appears also when the
|
|
412
650
|
dialog is closed, but only once the page has opened the dialog: a page that connects on
|
|
@@ -449,16 +687,21 @@ third-party script, another page on the same origin) or access to the device can
|
|
|
449
687
|
pair with.
|
|
450
688
|
|
|
451
689
|
When the connection breaks (reason `network`, `heartbeat` or `shutdown`: a dropped
|
|
452
|
-
network, a phone pausing the tab in the background, a relay restart
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
690
|
+
network, a phone pausing the tab in the background, a relay restart; close codes 1001,
|
|
691
|
+
1011, 1012 and 1013 from a proxy in front of the relay count as `shutdown`), the library
|
|
692
|
+
connects again by itself: after about 5, 10, 20 and 40 seconds, then about every 60
|
|
693
|
+
seconds (each wait varies by up to 20 %, so that the tabs of a restarted relay do not all
|
|
694
|
+
come back at once), and at once when the tab comes back to the front or the network
|
|
695
|
+
returns. Meanwhile `state` is `reconnecting` (while it waits and while it makes an
|
|
696
|
+
attempt), `reconnecting` says which attempt is next and when, and `statechange` fires
|
|
697
|
+
for each attempt it schedules; only the `statechange` that leaves `connected` carries the
|
|
698
|
+
`reason` ([Your own UI](#your-own-ui)). Calling `connect()` meanwhile tries at once. It never reconnects after `disconnect()`, a new address,
|
|
458
699
|
another tab taking over the address (`replaced`), an expired or idle session, or a
|
|
459
700
|
refusal by the relay, and an attempt the relay refuses stops it (an unreachable relay is
|
|
460
|
-
tried again); the event `error` then says why,
|
|
461
|
-
|
|
701
|
+
tried again); the event `error` then says why, with `state` already `disconnected`, just
|
|
702
|
+
before the `statechange` to `disconnected`, also when a `connect()` of the page waits on
|
|
703
|
+
that attempt and rejects with the same error. `reconnect: false` turns it off; the waits
|
|
704
|
+
are fixed.
|
|
462
705
|
|
|
463
706
|
### Keeping AI access on after a reload
|
|
464
707
|
|
|
@@ -468,11 +711,12 @@ AI access on, remember the user's choice and connect on load:
|
|
|
468
711
|
|
|
469
712
|
```ts
|
|
470
713
|
const key = "my-page:ai-on";
|
|
471
|
-
|
|
472
|
-
|
|
714
|
+
// On once an AI app is paired: opening the dialog and closing it again does not count.
|
|
715
|
+
mcp.addEventListener("grantschange", () => {
|
|
716
|
+
if (mcp.grants.length > 0) localStorage.setItem(key, "1");
|
|
473
717
|
});
|
|
474
718
|
// Turned off by the user (`disconnect()`): stay off after the next reload.
|
|
475
|
-
mcp.addEventListener("
|
|
719
|
+
mcp.addEventListener("statechange", (e) => {
|
|
476
720
|
if (e.detail.reason === "user") localStorage.removeItem(key);
|
|
477
721
|
});
|
|
478
722
|
turnOffButton.onclick = () => mcp.disconnect();
|
|
@@ -508,33 +752,98 @@ const mcp = new TabMcp({
|
|
|
508
752
|
});
|
|
509
753
|
|
|
510
754
|
// In your logout: the relay ends every AI connection of this page at once.
|
|
511
|
-
logoutButton.onclick = () => {
|
|
512
|
-
//
|
|
513
|
-
|
|
755
|
+
logoutButton.onclick = () => {
|
|
756
|
+
mcp.signOut().catch(() => {}); // settles once the relay removed the grants
|
|
757
|
+
// then your own logout
|
|
758
|
+
};
|
|
759
|
+
// After a sign-in without a reload: "bound", "refused" or "none".
|
|
760
|
+
const identity = await mcp.refreshIdentity();
|
|
514
761
|
```
|
|
515
762
|
|
|
516
763
|
The library asks the function after every connect. `identity` (`none`, `pending`,
|
|
517
764
|
`bound`, `refused`, with the reason in `identityMessage`) and the event
|
|
518
|
-
`identitychange` tell you whether the relay accepted it; when the function or the relay does not answer within
|
|
765
|
+
`identitychange` tell you whether the relay accepted it; when the function or the relay does not answer within 15 s (counted from when the function is asked), it goes back to `none`. `signOut()` and `refreshIdentity()` reject with an `InvalidStateError` while the page is not connected (`signOut()` also on a relay that pairs by code); `identity` is cleared either way. An AI app signed in as someone else gets "This AI tool is logged in as someone other
|
|
519
766
|
than the page."
|
|
520
767
|
|
|
768
|
+
The connect dialog follows `identity`: it shows the address and "When your AI app asks,
|
|
769
|
+
sign in with the account you use on this page" only once it is `bound`. Until then it
|
|
770
|
+
says what is missing: "Checking your sign-in…" (`pending`), "Sign in to this page first."
|
|
771
|
+
(`none`), or that the sign-in was not accepted, with `identityMessage` (`refused`).
|
|
772
|
+
|
|
521
773
|
### Your own UI
|
|
522
774
|
|
|
523
|
-
Instead of `showConnectDialog()`, use
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
`
|
|
775
|
+
Instead of `showConnectDialog()`, use the methods, properties and events below.
|
|
776
|
+
|
|
777
|
+
| Methods | |
|
|
778
|
+
|---|---|
|
|
779
|
+
| `connect(options?)` | resolves with the session (`SessionInfo`) |
|
|
780
|
+
| `disconnect()` | ends the session at once |
|
|
781
|
+
| `newAddress()` | resolves once the new address is in use (connected again if it was on) |
|
|
782
|
+
| `startPairing()`, `stopPairing()` | codes come only while pairing is on |
|
|
783
|
+
| `revoke(grantId)` | resolves once the relay removed the grant |
|
|
784
|
+
| `signOut()` | resolves once the relay removed the grants (identity binding) |
|
|
785
|
+
| `refreshIdentity()` | resolves with the new `identity`; a `TypeError` without `identityAssertion` |
|
|
786
|
+
|
|
787
|
+
A method that asks the relay rejects with an `InvalidStateError` while the page is not
|
|
788
|
+
connected: nothing was sent. `revoke()` and `signOut()` reject with a `NetworkError` when
|
|
789
|
+
the session ends after the message was sent and before the relay answered, and with a
|
|
790
|
+
`TimeoutError` when the relay has not answered within 15 s: in both cases the relay may
|
|
791
|
+
or may not have done it, and `grants` says so once the page is connected again. They
|
|
792
|
+
also reject with the relay's refusal of that message, an `Error` whose `code` is
|
|
793
|
+
`store_unavailable` (the relay could not save the change: try again) or `unknown_id`.
|
|
794
|
+
A relay after 0.10.2 names the message it answers (`for`, and `grantId` for a
|
|
795
|
+
revocation), so with several revocations pending only the one it answers rejects; an
|
|
796
|
+
older relay's refusal rejects every pending `revoke()` and `signOut()`. When the relay
|
|
797
|
+
does not know a grant the page lists (it expired), the grant leaves `grants` (with
|
|
798
|
+
`grantschange`) and its `revoke()` resolves.
|
|
799
|
+
|
|
800
|
+
With a relay, `newAddress()` rejects with an error whose `addressChanged` says what happened: `false`
|
|
801
|
+
when the old address could not be taken back (it is kept with its grants; call
|
|
802
|
+
`newAddress()` again), `true` when the address changed and only connecting with the new
|
|
803
|
+
one failed (call `connect()`).
|
|
804
|
+
|
|
805
|
+
The properties are `state` (`disconnected`, `connecting`, `connected`, `reconnecting`),
|
|
806
|
+
`session` (while connected; `session.address` is the MCP address, `session.appName` the
|
|
807
|
+
name the relay shows), `reconnecting` (the next attempt, while `state` is
|
|
808
|
+
`reconnecting`), `pairingCode`, `grants`, `identity`, `identityMessage`, `tools` and
|
|
809
|
+
`hasRelay`.
|
|
810
|
+
|
|
811
|
+
Every event's `detail` is an object. A change event names the new values like the
|
|
812
|
+
properties (`grantschange` has `grants`), and a value that is gone is `undefined`:
|
|
813
|
+
|
|
814
|
+
| Event | `detail` |
|
|
815
|
+
|---|---|
|
|
816
|
+
| `statechange` | `{ state, reason? }`, see below |
|
|
817
|
+
| `grantschange` | `{ grants }` |
|
|
818
|
+
| `pairingcode` | `{ pairingCode }` |
|
|
819
|
+
| `identitychange` | `{ identity, identityMessage }` |
|
|
820
|
+
| `toolschange` | `{ tools }` |
|
|
821
|
+
| `replaced` | `{ sibling }` (see above) |
|
|
822
|
+
| `error` | `{ error }` |
|
|
823
|
+
| `toolcall`, `toolcallend` | the call, see below |
|
|
824
|
+
|
|
825
|
+
`statechange`'s `reason` (a `DisconnectReason`) says why a session or an attempt to
|
|
826
|
+
open one ended, once: on the change from `connected` (to `disconnected`, or to
|
|
827
|
+
`reconnecting` when the connection broke), and when `disconnect()` or `newAddress()`
|
|
828
|
+
ends connecting or reconnecting. It is absent on every other change: `connecting`,
|
|
829
|
+
`connected`, each later reconnect attempt (`statechange` fires for each, and
|
|
830
|
+
`reconnecting.reason` keeps the reason of the break), a failed `connect()`, and the
|
|
831
|
+
`disconnected` after a refusal stopped reconnecting (the `error` just before says why).
|
|
832
|
+
|
|
833
|
+
For an activity log, `toolcall`
|
|
529
834
|
and `toolcallend` report every call by an AI app (or, with `webmcp`, an agent in the
|
|
530
|
-
browser): its id, tool, arguments and caller as it starts, then its outcome (`ok`,
|
|
835
|
+
browser, or the assistant on a served port): its id, tool, arguments and caller as it starts, then its outcome (`ok`,
|
|
531
836
|
`error`, `declined`, `unanswered` or `cancelled`), result and duration.
|
|
532
837
|
|
|
533
838
|
```ts
|
|
534
839
|
mcp.addEventListener("toolcallend", (e) => {
|
|
535
840
|
const { tool, client, outcome, durationMs } = e.detail;
|
|
536
841
|
const who =
|
|
537
|
-
client.kind === "relay"
|
|
842
|
+
client.kind === "relay"
|
|
843
|
+
? (client.name ?? client.host)
|
|
844
|
+
: client.kind === "local"
|
|
845
|
+
? (client.name ?? client.id)
|
|
846
|
+
: "An agent in this browser";
|
|
538
847
|
log(`${who}: ${tool} ${outcome} in ${durationMs} ms`);
|
|
539
848
|
});
|
|
540
849
|
```
|
|
@@ -546,7 +855,7 @@ add it?", as data for your own UI. An entry of its own, out of the core, and not
|
|
|
546
855
|
build. `setupInstructions(serverName, address)` returns one entry per AI app (Claude,
|
|
547
856
|
ChatGPT, Claude Code, Codex, in that order): `id` (`claude`, `chatgpt`, `claude-code`,
|
|
548
857
|
`codex`), `app` (its name as shown), `text` (what to do), `command` (lines to run, for
|
|
549
|
-
Claude Code and Codex) and `note` (text and code parts, for Codex). Every string is plain
|
|
858
|
+
Claude Code and Codex) and `note` (text and code parts, for Claude Code and Codex). Every string is plain
|
|
550
859
|
text: set it with `textContent`. The commands name the server by a label made from
|
|
551
860
|
`serverName` and contain the address only when every shell reads both as plain words (see
|
|
552
861
|
step 2 above), so they are safe to show with a Copy button; for an address that is not
|
|
@@ -568,14 +877,55 @@ for (const { app, text, command, note } of setupInstructions("notes", session.ad
|
|
|
568
877
|
guide.href = SETUP_GUIDE;
|
|
569
878
|
```
|
|
570
879
|
|
|
880
|
+
### Texts and help of the dialogs
|
|
881
|
+
|
|
882
|
+
Every text of the dialogs is in one table, [`src/strings.ts`](src/strings.ts) (the type
|
|
883
|
+
`DialogStrings`). Pass your own for some or all keys as `ui.strings`; the rest stay
|
|
884
|
+
English. `{name}` placeholders are filled in: names of tools and AI apps (isolated, so
|
|
885
|
+
they cannot reorder your sentence), the code, counts. Where English needs two forms there
|
|
886
|
+
are two keys (`waitingOne`, `waitingMany`). `lang` and `dir` are the language and direction
|
|
887
|
+
set on every dialog: set them with your texts.
|
|
888
|
+
|
|
889
|
+
`ui.setup` changes the help in the connect dialog: `guide` is the link at its end
|
|
890
|
+
(default the guide on ractive.ch; `null` for none, for example on your own relay, and
|
|
891
|
+
then your own `instructions` too, since the default steps refer to the guide), and
|
|
892
|
+
`instructions(serverName, address)` returns the steps per AI app, each `{ app, text,
|
|
893
|
+
command?, note? }` (default `setupInstructions` of `@ractive-ch/tab-mcp-client/setup`,
|
|
894
|
+
which you can filter or extend).
|
|
895
|
+
|
|
896
|
+
```ts
|
|
897
|
+
import { setupInstructions } from "@ractive-ch/tab-mcp-client/setup";
|
|
898
|
+
|
|
899
|
+
const mcp = new TabMcp({
|
|
900
|
+
relay: "https://relay.example",
|
|
901
|
+
server: { name: "notizen", version: "1.0.0" },
|
|
902
|
+
ui: {
|
|
903
|
+
strings: {
|
|
904
|
+
lang: "de",
|
|
905
|
+
connectTitle: "KI-App verbinden",
|
|
906
|
+
confirmTitle: "{tool} erlauben?",
|
|
907
|
+
allow: "Erlauben",
|
|
908
|
+
deny: "Nicht erlauben",
|
|
909
|
+
},
|
|
910
|
+
setup: {
|
|
911
|
+
guide: "https://example.com/hilfe/ki",
|
|
912
|
+
instructions: (name, address) =>
|
|
913
|
+
setupInstructions(name, address).filter((step) => step.id !== "codex"),
|
|
914
|
+
},
|
|
915
|
+
},
|
|
916
|
+
});
|
|
917
|
+
```
|
|
918
|
+
|
|
571
919
|
### Styling the default dialogs
|
|
572
920
|
|
|
573
921
|
The default dialogs bring their own stylesheet, a constructed stylesheet added to
|
|
574
922
|
`document.adoptedStyleSheets`, so a strict CSP needs no `style-src` exception. It uses
|
|
575
923
|
the page's font and system colours, and follows the page's `color-scheme`: declare
|
|
576
924
|
`color-scheme: light dark` (or `<meta name="color-scheme" content="light dark">`) and the
|
|
577
|
-
dialogs are dark in dark mode. They fit a
|
|
578
|
-
|
|
925
|
+
dialogs are dark in dark mode. They fit a 320 px wide screen (below 30rem the code's Copy
|
|
926
|
+
goes under the code), and they open without animation when the user prefers reduced
|
|
927
|
+
motion. The page's rules for elements (`button { … }`, `p { color: … }`, a reset) do not
|
|
928
|
+
reach into them, nor does `body { pointer-events: none }` of a modal layer (Radix, shadcn).
|
|
579
929
|
|
|
580
930
|
To adjust single values, set these custom properties on `:root` or on `.tab-mcp-dialog`:
|
|
581
931
|
|
|
@@ -600,34 +950,14 @@ To adjust single values, set these custom properties on `:root` or on `.tab-mcp-
|
|
|
600
950
|
| `--tab-mcp-title-size` | `1.25em` | |
|
|
601
951
|
| `--tab-mcp-mono` | `ui-monospace, …, monospace` | the address, the code, the arguments |
|
|
602
952
|
| `--tab-mcp-code-size` | `2em` | the pairing code |
|
|
603
|
-
| `--tab-mcp-arguments-height` | `
|
|
604
|
-
|
|
605
|
-
For more,
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
`
|
|
610
|
-
|
|
611
|
-
of them. A page rule of one element and one pseudo-class still changes a property that the
|
|
612
|
-
dialogs set only in their base rules (for example `button:hover { padding: 0 }`). Write
|
|
613
|
-
overrides outside cascade layers: the dialogs' rules are not in a layer, so they win over
|
|
614
|
-
any layered rule. All dialogs are `dialog.tab-mcp-dialog`; the connect dialog is
|
|
615
|
-
`.tab-mcp-connect` (with `.tab-mcp-status`, `.tab-mcp-address`, `.tab-mcp-code`,
|
|
616
|
-
`.tab-mcp-code-value`, `.tab-mcp-code-used`, `.tab-mcp-help`, `.tab-mcp-apps`,
|
|
617
|
-
`.tab-mcp-app`, `.tab-mcp-new-address-question`, `.tab-mcp-actions`; once an app is
|
|
618
|
-
connected `.tab-mcp-success`, with `.tab-mcp-success-icon` (the check mark, an inline
|
|
619
|
-
`svg` in `currentColor`), `.tab-mcp-success-title` and `.tab-mcp-success-next`, and the
|
|
620
|
-
button `.tab-mcp-setup-toggle`, "Connect another app", with `aria-expanded`, which shows
|
|
621
|
-
and hides `.tab-mcp-setup`, the address, code and help), the confirmation is
|
|
622
|
-
`.tab-mcp-confirm` (with `.tab-mcp-description`, `.tab-mcp-arguments`,
|
|
623
|
-
`.tab-mcp-always-note`, and the buttons `.tab-mcp-deny`, `.tab-mcp-allow`,
|
|
624
|
-
`.tab-mcp-always`, with `alwaysAllow`), and the notice after `replaced` is
|
|
625
|
-
`.tab-mcp-replaced` (with `.tab-mcp-description`, `.tab-mcp-status`, and the buttons
|
|
626
|
-
`.tab-mcp-replaced-close` and `.tab-mcp-new-address`). Buttons are `.tab-mcp-primary` (the one
|
|
627
|
-
next step: Copy the address until an app is connected, then Done, "Don't allow", the safe choice, and the notice's "New
|
|
628
|
-
address", or its "Close" after a sibling tab), `.tab-mcp-danger` (Disconnect and
|
|
629
|
-
"Disconnect and get new address") or plain; every Copy button is also `.tab-mcp-copy`, and
|
|
630
|
-
the Disconnect buttons in the apps list are `.tab-mcp-disconnect`. For example:
|
|
953
|
+
| `--tab-mcp-arguments-height` | `min(50vh, 24rem)` | the arguments scroll beyond this |
|
|
954
|
+
|
|
955
|
+
For more, override with two classes, `.tab-mcp-dialog .tab-mcp-x`, outside cascade layers;
|
|
956
|
+
a rule with one class does not win. The class names that stay are the dialogs:
|
|
957
|
+
`.tab-mcp-dialog` (all of them), `.tab-mcp-connect`, `.tab-mcp-confirm` and
|
|
958
|
+
`.tab-mcp-replaced`; and the kinds of button: `.tab-mcp-primary` (the one next step),
|
|
959
|
+
`.tab-mcp-danger` (what disconnects apps) and `.tab-mcp-copy` (every Copy). Other class
|
|
960
|
+
names inside the dialogs may change with any release. For example:
|
|
631
961
|
|
|
632
962
|
```css
|
|
633
963
|
:root {
|
|
@@ -673,20 +1003,23 @@ function TodoList() {
|
|
|
673
1003
|
its first render. A changed `relay` or `server.name` makes a new one (with a warning);
|
|
674
1004
|
other changes are ignored, except that `onConfirm` and `identityAssertion` are always
|
|
675
1005
|
the latest ones, also when added or removed after the first render (without
|
|
676
|
-
`onConfirm`, the library's dialog asks). It disconnects its instance when it unmounts
|
|
1006
|
+
`onConfirm`, the library's dialog asks). It disconnects its instance when it unmounts (a moment later, so that StrictMode's
|
|
1007
|
+
simulated unmount does not cancel a `connect()` a child started). `<TabMcpProvider
|
|
677
1008
|
client={mcp}>` uses an instance of your own instead (and leaves it connected).
|
|
678
1009
|
- `useTabMcp()` returns the instance, for everything the hooks do not cover
|
|
679
1010
|
(`newAddress()`, `signOut()`, `refreshIdentity()`, `revoke()`, …).
|
|
680
|
-
- `useTabMcpState()` returns `{ state,
|
|
681
|
-
identityMessage, reconnecting, tools }` through `useSyncExternalStore`: the component
|
|
1011
|
+
- `useTabMcpState()` returns `{ state, session, grants, pairingCode, identity,
|
|
1012
|
+
identityMessage, reconnecting, tools }` (the address is `session?.address`) through `useSyncExternalStore`: the component
|
|
682
1013
|
renders again only when one of them changed. On the server and during hydration it is
|
|
683
1014
|
`disconnected`.
|
|
684
|
-
- `useTool(tool,
|
|
1015
|
+
- `useTool(tool, options?)` registers the tool while the component is mounted and
|
|
685
1016
|
removes it on unmount. `execute` is always the latest closure, so it may use the
|
|
686
1017
|
component's current props and state without registering again; the tool is registered
|
|
687
|
-
again only when its name, title, description, input schema or annotations
|
|
688
|
-
`options.
|
|
689
|
-
|
|
1018
|
+
again only when its name, title, description, input or output schema or annotations
|
|
1019
|
+
change, or `options.callers` or `options.exposedTo` (by value: a new array with the same
|
|
1020
|
+
entries is no change; [which callers see a tool](#which-callers-see-a-tool),
|
|
1021
|
+
[WebMCP](#webmcp), whose `webmcp` on the provider turns it on). Registered again unchanged, a tool allowed until reload stays
|
|
1022
|
+
allowed. StrictMode's double mount registers it once at a time. A duplicate name or an
|
|
690
1023
|
invalid tool is thrown from the component's render, for an error boundary. Tools that
|
|
691
1024
|
read state right after another tool changed it should read it from a store (as the
|
|
692
1025
|
example does) or a ref ([Tools in a `useState` app](#tools-in-a-usestate-app)), not
|