| 1 | import { useBeforeLeave, useLocation, useParams } from "@solidjs/router"; |
| 2 | import { parseResponse } from "hono/client"; |
| 3 | import Eye from "lucide-solid/icons/eye"; |
| 4 | import GitBranch from "lucide-solid/icons/git-branch"; |
| 5 | import MonitorCheck from "lucide-solid/icons/monitor-check"; |
| 6 | import MousePointer2 from "lucide-solid/icons/mouse-pointer-2"; |
| 7 | import RefreshCw from "lucide-solid/icons/refresh-cw"; |
| 8 | import { createEffect, createResource, createSignal, For, type JSX, on, onCleanup, onMount, Show } from "solid-js"; |
| 9 | import { api, reason } from "../api.ts"; |
| 10 | import { showConfirmDialog } from "../components/Dialog.tsx"; |
| 11 | import snowflake from "../snowflake.svg"; |
| 12 | import { MCPAccess } from "./MCPAccess.tsx"; |
| 13 | import type { Access, Consent } from "../types/mcp.ts"; |
| 14 | import "./MCP.css"; |
| 15 | |
| 16 | /** Each requested ability as one line; `strong` ones act on the VMs rather than reading them. */ |
| 17 | function abilities(data: Consent) { |
| 18 | const has = (scope: string) => data.scopes.includes(scope); |
| 19 | const out: [JSX.Element, JSX.Element, boolean][] = [[<Eye size={16} />, |
| 20 | data.catalog === "computer" ? "See VM status, screenshots, and on-screen controls" : data.catalog === "shale" ? "Read issues and comments" : "Read logs and traces", false]]; |
| 21 | if (has("computer:control")) out.push([<MousePointer2 size={16} />, "Control the desktop, run commands, and transfer files in the VMs you choose", true]); |
| 22 | if (has("computer:create")) out.push([<MonitorCheck size={16} />, <>Create up to 2 Linux or Windows test VMs |
| 23 | <small>Each gets up to 4 vCPUs, 8 GiB memory, and an 80 GiB disk. They expire after an idle hour; extensions last up to 24 hours</small></>, true]); |
| 24 | if (has("shale:write")) out.push([<GitBranch size={16} />, "Create issues, comment, and edit titles and status", false]); |
| 25 | if (has("offline_access")) out.push([<RefreshCw size={16} />, "Stay connected without signing in again", false]); |
| 26 | return out; |
| 27 | } |
| 28 | |
| 29 | export function MCPConsent() { |
| 30 | const params = useParams<{ id: string }>(); |
| 31 | const location = useLocation(); |
| 32 | const request = () => params.id || new URLSearchParams(location.search).get("request") || ""; |
| 33 | const [consent, { refetch }] = createResource(request, |
| 34 | (id) => parseResponse(api.mcp.consent[":id"].$get({ param: { id } }))); |
| 35 | const details = () => consent.state === "ready" ? consent.latest : undefined; |
| 36 | const [selection, setSelection] = createSignal<Access>(); |
| 37 | const access = () => selection() ?? (details()?.catalog === "shale" ? "all" : []); |
| 38 | const [busy, setBusy] = createSignal(false); |
| 39 | const [error, setError] = createSignal(""); |
| 40 | let leaving = false; |
| 41 | createEffect(on(request, () => { setSelection(undefined); setError(""); })); |
| 42 | const redirect = (target: string) => { leaving = true; window.location.assign(target); }; |
| 43 | const answer = async (deny: boolean) => { |
| 44 | setBusy(true); setError(""); |
| 45 | try { |
| 46 | const result = await parseResponse(api.mcp.consent[":id"].$post({ param: { id: request() }, json: deny ? { deny: true } : { resources: access() } })); |
| 47 | redirect(result.redirect); |
| 48 | } catch (error) { setError(reason(error)); setBusy(false); throw error; } |
| 49 | }; |
| 50 | useBeforeLeave((event) => { |
| 51 | if (leaving) return; |
| 52 | event.preventDefault(); |
| 53 | if (busy()) return; |
| 54 | showConfirmDialog({ title: "Decline this connection?", description: "The client will receive no access.", confirmLabel: "Decline", onConfirm: () => answer(true) }); |
| 55 | }); |
| 56 | onMount(() => { |
| 57 | const guard = (event: BeforeUnloadEvent) => { if (!leaving) event.preventDefault(); }; |
| 58 | window.addEventListener("beforeunload", guard); |
| 59 | onCleanup(() => window.removeEventListener("beforeunload", guard)); |
| 60 | }); |
| 61 | const linkShale = async () => { |
| 62 | setBusy(true); setError(""); |
| 63 | try { |
| 64 | const result = await parseResponse(api.mcp.shale.$post({ json: { request: request() } })); |
| 65 | redirect(result.redirect); |
| 66 | } catch (error) { setError(reason(error)); setBusy(false); } |
| 67 | }; |
| 68 | const create = () => !!details()?.scopes.includes("computer:create"); |
| 69 | const empty = () => access() !== "all" && !access().length && !create(); |
| 70 | return <main class="mcp-authorization"> |
| 71 | <section class="mcp-approval" aria-labelledby="connection-title"> |
| 72 | <div class="mcp-approval-brand"><img src={snowflake} alt="" />the snow globe</div> |
| 73 | <Show when={details()} fallback={<> |
| 74 | <h1 id="connection-title">{consent.state === "errored" ? "Connection unavailable" : "Loading connection…"}</h1> |
| 75 | <Show when={consent.state === "errored"} fallback={<div class="skeleton" style={{ height: "120px" }} aria-label="Loading connection" />}> |
| 76 | <p class="error" role="alert">{reason(consent.error)}</p> |
| 77 | <div class="mcp-actions"><button class="button" onClick={() => refetch()}>Retry</button><button class="button" onClick={() => redirect("/mcp")}>Close request</button></div> |
| 78 | </Show> |
| 79 | </>}> |
| 80 | {(data) => <> |
| 81 | <h1 id="connection-title">Connect {data().client}</h1> |
| 82 | <p class="mcp-approval-intro">{data().catalog === "computer" ? "Grant access to your virtual machines." : data().catalog === "shale" ? "Grant access to Shale issues." : "Grant access to logs and traces."}</p> |
| 83 | <dl class="mcp-request-identity"><div><dt>Signed in as</dt><dd>{data().account}</dd></div><div><dt>Returns to</dt><dd>{data().redirectHost}</dd></div></dl> |
| 84 | <div class="mcp-permissions"><h2>{data().client} will be able to</h2> |
| 85 | <ul><For each={abilities(data())}>{([icon, text, strong]) => <li classList={{ strong }}>{icon}<span>{text}</span></li>}</For></ul> |
| 86 | </div> |
| 87 | <Show when={data().linked} fallback={<div class="mcp-link-step"><h2>Link your Shale account</h2><p>Sign in to Shale, then return here to approve access.</p><button class="button primary" disabled={busy()} onClick={linkShale}>Link account</button></div>}> |
| 88 | <MCPAccess catalog={data().catalog} resources={data().resources} value={access()} onChange={setSelection} disabled={busy()} create={create()} /> |
| 89 | <Show when={data().resourceError}><p class="error" role="alert">{data().resourceError} <button class="button small" disabled={busy()} onClick={() => refetch()}>Retry</button></p></Show> |
| 90 | <Show when={data().resources.length || create() || data().catalog === "shale"}><p class="hint">You can change this later in AI / MCP settings</p></Show> |
| 91 | </Show> |
| 92 | </>} |
| 93 | </Show> |
| 94 | <Show when={error()}><p class="error" role="alert">{error()}</p></Show> |
| 95 | <Show when={details()}><div class="mcp-approval-actions"> |
| 96 | <button class="button" disabled={busy()} onClick={() => answer(true).catch(() => {})}>Decline</button> |
| 97 | <Show when={details()?.linked}><span class="spacer" /><Show when={empty() && details()?.resources.length}><span class="hint">Choose at least one {details()?.catalog === "computer" ? "VM" : details()?.catalog === "shale" ? "repository" : "service"}</span></Show> |
| 98 | <button class="button primary" disabled={busy() || empty()} aria-busy={busy()} onClick={() => answer(false).catch(() => {})}>Allow access</button></Show> |
| 99 | </div></Show> |
| 100 | </section> |
| 101 | </main>; |
| 102 | } |