@ractive-ch/tab-mcp-client 0.18.1 → 0.19.0-rc.2

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.
Files changed (86) hide show
  1. package/CHANGELOG.md +175 -0
  2. package/README.md +360 -68
  3. package/allowed.d.ts +2 -2
  4. package/allowed.js.map +1 -1
  5. package/always-allowed.d.ts +15 -15
  6. package/cdn/allowed.min.js +2 -2
  7. package/cdn/allowed.min.js.map +2 -2
  8. package/cdn/confirm-ba3dkf9p.min.js +4 -0
  9. package/cdn/confirm-ba3dkf9p.min.js.map +10 -0
  10. package/cdn/connect-my0pbqd7.min.js +6 -0
  11. package/cdn/connect-my0pbqd7.min.js.map +12 -0
  12. package/cdn/connection-yf2x9zz2.min.js +17 -0
  13. package/cdn/{connection-0xqwz17v.min.js.map → connection-yf2x9zz2.min.js.map} +5 -5
  14. package/cdn/strings.min.js +4 -0
  15. package/cdn/strings.min.js.map +9 -0
  16. package/cdn/{tab-mcp-client-2vx9mcww.min.js → tab-mcp-client-8r6h3wza.min.js} +9 -8
  17. package/cdn/tab-mcp-client-8r6h3wza.min.js.map +11 -0
  18. package/cdn/tab-mcp-client-nnxcwrxp.min.js +5 -0
  19. package/cdn/tab-mcp-client-nnxcwrxp.min.js.map +11 -0
  20. package/cdn/{tab-mcp-client-5j3brdf3.min.js → tab-mcp-client-q758jr4g.min.js} +4 -4
  21. package/cdn/tab-mcp-client-q758jr4g.min.js.map +10 -0
  22. package/cdn/tab-mcp-client.min.js +3 -3
  23. package/cdn/tab-mcp-client.min.js.map +4 -4
  24. package/cdn/webmcp-84k58gsw.min.js +4 -0
  25. package/cdn/webmcp-84k58gsw.min.js.map +11 -0
  26. package/{confirm-bqfdrx3f.js → confirm-hymzwx3b.js} +7 -5
  27. package/confirm-hymzwx3b.js.map +10 -0
  28. package/confirm.d.ts +2 -2
  29. package/{connect-ztsgybpq.js → connect-fhtjdmf3.js} +75 -37
  30. package/connect-fhtjdmf3.js.map +10 -0
  31. package/connect.d.ts +7 -6
  32. package/{connection-gang7f5x.js → connection-73fbg99k.js} +70 -8
  33. package/connection-73fbg99k.js.map +16 -0
  34. package/connection.d.ts +23 -2
  35. package/dialog.d.ts +19 -9
  36. package/extension.d.ts +373 -0
  37. package/extension.js +415 -0
  38. package/extension.js.map +10 -0
  39. package/handshake.d.ts +82 -0
  40. package/index-00p8b969.js +221 -0
  41. package/index-00p8b969.js.map +11 -0
  42. package/index-1tssct09.js +58 -0
  43. package/index-1tssct09.js.map +11 -0
  44. package/{index-c1c266tm.js → index-7qqhn3zv.js} +28 -110
  45. package/index-7qqhn3zv.js.map +10 -0
  46. package/index-s31xrfa2.js.map +1 -1
  47. package/index.d.ts +1 -1
  48. package/index.js +59 -64
  49. package/index.js.map +4 -4
  50. package/internal.d.ts +2 -0
  51. package/mcp.d.ts +5 -3
  52. package/package.json +11 -2
  53. package/protocol.d.ts +11 -1
  54. package/react.d.ts +11 -4
  55. package/react.js +57 -10
  56. package/react.js.map +5 -4
  57. package/secret.d.ts +1 -1
  58. package/setup.d.ts +4 -1
  59. package/setup.js +3 -2
  60. package/setup.js.map +1 -1
  61. package/strings.d.ts +149 -28
  62. package/strings.js +13 -0
  63. package/strings.js.map +9 -0
  64. package/tab-mcp.d.ts +30 -7
  65. package/text.d.ts +39 -0
  66. package/types.d.ts +76 -17
  67. package/webmcp-d2ec3v3g.js +221 -0
  68. package/webmcp-d2ec3v3g.js.map +11 -0
  69. package/webmcp.d.ts +17 -4
  70. package/cdn/confirm-d722a9eb.min.js +0 -4
  71. package/cdn/confirm-d722a9eb.min.js.map +0 -10
  72. package/cdn/connect-x6ws6wqt.min.js +0 -6
  73. package/cdn/connect-x6ws6wqt.min.js.map +0 -12
  74. package/cdn/connection-0xqwz17v.min.js +0 -17
  75. package/cdn/tab-mcp-client-2vx9mcww.min.js.map +0 -12
  76. package/cdn/tab-mcp-client-5j3brdf3.min.js.map +0 -10
  77. package/cdn/webmcp-khy4f5rj.min.js +0 -4
  78. package/cdn/webmcp-khy4f5rj.min.js.map +0 -10
  79. package/confirm-bqfdrx3f.js.map +0 -10
  80. package/connect-ztsgybpq.js.map +0 -10
  81. package/connection-gang7f5x.js.map +0 -16
  82. package/index-5jyqehpk.js +0 -63
  83. package/index-5jyqehpk.js.map +0 -11
  84. package/index-c1c266tm.js.map +0 -11
  85. package/webmcp-zq1enyz0.js +0 -57
  86. package/webmcp-zq1enyz0.js.map +0 -10
package/README.md CHANGED
@@ -9,13 +9,15 @@ which page is meant; the AI app then calls the page's tools while the tab stays
9
9
  is installed on the user's computer. With `webmcp: true` the library also registers the
10
10
  tools with the browser's own [WebMCP](#webmcp), for agents in the browser. A site's own
11
11
  assistant can call the same tools over a port, without a relay:
12
- [Your own assistant on the page](#your-own-assistant-on-the-page).
12
+ [Your own assistant on the page](#your-own-assistant-on-the-page). A browser extension
13
+ that is a tab-mcp page itself can fill in its pairing code on the relay's page for its
14
+ user: [For browser extensions](#for-browser-extensions).
13
15
 
14
16
  How the library, the relay and the tab-mcp browser extension fit together, with a
15
17
  picture:
16
18
  [How the parts fit together](https://github.com/ractive/tab-mcp#how-the-parts-fit-together).
17
19
 
18
- **Status: 0.18.1.** Until 1.0, a new minor version can still change the API. Which AI apps
20
+ **Status: 0.19.0-rc.2.** Until 1.0, a new minor version can still change the API. Which AI apps
19
21
  work is listed in [compatibility](https://github.com/ractive/tab-mcp/blob/main/docs/compatibility.md).
20
22
 
21
23
  ## You need a relay
@@ -31,7 +33,7 @@ docker run --rm -p 8080:8080 \
31
33
  -e TAB_MCP_PUBLIC_URL=http://localhost:8080 \
32
34
  -e TAB_MCP_ALLOWED_ORIGINS=http://localhost:3000 \
33
35
  -e TAB_MCP_STORE=memory \
34
- ractive/tab-mcp:0.12.0
36
+ ractive/tab-mcp:0.13.0-rc.1
35
37
  ```
36
38
 
37
39
  For production, see
@@ -50,7 +52,7 @@ Or without a bundler, from a CDN as an ES module:
50
52
 
51
53
  ```html
52
54
  <script type="module">
53
- import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.18.1/cdn/tab-mcp-client.min.js";
55
+ import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.19.0-rc.2/cdn/tab-mcp-client.min.js";
54
56
  </script>
55
57
  ```
56
58
 
@@ -133,7 +135,9 @@ WebMCP draft's `registerTool` (`callers`: [which callers see a tool](#which-call
133
135
  `{ kind: "local", id, name? }` for the site's own assistant on a served port, with the
134
136
  `id` and `name` the page gave `serve` ([Your own assistant](#your-own-assistant-on-the-page)),
135
137
  or `{ kind: "webmcp" }` for an agent in the browser ([WebMCP](#webmcp)), of which nothing
136
- more is known. Check `client.kind` before you read `host`. Return a
138
+ more is known (unless the tab-mcp browser extension vouched for the call: then
139
+ `{ kind: "webmcp", via: "tab-mcp-extension", name, host }`, [with the
140
+ extension](#with-the-tab-mcp-browser-extension)). Check `client.kind` before you read `host`. Return a
137
141
  string (one text block), an MCP result (passed through with its `_meta`: an object
138
142
  whose `content` is an array of MCP content blocks, each with `type` `text`, `image`,
139
143
  `audio`, `resource_link` or `resource` and the fields that type needs), or any other JSON value (sent as JSON text,
@@ -246,7 +250,62 @@ trial token that a script adds later counts for a `TabMcp` made after it.
246
250
  names.
247
251
  - The tab-mcp browser extension offers mirrored tools like any page tool, since it cannot know
248
252
  whether the AI app is also paired with the page, so an AI app connected to the page and
249
- to the extension sees such a tool twice.
253
+ to the extension sees such a tool twice. The page and the extension know of each
254
+ other, though: see the next section.
255
+
256
+ #### With the tab-mcp browser extension
257
+
258
+ The [tab-mcp browser extension](https://github.com/ractive/tab-mcp/tree/main/packages/tab-mcp-extension)
259
+ calls a page's tools through WebMCP for AI apps connected to it, and asks the user
260
+ first about every tool that is not read-only. With `webmcp: true` the page and the
261
+ extension shake hands (DEC-207), so that a call is not asked twice and the page can say
262
+ that an AI app reaches it through the extension:
263
+
264
+ - **`extension`** (and the event `extensionchange`, `{ extension }`; in React the
265
+ snapshot's `extension`): while the extension serves the page,
266
+ `{ name, version, apps, whileInFront }`. `apps` are the AI apps connected to the
267
+ extension now, each `{ name, host }` (the AI app's own name and the host the relay
268
+ verified), empty while none is. `whileInFront` is `true`: the extension serves only the
269
+ tab in front of its window, so say "while this tab is in front". `undefined` without
270
+ the extension, while its AI access is off, and without WebMCP. The connect dialog says
271
+ it first ("Claude (claude.ai) is connected through the tab-mcp browser extension,
272
+ while this tab is in front.", `ui.strings.viaExtension…`) and keeps everything else:
273
+ a phone or another browser still connects to the page directly.
274
+ - **A vouched call.** Right before the extension runs a tool the user allowed there
275
+ (asked and allowed, or covered by its "Always allow" switch), it tells the page the
276
+ tool, the exact arguments and the AI app. The next WebMCP call of that tool with equal
277
+ arguments (key order aside) within 2 seconds runs without the page's question and
278
+ without `onConfirm`: the user just allowed exactly that call. Its caller in
279
+ `execute`, `toolcall` and `toolcallend` is
280
+ `{ kind: "webmcp", via: "tab-mcp-extension", name, host }`, so an activity view can say
281
+ "Claude (claude.ai) through the tab-mcp browser extension". A vouch is used once and
282
+ only for the tool registered under its name when it came (a tool registered anew
283
+ since asks). Everything else is as for any call: the arguments are checked, the
284
+ events fire, a tool that is gone does not run. Once the extension's call ended or
285
+ failed, the extension withdraws the vouch, and one still unused (the call never
286
+ reached the page) is dropped. Any call that matches no vouch (an agent in the
287
+ browser, a call after the 2 seconds) is asked as before.
288
+ - **`trustExtension: false`** keeps asking every WebMCP call, vouched or not, for a site
289
+ that insists on its own question; `extension` is still reported.
290
+ - **What the page tells the extension:** the library's version, the app id and the AI
291
+ apps connected to the page directly (`name`, `host`, as `grants` has them; at most
292
+ 64), again on
293
+ each `grantschange`. The extension's panel uses it to say that the page has tab-mcp
294
+ built in and when one AI app is connected both ways (it then sees the page's tools
295
+ twice). The page also marks `<html data-tab-mcp-handshake>`, so that an extension
296
+ that comes after the page loaded knows it may speak.
297
+ - **No proof.** Plain DOM events on `document` (`tab-mcp-page` from the page,
298
+ `tab-mcp-extension` from the extension, a JSON string as `detail`; the protocol is in
299
+ `src/handshake.ts`). Script in the page could fake the extension, but script in the
300
+ page can already skip any question of the page: this is a convenience between two
301
+ parts the user trusts, not a security boundary. Only the top frame takes part.
302
+ - Old and new mix: an extension that does not know the handshake says nothing, and the
303
+ page asks as before; the extension says nothing to a page that did not announce the
304
+ library.
305
+
306
+ A page that registers its WebMCP tools itself, without the library's mirror, can take
307
+ part the same way: answer the events as `src/handshake.ts` does, skip its own
308
+ question for a call that matches a vouch, and drop a vouch the extension withdraws.
250
309
 
251
310
  ### Your own assistant on the page
252
311
 
@@ -473,14 +532,14 @@ instructions, as the recipe does with a delimited block.
473
532
  relay: the MCP client reads an error, not a network failure. Only an aborted `signal`
474
533
  rejects. Call `close()` when the channel to the page is gone; a closed tunnel stays
475
534
  closed, so a new channel (or a new `serve`) gets a new `createTunnelFetch`.
476
- - Without `relay`, `connect()`, `showConnectDialog()` and `newAddress()` reject with a
535
+ - Without `relay`, `connect()`, `showConnectDialog()` and `disconnectAll()` reject with a
477
536
  `TypeError`, nothing is read from or written to `localStorage`, `mcp.hasRelay` is
478
537
  `false`, and React's `ConnectButton` renders nothing.
479
538
 
480
539
  ### Consequential tools
481
540
 
482
541
  Mark tools that change or delete something with `consequentialHint: true`. The page then
483
- asks the user before every call ("Don't allow" or "Allow"), showing the AI app and the
542
+ asks the user before every call ("Don't allow" or "Allow once"), showing the AI app and the
484
543
  arguments. A refusal reaches the AI app as an error result, and so does a call whose tool
485
544
  was removed while the user was asked.
486
545
 
@@ -489,7 +548,7 @@ AI app from then on; see [Tools that run without asking](#tools-that-run-without
489
548
  It is off by default.
490
549
 
491
550
  Tools are called while the user is in the AI app, so the dialog often opens in a tab in
492
- the background. "Allow" and "Always allow" can be clicked only once the page has
551
+ the background. "Allow once" and "Always allow" can be clicked only once the page has
493
552
  been visible and focused for 750 ms with the dialog open; leaving the page
494
553
  disables them and coming back starts the wait again, so the click that brings the page
495
554
  forward cannot approve. One confirmation is shown at a time; the others wait, the shown
@@ -541,7 +600,7 @@ const mcp = new TabMcp({
541
600
 
542
601
  `onConfirm` returns `true` (run), `false` (the user refused) or `"unanswered"`: the user was
543
602
  asked and did not answer, e.g. your question timed out. The AI app then reads "The user was
544
- asked and did not answer." instead of "The user declined", and `toolcallend` has the
603
+ asked and did not answer." instead of "The user did not allow this call.", and `toolcallend` has the
545
604
  outcome `unanswered`. Any other value, and a throw, is a refusal.
546
605
 
547
606
  `mcp.confirmInPage(request, { document })` is the library's own dialog, for an
@@ -563,8 +622,7 @@ a client with `kind: "relay"` can be allowed.
563
622
  ### Tools that run without asking
564
623
 
565
624
  The user can let a consequential tool run without asking, for one connected AI app: one
566
- tool at a time ("Always allow" this tool), or everything ("Allow everything, no questions
567
- asked"). An allowed call runs at once, with the library's dialog and with your own
625
+ tool at a time ("Always allow" this tool), or everything ("Always allow everything"). An allowed call runs at once, with the library's dialog and with your own
568
626
  `onConfirm` alike: neither is asked. Only AI apps through the relay can be allowed; an
569
627
  agent in the browser (WebMCP) and a served port have no grant to remember it for and are
570
628
  always asked.
@@ -573,16 +631,18 @@ The option `alwaysAllow` decides what the library's dialogs offer:
573
631
 
574
632
  | `alwaysAllow` | The confirmation offers | The connect dialog shows | Kept |
575
633
  | ---------------- | -------------------------- | ------------------------------------- | -------------------------------------------- |
576
- | unset or `false` | Don't allow, Allow | nothing (unless something is allowed) | in `storage`, for your own switches |
634
+ | unset or `false` | Don't allow, Allow once | nothing (unless something is allowed) | in `storage`, for your own switches |
577
635
  | `true` | … and "Always allow" | the switches under each app | in `storage`, until the app is disconnected |
578
636
  | `"until-reload"` | … and "Allow until reload" | the switches under each app | in memory, until the page is reloaded |
579
637
 
580
638
  With `true` and `storage: null`, nothing outlives the page either, and the button says
581
639
  "Allow until reload". The confirmation never offers "allow everything": that is a
582
640
  deliberate act in the connect dialog, not an answer to one question. In the connect
583
- dialog, each connected app gets a switch per tool that asks (its title or name;
584
- destructive ones marked) and "Allow everything, no questions asked", with a sentence on
585
- what it means. The switches are checkboxes with `role="switch"` whose accessible names
641
+ dialog, each connected AI app gets a switch per tool that asks (its title or name;
642
+ destructive ones marked) and "Always allow everything", with a sentence on what it means
643
+ and how long it lasts, under the confirmation's own label ("Always allow", or "Allow until
644
+ reload" when nothing outlives the page), so the button and the switches it turns on read
645
+ alike. The switches are checkboxes with `role="switch"` whose accessible names
586
646
  name the app. A tool that is allowed but not registered right now (a page may offer some
587
647
  tools only on some views) is listed by its name, as a switch that can only be turned
588
648
  off. When the page does not offer allowing (`alwaysAllow` unset) but something is
@@ -603,7 +663,7 @@ ones: that is why it is a switch of its own.
603
663
  the tool names and the title and description the user saw, and a fingerprint of the
604
664
  secret; no arguments, no results. An allowance ends with its grant, as the relay reports
605
665
  it: the app is disconnected (in the page or elsewhere), "Disconnect all apps"
606
- (`newAddress()`), the grant expired, the user signed out (identity binding). A dropped
666
+ (`disconnectAll()`), the grant expired, the user signed out (identity binding). A dropped
607
667
  connection, a reload or a page that has not connected yet keeps them. Tabs that share the
608
668
  address share them, and a change in one counts in the others at once.
609
669
 
@@ -622,7 +682,7 @@ function drawSwitches() {
622
682
  allowed.has(grant.grantId, tool.name),
623
683
  (on) => on ? allowed.allow(grant.grantId, tool.name) : allowed.ask(grant.grantId, tool.name)),
624
684
  ),
625
- mySwitch("Allow everything, no questions asked",
685
+ mySwitch("Always allow everything",
626
686
  allowed.everything(grant.grantId),
627
687
  (on) => allowed.setEverything(grant.grantId, on)),
628
688
  ]),
@@ -652,8 +712,8 @@ mcp.addEventListener("grantschange", drawSwitches);
652
712
  that does not import it loads nothing of it. Take both from the same version:
653
713
 
654
714
  ```js
655
- import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.18.1/cdn/tab-mcp-client.min.js";
656
- import { allowedTools } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.18.1/cdn/allowed.min.js";
715
+ import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.19.0-rc.2/cdn/tab-mcp-client.min.js";
716
+ import { allowedTools } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.19.0-rc.2/cdn/allowed.min.js";
657
717
  ```
658
718
 
659
719
  **The risk.** An allowance outlives the user's attention: for up to the grant's 90 days,
@@ -693,7 +753,7 @@ not seen, and ones the page adds later. The relay cannot help: its one OAuth sco
693
753
  revision, so its command turns that on first. The steps are the minimum; the link at
694
754
  the end of the help leads to the guide with the rest (ChatGPT's Mac app, turning the
695
755
  Codex feature off, what to do when it fails). A page can link its own guide and give
696
- its own steps (`ui.setup`, see [Texts and help of the dialogs](#texts-and-help-of-the-dialogs)).
756
+ its own steps (`ui.setup`, see [Help in the connect dialog](#help-in-the-connect-dialog)).
697
757
  The user adds the address to their AI app. It is the app's address
698
758
  (`https://<relay>/s/<app>/mcp`), the same in every user's dialog.
699
759
  3. The AI app opens the relay's sign-in page in the browser. The dialog shows a code of
@@ -703,7 +763,7 @@ not seen, and ones the page adds later. The relay cannot help: its one OAuth sco
703
763
  4. Once the AI app connects, the dialog says so at the top: a check mark, "Claude
704
764
  (claude.ai) is connected" (every connected app is named) and what to do next (close it and ask the app to use the
705
765
  page's tools); screen readers hear the same. The setup (address, code, help) moves
706
- behind "Connect another app", closed, so the next code does not appear in view; the
766
+ behind "Connect another AI app", closed, so the next code does not appear in view; the
707
767
  connected app is listed with a Disconnect button (and, with `alwaysAllow`, the
708
768
  switches for what it may run without asking), and "Done" closes the dialog. A Disconnect the relay could
709
769
  not save says so in the list ("The relay could not save this. Try again.").
@@ -717,7 +777,7 @@ code, for up to 10 seconds; if no app connects by then, the new code appears. A
717
777
  replaces an expired one, or one that comes without a reason, shows at once.
718
778
 
719
779
  Until an app is connected, the dialog offers no prominent button under the code: only a small
720
- "Close" at the bottom (and Escape). Closing it stops the code (`stopPairing()`); the page
780
+ "Close" at the bottom (and Escape). Closing it ends the code (`stopPairing()`); the page
721
781
  stays connected and the address stays the same, so calling `showConnectDialog()` again
722
782
  shows the same address with a new code. While the dialog is open, calling it again (a
723
783
  double click) opens no second one; the call resolves when that one closes. A call for another
@@ -738,10 +798,9 @@ their app ids do. The key of 0.4 and older, `tab-mcp:secret:<relay origin>`, is
738
798
  read: a visitor whose last visit predates 0.5 pairs their AI apps again.
739
799
 
740
800
  "Disconnect all apps" in the dialog (at the end of the setup, below the code, with a
741
- line on what it does) calls `newAddress()`, which disconnects every app and makes a new
742
- secret; when apps are connected, the dialog asks first. The method has its name from
743
- when every page had an address of its own: the address the user sees does not change,
744
- and each AI app connects again at the same address with a new code. It first takes the
801
+ line on what it does) calls `disconnectAll()`, which disconnects every app and makes a new
802
+ secret; when apps are connected, the dialog asks first. The address the user sees does
803
+ not change, and each AI app connects again at the same address with a new code. It first takes the
745
804
  page back: it connects with the stored secret, and the relay revokes every grant made
746
805
  for it in the same step (`hello.revokeAll`), whether the page was connected or not.
747
806
  Then it makes the new secret, clears `grants`, and connects with it if the page was
@@ -750,7 +809,7 @@ makes one without connecting. If the page cannot be taken back (the relay is
750
809
  unreachable, or someone takes it over again first), it rejects and keeps the old secret,
751
810
  so calling it again tries again. A second call while it runs returns the same promise;
752
811
  `connect()` meanwhile waits for it and then connects with the new secret. A page with
753
- its own UI calls `newAddress()` from a click. Connected apps keep working after a reload once the page connects
812
+ its own UI calls `disconnectAll()` from a click. Connected apps keep working after a reload once the page connects
754
813
  again; while the page is closed, they get "the page is not open".
755
814
 
756
815
  When another tab or program connects with the same secret, it takes the page's place:
@@ -769,16 +828,16 @@ accepted their session, so `replaced` says which it probably was:
769
828
  the `statechange`. The usual cause, and harmless. Once the page has opened the default
770
829
  dialog, the library shows "Opened in another tab".
771
830
  - `false`: none did (or the browser has no `BroadcastChannel`). It may be a program with
772
- a copy of the address secret, and the AI apps paired with the page now talk to it.
831
+ a copy of the address secret, and the AI apps paired with the page now use it.
773
832
  The library shows "Another tab or program took over this page" with "Disconnect all
774
833
  apps".
775
834
 
776
- After a takeover, `newAddress()` (from the `replaced` listener, before it, or later, also
835
+ After a takeover, `disconnectAll()` (from the `replaced` listener, before it, or later, also
777
836
  after a reload) takes the page back as above and makes the new secret only once the
778
837
  relay confirmed that no grant is left. If the page is taken over again before that,
779
838
  it rejects with an error whose `reason` is `"replaced"` and keeps the old secret, so
780
839
  calling it again takes it back again. A relay of 0.4 or older cannot take a page
781
- back: `newAddress()` rejects there with `reason` `"policy"`. When the takeover ended a
840
+ back: `disconnectAll()` rejects there with `reason` `"policy"`. When the takeover ended a
782
841
  session of this page, it then connects with the new secret; the default notice opens
783
842
  the connect dialog, where the user pairs the AI apps again.
784
843
 
@@ -792,7 +851,7 @@ mcp.addEventListener("replaced", (e) => {
792
851
  if (e.detail.sibling) showNote("Opened in another tab.");
793
852
  else showWarning("Another tab or program took over this page.", {
794
853
  action: "Disconnect all apps",
795
- onClick: () => mcp.newAddress(), // from the click
854
+ onClick: () => mcp.disconnectAll(), // from the click
796
855
  });
797
856
  });
798
857
  ```
@@ -804,7 +863,7 @@ sibling; such script can read the secret anyway, so `sibling` is a hint, not a p
804
863
 
805
864
  Whoever has the address secret can take the page's place from anywhere, and the AI apps
806
865
  paired with the page then send their arguments to them and get their tool descriptions
807
- and results, until the page makes a new secret (`newAddress()`). They get no token and
866
+ and results, until the page makes a new secret (`disconnectAll()`). They get no token and
808
867
  cannot pair a new app without a code. Script on your origin (cross-site scripting, a compromised
809
868
  third-party script, another page on the same origin) or access to the device can copy it
810
869
  ([threat model](https://github.com/ractive/tab-mcp/blob/main/docs/threat-model.md), A4).
@@ -816,7 +875,7 @@ third-party script, another page on the same origin) or access to the device can
816
875
  third-party scripts off it.
817
876
  - On a page that pairs by code, grants follow the browser profile, not the person: on a
818
877
  shared profile, or in a web app with accounts that pairs by code, the next user of the
819
- page gets the AI apps the previous one paired. Call `newAddress()` when the user logs
878
+ page gets the AI apps the previous one paired. Call `disconnectAll()` when the user logs
820
879
  out, or use identity binding.
821
880
  - The relay's sign-in page names the AI app and the app id of the address, not the page
822
881
  or its origin: the code alone says which page is paired. Whoever types a user's code
@@ -832,7 +891,7 @@ come back at once), and at once when the tab comes back to the front or the netw
832
891
  returns. Meanwhile `state` is `reconnecting` (while it waits and while it makes an
833
892
  attempt), `reconnecting` says which attempt is next and when, and `statechange` fires
834
893
  for each attempt it schedules; only the `statechange` that leaves `connected` carries the
835
- `reason` ([Your own UI](#your-own-ui)). Calling `connect()` meanwhile tries at once. It never reconnects after `disconnect()`, `newAddress()`,
894
+ `reason` ([Your own UI](#your-own-ui)). Calling `connect()` meanwhile tries at once. It never reconnects after `disconnect()`, `disconnectAll()`,
836
895
  another tab taking the page's place (`replaced`), an expired or idle session, or a
837
896
  refusal by the relay, and an attempt the relay refuses stops it (an unreachable relay is
838
897
  tried again); the event `error` then says why, with `state` already `disconnected`, just
@@ -901,14 +960,34 @@ const identity = await mcp.refreshIdentity();
901
960
 
902
961
  The library asks the function after every connect. `identity` (`none`, `pending`,
903
962
  `bound`, `refused`, with the reason in `identityMessage`) and the event
904
- `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
905
- than the page."
963
+ `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 app is signed in as someone other
964
+ than the page's user."
906
965
 
907
966
  The connect dialog follows `identity`: it shows the address and "When your AI app asks,
908
967
  sign in with the account you use on this page" only once it is `bound`. Until then it
909
968
  says what is missing: "Checking your sign-in…" (`pending`), "Sign in to this page first."
910
969
  (`none`), or that the sign-in was not accepted, with `identityMessage` (`refused`).
911
970
 
971
+ An open dialog counts for 10 minutes (relays after 0.13.0-rc.1): a sign-in pairs the page
972
+ only until 10 minutes after the last `startPairing()`, which the dialog calls when it
973
+ opens. The relay says for how long (`pairing.open`); the library counts that on the
974
+ page's own clock from the message's arrival and keeps the end in `pairingUntil` (an ISO
975
+ time, event `pairinguntil`), so a page whose clock differs from the relay's still shows
976
+ the right moment. `pairingUntil` is `undefined` while pairing is off, from each
977
+ `startPairing()` until the relay answers, after the session ended (`disconnect()`,
978
+ `disconnectAll()`, a refusal), and with a relay up to 0.13.0-rc.1. The dialog shows "Your AI
979
+ app can connect until 14:32." under the sign-in line and, once that time is up (also
980
+ checked when the tab comes back to the front), "The time to connect your AI app is up"
981
+ with Continue, which calls `startPairing()` again. Nothing starts the 10 minutes again
982
+ by itself: not a timer, and not a reconnect the library makes by itself once the start
983
+ was sent, answered or not (after one, `pairingUntil` is the time of the reconnect, so
984
+ the dialog shows Continue). `connect()` from a click while reconnecting ("Connect again")
985
+ does ask again; codes always come again after a reconnect. A custom UI does the same:
986
+ call `startPairing()` only from the user's click. With a relay up to 0.13.0-rc.1 an open dialog
987
+ counts as long as it is open, and after a reconnect the dialog shows Continue too. A
988
+ sign-in that comes back after the time is up gets the relay's answer that no page is
989
+ connecting with that account; Continue, then Connect again on the relay's page, pairs it.
990
+
912
991
  ### Your own UI
913
992
 
914
993
  Instead of `showConnectDialog()`, use the methods, properties and events below.
@@ -917,8 +996,8 @@ Instead of `showConnectDialog()`, use the methods, properties and events below.
917
996
  |---|---|
918
997
  | `connect(options?)` | resolves with the session (`SessionInfo`) |
919
998
  | `disconnect()` | ends the session at once |
920
- | `newAddress()` | disconnects every app ("Disconnect all apps"); resolves once the new secret is in use (connected again if it was on) |
921
- | `startPairing()`, `stopPairing()` | codes come only while pairing is on |
999
+ | `disconnectAll()` | disconnects every app ("Disconnect all apps"); resolves once the new secret is in use (connected again if it was on) |
1000
+ | `startPairing()`, `stopPairing()` | codes come only while pairing is on; with identity binding each `startPairing()` starts the 10 minutes anew |
922
1001
  | `revoke(grantId)` | resolves once the relay removed the grant |
923
1002
  | `signOut()` | resolves once the relay removed the grants (identity binding) |
924
1003
  | `refreshIdentity()` | resolves with the new `identity`; a `TypeError` without `identityAssertion` |
@@ -930,21 +1009,23 @@ the session ends after the message was sent and before the relay answered, and w
930
1009
  or may not have done it, and `grants` says so once the page is connected again. They
931
1010
  also reject with the relay's refusal of that message, an `Error` whose `code` is
932
1011
  `store_unavailable` (the relay could not save the change: try again) or `unknown_id`.
933
- A relay after 0.12.0 names the message it answers (`for`, and `grantId` for a
1012
+ A relay after 0.13.0-rc.1 names the message it answers (`for`, and `grantId` for a
934
1013
  revocation), so with several revocations pending only the one it answers rejects; an
935
1014
  older relay's refusal rejects every pending `revoke()` and `signOut()`. When the relay
936
1015
  does not know a grant the page lists (it expired), the grant leaves `grants` (with
937
1016
  `grantschange`) and its `revoke()` resolves.
938
1017
 
939
- With a relay, `newAddress()` rejects with an error whose `addressChanged` says what happened: `false`
1018
+ With a relay, `disconnectAll()` rejects with an error whose `secretChanged` says what happened: `false`
940
1019
  when the page could not be taken back (the old secret is kept with its grants; call
941
- `newAddress()` again), `true` when the secret changed, so every app is disconnected, and
1020
+ `disconnectAll()` again), `true` when the secret changed, so every app is disconnected, and
942
1021
  only connecting with the new one failed (call `connect()`).
943
1022
 
944
1023
  The properties are `state` (`disconnected`, `connecting`, `connected`, `reconnecting`),
945
1024
  `session` (while connected; `session.address` is the MCP address, `session.appName` the
946
1025
  name the relay shows), `reconnecting` (the next attempt, while `state` is
947
- `reconnecting`), `pairingCode`, `grants`, `identity`, `identityMessage`, `tools` and
1026
+ `reconnecting`), `pairingCode`, `pairingUntil` (identity binding, see there), `grants`,
1027
+ `identity`, `identityMessage`, `tools`, `extension` ([with the tab-mcp browser
1028
+ extension](#with-the-tab-mcp-browser-extension)) and
948
1029
  `hasRelay`.
949
1030
 
950
1031
  Every event's `detail` is an object. A change event names the new values like the
@@ -955,15 +1036,17 @@ properties (`grantschange` has `grants`), and a value that is gone is `undefined
955
1036
  | `statechange` | `{ state, reason? }`, see below |
956
1037
  | `grantschange` | `{ grants }` |
957
1038
  | `pairingcode` | `{ pairingCode }` |
1039
+ | `pairinguntil` | `{ pairingUntil }` |
958
1040
  | `identitychange` | `{ identity, identityMessage }` |
959
1041
  | `toolschange` | `{ tools }` |
1042
+ | `extensionchange` | `{ extension }` |
960
1043
  | `replaced` | `{ sibling }` (see above) |
961
1044
  | `error` | `{ error }` |
962
1045
  | `toolcall`, `toolcallend` | the call, see below |
963
1046
 
964
1047
  `statechange`'s `reason` (a `DisconnectReason`) says why a session or an attempt to
965
1048
  open one ended, once: on the change from `connected` (to `disconnected`, or to
966
- `reconnecting` when the connection broke), and when `disconnect()` or `newAddress()`
1049
+ `reconnecting` when the connection broke), and when `disconnect()` or `disconnectAll()`
967
1050
  ends connecting or reconnecting. It is absent on every other change: `connecting`,
968
1051
  `connected`, each later reconnect attempt (`statechange` fires for each, and
969
1052
  `reconnecting.reason` keeps the reason of the break), a failed `connect()`, and the
@@ -991,15 +1074,16 @@ mcp.addEventListener("toolcallend", (e) => {
991
1074
 
992
1075
  `@ractive-ch/tab-mcp-client/setup` has the steps the connect dialog shows under "How do I
993
1076
  add it?", as data for your own UI. An entry of its own, out of the core, and not in the CDN
994
- build. `setupInstructions(serverName, address)` returns one entry per AI app (Claude,
1077
+ build. `setupInstructions(serverName, address, strings?)` returns one entry per AI app (Claude,
995
1078
  ChatGPT, Claude Code, Codex, in that order): `id` (`claude`, `chatgpt`, `claude-code`,
996
1079
  `codex`), `app` (its name as shown), `text` (what to do), `command` (lines to run, for
997
1080
  Claude Code and Codex) and `note` (text and code parts, for Claude Code and Codex). Every string is plain
998
1081
  text: set it with `textContent`. The commands name the server by a label made from
999
1082
  `serverName` and contain the address only when every shell reads both as plain words (see
1000
1083
  step 2 above), so they are safe to show with a Copy button; for an address that is not
1001
- plain, `command` is left out and `text` names the command instead. `SETUP_GUIDE` is the
1002
- guide with every step and what to do when it fails.
1084
+ plain, `command` is left out and `text` names the command instead. `strings` is your
1085
+ table of texts ([Translating](#translating)); the steps are English without it.
1086
+ `SETUP_GUIDE` is the guide with every step and what to do when it fails.
1003
1087
 
1004
1088
  ```ts
1005
1089
  import { SETUP_GUIDE, setupInstructions } from "@ractive-ch/tab-mcp-client/setup";
@@ -1016,21 +1100,96 @@ for (const { app, text, command, note } of setupInstructions("notes", session.ad
1016
1100
  guide.href = SETUP_GUIDE;
1017
1101
  ```
1018
1102
 
1019
- ### Texts and help of the dialogs
1103
+ ### Translating
1104
+
1105
+ The package ships English only. Every text the library shows a person is in one table,
1106
+ [`src/strings.ts`](src/strings.ts): the connect dialog and its setup steps, the question
1107
+ before a tool runs, the switches of the tools that run without asking, the line about AI
1108
+ apps connected through the tab-mcp browser extension, the replaced notice and the label of
1109
+ the React `ConnectButton`. Give your own table once, as `ui.strings` of `TabMcp` (or of
1110
+ `<TabMcpProvider>`), and every part reads it; a key you leave out stays English. With
1111
+ your tables loaded only for the users who need them, it takes three lines:
1112
+
1113
+ ```ts
1114
+ import { pickStrings } from "@ractive-ch/tab-mcp-client/strings";
1115
+ const strings = await pickStrings({ de: () => import("./tab-mcp.de.ts"), fr: () => import("./tab-mcp.fr.ts") });
1116
+ const mcp = new TabMcp({ relay: "https://relay.example", server: { name: "notizen", version: "1.0.0" }, ui: { strings } });
1117
+ ```
1118
+
1119
+ `pickStrings(tables, languages = navigator.languages)` goes through the user's languages
1120
+ in order and loads the first table that matches: the same tag, else a table of the same
1121
+ language, preferring the same script (`zh-TW` takes `zh-Hant` over `zh-Hans` or `zh`),
1122
+ then the same region, then the language alone (`de-CH` takes `de` over `de-AT`; `pt-PT`
1123
+ takes `pt` over `pt-BR`, or `pt-BR` if it is the only one), then the first by name.
1124
+ English is built in: a user who prefers English before any of your languages, or none of
1125
+ them, gets `undefined`, which keeps the English texts. The table comes with `lang` set to
1126
+ the tag it was found by unless it has its own. It never rejects: a table that does not
1127
+ load (a chunk gone after a deploy, offline) or is not an object is said in the console
1128
+ and gives `undefined`, so the page starts in English. A table with a few keys:
1129
+
1130
+ ```ts
1131
+ // tab-mcp.de.ts
1132
+ import type { DialogStrings } from "@ractive-ch/tab-mcp-client";
1133
+
1134
+ export default {
1135
+ lang: "de",
1136
+ connectTitle: "KI-App verbinden",
1137
+ confirmTitle: "{tool} erlauben?",
1138
+ confirmWho: "{client} möchte {tool} ausführen.",
1139
+ deny: "Nicht erlauben",
1140
+ allow: "Einmal erlauben",
1141
+ alwaysAllow: "Immer erlauben",
1142
+ waiting: {
1143
+ one: "{n} weiterer Aufruf wartet nach diesem.",
1144
+ other: "{n} weitere Aufrufe warten nach diesem.",
1145
+ },
1146
+ connected: { one: "{apps} ist verbunden", other: "{apps} sind verbunden" },
1147
+ buttonConnected: { one: "KI-App verbunden", other: "{n} KI-Apps verbunden" },
1148
+ } satisfies Partial<DialogStrings>;
1149
+ ```
1020
1150
 
1021
- Every text of the dialogs is in one table, [`src/strings.ts`](src/strings.ts) (the type
1022
- `DialogStrings`). Pass your own for some or all keys as `ui.strings`; the rest stay
1023
- English. `{name}` placeholders are filled in: names of tools and AI apps (isolated, so
1024
- they cannot reorder your sentence), the code, counts. Where English needs two forms there
1025
- are two keys (`waitingOne`, `waitingMany`). `lang` and `dir` are the language and direction
1026
- set on every dialog: set them with your texts.
1151
+ - **Placeholders.** `{name}` is filled in: names of tools and AI apps (isolated, so that
1152
+ they cannot reorder your sentence), the code, a time, a count. Put them where your
1153
+ language needs them.
1154
+ - **Plural forms.** A text that depends on a count is an object with one form per plural
1155
+ category of your language, as `Intl.PluralRules` names them (`zero`, `one`, `two`,
1156
+ `few`, `many`, `other`): English needs `one` and `other`, Polish `one`, `few`, `many`
1157
+ and `other`, Japanese only `other`. `other` is required and stands in for a category
1158
+ you leave out. `{n}` is the count; in a text with `{apps}` the count is the number of
1159
+ AI apps named.
1160
+ - **Lists.** Names are joined by `Intl.ListFormat` in your language ("A, B und C"):
1161
+ there is nothing to translate.
1162
+ - **Language and direction.** `lang` (a BCP 47 tag) is what plural forms and lists follow,
1163
+ and every dialog carries it, so a screen reader reads it with the right voice. Always
1164
+ give it: a table with texts but no `lang` counts as English (English plural rules,
1165
+ `lang="en"`), and the console says so once; a page that only changes a few English
1166
+ texts sets `lang: "en"`. The
1167
+ dialogs take the direction from `lang` where the browser knows it; set `dir: "rtl"` in a
1168
+ right-to-left table to be sure (Firefox does not tell).
1169
+ - **Complete across upgrades.** Type a full translation as `DialogStrings` instead of
1170
+ `Partial<DialogStrings>` (start from a copy of `STRINGS`, the English table of
1171
+ `@ractive-ch/tab-mcp-client/strings`): the type check then names every key you lack,
1172
+ also one a new version added, and a misspelt key is a type error either way. Keys that
1173
+ change name are listed in the changelog under "Changed". At run time a missing or wrong
1174
+ value keeps the English text.
1175
+ - **Texts an AI app reads stay English** on purpose: tool results, refusals ("The user did
1176
+ not allow this call.") and errors go to the AI app, which answers the user in their
1177
+ language anyway.
1178
+
1179
+ `Intl.PluralRules` and `Intl.ListFormat` are in every browser the library runs in (the
1180
+ later of the two since Chrome 72, Firefox 78 and Safari 14.1, 2019 to 2021). Without a
1181
+ bundler, the same entry is `cdn/strings.min.js` next to `cdn/tab-mcp-client.min.js`. A
1182
+ page that shows these texts in its own UI gets the whole table, its own texts where it
1183
+ has them and English for the rest, from `withEnglish(strings)`.
1184
+
1185
+ ### Help in the connect dialog
1027
1186
 
1028
1187
  `ui.setup` changes the help in the connect dialog: `guide` is the link at its end
1029
1188
  (default the guide on ractive.ch; `null` for none, for example on your own relay, and
1030
1189
  then your own `instructions` too, since the default steps refer to the guide), and
1031
1190
  `instructions(serverName, address)` returns the steps per AI app, each `{ app, text,
1032
1191
  command?, note? }` (default `setupInstructions` of `@ractive-ch/tab-mcp-client/setup`,
1033
- which you can filter or extend).
1192
+ which you can filter or extend; pass it your `strings` to keep them translated).
1034
1193
 
1035
1194
  ```ts
1036
1195
  import { setupInstructions } from "@ractive-ch/tab-mcp-client/setup";
@@ -1039,17 +1198,11 @@ const mcp = new TabMcp({
1039
1198
  relay: "https://relay.example",
1040
1199
  server: { name: "notizen", version: "1.0.0" },
1041
1200
  ui: {
1042
- strings: {
1043
- lang: "de",
1044
- connectTitle: "KI-App verbinden",
1045
- confirmTitle: "{tool} erlauben?",
1046
- allow: "Erlauben",
1047
- deny: "Nicht erlauben",
1048
- },
1201
+ strings,
1049
1202
  setup: {
1050
1203
  guide: "https://example.com/hilfe/ki",
1051
1204
  instructions: (name, address) =>
1052
- setupInstructions(name, address).filter((step) => step.id !== "codex"),
1205
+ setupInstructions(name, address, strings).filter((step) => step.id !== "codex"),
1053
1206
  },
1054
1207
  },
1055
1208
  });
@@ -1146,9 +1299,9 @@ function TodoList() {
1146
1299
  simulated unmount does not cancel a `connect()` a child started). `<TabMcpProvider
1147
1300
  client={mcp}>` uses an instance of your own instead (and leaves it connected).
1148
1301
  - `useTabMcp()` returns the instance, for everything the hooks do not cover
1149
- (`newAddress()`, `signOut()`, `refreshIdentity()`, `revoke()`, …).
1150
- - `useTabMcpState()` returns `{ state, session, grants, pairingCode, identity,
1151
- identityMessage, reconnecting, tools }` (the address is `session?.address`) through `useSyncExternalStore`: the component
1302
+ (`disconnectAll()`, `signOut()`, `refreshIdentity()`, `revoke()`, …).
1303
+ - `useTabMcpState()` returns `{ state, session, grants, pairingCode, pairingUntil,
1304
+ identity, identityMessage, reconnecting, tools, extension }` (the address is `session?.address`) through `useSyncExternalStore`: the component
1152
1305
  renders again only when one of them changed. On the server and during hydration it is
1153
1306
  `disconnected`.
1154
1307
  - `useTool(tool, options?)` registers the tool while the component is mounted and
@@ -1167,8 +1320,9 @@ function TodoList() {
1167
1320
  newest first (default 20), each with its `end` (outcome, result, duration) once over.
1168
1321
  - `<ConnectButton>` calls `showConnectDialog()`, so it needs a real click (see
1169
1322
  [Connecting and pairing](#connecting-and-pairing)). Its label follows the state
1170
- ("Connect your AI app", "Connecting…", "Reconnecting…", "AI app connected"), and
1171
- "Connection lost, retrying…" once three reconnect attempts have failed; a click then
1323
+ ("Connect your AI app", "Connecting…", "Reconnecting…", "AI app connected", "2 AI apps
1324
+ connected"), and "Connection lost. Reconnecting…" once three reconnect attempts have
1325
+ failed; a click then
1172
1326
  tries again at once. `children` replaces the label: a node, or a function of the state
1173
1327
  from `useTabMcpState()`, such as
1174
1328
  `{(s) => (s.grants.length > 0 ? "Claude is playing" : "Let Claude play")}`. The usual
@@ -1217,6 +1371,144 @@ For end-to-end tests of a page's tools, `@ractive-ch/tab-mcp/testing` (the relay
1217
1371
  package) starts a relay in the test process and issues a token for the page connected
1218
1372
  at the app's address without pairing; see the relay's [README](https://github.com/ractive/tab-mcp/tree/main/packages/tab-mcp#testing-a-pages-tools).
1219
1373
 
1374
+ ## For browser extensions
1375
+
1376
+ A browser extension can be a tab-mcp page itself: its service worker holds the `TabMcp`
1377
+ and offers the extension's tools (the
1378
+ [tab-mcp browser extension](https://github.com/ractive/tab-mcp/tree/main/packages/tab-mcp-extension)
1379
+ and Sitegloss do). When the user adds it to an AI app, the relay's pairing page
1380
+ (`/authorize`) often opens in the same browser. `@ractive-ch/tab-mcp-client/extension`
1381
+ lets the extension fill in its own code there: it asks its user "Connect Claude
1382
+ (claude.ai)?", and on Allow writes the code into the page's form and submits it. The
1383
+ relay checks the code as it checks a typed one, and typing it by hand keeps working
1384
+ (another browser, a user who ignores the question).
1385
+
1386
+ It is an entry of its own, which a web page never loads. It imports nothing of the rest
1387
+ of the library at run time, touches no `window` or `document` until called, and needs no
1388
+ `chrome` types: it takes the browser's ports as small structural types, which
1389
+ `chrome.runtime.Port` fits. A content script that imports `runPairingPage` bundles less
1390
+ than 1 KB gzipped of it.
1391
+
1392
+ **What the entry gives:**
1393
+
1394
+ - The content script's part, `runPairingPage({ app, connect })`: on the relay's
1395
+ `/authorize` page in a tab's top frame, with exactly one form for `app` that has a code
1396
+ field, a Connect button and the relay's request id (the hooks of DEC-199), it opens a
1397
+ port to the worker (`PAIRING_PORT`), says who asks (the AI app's name and verified
1398
+ host, the request id, a nonce of this page load) and waits. It fills in only the code
1399
+ the worker sends back with that nonce, once (`requestSubmit` with Connect, the page's
1400
+ own post). Its pieces are exported too: `findPairingForm`, `fillIn`, `helloFor`,
1401
+ `parseFill`.
1402
+ - The worker's part, `CodeFiller`, the rules as one state machine. A port is taken only
1403
+ from this extension's own content script, in a tab's top frame, at exactly the relay's
1404
+ origin (port included) and path (`isPairingSender`). One question per page load; on
1405
+ Allow the code goes to that one page. The same request's page back in the same tab
1406
+ within 30 s is the relay's answer that the code was not taken: filled once more
1407
+ without asking, never a third time; any other request asks. With
1408
+ `new CodeFiller(host, { retry: false })` that page is asked about like any other (one
1409
+ question per page load), so no code is ever filled in without a yes of its own. A
1410
+ question that waits is withdrawn when the page goes, AI access or the setting goes off,
1411
+ the relay changes (also during the page's hello), or the AI app connects meanwhile. A
1412
+ page that could not be asked about is reported, not dropped. `pagePort(port)` takes
1413
+ the browser's port over in `onConnect`.
1414
+ - Which code to fill in: `takeCode(pairingCodes(mcp), signal, { now })` gives the current
1415
+ code unless it is about to change, else the next one, and never fails while a code
1416
+ exists: on a computer whose clock is ahead every code would look expired, so after
1417
+ waiting 10 s for a next one it gives the current code. If the relay refuses that one,
1418
+ the one more fill covers it, or with `retry: false` a new question. The same code sent
1419
+ again, as the relay answers `startPairing()` while pairing is on, is not the next one.
1420
+ - The relay's addresses: `relayPageOrigin(relay)`, `relaySitePattern(relay)` (the host
1421
+ permission to ask for, `<scheme>://<host>/*`) and `pairingPagePattern(relay)` (a
1422
+ content script's `matches`); `undefined` where an address has none (an IPv6 literal).
1423
+ - The messages between the two (`PairingHello`, `FillMessage`, `parseHello`).
1424
+
1425
+ **What the extension supplies** (`FillHost`): its id and app id; the relay origin it fills
1426
+ in for (`undefined` for none); whether AI access is on, and whether the user turned
1427
+ filling in on; how the user is asked (`ask`, in its own panel or notification, the name
1428
+ and host shown with invisible characters escaped, `@ractive-ch/tab-mcp-client/visible`);
1429
+ the code with pairing held on (`startPairing()` before `takeCode`, kept on long enough
1430
+ after the fill for the relay to check the code); `release()`, called when no fill needs
1431
+ pairing held any more (the AI app connected, or a fill ended without sending its code
1432
+ and no other fill is under way or waits for the relay's check); and
1433
+ `failed("busy" | "no-code", context)` to tell its user. `code(signal, context)` and
1434
+ `failed(why, context)` get the page's `context` (`FillContext`): `{ tabId, client }`,
1435
+ the same `client` (`{ name, host }`) that `ask` got. Besides, the extension keeps the
1436
+ on/off setting, asks for the relay's site with a click
1437
+ (`permissions.request({ origins: [relaySitePattern(relay)] })`, an optional host
1438
+ permission), registers the content script for `pairingPagePattern(relay)` or puts it in
1439
+ with `scripting.executeScript`, and tells the filler what changed (`grantsChanged`,
1440
+ `withdrawAll`, `reset`).
1441
+
1442
+ The content script:
1443
+
1444
+ ```ts
1445
+ import { PAIRING_PORT, runPairingPage } from "@ractive-ch/tab-mcp-client/extension";
1446
+
1447
+ runPairingPage({
1448
+ app: "my-app", // the extension's appName
1449
+ connect: () => chrome.runtime.connect({ name: PAIRING_PORT }),
1450
+ });
1451
+ ```
1452
+
1453
+ The worker:
1454
+
1455
+ ```ts
1456
+ import {
1457
+ CodeFiller, PAIRING_PORT, pagePort, pairingCodes, relayPageOrigin, takeCode,
1458
+ } from "@ractive-ch/tab-mcp-client/extension";
1459
+
1460
+ const filler = new CodeFiller({
1461
+ extensionId: chrome.runtime.id,
1462
+ app: "my-app",
1463
+ relayOrigin: () => relayPageOrigin(relay),
1464
+ aiOn: () => mcp.state !== "disconnected",
1465
+ enabled: () => settings.fillCode, // off until the user turns it on
1466
+ ask: (tabId, client, signal) => askConnect(tabId, client, signal), // "allow" | "deny" | "timeout" | "busy"
1467
+ code: (signal) => {
1468
+ holdPairing(); // mcp.startPairing(), and stopPairing() a while after
1469
+ return takeCode(pairingCodes(mcp), signal, { now: Date.now() });
1470
+ },
1471
+ release: () => releasePairing(), // no fill needs pairing held now
1472
+ failed: (why, { client }) => showProblem(why, client.host), // "busy" | "no-code"
1473
+ now: () => Date.now(),
1474
+ });
1475
+ // new CodeFiller(host, { retry: false }): never fill in without a yes, also not
1476
+ // the relay's answer to a fill.
1477
+
1478
+ // At the top level, so that a page's port wakes the worker; taken over at once.
1479
+ // `page()` never rejects: a callback that throws closes the port and is logged.
1480
+ chrome.runtime.onConnect.addListener((port) => {
1481
+ if (port.name === PAIRING_PORT) void filler.page(pagePort(port));
1482
+ });
1483
+ // The first list is taken as the grants there are; a grant in a later one is
1484
+ // an AI app that just connected, which withdraws its question.
1485
+ mcp.addEventListener("grantschange", () =>
1486
+ filler.grantsChanged(mcp.grants.map((g) => ({ grantId: g.grantId, host: g.client.host }))),
1487
+ );
1488
+ // AI access or the setting turned off: filler.withdrawAll(); another relay: filler.reset().
1489
+ ```
1490
+
1491
+ A service worker usually starts before its connection is up, when `mcp.grants` is still
1492
+ empty: build the filler without `grants`, as above, and the relay's first list is the
1493
+ baseline. An extension that keeps the grants between worker starts can pass them
1494
+ (`new CodeFiller(host, { grants: kept })`), so that a grant made while the worker slept
1495
+ counts as new.
1496
+
1497
+ **Rules the extension must not weaken:**
1498
+
1499
+ - **The origin check.** Any site can copy the relay's markup, and any AI app can name
1500
+ itself as it likes. The worker decides by the port's sender, which the browser sets,
1501
+ never by what the page says: give `relayOrigin()` the configured relay's origin,
1502
+ exactly, and show the verified host in the question.
1503
+ - **The user's Allow.** No code leaves the worker without a yes to a question that names
1504
+ the AI app's verified host, and never "always allow": each pairing page asks. A
1505
+ withdrawn, timed-out or unanswered question fills in nothing.
1506
+ - **No loops.** One question per page load; the one fill without a question is bound to
1507
+ the same tab and the same relay request. Do not retry around the filler.
1508
+ - **Opt-in.** Off by default, also for an extension with access to all sites (that grant
1509
+ was for something else): `enabled()` is false until the user turns it on, and the
1510
+ content script is not registered while it is off.
1511
+
1220
1512
  ## Security
1221
1513
 
1222
1514
  Connecting an AI app to a page means trusting that page, as with any MCP server. Keep