@ractive-ch/tab-mcp-client 0.6.0 → 0.8.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/ui-xkw7g2v2.js ADDED
@@ -0,0 +1,660 @@
1
+ // src/always-allowed.ts
2
+ class AlwaysAllowed {
3
+ #tools = new Map;
4
+ has(grantId, tool) {
5
+ return this.#tools.get(grantId)?.has(tool) === true;
6
+ }
7
+ add(grantId, tool) {
8
+ const tools = this.#tools.get(grantId) ?? new Set;
9
+ tools.add(tool);
10
+ this.#tools.set(grantId, tools);
11
+ }
12
+ keepGrants(grantIds) {
13
+ const keep = new Set(grantIds);
14
+ for (const grantId of this.#tools.keys()) {
15
+ if (!keep.has(grantId))
16
+ this.#tools.delete(grantId);
17
+ }
18
+ }
19
+ keepTools(names) {
20
+ const keep = new Set(names);
21
+ for (const tools of this.#tools.values()) {
22
+ for (const tool of tools)
23
+ if (!keep.has(tool))
24
+ tools.delete(tool);
25
+ }
26
+ }
27
+ }
28
+
29
+ // src/ui.ts
30
+ var counter = 0;
31
+ function el(doc, tag, props = {}, ...children) {
32
+ const node = doc.createElement(tag);
33
+ if (props.className !== undefined)
34
+ node.className = props.className;
35
+ if (props.id !== undefined)
36
+ node.id = props.id;
37
+ if (props.text !== undefined)
38
+ node.textContent = props.text;
39
+ node.append(...children);
40
+ return node;
41
+ }
42
+ function button(doc, text, onClick, className, kind) {
43
+ const node = el(doc, "button", { text });
44
+ node.type = "button";
45
+ if (className !== undefined)
46
+ node.classList.add(className);
47
+ if (kind !== undefined)
48
+ node.classList.add(`tab-mcp-${kind}`);
49
+ node.addEventListener("click", onClick);
50
+ return node;
51
+ }
52
+ var DIALOG_CSS = `.tab-mcp-dialog {
53
+ --_tab-mcp-fg: var(--tab-mcp-color, CanvasText);
54
+ --_tab-mcp-bg: var(--tab-mcp-background, Canvas);
55
+ --_tab-mcp-muted: var(--tab-mcp-muted, color-mix(in srgb, var(--_tab-mcp-fg) 72%, transparent));
56
+ --_tab-mcp-line: color-mix(in srgb, var(--_tab-mcp-fg) 22%, transparent);
57
+ --_tab-mcp-surface: var(--tab-mcp-surface, color-mix(in srgb, var(--_tab-mcp-fg) 6%, transparent));
58
+ --_tab-mcp-danger: var(--tab-mcp-danger, light-dark(#b3261e, #ffb4ab));
59
+ --_tab-mcp-mono: var(--tab-mcp-mono, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace);
60
+ --_tab-mcp-small-radius: calc(var(--tab-mcp-radius, 0.75rem) * 0.6);
61
+ box-sizing: border-box; width: calc(100% - 2rem); max-width: var(--tab-mcp-max-width, 34rem); max-height: calc(100% - 2rem); overflow: auto;
62
+ font: var(--tab-mcp-font, inherit); line-height: 1.5; color: var(--_tab-mcp-fg); background: var(--_tab-mcp-bg);
63
+ border: var(--tab-mcp-border, 1px solid var(--_tab-mcp-line)); border-radius: var(--tab-mcp-radius, 0.75rem);
64
+ padding: var(--tab-mcp-padding, clamp(1rem, 5vw, 1.5rem)); box-shadow: var(--tab-mcp-shadow, 0 1rem 3rem rgb(0 0 0 / 0.3));
65
+ }
66
+ .tab-mcp-dialog::backdrop { background: var(--tab-mcp-backdrop, rgb(0 0 0 / 0.4)); }
67
+ .tab-mcp-dialog:where([open]) { display: flex; flex-direction: column; gap: 1rem; }
68
+ .tab-mcp-dialog:where([open]) > * { flex-shrink: 0; }
69
+ .tab-mcp-dialog :where([hidden]) { display: none !important; }
70
+ html dialog.tab-mcp-dialog:where(:focus) { outline: none; }
71
+ .tab-mcp-dialog :where(*) { box-sizing: border-box; }
72
+ .tab-mcp-dialog :where(h2, h3, p, ul, li, pre) { margin: 0; }
73
+ .tab-mcp-dialog :where(h2) { font-size: var(--tab-mcp-title-size, 1.25em); line-height: 1.3; font-weight: 600; overflow-wrap: anywhere; }
74
+ .tab-mcp-dialog :where(h3) { font-size: 0.875em; font-weight: 600; color: var(--_tab-mcp-muted); margin-bottom: 0.5rem; }
75
+ .tab-mcp-dialog :where(small) { font-size: 0.875em; }
76
+ .tab-mcp-dialog :where(small, .tab-mcp-status, .tab-mcp-description, .tab-mcp-always-note, .tab-mcp-apps-note) { color: var(--_tab-mcp-muted); }
77
+ html dialog.tab-mcp-dialog :where(:focus-visible) { outline: 2px solid var(--tab-mcp-focus, var(--_tab-mcp-fg)); outline-offset: 2px; }
78
+ .tab-mcp-dialog :where(button) { font: inherit; font-size: 0.9375em; line-height: 1.25; min-height: 2.5rem; padding: 0.5rem 1rem; margin: 0; border: 1px solid color-mix(in srgb, currentColor 35%, transparent); border-radius: var(--_tab-mcp-small-radius); background: transparent; color: inherit; cursor: pointer; }
79
+ html dialog.tab-mcp-dialog :where(button:hover:not(:disabled)) { color: inherit; background: color-mix(in srgb, currentColor 8%, transparent); }
80
+ html dialog.tab-mcp-dialog :where(button:disabled) { opacity: 0.45; cursor: not-allowed; }
81
+ html button.tab-mcp-primary { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); color: var(--tab-mcp-accent-text, var(--_tab-mcp-bg)); border-color: transparent; font-weight: 600; }
82
+ html button.tab-mcp-primary:where(:hover:not(:disabled)) { background: var(--tab-mcp-accent, var(--_tab-mcp-fg)); opacity: 0.85; }
83
+ html button.tab-mcp-danger { color: var(--_tab-mcp-danger); border-color: color-mix(in srgb, currentColor 45%, transparent); }
84
+ .tab-mcp-copy { min-width: 6em; }
85
+ .tab-mcp-close { font-size: 0.875em; }
86
+ .tab-mcp-status { margin-top: -0.5rem; }
87
+ .tab-mcp-description:where(:empty) { display: none; }
88
+ .tab-mcp-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 0.5rem; }
89
+ .tab-mcp-address { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }
90
+ .tab-mcp-address :where(label) { flex-basis: 100%; font-weight: 600; }
91
+ .tab-mcp-address :where(input) { flex: 1 1 12rem; min-width: 0; min-height: 2.5rem; margin: 0; padding: 0.5rem 0.625rem; font: 0.875em var(--_tab-mcp-mono); text-overflow: ellipsis; color: inherit; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
92
+ .tab-mcp-code { display: grid; grid-template-columns: 1fr auto; align-items: end; gap: 0.25rem 1rem; padding: 1rem; background: var(--_tab-mcp-surface); border-radius: var(--_tab-mcp-small-radius); }
93
+ .tab-mcp-code > :where(span) { font-weight: 600; }
94
+ .tab-mcp-code :where(br) { display: none; }
95
+ .tab-mcp-code :where(small) { grid-column: 1 / -1; }
96
+ .tab-mcp-code-value { display: block; font: 700 var(--tab-mcp-code-size, 2em) / 1.2 var(--_tab-mcp-mono); letter-spacing: 0.12em; font-variant-numeric: tabular-nums; }
97
+ .tab-mcp-help :where(summary) { width: fit-content; cursor: pointer; }
98
+ .tab-mcp-dialog :where(.tab-mcp-help ul, .tab-mcp-apps ul) { list-style: none; padding: 0; display: grid; gap: 0.5rem; }
99
+ .tab-mcp-help :where(ul) { margin-top: 0.75rem; gap: 0.75rem; }
100
+ .tab-mcp-help :where(li) { overflow-wrap: anywhere; }
101
+ .tab-mcp-help :where(strong) { display: block; font-weight: 600; }
102
+ .tab-mcp-command { display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: flex-start; gap: 0.5rem; margin-top: 0.375rem; }
103
+ .tab-mcp-command > :where(pre) { flex: 1 1 16rem; min-width: 0; margin: 0; padding: 0.5rem 0.625rem; font: 0.8125em / 1.5 var(--_tab-mcp-mono); white-space: pre-wrap; word-break: break-all; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
104
+ .tab-mcp-command-note { display: block; margin-top: 0.375rem; }
105
+ .tab-mcp-help :where(code) { font: 0.875em var(--_tab-mcp-mono); padding: 0.125em 0.375em; background: var(--_tab-mcp-surface); border-radius: 0.25rem; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
106
+ .tab-mcp-apps :where(li) { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; padding: 0.5rem 0.5rem 0.5rem 0.75rem; border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
107
+ .tab-mcp-app { min-width: 0; overflow-wrap: anywhere; }
108
+ .tab-mcp-apps-note { margin-top: 0.5rem; }
109
+ .tab-mcp-new-address-question { display: grid; gap: 0.75rem; padding: 1rem; border: 1px solid color-mix(in srgb, var(--_tab-mcp-danger) 50%, transparent); border-radius: var(--_tab-mcp-small-radius); }
110
+ .tab-mcp-connect > :where(.tab-mcp-actions) { gap: 0.75rem; padding-top: 1rem; border-top: 1px solid var(--_tab-mcp-line); }
111
+ .tab-mcp-close-note { flex: 1 1 12rem; }
112
+ .tab-mcp-confirm :where(p bdi) { font-weight: 600; }
113
+ .tab-mcp-arguments { box-sizing: content-box; font: 0.875em / 1.45 var(--_tab-mcp-mono); padding: 0.75rem; background: var(--_tab-mcp-surface); border: 1px solid var(--_tab-mcp-line); border-radius: var(--_tab-mcp-small-radius); }
114
+ .tab-mcp-always-note { font-size: 0.875em; }
115
+ @media (max-width: 30rem) { .tab-mcp-actions > :where(button) { flex: 1 1 auto; } .tab-mcp-connect > :where(.tab-mcp-actions) > :where(button) { flex: 0 0 auto; } }
116
+ @media (prefers-reduced-motion: no-preference) { .tab-mcp-dialog:where([open]) { animation: tab-mcp-in 0.15s ease-out; } }
117
+ @media (forced-colors: active) { html button.tab-mcp-primary { border-color: ButtonText; } }
118
+ @keyframes tab-mcp-in { from { opacity: 0; transform: translateY(0.5rem); } }
119
+ `;
120
+ var sheets = new WeakMap;
121
+ function adoptStyles(doc) {
122
+ try {
123
+ let sheet = sheets.get(doc);
124
+ if (sheet === undefined) {
125
+ const Sheet = doc.defaultView?.CSSStyleSheet;
126
+ if (Sheet === undefined || !Array.isArray(doc.adoptedStyleSheets))
127
+ return;
128
+ sheet = new Sheet;
129
+ sheet.replaceSync(DIALOG_CSS);
130
+ sheets.set(doc, sheet);
131
+ }
132
+ if (!doc.adoptedStyleSheets.includes(sheet))
133
+ doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, sheet];
134
+ } catch {}
135
+ }
136
+ function copyButton(doc, noun, value, select, announce, className, kind) {
137
+ const node = button(doc, "Copy", () => {
138
+ const done = () => {
139
+ node.textContent = "Copied";
140
+ node.setAttribute("aria-label", `Copied ${noun}`);
141
+ announce(`${noun[0]?.toUpperCase()}${noun.slice(1)} copied`);
142
+ };
143
+ const selected = () => {
144
+ select();
145
+ announce("Selected: copy it with Ctrl+C or ⌘C");
146
+ };
147
+ const clipboard = doc.defaultView?.navigator?.clipboard;
148
+ if (clipboard !== undefined)
149
+ clipboard.writeText(value()).then(done, selected);
150
+ else
151
+ selected();
152
+ }, className, kind);
153
+ node.classList.add("tab-mcp-copy");
154
+ const reset = () => {
155
+ node.textContent = "Copy";
156
+ node.setAttribute("aria-label", `Copy ${noun}`);
157
+ };
158
+ reset();
159
+ return { button: node, reset };
160
+ }
161
+ function newDialog(doc, className, title) {
162
+ const id = `tab-mcp-${++counter}`;
163
+ const dialog = el(doc, "dialog", {
164
+ className: `tab-mcp-dialog ${className}`
165
+ });
166
+ dialog.setAttribute("aria-labelledby", `${id}-title`);
167
+ adoptStyles(doc);
168
+ const heading = el(doc, "h2", { id: `${id}-title`, text: title });
169
+ dialog.append(heading);
170
+ return { dialog, heading };
171
+ }
172
+ var INVISIBLE = /[\p{Cf}\p{Default_Ignorable_Code_Point}\p{Co}\p{Cn}\u0080-\u009F\u2028\u2029]/gu;
173
+ function visible(text) {
174
+ return text.replace(INVISIBLE, (ch) => {
175
+ let escaped = "";
176
+ for (let i = 0;i < ch.length; i++)
177
+ escaped += `\\u${ch.charCodeAt(i).toString(16).toUpperCase().padStart(4, "0")}`;
178
+ return escaped;
179
+ });
180
+ }
181
+ function clientLabel(client) {
182
+ const host = visible(client.host || client.id);
183
+ return client.name !== undefined && client.name !== "" ? `${visible(client.name)} (${host})` : host;
184
+ }
185
+ function name(doc, text) {
186
+ const node = el(doc, "bdi", { text });
187
+ node.dir = "ltr";
188
+ return node;
189
+ }
190
+ function showArguments(args) {
191
+ try {
192
+ return visible(JSON.stringify(args, null, 2));
193
+ } catch {
194
+ return;
195
+ }
196
+ }
197
+ var ALLOW_DELAY_MS = 750;
198
+ var MIN_ALLOW_DELAY_MS = 500;
199
+ function allowDelay(ms) {
200
+ const value = typeof ms === "number" && Number.isFinite(ms) && ms < 2 ** 31 ? ms : ALLOW_DELAY_MS;
201
+ return Math.max(MIN_ALLOW_DELAY_MS, value);
202
+ }
203
+ function confirmDialog(request, doc, options = {}) {
204
+ const delay = allowDelay(options.allowDelayMs);
205
+ if (request.signal.aborted)
206
+ return Promise.resolve("deny");
207
+ const title = request.tool.title ?? request.tool.name;
208
+ const { dialog, heading } = newDialog(doc, "tab-mcp-confirm", "");
209
+ heading.append("Allow ", name(doc, title), "?");
210
+ const client = clientLabel(request.client);
211
+ const deny = button(doc, "Don't allow", () => dialog.close("deny"), "tab-mcp-deny", "primary");
212
+ const allow = button(doc, "Allow", () => {
213
+ if (!allow.disabled)
214
+ dialog.close("allow");
215
+ }, "tab-mcp-allow");
216
+ const approve = [allow];
217
+ const actions = el(doc, "div", { className: "tab-mcp-actions" }, deny, " ", allow);
218
+ const extra = [];
219
+ if (options.alwaysAllow === true) {
220
+ const always = button(doc, "Always allow", () => {
221
+ if (!always.disabled)
222
+ dialog.close("always");
223
+ }, "tab-mcp-always");
224
+ approve.push(always);
225
+ actions.append(" ", always);
226
+ extra.push(el(doc, "p", { className: "tab-mcp-always-note" }, "Always allow: ", name(doc, title), " runs without asking for ", name(doc, client), " until you reload this page or disconnect it."));
227
+ }
228
+ const enable = (on) => {
229
+ for (const b of approve)
230
+ b.disabled = !on;
231
+ };
232
+ enable(false);
233
+ const shown = showArguments(request.arguments);
234
+ const args = el(doc, "pre", {
235
+ className: "tab-mcp-arguments",
236
+ text: shown ?? "The arguments cannot be shown, so this call cannot be allowed."
237
+ });
238
+ if (shown === undefined) {
239
+ for (const node of [...approve, ...extra])
240
+ node.hidden = true;
241
+ }
242
+ args.dir = "ltr";
243
+ args.style.setProperty("white-space", "pre-wrap");
244
+ args.style.setProperty("overflow-wrap", "anywhere");
245
+ args.style.setProperty("max-height", "var(--tab-mcp-arguments-height, 12rem)");
246
+ args.style.setProperty("overflow", "auto");
247
+ args.hidden = Object.keys(request.arguments).length === 0;
248
+ dialog.append(el(doc, "p", {}, name(doc, client), " wants to run ", name(doc, title), "."), el(doc, "p", {
249
+ className: "tab-mcp-description",
250
+ text: request.tool.description
251
+ }), args, actions, ...extra);
252
+ doc.body.append(dialog);
253
+ const view = doc.defaultView;
254
+ return new Promise((resolve) => {
255
+ const onAbort = () => dialog.close("deny");
256
+ request.signal.addEventListener("abort", onAbort, { once: true });
257
+ let timer;
258
+ const arm = (event) => {
259
+ clearTimeout(timer);
260
+ timer = undefined;
261
+ enable(false);
262
+ if (event?.type === "blur" || doc.visibilityState === "hidden" || !doc.hasFocus())
263
+ return;
264
+ timer = setTimeout(() => enable(true), delay);
265
+ };
266
+ doc.addEventListener("visibilitychange", arm);
267
+ view?.addEventListener("focus", arm);
268
+ view?.addEventListener("blur", arm);
269
+ dialog.addEventListener("close", () => {
270
+ clearTimeout(timer);
271
+ doc.removeEventListener("visibilitychange", arm);
272
+ view?.removeEventListener("focus", arm);
273
+ view?.removeEventListener("blur", arm);
274
+ request.signal.removeEventListener("abort", onAbort);
275
+ const value = dialog.returnValue;
276
+ dialog.remove();
277
+ resolve(value === "allow" || value === "always" ? value : "deny");
278
+ }, { once: true });
279
+ dialog.showModal();
280
+ deny.focus();
281
+ arm();
282
+ });
283
+ }
284
+ var allowedAlways = new WeakMap;
285
+ function confirmInPage(mcp, request, doc, options) {
286
+ const { grantId } = request.client;
287
+ const tool = request.tool.name;
288
+ if (allowedAlways.get(mcp)?.has(grantId, tool))
289
+ return Promise.resolve(true);
290
+ return confirmDialog(request, doc, options).then((answer) => {
291
+ if (answer === "always" && mcp.grants.some((g) => g.grantId === grantId)) {
292
+ let allowed = allowedAlways.get(mcp);
293
+ if (allowed === undefined) {
294
+ const kept = new AlwaysAllowed;
295
+ mcp.addEventListener("grantschange", (e) => kept.keepGrants(e.detail.grants.map((g) => g.grantId)));
296
+ mcp.addEventListener("toolschange", (e) => kept.keepTools(e.detail.tools));
297
+ allowedAlways.set(mcp, kept);
298
+ allowed = kept;
299
+ }
300
+ allowed.add(grantId, tool);
301
+ }
302
+ return answer !== "deny";
303
+ });
304
+ }
305
+ function commandName(name) {
306
+ const label = name.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/-{2,}/g, "-").replace(/^-+|-+$/g, "").slice(0, 40).replace(/-+$/, "");
307
+ return label === "" ? "tools" : label;
308
+ }
309
+ function commandAddress(address) {
310
+ try {
311
+ const href = new URL(address).href;
312
+ return /^https?:\/\/[A-Za-z0-9.:[\]/_~-]+$/.test(href) ? href : undefined;
313
+ } catch {
314
+ return;
315
+ }
316
+ }
317
+ var CODEX_FEATURE = "mcp_2026_07_28";
318
+ function instructions(name, address) {
319
+ const n = commandName(name);
320
+ const a = commandAddress(address);
321
+ return [
322
+ [
323
+ "Claude (claude.ai and the desktop app)",
324
+ "Settings → Connectors → Add custom connector, then paste the address."
325
+ ],
326
+ [
327
+ "ChatGPT",
328
+ "Settings → Apps and connectors → Advanced settings: turn on developer mode, then Create and paste the address."
329
+ ],
330
+ a === undefined ? ["Claude Code", "Add the address with claude mcp add --transport http."] : ["Claude Code", "Run:", `claude mcp add --transport http ${n} ${a}`],
331
+ [
332
+ "Codex",
333
+ ...a === undefined ? [
334
+ `Run codex features enable ${CODEX_FEATURE}, then add the address with codex mcp add --url.`,
335
+ undefined
336
+ ] : [
337
+ "Run:",
338
+ `codex features enable ${CODEX_FEATURE}
339
+ codex mcp add ${n} --url ${a}`
340
+ ],
341
+ [
342
+ "Start a new chat in Codex after it connects. The first line turns on a feature for all of Codex's MCP servers; ",
343
+ `codex features disable ${CODEX_FEATURE}`,
344
+ " turns it off."
345
+ ]
346
+ ]
347
+ ];
348
+ }
349
+ function listOf(items) {
350
+ return items.length < 2 ? items[0] ?? "" : `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
351
+ }
352
+ var watched = new WeakMap;
353
+ function watchReplaced(mcp, doc) {
354
+ const known = watched.has(mcp);
355
+ watched.set(mcp, doc);
356
+ if (known)
357
+ return;
358
+ mcp.addEventListener("replaced", (event) => {
359
+ const current = watched.get(mcp);
360
+ if (current !== undefined && mcp.state === "disconnected")
361
+ replacedNotice(mcp, current, event.detail.sibling);
362
+ });
363
+ }
364
+ function replacedNotice(mcp, doc, sibling) {
365
+ doc.querySelector("dialog.tab-mcp-replaced")?.close();
366
+ const { dialog } = newDialog(doc, "tab-mcp-replaced", sibling ? "Opened in another tab" : "Another tab or program took over this page");
367
+ const status = el(doc, "p", { className: "tab-mcp-status" });
368
+ status.setAttribute("role", "status");
369
+ status.hidden = true;
370
+ const close = button(doc, "Close", () => dialog.close(), "tab-mcp-replaced-close", sibling ? "primary" : undefined);
371
+ const actions = el(doc, "div", { className: "tab-mcp-actions" }, close);
372
+ let first = close;
373
+ if (!sibling) {
374
+ const fresh = button(doc, "New address", () => {
375
+ fresh.disabled = true;
376
+ status.hidden = false;
377
+ status.textContent = "Getting a new address…";
378
+ mcp.newAddress().then(() => {
379
+ dialog.close();
380
+ mcp.showConnectDialog({ document: doc }).catch(() => {});
381
+ }, (error) => {
382
+ fresh.disabled = false;
383
+ status.textContent = error?.reason === "replaced" ? "It was taken over again before its AI apps were disconnected. Try again." : `Could not connect: ${error instanceof Error ? error.message : String(error)}. Try again, or close this.`;
384
+ });
385
+ }, "tab-mcp-new-address", "primary");
386
+ actions.append(" ", fresh);
387
+ first = fresh;
388
+ }
389
+ dialog.append(el(doc, "p", {
390
+ className: "tab-mcp-description",
391
+ text: sibling ? "This page is open in another tab, and your AI apps now use that one. To use them here, connect this page again." : "Your AI apps now reach that tab or program instead of this page. If you did not open this page in another tab or browser, someone may have copied its address. A new address disconnects every AI app from the old one; then add the new address to your AI apps again."
392
+ }), status, actions);
393
+ dialog.addEventListener("close", () => dialog.remove(), { once: true });
394
+ doc.body.append(dialog);
395
+ dialog.showModal();
396
+ first.focus();
397
+ }
398
+ function showConnectDialog(mcp, connecting, doc, serverName = "tools") {
399
+ const { dialog } = newDialog(doc, "tab-mcp-connect", "Connect your AI app");
400
+ dialog.tabIndex = -1;
401
+ const status = el(doc, "p", {
402
+ className: "tab-mcp-status",
403
+ text: "Connecting…"
404
+ });
405
+ status.setAttribute("role", "status");
406
+ const live = el(doc, "span", { className: "tab-mcp-announce" });
407
+ live.setAttribute("aria-live", "polite");
408
+ for (const [property, value] of [
409
+ ["position", "absolute"],
410
+ ["width", "1px"],
411
+ ["height", "1px"],
412
+ ["overflow", "hidden"],
413
+ ["clip-path", "inset(50%)"],
414
+ ["white-space", "nowrap"]
415
+ ])
416
+ live.style.setProperty(property, value);
417
+ const announce = (text) => {
418
+ live.textContent = live.textContent === text ? `${text} ` : text;
419
+ };
420
+ const addressId = `tab-mcp-${++counter}-address`;
421
+ const label = el(doc, "label", { text: "Add this address to your AI app:" });
422
+ label.htmlFor = addressId;
423
+ const address = el(doc, "input", {
424
+ id: addressId,
425
+ className: "tab-mcp-address-input"
426
+ });
427
+ address.readOnly = true;
428
+ const { button: copy, reset: resetCopy } = copyButton(doc, "address", () => address.value, () => address.select(), announce, "tab-mcp-address-copy", "primary");
429
+ const fresh = button(doc, "New address", () => askNewAddress(), "tab-mcp-new-address");
430
+ const addressBox = el(doc, "div", { className: "tab-mcp-address" }, label, address, " ", copy, " ", fresh);
431
+ const confirmId = `tab-mcp-${++counter}-new-address`;
432
+ const confirmText = el(doc, "p", { id: confirmId });
433
+ const cancelNew = button(doc, "Cancel", () => {
434
+ confirmBox.hidden = true;
435
+ fresh.focus();
436
+ });
437
+ const confirmNew = button(doc, "Disconnect and get new address", () => getNewAddress(), "tab-mcp-new-address-confirm", "danger");
438
+ const confirmBox = el(doc, "div", { className: "tab-mcp-new-address-question" }, confirmText, el(doc, "div", { className: "tab-mcp-actions" }, cancelNew, " ", confirmNew));
439
+ confirmBox.setAttribute("role", "group");
440
+ confirmBox.setAttribute("aria-labelledby", confirmId);
441
+ confirmBox.hidden = true;
442
+ const code = el(doc, "strong", { className: "tab-mcp-code-value" });
443
+ code.style.setProperty("user-select", "all");
444
+ code.style.setProperty("-webkit-user-select", "all");
445
+ const { button: copyCode, reset: resetCopyCode } = copyButton(doc, "code", () => code.textContent ?? "", () => doc.getSelection()?.selectAllChildren(code), announce, "tab-mcp-code-copy");
446
+ const codeSentence = el(doc, "span", {}, "When asked, enter the code ", code);
447
+ codeSentence.setAttribute("aria-live", "polite");
448
+ const codeLine = el(doc, "p", { className: "tab-mcp-code" }, codeSentence, " ", copyCode, el(doc, "br"), el(doc, "small", {
449
+ className: "tab-mcp-code-note",
450
+ text: "A new code appears every 2 minutes and after each use."
451
+ }));
452
+ const signInLine = el(doc, "p", {
453
+ className: "tab-mcp-sign-in",
454
+ text: "When your AI app asks, sign in with the account you use on this page. No code is needed."
455
+ });
456
+ const steps = el(doc, "ul");
457
+ const help = el(doc, "details", { className: "tab-mcp-help" }, el(doc, "summary", { text: "How do I add it?" }), steps);
458
+ const appList = el(doc, "ul");
459
+ const apps = el(doc, "div", { className: "tab-mcp-apps" }, el(doc, "h3", { text: "Connected" }), appList);
460
+ const closeNote = el(doc, "small", {
461
+ className: "tab-mcp-close-note",
462
+ text: "Closing this stops the code. The address stays the same: to connect later, open this again from the page."
463
+ });
464
+ const close = button(doc, "Close", () => dialog.close(), "tab-mcp-close");
465
+ const done = button(doc, "Done", () => dialog.close(), "tab-mcp-done", "primary");
466
+ done.hidden = true;
467
+ const actions = el(doc, "div", { className: "tab-mcp-actions" }, closeNote, " ", close, done);
468
+ let paired = false;
469
+ const pairedGrants = new Set;
470
+ const renderCloseNote = () => {
471
+ closeNote.hidden = paired || codeLine.hidden;
472
+ };
473
+ const setPaired = (value) => {
474
+ paired = value;
475
+ if (!paired)
476
+ pairedGrants.clear();
477
+ const focused = doc.activeElement;
478
+ renderCloseNote();
479
+ close.hidden = paired;
480
+ done.hidden = !paired;
481
+ if (paired && focused === close)
482
+ done.focus();
483
+ if (!paired && focused === done)
484
+ close.focus();
485
+ };
486
+ const closer = () => paired ? done : close;
487
+ dialog.append(status, addressBox, confirmBox, codeLine, signInLine, help, apps, actions, live);
488
+ addressBox.hidden = true;
489
+ codeLine.hidden = true;
490
+ signInLine.hidden = true;
491
+ help.hidden = true;
492
+ apps.hidden = true;
493
+ let renewing = false;
494
+ const renderQuestion = () => {
495
+ const names = mcp.grants.map((g) => clientLabel(g.client));
496
+ if (names.length === 0)
497
+ return false;
498
+ confirmText.textContent = `A new address disconnects ${listOf(names)}. To use this page again, add the new address to ${names.length === 1 ? "it" : "them"}.`;
499
+ return true;
500
+ };
501
+ const askNewAddress = () => {
502
+ if (mcp.state !== "connected")
503
+ return;
504
+ if (!renderQuestion()) {
505
+ getNewAddress();
506
+ return;
507
+ }
508
+ confirmBox.hidden = false;
509
+ cancelNew.focus();
510
+ };
511
+ const getNewAddress = () => {
512
+ setPaired(false);
513
+ close.focus();
514
+ confirmBox.hidden = true;
515
+ renewing = true;
516
+ status.textContent = "Getting a new address…";
517
+ mcp.newAddress().then(() => {
518
+ renewing = false;
519
+ status.textContent = "This is a new address. Add it to your AI app again.";
520
+ copy.focus();
521
+ }, (error) => {
522
+ renewing = false;
523
+ status.textContent = `Could not connect: ${error instanceof Error ? error.message : String(error)}. Close this and connect again.`;
524
+ closer().focus();
525
+ });
526
+ };
527
+ const renderSession = (session) => {
528
+ if (session === undefined) {
529
+ addressBox.hidden = true;
530
+ confirmBox.hidden = true;
531
+ help.hidden = true;
532
+ return;
533
+ }
534
+ address.value = session.address;
535
+ resetCopy();
536
+ const appName = session.appName ?? mcp.appName;
537
+ if (!renewing)
538
+ status.textContent = appName !== undefined ? `${appName} is ready for your AI app.` : "This page is ready for your AI app.";
539
+ fresh.disabled = mcp.state !== "connected";
540
+ addressBox.hidden = false;
541
+ if (doc.activeElement === dialog)
542
+ copy.focus();
543
+ help.hidden = false;
544
+ steps.replaceChildren(...instructions(serverName, session.address).map(([app, how, command, note]) => {
545
+ const item = el(doc, "li", {}, el(doc, "strong", { text: `${app}:` }), ` ${how}`);
546
+ if (command !== undefined) {
547
+ const text = el(doc, "pre", { text: command });
548
+ text.dir = "ltr";
549
+ const { button: copyCommand } = copyButton(doc, `${app} command`, () => text.textContent ?? "", () => doc.getSelection()?.selectAllChildren(text), announce, "tab-mcp-command-copy");
550
+ item.append(el(doc, "div", { className: "tab-mcp-command" }, text, copyCommand));
551
+ }
552
+ if (note !== undefined)
553
+ item.append(el(doc, "small", { className: "tab-mcp-command-note" }, ...note.map((part, i) => i % 2 === 1 ? el(doc, "code", { text: part }) : part)));
554
+ return item;
555
+ }));
556
+ };
557
+ const renderCode = () => {
558
+ const current = mcp.pairingCode;
559
+ const oidc = mcp.session?.pairing === "oidc";
560
+ if (code.textContent !== (current?.code ?? ""))
561
+ resetCopyCode();
562
+ code.textContent = current?.code ?? "";
563
+ codeLine.hidden = oidc || current === undefined;
564
+ signInLine.hidden = !oidc;
565
+ renderCloseNote();
566
+ };
567
+ const appsNote = el(doc, "p", { className: "tab-mcp-apps-note" });
568
+ apps.append(appsNote);
569
+ const known = new Set(mcp.grants.map((g) => g.grantId));
570
+ let session = mcp.session;
571
+ const renderGrants = () => {
572
+ if (mcp.session !== session) {
573
+ session = mcp.session;
574
+ for (const grant of mcp.grants)
575
+ known.add(grant.grantId);
576
+ }
577
+ for (const grant of mcp.grants) {
578
+ if (known.has(grant.grantId))
579
+ continue;
580
+ known.add(grant.grantId);
581
+ status.textContent = `${clientLabel(grant.client)} is connected.`;
582
+ pairedGrants.add(grant.grantId);
583
+ if (!paired)
584
+ setPaired(true);
585
+ }
586
+ if (paired && !mcp.grants.some((g) => pairedGrants.has(g.grantId))) {
587
+ setPaired(false);
588
+ }
589
+ const connected = mcp.state === "connected";
590
+ appList.replaceChildren(...mcp.grants.map((grant) => {
591
+ const disconnect = button(doc, "Disconnect", () => {
592
+ if (!mcp.revoke(grant.grantId))
593
+ renderGrants();
594
+ }, "tab-mcp-disconnect", "danger");
595
+ disconnect.disabled = !connected;
596
+ return el(doc, "li", {}, el(doc, "span", {
597
+ className: "tab-mcp-app",
598
+ text: `${clientLabel(grant.client)} connected `
599
+ }), disconnect);
600
+ }));
601
+ appsNote.textContent = connected ? "" : "To disconnect an app, connect this page again first.";
602
+ appsNote.hidden = connected;
603
+ apps.hidden = mcp.grants.length === 0;
604
+ if (!confirmBox.hidden && !renderQuestion()) {
605
+ confirmBox.hidden = true;
606
+ fresh.focus();
607
+ }
608
+ };
609
+ const onDisconnected = (event) => {
610
+ if (event.detail.reason !== "new_address")
611
+ status.textContent = "Disconnected. AI apps can use this page again after you connect.";
612
+ renderSession(undefined);
613
+ renderGrants();
614
+ renderCode();
615
+ };
616
+ const onConnected = () => {
617
+ renderSession(mcp.session);
618
+ renderGrants();
619
+ renderCode();
620
+ };
621
+ const onReplaced = () => dialog.close();
622
+ watchReplaced(mcp, doc);
623
+ mcp.addEventListener("pairingcode", renderCode);
624
+ mcp.addEventListener("grantschange", renderGrants);
625
+ mcp.addEventListener("connected", onConnected);
626
+ mcp.addEventListener("disconnected", onDisconnected);
627
+ mcp.addEventListener("replaced", onReplaced);
628
+ doc.body.append(dialog);
629
+ mcp.startPairing();
630
+ renderGrants();
631
+ renderCode();
632
+ renderSession(mcp.session);
633
+ connecting.then((session) => renderSession(session), (error) => {
634
+ status.textContent = `Could not connect: ${error instanceof Error ? error.message : String(error)}`;
635
+ });
636
+ return new Promise((resolve) => {
637
+ dialog.addEventListener("close", () => {
638
+ mcp.stopPairing();
639
+ mcp.removeEventListener("pairingcode", renderCode);
640
+ mcp.removeEventListener("grantschange", renderGrants);
641
+ mcp.removeEventListener("connected", onConnected);
642
+ mcp.removeEventListener("disconnected", onDisconnected);
643
+ mcp.removeEventListener("replaced", onReplaced);
644
+ dialog.remove();
645
+ resolve();
646
+ }, { once: true });
647
+ dialog.showModal();
648
+ if (!addressBox.hidden)
649
+ copy.focus();
650
+ else
651
+ dialog.focus();
652
+ });
653
+ }
654
+ export {
655
+ confirmInPage,
656
+ showConnectDialog
657
+ };
658
+
659
+ //# debugId=CB9E883879351AA764756E2164756E21
660
+ //# sourceMappingURL=ui-xkw7g2v2.js.map