@ractive-ch/tab-mcp-client 0.22.0-rc.1 → 0.23.0

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/CHANGELOG.md CHANGED
@@ -6,7 +6,22 @@ the Git tag `client-v<version>`. The format follows
6
6
  [Semantic Versioning](https://semver.org/). The relay has its own
7
7
  [changelog](https://github.com/ractive/tab-mcp/blob/main/packages/tab-mcp/CHANGELOG.md).
8
8
 
9
- ## [0.22.0-rc.1][client-0.22.0-rc.1] - 2026-11-23
9
+ ## [0.23.0][client-0.23.0] - 2026-11-23
10
+
11
+ ### Breaking
12
+
13
+ - **`webmcp` is on by default** (DEC-222): the page's tools are also registered with the
14
+ browser's WebMCP, where the tab has it. Set `webmcp: false` for the old behaviour.
15
+ - In a browser extension's own pages, and on a page that registers the same tool names
16
+ with `document.modelContext` itself, set `webmcp: false`.
17
+
18
+ ### Changed
19
+
20
+ - Several `TabMcp` of one app in a page (React's strict mode makes a second one and
21
+ drops it) tell the tab-mcp browser extension one list of AI apps: before, the one
22
+ made last, with none connected, replaced the other's.
23
+
24
+ ## [0.22.0][client-0.22.0] - 2026-11-23
10
25
 
11
26
  ### Breaking
12
27
 
@@ -14,6 +29,12 @@ the Git tag `client-v<version>`. The format follows
14
29
  `extensionchange`, the React snapshot's `extension`; DEC-221). The tab-mcp browser
15
30
  extension no longer tells a page its version. A page that showed or checked it drops
16
31
  that; `name`, `apps` and `whileInFront` are unchanged.
32
+ - **A page that stays on 0.21.0 or older loses the extension's state** once its users
33
+ have the tab-mcp browser extension after 0.3.2: such a page takes the extension's
34
+ "on" only with a version, so `extension` stays `undefined` and the page no longer
35
+ shows the AI apps connected through the extension. Calls the user allowed in the
36
+ extension are still not asked again in the page. Update to this version to get it
37
+ back.
17
38
 
18
39
  ### Changed
19
40
 
@@ -1188,7 +1209,8 @@ First alpha of the browser library, on the npm dist-tag `alpha`.
1188
1209
  `<script type="module">` under `@ractive-ch/tab-mcp-client/cdn`.
1189
1210
  - The connect dialog shows the pairing code from the relay.
1190
1211
 
1191
- [client-0.22.0-rc.1]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.22.0-rc.1
1212
+ [client-0.23.0]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.23.0
1213
+ [client-0.22.0]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.22.0
1192
1214
  [client-0.21.0]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.21.0
1193
1215
  [client-0.20.0]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.20.0
1194
1216
  [client-0.19.0]: https://github.com/ractive/tab-mcp/releases/tag/client-v0.19.0
package/README.md CHANGED
@@ -6,8 +6,8 @@ Offer a web page's functions as [MCP](https://modelcontextprotocol.io) tools to
6
6
  shows its app's address, which is the same for every page of the app. The user adds that
7
7
  address to their AI app and types a pairing code from the page, which tells the relay
8
8
  which page is meant; the AI app then calls the page's tools while the tab stays open. Nothing
9
- is installed on the user's computer. With `webmcp: true` the library also registers the
10
- tools with the browser's own [WebMCP](#webmcp), for agents in the browser. A site's own
9
+ is installed on the user's computer. Unless a page sets `webmcp: false`, the library also
10
+ registers the 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
12
  [Your own assistant on the page](#your-own-assistant-on-the-page). A browser extension
13
13
  that is a tab-mcp page itself can fill in its pairing code on the relay's page for its
@@ -17,7 +17,7 @@ How the library, the relay and the tab-mcp browser extension fit together, with
17
17
  picture:
18
18
  [How the parts fit together](https://github.com/ractive/tab-mcp#how-the-parts-fit-together).
19
19
 
20
- **Status: 0.22.0-rc.1.** Until 1.0, a new minor version can still change the API. Which AI apps
20
+ **Status: 0.23.0.** Until 1.0, a new minor version can still change the API. Which AI apps
21
21
  work is listed in [compatibility](https://github.com/ractive/tab-mcp/blob/main/docs/compatibility.md).
22
22
 
23
23
  ## With a coding agent
@@ -57,7 +57,7 @@ docker run --rm -p 8080:8080 \
57
57
  -e TAB_MCP_PUBLIC_URL=http://localhost:8080 \
58
58
  -e TAB_MCP_ALLOWED_ORIGINS=http://localhost:3000 \
59
59
  -e TAB_MCP_STORE=memory \
60
- ractive/tab-mcp:0.13.3-rc.1
60
+ ractive/tab-mcp:0.13.3
61
61
  ```
62
62
 
63
63
  For production, see
@@ -76,7 +76,7 @@ Or without a bundler, from a CDN as an ES module:
76
76
 
77
77
  ```html
78
78
  <script type="module">
79
- import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.22.0-rc.1/cdn/tab-mcp-client.min.js";
79
+ import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.23.0/cdn/tab-mcp-client.min.js";
80
80
  </script>
81
81
  ```
82
82
 
@@ -108,7 +108,7 @@ button.addEventListener("click", () => mcp.showConnectDialog());
108
108
  ```
109
109
 
110
110
  Importing the library loads only a small core; `connect()` loads the rest, and
111
- `webmcp: true` loads the WebMCP mirror where the browser has WebMCP.
111
+ the WebMCP mirror loads where the browser has WebMCP (unless `webmcp: false`).
112
112
 
113
113
  `server` is what the AI app sees: `name`, `version`, optional `title` and
114
114
  `instructions`, and `icons`. Without `icons`, the page's own `<link rel="icon">` and
@@ -210,18 +210,22 @@ added and removed tools.
210
210
 
211
211
  ### WebMCP
212
212
 
213
- `new TabMcp({ ..., webmcp: true })` also registers the tools with the browser's own
213
+ `new TabMcp({ ... })` also registers the tools with the browser's own
214
214
  WebMCP (`document.modelContext.registerTool()`, the
215
215
  [WebMCP draft](https://webmachinelearning.github.io/webmcp/) of the W3C Web Machine
216
216
  Learning Community Group; [explainer](https://github.com/webmachinelearning/webmcp)),
217
217
  where the browser has it, so that agents in the browser can call them: an agent built
218
- into the browser, or an extension. Off by default. Turning it on is a change in what the
219
- page exposes: any agent in the browser can then call the mirrored tools without pairing.
218
+ into the browser, or an extension such as the
219
+ [tab-mcp browser extension](#with-the-tab-mcp-browser-extension), which sees a page's
220
+ tools only there. On by default: where the tab has WebMCP, any agent in the browser can
221
+ call the mirrored tools without pairing. `webmcp: false` keeps every tool out of WebMCP
222
+ (and the page out of the handshake with the extension); `callers: ["relay", "local"]`
223
+ keeps one tool out ([which callers see a tool](#which-callers-see-a-tool)).
220
224
  Chrome has WebMCP for sites in its
221
225
  [origin trial](https://developer.chrome.com/blog/ai-webmcp-origin-trial) (Chrome 149 to
222
226
  162; a site needs a trial token) and with `chrome://flags/#enable-webmcp-testing`
223
227
  ([Chrome's WebMCP docs](https://developer.chrome.com/docs/ai/webmcp)). Without either,
224
- `webmcp: true` does nothing. Each `new TabMcp` looks for WebMCP when it is made, so a
228
+ the mirror does nothing. Each `new TabMcp` looks for WebMCP when it is made, so a
225
229
  trial token that a script adds later counts for a `TabMcp` made after it.
226
230
 
227
231
  #### Who asks before a tool that asks runs
@@ -263,12 +267,15 @@ tool away:
263
267
  a rule of your own (arguments, the time of day, what the user is signed in as). A
264
268
  question of your own in the page is no better than the library's against an agent
265
269
  that drives the tab.
270
+ - **`webmcp: false`** keeps every tool of the page out of WebMCP. The mirror is on by
271
+ default (it was off until 0.22): a page that upgrades without setting it offers its
272
+ tools there.
266
273
  - **`callers`** without `"webmcp"` keeps the tool out of WebMCP altogether
267
274
  ([which callers see a tool](#which-callers-see-a-tool)), for example
268
275
  `{ callers: ["relay", "local"] }`.
269
276
  - **`exposedTo`**: leave it off a tool that asks unless the listed origins may run it
270
277
  unasked. A document of such an origin lists and runs the tool (Chrome 156). With
271
- `webmcp: true` the library warns in the console, once per tool, about a tool that asks
278
+ the mirror on the library warns in the console, once per tool, about a tool that asks
272
279
  whose `exposedTo` names other origins on a page without `onConfirm` (in React: the
273
280
  provider without one), when the browser accepted the registration. It is decided then:
274
281
  an `onConfirm` removed later is not caught.
@@ -313,10 +320,11 @@ which ask nothing either.
313
320
  Content Security Policy); reloading the page fixes the latter.
314
321
  `toolcallend` reports the outcome as for an AI app (`error`, `declined`, `unanswered`,
315
322
  `cancelled`).
316
- - Only the native object is used, read through `Document.prototype`'s getter: an own
317
- `document.modelContext` (a polyfill such as `@mcp-b/global`, or a page script) is
318
- passed by, and with native WebMCP and the polyfill both present the tools go to the
319
- native object.
323
+ - Only the object behind `Document.prototype`'s getter is used: the browser's, or the one
324
+ a browser extension put there as the page started (the tab-mcp browser extension does,
325
+ where the browser has no WebMCP). An own `document.modelContext` (a polyfill such as
326
+ `@mcp-b/global`, or a page script) is passed by, and with native WebMCP and the
327
+ polyfill both present the tools go to the native object.
320
328
  - A tool leaves WebMCP whenever it leaves the page's registry: its `signal`, or the
321
329
  validator, which the first WebMCP call loads (as `connect()` does), refusing its
322
330
  schema.
@@ -327,7 +335,7 @@ which ask nothing either.
327
335
  once): a name the page or another script already registered
328
336
  natively keeps its owner, and a frame without `allow="tools"` refuses all. The other
329
337
  way round, once a name is mirrored, the page's own native `registerTool()` of that name
330
- fails: a page that registers tools natively itself leaves `webmcp` off or uses other
338
+ fails: a page that registers tools natively itself sets `webmcp: false` or uses other
331
339
  names.
332
340
  - The tab-mcp browser extension offers mirrored tools like any page tool, since it cannot know
333
341
  whether the AI app is also paired with the page, so an AI app connected to the page and
@@ -338,7 +346,7 @@ which ask nothing either.
338
346
 
339
347
  The [tab-mcp browser extension](https://github.com/ractive/tab-mcp/tree/main/packages/tab-mcp-extension)
340
348
  calls a page's tools through WebMCP for AI apps connected to it, and asks the user
341
- first about every tool that is not read-only. With `webmcp: true` the page and the
349
+ first about every tool that is not read-only. Unless `webmcp: false`, the page and the
342
350
  extension shake hands (DEC-207), so that a call is not asked twice and the page can say
343
351
  that an AI app reaches it through the extension:
344
352
 
@@ -620,7 +628,7 @@ instructions, as the recipe does with a delimited block.
620
628
  open confirmation would stay otherwise. Where the port fires `close` when its other end
621
629
  goes away (Bun does; Chrome only with the `MessagePortCloseEvent` Blink feature), the
622
630
  serve closes itself then. `serve` may be called for several ports (each port once at a
623
- time), and next to a relay connection and `webmcp: true`: one registration reaches all
631
+ time), and next to a relay connection and the WebMCP mirror: one registration reaches all
624
632
  three.
625
633
  - A port has none of the relay's limits: no cap on requests in flight, listen streams,
626
634
  body size or rate. The other end is the site's own code (threat model A15). The
@@ -818,8 +826,8 @@ mcp.addEventListener("grantschange", drawSwitches);
818
826
  that does not import it loads nothing of it. Take both from the same version:
819
827
 
820
828
  ```js
821
- import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.22.0-rc.1/cdn/tab-mcp-client.min.js";
822
- import { allowedTools } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.22.0-rc.1/cdn/allowed.min.js";
829
+ import { TabMcp } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.23.0/cdn/tab-mcp-client.min.js";
830
+ import { allowedTools } from "https://cdn.jsdelivr.net/npm/@ractive-ch/tab-mcp-client@0.23.0/cdn/allowed.min.js";
823
831
  ```
824
832
 
825
833
  **The risk.** An allowance outlives the user's attention: for up to the grant's 90 days,
@@ -1077,14 +1085,14 @@ sign in with the account you use on this page" only once it is `bound`. Until th
1077
1085
  says what is missing: "Checking your sign-in…" (`pending`), "Sign in to this page first."
1078
1086
  (`none`), or that the sign-in was not accepted, with `identityMessage` (`refused`).
1079
1087
 
1080
- An open dialog counts for 10 minutes (relays after 0.13.3-rc.1): a sign-in pairs the page
1088
+ An open dialog counts for 10 minutes (relays after 0.13.3): a sign-in pairs the page
1081
1089
  only until 10 minutes after the last `startPairing()`, which the dialog calls when it
1082
1090
  opens. The relay says for how long (`pairing.open`); the library counts that on the
1083
1091
  page's own clock from the message's arrival and keeps the end in `pairingUntil` (an ISO
1084
1092
  time, event `pairinguntil`), so a page whose clock differs from the relay's still shows
1085
1093
  the right moment. `pairingUntil` is `undefined` while pairing is off, from each
1086
1094
  `startPairing()` until the relay answers, after the session ended (`disconnect()`,
1087
- `disconnectAll()`, a refusal), and with a relay up to 0.13.3-rc.1. The dialog shows "Your AI
1095
+ `disconnectAll()`, a refusal), and with a relay up to 0.13.3. The dialog shows "Your AI
1088
1096
  app can connect until 14:32." under the sign-in line and, once that time is up (also
1089
1097
  checked when the tab comes back to the front), "The time to connect your AI app is up"
1090
1098
  with Continue, which calls `startPairing()` again. Nothing starts the 10 minutes again
@@ -1092,7 +1100,7 @@ by itself: not a timer, and not a reconnect the library makes by itself once the
1092
1100
  was sent, answered or not (after one, `pairingUntil` is the time of the reconnect, so
1093
1101
  the dialog shows Continue). `connect()` from a click while reconnecting ("Connect again")
1094
1102
  does ask again; codes always come again after a reconnect. A custom UI does the same:
1095
- call `startPairing()` only from the user's click. With a relay up to 0.13.3-rc.1 an open dialog
1103
+ call `startPairing()` only from the user's click. With a relay up to 0.13.3 an open dialog
1096
1104
  counts as long as it is open, and after a reconnect the dialog shows Continue too. A
1097
1105
  sign-in that comes back after the time is up gets the relay's answer that no page is
1098
1106
  connecting with that account; Continue, then Connect again on the relay's page, pairs it.
@@ -1118,7 +1126,7 @@ the session ends after the message was sent and before the relay answered, and w
1118
1126
  or may not have done it, and `grants` says so once the page is connected again. They
1119
1127
  also reject with the relay's refusal of that message, an `Error` whose `code` is
1120
1128
  `store_unavailable` (the relay could not save the change: try again) or `unknown_id`.
1121
- A relay after 0.13.3-rc.1 names the message it answers (`for`, and `grantId` for a
1129
+ A relay after 0.13.3 names the message it answers (`for`, and `grantId` for a
1122
1130
  revocation), so with several revocations pending only the one it answers rejects; an
1123
1131
  older relay's refusal rejects every pending `revoke()` and `signOut()`. When the relay
1124
1132
  does not know a grant the page lists (it expired), the grant leaves `grants` (with
@@ -1162,7 +1170,7 @@ ends connecting or reconnecting. It is absent on every other change: `connecting
1162
1170
  `disconnected` after a refusal stopped reconnecting (the `error` just before says why).
1163
1171
 
1164
1172
  For an activity log, `toolcall`
1165
- and `toolcallend` report every call by an AI app (or, with `webmcp`, an agent in the
1173
+ and `toolcallend` report every call by an AI app (or, through the WebMCP mirror, an agent in the
1166
1174
  browser, or the assistant on a served port): its id, tool, arguments and caller as it starts, then its outcome (`ok`,
1167
1175
  `error`, `declined`, `unanswered` or `cancelled`), result and duration.
1168
1176
 
@@ -1419,7 +1427,7 @@ function TodoList() {
1419
1427
  again only when its name, title, description, input or output schema or annotations
1420
1428
  change, or `options.callers` or `options.exposedTo` (by value: a new array with the same
1421
1429
  entries is no change; [which callers see a tool](#which-callers-see-a-tool),
1422
- [WebMCP](#webmcp), whose `webmcp` on the provider turns it on). Registered again unchanged, an allowed tool stays
1430
+ [WebMCP](#webmcp), which `webmcp: false` on the provider turns off). Registered again unchanged, an allowed tool stays
1423
1431
  allowed. StrictMode's double mount registers it once at a time. A duplicate name or an
1424
1432
  invalid tool is thrown from the component's render, for an error boundary. Tools that
1425
1433
  read state right after another tool changed it should read it from a store (as the
@@ -1,5 +1,5 @@
1
- var O,b,p,f=(e,t)=>{for(var[i,n,r]of O||[])for(var o=[e],s=globalThis,a=s.document,d,h,u,c,l;h=o.pop();)if(!b[h]&&(u=n[h])){b[h]=1;for(c=1;c<u.length;c++)o.push(r[u[c]]);if(!t&&h!==e&&(d=a&&a.head)){if(p===void 0)p=(c=a.querySelector("meta[property=csp-nonce]"))&&(c.nonce||c.getAttribute("nonce"))||"";if(l=a.createElement("link"),l.rel="modulepreload",l.crossOrigin="",p)l.nonce=p;l.href=new s.URL(u[0],i),d.appendChild(l)}}},k=(e,t,i,n)=>{for(var r={},o=0;o<t.length;o++)r[t[o]]=i[o];(O||=[]).push([e,r,t]),b||={},f(t[n],1)};k(import.meta.url,["0dgmgczn","vqs5smxj","m0e4xvcy","3reh9k1f","zrz65vva","gdj1e1wh","yjcmhpdf","q758jr4g"],[["./tab-mcp-client.min.js"],["./webmcp-vqs5smxj.min.js"],["./connection-m0e4xvcy.min.js"],["./connect-3reh9k1f.min.js",5,6,7],["./confirm-zrz65vva.min.js",5,6,7],["./tab-mcp-client-gdj1e1wh.min.js"],["./tab-mcp-client-yjcmhpdf.min.js",5],["./tab-mcp-client-q758jr4g.min.js"]],0);
2
- var L=/^[A-Za-z0-9_.-]{1,128}$/;function g(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}var D=/^(default|examples|const|enum)$/,j=/^(properties|patternProperties|\$defs|definitions|dependentSchemas)$/;function R(e,t,i=0,n=!1){if(i>64||typeof e!=="object"||e===null)return;if(n||=Array.isArray(e),!n)t(e);for(let[r,o]of Object.entries(e))if(n||!D.test(r))R(o,t,i+1,!n&&j.test(r))}function N(e,t){let i=e;for(let n of t.split("/").slice(1)){let r;try{r=decodeURIComponent(n)}catch{return}if(r=r.replaceAll("~1","/").replaceAll("~0","~"),typeof i!=="object"||i===null||!Object.hasOwn(i,r))return;i=i[r]}return i}function H(e){let t=new Set,i=[],n=[],r=!1;if(R(e,(o)=>{if(typeof o.$anchor==="string")t.add(o.$anchor);if(typeof o.$ref==="string")i.push(o.$ref);if(typeof o.pattern==="string")n.push(o.pattern);let s=o.patternProperties;if(g(s))n.push(...Object.keys(s));if("x-mcp-header"in o)r=!0}),r)return"x-mcp-header is not supported by tab-mcp";for(let o of n)try{new RegExp(o,"u")}catch{return`pattern ${JSON.stringify(o)} is not a valid regular expression`}for(let o of i){if(!o.startsWith("#"))return`$ref ${JSON.stringify(o)} points outside the schema`;let s=o.slice(1);if(!(s===""||(s.startsWith("/")?N(e,s)!==void 0:t.has(s))))return`$ref ${JSON.stringify(o)} does not resolve`}return}function _(e){if(!g(e))throw TypeError("registerTool: the tool must be an object");let{name:t,title:i,description:n,inputSchema:r,outputSchema:o,execute:s,annotations:a}=e;if(typeof t!=="string"||!L.test(t))throw TypeError(`registerTool: the name must be 1–128 characters of A–Z, a–z, 0–9, "_", "-" or "." (got ${JSON.stringify(t)})`);if(typeof n!=="string"||n.trim()==="")throw TypeError(`registerTool: tool ${t} needs a description`);if(i!==void 0&&typeof i!=="string")throw TypeError(`registerTool: the title of ${t} must be a string`);if(typeof s!=="function")throw TypeError(`registerTool: tool ${t} needs an execute function`);let d=(c,l)=>{if(!g(c)||c.type!=="object")throw TypeError(`registerTool: the ${l} of ${t} must be a JSON Schema object with "type": "object"`);let v;try{v=JSON.parse(JSON.stringify(c))}catch{throw TypeError(`registerTool: the ${l} of ${t} must be JSON`)}let C=H(v);if(C!==void 0)throw TypeError(`registerTool: the ${l} of ${t}: ${C}`);return v},h=d(r??{type:"object"},"inputSchema");if(a!==void 0&&!g(a))throw TypeError(`registerTool: the annotations of ${t} must be an object`);let u={};for(let[c,l]of Object.entries(a??{}))if(typeof l==="boolean")u[c]=l;else if(l!==void 0&&/Hint$|^debugging$/.test(c))throw TypeError(`registerTool: ${c} of ${t} must be a boolean`);return{name:t,title:i,description:n,inputSchema:h,outputSchema:o===void 0?void 0:d(o,"outputSchema"),annotations:u,execute:s}}class T{#e=new Map;#i=new Set;#m;register(e,t={}){let{signal:i,exposedTo:n}=t,r=t.callers&&[...t.callers];if("webmcp"in t||r?.some((s)=>!/^(relay|local|webmcp)$/.test(s)))throw TypeError('registerTool: callers must be "relay", "local" or "webmcp"; option webmcp is gone');let o={..._(e),callers:r,exposedTo:n};if(i?.throwIfAborted(),this.#e.has(o.name))throw new DOMException(`registerTool: a tool named ${o.name} is already registered`,"InvalidStateError");this.#m?.(o),this.#e.set(o.name,o),i?.addEventListener("abort",()=>this.remove(o),{once:!0}),this.#y()}setCompiler(e){this.#m??=e}remove(e){if(this.#e.get(e.name)!==e)return;this.#e.delete(e.name),this.#y()}get(e){return this.#e.get(e)}list(){return[...this.#e.values()]}onChange(e){return this.#i.add(e),()=>this.#i.delete(e)}#y(){for(let e of[...this.#i])e()}}var z=/^[A-Za-z0-9_-]{43}$/;function G(e){return btoa(String.fromCharCode(...e)).replaceAll("+","-").replaceAll("/","_").replace(/=+$/,"")}function J(){return G(crypto.getRandomValues(new Uint8Array(32)))}function K(e){return typeof e==="string"&&z.test(e)}function U(){try{return globalThis.localStorage??null}catch{return null}}class S{storage;key;#e;#i=!1;constructor(e,t){this.storage=e===void 0?U():e,this.key=t}current(){if(this.#i)return this.#e;let e=null;try{e=this.storage?.getItem(this.key)??null}catch{}if(K(e))return this.#e=e,e;return this.#e}rotate(){let e=J();this.#e=e,this.#i=!0;try{this.storage?.setItem(this.key,e),this.#i=!1}catch{}return e}}var I="0.22.0-rc.1";var M="@ractive-ch/tab-mcp-client",m=I;function W(){try{return Object.getOwnPropertyDescriptor(Document.prototype,"modelContext")?.get?.call(document)}catch{return}}var V=new Set(["localhost","127.0.0.1","[::1]"]),q=1000,y={reconnectDelaysMs:[5000,1e4,20000,40000,60000],answerTimeoutMs:15000,siblingGraceMs:q},P=new Set(["network","heartbeat","shutdown"]);function F(e){return Object.freeze({...e,client:Object.freeze({...e.client})})}function Y(e){return typeof e==="string"&&e.trim().length>0&&e.length<=64&&![...e].some((t)=>{let i=t.codePointAt(0)??0;return i<32||i>=127&&i<=159})}var Z=/^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;function Q(e){return e.normalize("NFKD").replace(/\p{M}/gu,"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"").slice(0,32).replace(/-$/,"")}function X(e){let t;try{t=new URL(e)}catch{throw TypeError(`TabMcp: relay is not a URL: ${e}`)}let i=t.protocol==="https:"||t.protocol==="wss:";if(!i&&t.protocol!=="http:"&&t.protocol!=="ws:")throw TypeError(`TabMcp: relay must be an https: or wss: URL: ${e}`);if(!i&&!V.has(t.hostname))throw TypeError(`TabMcp: relay must use https: or wss: (http: only for localhost): ${e}`);let n=`${i?"https:":"http:"}//${t.host}`,r=t.pathname.replace(/\/+$/,"");return t.protocol=i?"wss:":"ws:",t.pathname=r.endsWith("/tab")?r:`${r}/tab`,t.search="",t.hash="",{url:t.href,origin:n}}function ee(e){try{return new URL(e).host}catch{return""}}function te(e){let t={sessionId:e.sessionId,address:e.address,origin:e.origin,pairing:e.pairing==="oidc"?"oidc":"code",expiresAt:e.expiresAt,limits:e.limits};if(e.appName!==void 0)t.appName=e.appName;return t}function w(){return Promise.reject(TypeError("TabMcp: this TabMcp has no relay"))}function x(e,t){return Object.assign(Object(e),{secretChanged:t})}function E(){return Promise.reject(new DOMException("TabMcp: not connected","InvalidStateError"))}class A extends EventTarget{#e=new T;#i;#m;#y;#R;#G;#b;#w;#g;#J;#v=!1;#T;#I;#S="none";#M;#c=0;#r="disconnected";#s;#l=Object.freeze([]);#f=!1;#P;#x;#A=!1;#k=!1;#n;#o;#h=0;#K;#u;#p;#L;#$;#E;#D={registry:this.#e,confirm:(e)=>this.#X(e),onConfirm:()=>this.#R,toolCallStarted:(e)=>this.#t("toolcall",e),toolCallEnded:(e)=>this.#t("toolcallend",e)};constructor(e){super();let{server:t,appName:i}=e,n=e.relay===void 0?void 0:X(e.relay);if(typeof t?.name!=="string"||typeof t.version!=="string")throw TypeError("TabMcp: server needs a name and a version");if(i!==void 0&&!Y(i))throw TypeError("TabMcp: appName must be 1-64 characters without control characters");let r=e.app??Q(t.name);if(e.app!==void 0?typeof r!=="string"||!Z.test(r):n&&r==="")throw TypeError(e.app!==void 0?"TabMcp: app must be 1-32 characters of a-z, 0-9 and -, not starting or ending with -":"TabMcp: server.name has no letter a-z and no digit to make the address of: set app");if(this.#m=r,this.#w=n?.url,this.#i={...t},this.#y=i,this.#R=e.onConfirm,this.#G=e.alwaysAllow,this.#b=e.ui,this.#I=e.identityAssertion,this.#K=e.reconnect!==!1,n!==void 0){let s=e.storageKey??`tab-mcp:secret:${n.origin}:${encodeURIComponent(t.name)}`;this.#g=new S(e.storage,s),this.#J=e.storage===null?void 0:`${s}#tabs`}this.#e.onChange(()=>this.#t("toolschange",{tools:this.tools}));let o=e.webmcp===!0?W():void 0;if(o!==void 0)(f("vqs5smxj"),import("./webmcp-vqs5smxj.min.js")).then((s)=>s.mirror(this.#e,o,(a,d,h,u)=>this.#N().then((c)=>c.callFromWebMcp(this.#D,a,d,h,u)),this,r,e,m),(s)=>console.warn("tab-mcp: no tools are offered through WebMCP:",s))}registerTool(e,t={}){try{this.#e.register(e,t)}catch(i){return Promise.reject(i)}return Promise.resolve()}get tools(){return this.#e.list().map((e)=>e.name)}get state(){return this.#r}get session(){return this.#s}get grants(){return this.#l}get reconnecting(){return this.#u}get identity(){return this.#S}get identityMessage(){return this.#M}refreshIdentity(){let e=this.#s,t=this.#I;if(t===void 0)return Promise.reject(TypeError("TabMcp: no identityAssertion"));if(e===void 0)return E();let i=++this.#c,n=this.#n;this.#a("pending");let r=setTimeout(()=>{if(i!==this.#c||this.#S!=="pending")return;this.#c++,this.#a("none")},y.answerTimeoutMs);return new Promise((o)=>{let s=(a)=>{let{identity:d}=a.detail;if(d==="pending")return;this.removeEventListener("identitychange",s),clearTimeout(r),o(d)};this.addEventListener("identitychange",s),Promise.resolve().then(()=>t({address:e.address,sessionId:e.sessionId})).then((a)=>{if(i!==this.#c||this.#n!==n)return;if(typeof a!=="string"||a===""){this.#a("none");return}if(n?.send({type:"identity",assertion:a,round:i})!==!0)this.#a("none")},(a)=>{if(i!==this.#c)return;this.#t("error",{error:a instanceof Error?a:Error(String(a))}),this.#a("none")})})}signOut(){return this.#c++,this.#a("none"),this.#s?.pairing==="oidc"?this.#q({type:"identity.logout"},()=>!this.#l.length):E()}get pairingCode(){return this.#P}get pairingUntil(){return this.#x}connect(e={}){if(this.#w===void 0)return w();if(this.#r==="connected"&&this.#s!==void 0)return Promise.resolve(this.#s);if(this.#u!==void 0)return this.#k||=e.resume!==!0,this.#H();if(this.#o!==void 0)return this.#o;let t=globalThis.navigator?.userActivation;if(e.resume!==!0&&t!==void 0&&!t.isActive)return Promise.reject(new DOMException("connect() must be called from a user action, such as a click","NotAllowedError"));return this.#T?this.#T.then(()=>this.connect({resume:!0})):this.#j()}#j(){if(!this.#u)this.#d("connecting");let e=this.#U(this.#h);return this.#o=e,e.then(()=>{if(this.#o===e)this.#o=void 0},()=>{if(this.#o!==e)return;if(this.#o=void 0,this.#n=void 0,!this.#u)this.#d("disconnected")}),e}async#N(){let e=await (f("m0e4xvcy"),import("./connection-m0e4xvcy.min.js"));for(let t of e.compileRegistry(this.#e))this.#t("error",{error:t});return this.#$=e,e}async#U(e,t){let{openConnection:i,addressIdOf:n}=await this.#N(),r=this.#w;if(r===void 0||this.#g===void 0)return w();let o=t??this.#g.current()??this.#g.rotate(),s=await n(o);if(e!==this.#h)throw new DOMException("The connection was cancelled","AbortError");let a={},d=i(this.#ee(a),r,o,{address:s,revokeAll:!!t,siblingGraceMs:y.siblingGraceMs});if(!t)a.connection=d;return this.#n=d,d.ready}get hasRelay(){return this.#w!==void 0}serve(e,t){return this.#N().then((i)=>i.servePort({...this.#D,server:this.#i,error:(n)=>this.#t("error",{error:n})},e,t))}disconnect(){this.#B("user")}disconnectAll(){let e=this.#g;if(!e)return w();return this.#T??=this.#Y(e).finally(()=>{this.#T=void 0})}async#Y(e){let t=this.#r!=="disconnected"||this.#v,i=e.current();this.#B("disconnect_all");let n=this.#h;if(i){this.#d("connecting");try{await this.#U(n,i)}catch(r){if(n===this.#h)this.#d("disconnected"),this.#v=t;throw x(r,!1)}finally{if(n===this.#h)this.#n?.close("disconnect_all"),this.#n=void 0}}if(e.rotate(),this.#O([]),t)await this.#j().catch((r)=>{throw x(r,!0)});else this.#d("disconnected")}#B(e){this.#_(),this.#v=!1,this.#h++;let t=this.#n;if(this.#n=void 0,this.#o=void 0,t?.close(e),this.#r==="disconnected")return;this.#s=void 0,this.#z(),this.#c++,this.#a("none"),this.#d("disconnected",e)}#W(e,t){let i=y.reconnectDelaysMs,n=(i[Math.min(t,i.length)-1]??0)*(0.8+0.4*Math.random());clearTimeout(this.#p),this.#p=setTimeout(()=>{this.#H().catch(()=>{})},n),this.#Z(),this.#u={reason:e,attempt:t,at:Date.now()+n},this.#d("reconnecting",t>1?void 0:e)}#H(){if(clearTimeout(this.#p),this.#p=void 0,this.#o!==void 0)return this.#o;let e=this.#j();return e.catch((t)=>{let i=this.#u;if(i===void 0||this.#o!==void 0)return;let n=t?.reason;if(n===void 0||P.has(n)){this.#W(i.reason,i.attempt+1);return}if(this.#_(),this.#r="disconnected",this.#t("error",{error:t instanceof Error?t:Error(String(t))}),this.#r==="disconnected")this.#t("statechange",{state:"disconnected"})}),e}#Z(){this.#L??=this.#$?.onWake(()=>{if(this.#p!==void 0)this.#H().catch(()=>{})})}#_(){clearTimeout(this.#p),this.#p=void 0,this.#L?.(),this.#L=void 0,this.#u=void 0}startPairing(){if(this.#f=!0,this.#A=!1,this.#r==="connected")this.#V()}#V(){this.#C(void 0),this.#A=this.#s?.pairing==="oidc",this.#n?.send({type:"pairing.start"})}stopPairing(){if(!this.#f)return;if(this.#f=!1,this.#r==="connected")this.#n?.send({type:"pairing.stop"});this.#z()}#z(){this.#F(void 0),this.#C(void 0)}revoke(e){let t=()=>!this.#l.some((i)=>i.grantId===e);return t()?Promise.resolve():this.#q({type:"grant.revoke",grantId:e},t)}#q(e,t){return!this.#s||this.#n?.send(e)!==!0?E():this.#$.answer(this,e,t,()=>this.#r==="connected",y.answerTimeoutMs)}showConnectDialog(e={}){if(this.#w===void 0)return w();let t=e.document??globalThis.document,i=this.#E;if(i!==void 0)return i.doc===t?i.shown:Promise.reject(new DOMException("showConnectDialog: the dialog is already open in another document","InvalidStateError"));let n=this.#Q(t).finally(()=>{if(this.#E?.shown===n)this.#E=void 0});return this.#E={doc:t,shown:n},n}async#Q(e){let t=this.connect();t.catch(()=>{});let{showConnectDialog:i}=await (f("3reh9k1f"),import("./connect-3reh9k1f.min.js"));await i(this,t,e,this.#i.name,this.#b)}addEventListener(e,t,i){super.addEventListener(e,t,i)}removeEventListener(e,t,i){super.removeEventListener(e,t,i)}#t(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))}#d(e,t){if(this.#r===e&&e!=="reconnecting")return;this.#r=e,this.#t("statechange",t?{state:e,reason:t}:{state:e})}#a(e,t){if(this.#S===e&&t===void 0)return;this.#S=e,this.#M=e==="refused"?t:void 0,this.#t("identitychange",{identity:e,identityMessage:this.#M})}#F(e){if(this.#P===void 0&&e===void 0)return;this.#P=e,this.#t("pairingcode",{pairingCode:e})}#C(e){if(this.#x===e)return;this.#x=e,this.#t("pairinguntil",{pairingUntil:e})}#O(e){this.#l=Object.freeze(e.map(F)),this.#t("grantschange",{grants:this.#l})}#X(e){let t=this.#R;return (f("zrz65vva"),import("./confirm-zrz65vva.min.js")).then((i)=>i.confirmCall(this,e,t),()=>t?.(e)??!1)}get _allowed(){return[this.#e,this.#g,this.#G]}get _ui(){return this.#b}confirmInPage(e,t={}){if(e.client.kind==="webmcp")return Promise.resolve(!0);let i=t.document??globalThis.document;if(i===void 0)return Promise.resolve(!1);return (f("zrz65vva"),import("./confirm-zrz65vva.min.js")).then((n)=>n.confirmInPage(this,e,i,{ui:this.#b}))}#ee(e){let t=()=>e.connection!==void 0&&this.#n===e.connection,i=!1;return{...this.#D,server:this.#i,app:this.#m,appName:this.#y,tabs:this.#J,library:{name:M,version:m},clientFor:(n)=>{let r=this.#l.find((s)=>s.grantId===n.grantId),o={kind:"relay",grantId:n.grantId,id:n.clientId,host:ee(n.clientId)};if(r!==void 0)o.name=r.client.name;return o},sessionStarted:(n)=>{let r=te(n);if(!t())return r;this.#s=r,this.#v=!1;let o=this.#f&&this.#u&&!this.#k&&this.#A&&r.pairing==="oidc";if(this.#k=!1,this.#_(),this.#O([...n.grants]),o)this.#C(new Date().toISOString());else if(this.#f)this.#V();if(this.#d("connected"),r.pairing==="oidc"&&this.#I)this.refreshIdentity();return r},pairingCode:(n)=>{if(t()&&this.#f)this.#F(n)},pairingOpen:(n)=>{if(t()&&this.#f)this.#C(n)},grantAdded:(n)=>{if(!t())return;this.#O([...this.#l.filter((r)=>r.grantId!==n.grantId),n])},grantRemoved:(n)=>{if(!t())return;this.#O(this.#l.filter((r)=>r.grantId!==n))},replaced:(n)=>{if(i)this.#t("replaced",n)},identityResult:(n,r,o)=>{if(!t())return;if(r!==this.#c)return;if(n)this.#a("bound");else this.#a("refused",o??"")},error:(n)=>{if(t())this.#t("error",{error:n})},closed:(n)=>{if(!t())return;if(n==="replaced")this.#v=i=!0;this.#n=void 0,this.#s=void 0;let r=this.#h;if(this.#z(),this.#c++,this.#a("none"),r!==this.#h)return;if(this.#K&&P.has(n))this.#W(n,1);else this.#d("disconnected",n)}}}}export{A as TabMcp,m as VERSION};
1
+ var O,b,p,f=(e,t)=>{for(var[i,n,r]of O||[])for(var o=[e],s=globalThis,a=s.document,d,h,u,c,l;h=o.pop();)if(!b[h]&&(u=n[h])){b[h]=1;for(c=1;c<u.length;c++)o.push(r[u[c]]);if(!t&&h!==e&&(d=a&&a.head)){if(p===void 0)p=(c=a.querySelector("meta[property=csp-nonce]"))&&(c.nonce||c.getAttribute("nonce"))||"";if(l=a.createElement("link"),l.rel="modulepreload",l.crossOrigin="",p)l.nonce=p;l.href=new s.URL(u[0],i),d.appendChild(l)}}},k=(e,t,i,n)=>{for(var r={},o=0;o<t.length;o++)r[t[o]]=i[o];(O||=[]).push([e,r,t]),b||={},f(t[n],1)};k(import.meta.url,["h64fd1em","kkrmjqfh","m0e4xvcy","3reh9k1f","zrz65vva","gdj1e1wh","yjcmhpdf","q758jr4g"],[["./tab-mcp-client.min.js"],["./webmcp-kkrmjqfh.min.js"],["./connection-m0e4xvcy.min.js"],["./connect-3reh9k1f.min.js",5,6,7],["./confirm-zrz65vva.min.js",5,6,7],["./tab-mcp-client-gdj1e1wh.min.js"],["./tab-mcp-client-yjcmhpdf.min.js",5],["./tab-mcp-client-q758jr4g.min.js"]],0);
2
+ var L=/^[A-Za-z0-9_.-]{1,128}$/;function g(e){return typeof e==="object"&&e!==null&&!Array.isArray(e)}var D=/^(default|examples|const|enum)$/,j=/^(properties|patternProperties|\$defs|definitions|dependentSchemas)$/;function R(e,t,i=0,n=!1){if(i>64||typeof e!=="object"||e===null)return;if(n||=Array.isArray(e),!n)t(e);for(let[r,o]of Object.entries(e))if(n||!D.test(r))R(o,t,i+1,!n&&j.test(r))}function N(e,t){let i=e;for(let n of t.split("/").slice(1)){let r;try{r=decodeURIComponent(n)}catch{return}if(r=r.replaceAll("~1","/").replaceAll("~0","~"),typeof i!=="object"||i===null||!Object.hasOwn(i,r))return;i=i[r]}return i}function H(e){let t=new Set,i=[],n=[],r=!1;if(R(e,(o)=>{if(typeof o.$anchor==="string")t.add(o.$anchor);if(typeof o.$ref==="string")i.push(o.$ref);if(typeof o.pattern==="string")n.push(o.pattern);let s=o.patternProperties;if(g(s))n.push(...Object.keys(s));if("x-mcp-header"in o)r=!0}),r)return"x-mcp-header is not supported by tab-mcp";for(let o of n)try{new RegExp(o,"u")}catch{return`pattern ${JSON.stringify(o)} is not a valid regular expression`}for(let o of i){if(!o.startsWith("#"))return`$ref ${JSON.stringify(o)} points outside the schema`;let s=o.slice(1);if(!(s===""||(s.startsWith("/")?N(e,s)!==void 0:t.has(s))))return`$ref ${JSON.stringify(o)} does not resolve`}return}function _(e){if(!g(e))throw TypeError("registerTool: the tool must be an object");let{name:t,title:i,description:n,inputSchema:r,outputSchema:o,execute:s,annotations:a}=e;if(typeof t!=="string"||!L.test(t))throw TypeError(`registerTool: the name must be 1–128 characters of A–Z, a–z, 0–9, "_", "-" or "." (got ${JSON.stringify(t)})`);if(typeof n!=="string"||n.trim()==="")throw TypeError(`registerTool: tool ${t} needs a description`);if(i!==void 0&&typeof i!=="string")throw TypeError(`registerTool: the title of ${t} must be a string`);if(typeof s!=="function")throw TypeError(`registerTool: tool ${t} needs an execute function`);let d=(c,l)=>{if(!g(c)||c.type!=="object")throw TypeError(`registerTool: the ${l} of ${t} must be a JSON Schema object with "type": "object"`);let v;try{v=JSON.parse(JSON.stringify(c))}catch{throw TypeError(`registerTool: the ${l} of ${t} must be JSON`)}let C=H(v);if(C!==void 0)throw TypeError(`registerTool: the ${l} of ${t}: ${C}`);return v},h=d(r??{type:"object"},"inputSchema");if(a!==void 0&&!g(a))throw TypeError(`registerTool: the annotations of ${t} must be an object`);let u={};for(let[c,l]of Object.entries(a??{}))if(typeof l==="boolean")u[c]=l;else if(l!==void 0&&/Hint$|^debugging$/.test(c))throw TypeError(`registerTool: ${c} of ${t} must be a boolean`);return{name:t,title:i,description:n,inputSchema:h,outputSchema:o===void 0?void 0:d(o,"outputSchema"),annotations:u,execute:s}}class T{#e=new Map;#i=new Set;#m;register(e,t={}){let{signal:i,exposedTo:n}=t,r=t.callers&&[...t.callers];if("webmcp"in t||r?.some((s)=>!/^(relay|local|webmcp)$/.test(s)))throw TypeError('registerTool: callers must be "relay", "local" or "webmcp"; option webmcp is gone');let o={..._(e),callers:r,exposedTo:n};if(i?.throwIfAborted(),this.#e.has(o.name))throw new DOMException(`registerTool: a tool named ${o.name} is already registered`,"InvalidStateError");this.#m?.(o),this.#e.set(o.name,o),i?.addEventListener("abort",()=>this.remove(o),{once:!0}),this.#y()}setCompiler(e){this.#m??=e}remove(e){if(this.#e.get(e.name)!==e)return;this.#e.delete(e.name),this.#y()}get(e){return this.#e.get(e)}list(){return[...this.#e.values()]}onChange(e){return this.#i.add(e),()=>this.#i.delete(e)}#y(){for(let e of[...this.#i])e()}}var z=/^[A-Za-z0-9_-]{43}$/;function G(e){return btoa(String.fromCharCode(...e)).replaceAll("+","-").replaceAll("/","_").replace(/=+$/,"")}function J(){return G(crypto.getRandomValues(new Uint8Array(32)))}function K(e){return typeof e==="string"&&z.test(e)}function U(){try{return globalThis.localStorage??null}catch{return null}}class S{storage;key;#e;#i=!1;constructor(e,t){this.storage=e===void 0?U():e,this.key=t}current(){if(this.#i)return this.#e;let e=null;try{e=this.storage?.getItem(this.key)??null}catch{}if(K(e))return this.#e=e,e;return this.#e}rotate(){let e=J();this.#e=e,this.#i=!0;try{this.storage?.setItem(this.key,e),this.#i=!1}catch{}return e}}var I="0.23.0";var M="@ractive-ch/tab-mcp-client",m=I;function W(){try{return Object.getOwnPropertyDescriptor(Document.prototype,"modelContext")?.get?.call(document)}catch{return}}var V=new Set(["localhost","127.0.0.1","[::1]"]),q=1000,y={reconnectDelaysMs:[5000,1e4,20000,40000,60000],answerTimeoutMs:15000,siblingGraceMs:q},P=new Set(["network","heartbeat","shutdown"]);function F(e){return Object.freeze({...e,client:Object.freeze({...e.client})})}function Y(e){return typeof e==="string"&&e.trim().length>0&&e.length<=64&&![...e].some((t)=>{let i=t.codePointAt(0)??0;return i<32||i>=127&&i<=159})}var Z=/^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;function Q(e){return e.normalize("NFKD").replace(/\p{M}/gu,"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"").slice(0,32).replace(/-$/,"")}function X(e){let t;try{t=new URL(e)}catch{throw TypeError(`TabMcp: relay is not a URL: ${e}`)}let i=t.protocol==="https:"||t.protocol==="wss:";if(!i&&t.protocol!=="http:"&&t.protocol!=="ws:")throw TypeError(`TabMcp: relay must be an https: or wss: URL: ${e}`);if(!i&&!V.has(t.hostname))throw TypeError(`TabMcp: relay must use https: or wss: (http: only for localhost): ${e}`);let n=`${i?"https:":"http:"}//${t.host}`,r=t.pathname.replace(/\/+$/,"");return t.protocol=i?"wss:":"ws:",t.pathname=r.endsWith("/tab")?r:`${r}/tab`,t.search="",t.hash="",{url:t.href,origin:n}}function ee(e){try{return new URL(e).host}catch{return""}}function te(e){let t={sessionId:e.sessionId,address:e.address,origin:e.origin,pairing:e.pairing==="oidc"?"oidc":"code",expiresAt:e.expiresAt,limits:e.limits};if(e.appName!==void 0)t.appName=e.appName;return t}function w(){return Promise.reject(TypeError("TabMcp: this TabMcp has no relay"))}function x(e,t){return Object.assign(Object(e),{secretChanged:t})}function E(){return Promise.reject(new DOMException("TabMcp: not connected","InvalidStateError"))}class A extends EventTarget{#e=new T;#i;#m;#y;#R;#G;#b;#w;#g;#J;#v=!1;#T;#I;#S="none";#M;#c=0;#r="disconnected";#s;#l=Object.freeze([]);#f=!1;#P;#x;#A=!1;#k=!1;#n;#o;#h=0;#K;#u;#p;#L;#$;#E;#D={registry:this.#e,confirm:(e)=>this.#X(e),onConfirm:()=>this.#R,toolCallStarted:(e)=>this.#t("toolcall",e),toolCallEnded:(e)=>this.#t("toolcallend",e)};constructor(e){super();let{server:t,appName:i}=e,n=e.relay===void 0?void 0:X(e.relay);if(typeof t?.name!=="string"||typeof t.version!=="string")throw TypeError("TabMcp: server needs a name and a version");if(i!==void 0&&!Y(i))throw TypeError("TabMcp: appName must be 1-64 characters without control characters");let r=e.app??Q(t.name);if(e.app!==void 0?typeof r!=="string"||!Z.test(r):n&&r==="")throw TypeError(e.app!==void 0?"TabMcp: app must be 1-32 characters of a-z, 0-9 and -, not starting or ending with -":"TabMcp: server.name has no letter a-z and no digit to make the address of: set app");if(this.#m=r,this.#w=n?.url,this.#i={...t},this.#y=i,this.#R=e.onConfirm,this.#G=e.alwaysAllow,this.#b=e.ui,this.#I=e.identityAssertion,this.#K=e.reconnect!==!1,n!==void 0){let s=e.storageKey??`tab-mcp:secret:${n.origin}:${encodeURIComponent(t.name)}`;this.#g=new S(e.storage,s),this.#J=e.storage===null?void 0:`${s}#tabs`}this.#e.onChange(()=>this.#t("toolschange",{tools:this.tools}));let o=e.webmcp!==!1?W():void 0;if(o!==void 0)(f("kkrmjqfh"),import("./webmcp-kkrmjqfh.min.js")).then((s)=>s.mirror(this.#e,o,(a,d,h,u)=>this.#N().then((c)=>c.callFromWebMcp(this.#D,a,d,h,u)),this,r,e,m),(s)=>console.warn("tab-mcp: no tools are offered through WebMCP:",s))}registerTool(e,t={}){try{this.#e.register(e,t)}catch(i){return Promise.reject(i)}return Promise.resolve()}get tools(){return this.#e.list().map((e)=>e.name)}get state(){return this.#r}get session(){return this.#s}get grants(){return this.#l}get reconnecting(){return this.#u}get identity(){return this.#S}get identityMessage(){return this.#M}refreshIdentity(){let e=this.#s,t=this.#I;if(t===void 0)return Promise.reject(TypeError("TabMcp: no identityAssertion"));if(e===void 0)return E();let i=++this.#c,n=this.#n;this.#a("pending");let r=setTimeout(()=>{if(i!==this.#c||this.#S!=="pending")return;this.#c++,this.#a("none")},y.answerTimeoutMs);return new Promise((o)=>{let s=(a)=>{let{identity:d}=a.detail;if(d==="pending")return;this.removeEventListener("identitychange",s),clearTimeout(r),o(d)};this.addEventListener("identitychange",s),Promise.resolve().then(()=>t({address:e.address,sessionId:e.sessionId})).then((a)=>{if(i!==this.#c||this.#n!==n)return;if(typeof a!=="string"||a===""){this.#a("none");return}if(n?.send({type:"identity",assertion:a,round:i})!==!0)this.#a("none")},(a)=>{if(i!==this.#c)return;this.#t("error",{error:a instanceof Error?a:Error(String(a))}),this.#a("none")})})}signOut(){return this.#c++,this.#a("none"),this.#s?.pairing==="oidc"?this.#q({type:"identity.logout"},()=>!this.#l.length):E()}get pairingCode(){return this.#P}get pairingUntil(){return this.#x}connect(e={}){if(this.#w===void 0)return w();if(this.#r==="connected"&&this.#s!==void 0)return Promise.resolve(this.#s);if(this.#u!==void 0)return this.#k||=e.resume!==!0,this.#H();if(this.#o!==void 0)return this.#o;let t=globalThis.navigator?.userActivation;if(e.resume!==!0&&t!==void 0&&!t.isActive)return Promise.reject(new DOMException("connect() must be called from a user action, such as a click","NotAllowedError"));return this.#T?this.#T.then(()=>this.connect({resume:!0})):this.#j()}#j(){if(!this.#u)this.#d("connecting");let e=this.#U(this.#h);return this.#o=e,e.then(()=>{if(this.#o===e)this.#o=void 0},()=>{if(this.#o!==e)return;if(this.#o=void 0,this.#n=void 0,!this.#u)this.#d("disconnected")}),e}async#N(){let e=await (f("m0e4xvcy"),import("./connection-m0e4xvcy.min.js"));for(let t of e.compileRegistry(this.#e))this.#t("error",{error:t});return this.#$=e,e}async#U(e,t){let{openConnection:i,addressIdOf:n}=await this.#N(),r=this.#w;if(r===void 0||this.#g===void 0)return w();let o=t??this.#g.current()??this.#g.rotate(),s=await n(o);if(e!==this.#h)throw new DOMException("The connection was cancelled","AbortError");let a={},d=i(this.#ee(a),r,o,{address:s,revokeAll:!!t,siblingGraceMs:y.siblingGraceMs});if(!t)a.connection=d;return this.#n=d,d.ready}get hasRelay(){return this.#w!==void 0}serve(e,t){return this.#N().then((i)=>i.servePort({...this.#D,server:this.#i,error:(n)=>this.#t("error",{error:n})},e,t))}disconnect(){this.#B("user")}disconnectAll(){let e=this.#g;if(!e)return w();return this.#T??=this.#Y(e).finally(()=>{this.#T=void 0})}async#Y(e){let t=this.#r!=="disconnected"||this.#v,i=e.current();this.#B("disconnect_all");let n=this.#h;if(i){this.#d("connecting");try{await this.#U(n,i)}catch(r){if(n===this.#h)this.#d("disconnected"),this.#v=t;throw x(r,!1)}finally{if(n===this.#h)this.#n?.close("disconnect_all"),this.#n=void 0}}if(e.rotate(),this.#O([]),t)await this.#j().catch((r)=>{throw x(r,!0)});else this.#d("disconnected")}#B(e){this.#_(),this.#v=!1,this.#h++;let t=this.#n;if(this.#n=void 0,this.#o=void 0,t?.close(e),this.#r==="disconnected")return;this.#s=void 0,this.#z(),this.#c++,this.#a("none"),this.#d("disconnected",e)}#W(e,t){let i=y.reconnectDelaysMs,n=(i[Math.min(t,i.length)-1]??0)*(0.8+0.4*Math.random());clearTimeout(this.#p),this.#p=setTimeout(()=>{this.#H().catch(()=>{})},n),this.#Z(),this.#u={reason:e,attempt:t,at:Date.now()+n},this.#d("reconnecting",t>1?void 0:e)}#H(){if(clearTimeout(this.#p),this.#p=void 0,this.#o!==void 0)return this.#o;let e=this.#j();return e.catch((t)=>{let i=this.#u;if(i===void 0||this.#o!==void 0)return;let n=t?.reason;if(n===void 0||P.has(n)){this.#W(i.reason,i.attempt+1);return}if(this.#_(),this.#r="disconnected",this.#t("error",{error:t instanceof Error?t:Error(String(t))}),this.#r==="disconnected")this.#t("statechange",{state:"disconnected"})}),e}#Z(){this.#L??=this.#$?.onWake(()=>{if(this.#p!==void 0)this.#H().catch(()=>{})})}#_(){clearTimeout(this.#p),this.#p=void 0,this.#L?.(),this.#L=void 0,this.#u=void 0}startPairing(){if(this.#f=!0,this.#A=!1,this.#r==="connected")this.#V()}#V(){this.#C(void 0),this.#A=this.#s?.pairing==="oidc",this.#n?.send({type:"pairing.start"})}stopPairing(){if(!this.#f)return;if(this.#f=!1,this.#r==="connected")this.#n?.send({type:"pairing.stop"});this.#z()}#z(){this.#F(void 0),this.#C(void 0)}revoke(e){let t=()=>!this.#l.some((i)=>i.grantId===e);return t()?Promise.resolve():this.#q({type:"grant.revoke",grantId:e},t)}#q(e,t){return!this.#s||this.#n?.send(e)!==!0?E():this.#$.answer(this,e,t,()=>this.#r==="connected",y.answerTimeoutMs)}showConnectDialog(e={}){if(this.#w===void 0)return w();let t=e.document??globalThis.document,i=this.#E;if(i!==void 0)return i.doc===t?i.shown:Promise.reject(new DOMException("showConnectDialog: the dialog is already open in another document","InvalidStateError"));let n=this.#Q(t).finally(()=>{if(this.#E?.shown===n)this.#E=void 0});return this.#E={doc:t,shown:n},n}async#Q(e){let t=this.connect();t.catch(()=>{});let{showConnectDialog:i}=await (f("3reh9k1f"),import("./connect-3reh9k1f.min.js"));await i(this,t,e,this.#i.name,this.#b)}addEventListener(e,t,i){super.addEventListener(e,t,i)}removeEventListener(e,t,i){super.removeEventListener(e,t,i)}#t(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t}))}#d(e,t){if(this.#r===e&&e!=="reconnecting")return;this.#r=e,this.#t("statechange",t?{state:e,reason:t}:{state:e})}#a(e,t){if(this.#S===e&&t===void 0)return;this.#S=e,this.#M=e==="refused"?t:void 0,this.#t("identitychange",{identity:e,identityMessage:this.#M})}#F(e){if(this.#P===void 0&&e===void 0)return;this.#P=e,this.#t("pairingcode",{pairingCode:e})}#C(e){if(this.#x===e)return;this.#x=e,this.#t("pairinguntil",{pairingUntil:e})}#O(e){this.#l=Object.freeze(e.map(F)),this.#t("grantschange",{grants:this.#l})}#X(e){let t=this.#R;return (f("zrz65vva"),import("./confirm-zrz65vva.min.js")).then((i)=>i.confirmCall(this,e,t),()=>t?.(e)??!1)}get _allowed(){return[this.#e,this.#g,this.#G]}get _ui(){return this.#b}confirmInPage(e,t={}){if(e.client.kind==="webmcp")return Promise.resolve(!0);let i=t.document??globalThis.document;if(i===void 0)return Promise.resolve(!1);return (f("zrz65vva"),import("./confirm-zrz65vva.min.js")).then((n)=>n.confirmInPage(this,e,i,{ui:this.#b}))}#ee(e){let t=()=>e.connection!==void 0&&this.#n===e.connection,i=!1;return{...this.#D,server:this.#i,app:this.#m,appName:this.#y,tabs:this.#J,library:{name:M,version:m},clientFor:(n)=>{let r=this.#l.find((s)=>s.grantId===n.grantId),o={kind:"relay",grantId:n.grantId,id:n.clientId,host:ee(n.clientId)};if(r!==void 0)o.name=r.client.name;return o},sessionStarted:(n)=>{let r=te(n);if(!t())return r;this.#s=r,this.#v=!1;let o=this.#f&&this.#u&&!this.#k&&this.#A&&r.pairing==="oidc";if(this.#k=!1,this.#_(),this.#O([...n.grants]),o)this.#C(new Date().toISOString());else if(this.#f)this.#V();if(this.#d("connected"),r.pairing==="oidc"&&this.#I)this.refreshIdentity();return r},pairingCode:(n)=>{if(t()&&this.#f)this.#F(n)},pairingOpen:(n)=>{if(t()&&this.#f)this.#C(n)},grantAdded:(n)=>{if(!t())return;this.#O([...this.#l.filter((r)=>r.grantId!==n.grantId),n])},grantRemoved:(n)=>{if(!t())return;this.#O(this.#l.filter((r)=>r.grantId!==n))},replaced:(n)=>{if(i)this.#t("replaced",n)},identityResult:(n,r,o)=>{if(!t())return;if(r!==this.#c)return;if(n)this.#a("bound");else this.#a("refused",o??"")},error:(n)=>{if(t())this.#t("error",{error:n})},closed:(n)=>{if(!t())return;if(n==="replaced")this.#v=i=!0;this.#n=void 0,this.#s=void 0;let r=this.#h;if(this.#z(),this.#c++,this.#a("none"),r!==this.#h)return;if(this.#K&&P.has(n))this.#W(n,1);else this.#d("disconnected",n)}}}}export{A as TabMcp,m as VERSION};
3
3
 
4
- //# debugId=557D70D6412B884B64756E2164756E21
4
+ //# debugId=35F1CD4B55C1C8BC64756E2164756E21
5
5
  //# sourceMappingURL=tab-mcp-client.min.js.map