| 1 | import { A, useNavigate, useParams } from "@solidjs/router"; |
| 2 | import { parseResponse } from "hono/client"; |
| 3 | import ChevronRight from "lucide-solid/icons/chevron-right"; |
| 4 | import Eye from "lucide-solid/icons/eye"; |
| 5 | import GitBranch from "lucide-solid/icons/git-branch"; |
| 6 | import Lock from "lucide-solid/icons/lock"; |
| 7 | import MonitorCheck from "lucide-solid/icons/monitor-check"; |
| 8 | import MousePointer2 from "lucide-solid/icons/mouse-pointer-2"; |
| 9 | import RefreshCw from "lucide-solid/icons/refresh-cw"; |
| 10 | import { createEffect, createResource, createSignal, For, type JSX, Show } from "solid-js"; |
| 11 | import { api, reason } from "../api.ts"; |
| 12 | import { Ago } from "../components/Ago.tsx"; |
| 13 | import { showConfirmDialog } from "../components/Dialog.tsx"; |
| 14 | import { Loaded } from "../components/Loaded.tsx"; |
| 15 | import { StatusLabel } from "../components/Status.tsx"; |
| 16 | import { TabBar } from "../components/TabBar.tsx"; |
| 17 | import { toast } from "../components/Toast.tsx"; |
| 18 | import { AI, ClientMark } from "./AI.tsx"; |
| 19 | import { CopyField } from "../components/Copy.tsx"; |
| 20 | import { MCPAccess } from "./MCPAccess.tsx"; |
| 21 | import type { Me } from "../types/model.ts"; |
| 22 | import type { Access, Catalog, Connections } from "../types/mcp.ts"; |
| 23 | import "./MCP.css"; |
| 24 | |
| 25 | const descriptions: Record<Catalog, string> = { |
| 26 | computer: "Inspect and control selected virtual machines, run commands, transfer files, and create disposable test VMs.", |
| 27 | shale: "Read and edit issues across your Shale repositories.", |
| 28 | observability: "Read logs and traces from your services.", |
| 29 | }; |
| 30 | const VIEWING = "Switch back to your account to change access"; |
| 31 | |
| 32 | /** What a connection's scopes let it do, strongest first; scopes come from the client's request, never from settings. */ |
| 33 | function abilities(client: Connections["connections"][number]) { |
| 34 | const has = (scope: string) => client.scopes.includes(scope); |
| 35 | const out: [JSX.Element, string, boolean][] = []; |
| 36 | if (client.catalog === "computer") out.push(has("computer:control") ? [<MousePointer2 size={12} />, "Control, commands, and files", true] : [<Eye size={12} />, "View only", false]); |
| 37 | if (has("computer:create")) out.push([<MonitorCheck size={12} />, "Creates test VMs", true]); |
| 38 | if (client.catalog === "shale") out.push(has("shale:write") ? [<GitBranch size={12} />, "Edits issues", false] : [<Eye size={12} />, "Read only", false]); |
| 39 | if (client.catalog === "observability") out.push([<Eye size={12} />, "Read only", false]); |
| 40 | if (has("offline_access")) out.push([<RefreshCw size={12} />, "Stays signed in", false]); |
| 41 | return out; |
| 42 | } |
| 43 | |
| 44 | export function MCP(props: { me: Me }) { |
| 45 | const params = useParams<{ catalog?: string }>(); |
| 46 | const navigate = useNavigate(); |
| 47 | createEffect(() => { if (!params.catalog) navigate("/mcp/settings/ai", { replace: true }); }); |
| 48 | const [overview, { refetch }] = createResource(() => parseResponse(api.mcp.$get())); |
| 49 | const [shale, { refetch: retryShale }] = createResource(() => params.catalog === "shale", |
| 50 | () => parseResponse(api.mcp.shale.$get())); |
| 51 | const [editing, setEditing] = createSignal(""); |
| 52 | const [selection, setSelection] = createSignal<Access>([]); |
| 53 | const [saveError, setSaveError] = createSignal(""); |
| 54 | const [connection, { refetch: retryConnection, mutate: clearConnection }] = createResource(() => editing() || false, async (id) => { |
| 55 | clearConnection(undefined); |
| 56 | const result = await parseResponse(api.mcp.connections[":id"].$get({ param: { id } })); |
| 57 | if (editing() === id) setSelection(result.selected === "all" ? "all" : result.selected.filter((id) => result.resources.some((resource) => resource.id === id))); |
| 58 | return result; |
| 59 | }); |
| 60 | const [busy, setBusy] = createSignal(false); |
| 61 | createEffect(() => { params.catalog; setEditing(""); }); |
| 62 | const locked = () => busy() || props.me.viewing; |
| 63 | const linkShale = async () => { |
| 64 | setBusy(true); |
| 65 | try { const result = await parseResponse(api.mcp.shale.$post({ json: {} })); window.location.assign(result.redirect); } |
| 66 | catch (error) { toast(reason(error)); setBusy(false); } |
| 67 | }; |
| 68 | return <div class="page mcp-page"> |
| 69 | <header class="page-head"><h1>AI / MCP</h1></header> |
| 70 | <Loaded data={overview} what="AI / MCP settings" retry={refetch}> |
| 71 | {(data) => { |
| 72 | const catalog = () => data().catalogs.find((catalog) => catalog.id === params.catalog); |
| 73 | const connections = (id: Catalog) => data().connections.filter((connection) => connection.catalog === id); |
| 74 | return <> |
| 75 | <div class="mcp-tabs"><TabBar label="AI / MCP settings"> |
| 76 | <A href="/mcp/settings/ai" end>Local AI</A> |
| 77 | <For each={data().catalogs}>{(catalog) => <A href={`/mcp/settings/${catalog.id}`} end> |
| 78 | {catalog.name}<Show when={connections(catalog.id).length}>{(count) => <> <span class="count">{count()}</span></>}</Show> |
| 79 | </A>}</For> |
| 80 | </TabBar></div> |
| 81 | <Show when={params.catalog === "ai"}><AI viewing={props.me.viewing} /></Show> |
| 82 | <Show when={params.catalog && params.catalog !== "ai" && !catalog()}><p class="empty">No connector here.</p></Show> |
| 83 | <Show when={catalog()}>{(current) => <> |
| 84 | <section class="mcp-section"> |
| 85 | <p>{descriptions[current().id]}</p> |
| 86 | <h2 class="mcp-label">Connector URL</h2> |
| 87 | <CopyField value={current().endpoint} label="connector URL" /> |
| 88 | <p class="hint">Add this URL to your AI client’s MCP settings, then approve access</p> |
| 89 | </section> |
| 90 | <Show when={current().id === "shale"}> |
| 91 | <section class="mcp-section"> |
| 92 | <div class="mcp-heading"><h2>Shale account</h2> |
| 93 | <Show when={data().shale}>{(linked) => <span class="stat"><StatusLabel health="healthy">Linked <Ago t={linked().linkedAt} /></StatusLabel></span>}</Show> |
| 94 | <Show when={data().shale}><div class="mcp-actions"> |
| 95 | <button class="button small" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={linkShale}>Relink</button> |
| 96 | <button class="button small danger" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={(event) => showConfirmDialog({ |
| 97 | title: "Unlink Shale?", description: "Every Shale client connection will lose access.", confirmLabel: "Unlink", destructive: true, returnFocus: event.currentTarget, |
| 98 | onConfirm: async () => { await parseResponse(api.mcp.shale.$delete()); await refetch(); await retryShale(); }, |
| 99 | })}>Unlink</button> |
| 100 | </div></Show> |
| 101 | </div> |
| 102 | <Loaded data={shale} what="Shale repositories" retry={retryShale}> |
| 103 | {(account) => <Show when={account().linked} fallback={<div class="mcp-link"> |
| 104 | <p>Link your Shale account to connect clients.</p> |
| 105 | <button class="button primary" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={linkShale}>Link account</button> |
| 106 | </div>}> |
| 107 | <Show when={account().resources.length} fallback={<p class="hint">Your account has no repositories yet</p>}> |
| 108 | <details class="mcp-repositories"><summary><ChevronRight class="chevron" />{account().resources.length} repositories available</summary> |
| 109 | <ul class="mcp-repository-list"><For each={account().resources}>{(resource) => <li><span>{resource.name}</span><span class="muted">{resource.description}</span></li>}</For></ul> |
| 110 | </details> |
| 111 | </Show> |
| 112 | </Show>} |
| 113 | </Loaded> |
| 114 | </section> |
| 115 | </Show> |
| 116 | <section class="mcp-section flush"> |
| 117 | <h2>Connected clients</h2> |
| 118 | <Show when={connections(current().id).length} fallback={<div class="mcp-empty"> |
| 119 | <p class="mcp-empty-title">No clients connected yet.</p> |
| 120 | <p>Clients appear here after you approve them.</p> |
| 121 | </div>}> |
| 122 | <ul class="mcp-clients"><For each={connections(current().id)}>{(client) => { |
| 123 | let editButton!: HTMLButtonElement; |
| 124 | let editor!: HTMLDivElement; |
| 125 | const open = () => editing() === client.id; |
| 126 | const create = client.scopes.includes("computer:create"); |
| 127 | const close = () => { setEditing(""); editButton.focus(); }; |
| 128 | const save = async () => { |
| 129 | setBusy(true); setSaveError(""); |
| 130 | try { |
| 131 | await parseResponse(api.mcp.connections[":id"].$post({ param: { id: client.id }, json: { resources: selection() } })); |
| 132 | await refetch(); close(); |
| 133 | } catch (error) { setSaveError(reason(error)); } |
| 134 | finally { setBusy(false); } |
| 135 | }; |
| 136 | const empty = () => selection() !== "all" && !selection().length && !create; |
| 137 | return <li class="mcp-client" classList={{ editing: open() }} onKeyDown={(event) => { |
| 138 | if (event.key === "Escape" && open() && !busy()) { event.preventDefault(); event.stopPropagation(); close(); } |
| 139 | }}> |
| 140 | <ClientMark name={client.name} /> |
| 141 | <div class="mcp-client-main"> |
| 142 | <h3>{client.name}</h3> |
| 143 | <div class="chips"> |
| 144 | <Show when={client.resources === "all"} fallback={<For each={client.resources as string[]} fallback={<span class="chip">{client.catalog === "computer" ? "No VMs yet" : "Nothing selected"}</span>}>{(name) => <span class="chip">{name}</span>}</For>}> |
| 145 | <span class="chip">All repositories</span> |
| 146 | </Show> |
| 147 | <For each={abilities(client)}>{([icon, label, strong]) => <span class="chip ability" classList={{ strong }}>{icon}{label}</span>}</For> |
| 148 | </div> |
| 149 | </div> |
| 150 | <span class="mcp-client-when">Connected <Ago t={client.createdAt} /></span> |
| 151 | <div class="mcp-row-actions"> |
| 152 | <button ref={editButton} aria-expanded={open()} disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={() => { |
| 153 | if (open()) return close(); |
| 154 | setSaveError(""); setEditing(client.id); |
| 155 | }}>Edit access</button> |
| 156 | <button class="danger" disabled={locked()} data-tip={props.me.viewing ? VIEWING : undefined} onClick={(event) => showConfirmDialog({ |
| 157 | title: `Revoke ${client.name}?`, description: `${client.name} loses access immediately.`, confirmLabel: "Revoke", destructive: true, returnFocus: event.currentTarget, |
| 158 | onConfirm: async () => { |
| 159 | await parseResponse(api.mcp.connections[":id"].$delete({ param: { id: client.id } })); |
| 160 | if (open()) setEditing(""); |
| 161 | await refetch(); toast("Connection revoked"); |
| 162 | }, |
| 163 | })}>Revoke</button> |
| 164 | </div> |
| 165 | <Show when={open()}><div ref={editor} class="mcp-editor"> |
| 166 | <Loaded data={connection} what="connection access" retry={retryConnection}> |
| 167 | {(details) => { queueMicrotask(() => editor.querySelector<HTMLElement>("input:not(:disabled), .segmented button, .button")?.focus()); return <Show when={details().linked} fallback={<> |
| 168 | <p>Link your Shale account to change repository access.</p> |
| 169 | <div class="mcp-actions"><button class="button primary" disabled={busy()} onClick={linkShale}>Link account</button><button class="button" disabled={busy()} onClick={close}>Cancel</button></div> |
| 170 | </>}> |
| 171 | <MCPAccess catalog={current().id} resources={details().resources} value={selection()} onChange={setSelection} disabled={busy()} create={create} /> |
| 172 | <Show when={details().resourceError}><p class="error" role="alert">{details().resourceError}</p></Show> |
| 173 | <p class="mcp-fixed"><Lock size={13} class="icon" /><span>{client.name} keeps the abilities it asked for. To change them, revoke it and connect again.</span></p> |
| 174 | <Show when={saveError()}><p class="error" role="alert">Couldn't save access. {saveError()}</p></Show> |
| 175 | <div class="mcp-actions"> |
| 176 | <button class="button primary" disabled={busy() || empty()} aria-busy={busy()} onClick={save}>Save access</button> |
| 177 | <button class="button" disabled={busy()} onClick={close}>Cancel</button> |
| 178 | <Show when={empty()}><span class="hint">Choose at least one {current().id === "computer" ? "VM" : current().id === "shale" ? "repository" : "service"}</span></Show> |
| 179 | </div> |
| 180 | </Show>; }} |
| 181 | </Loaded> |
| 182 | </div></Show> |
| 183 | </li>; |
| 184 | }}</For></ul> |
| 185 | </Show> |
| 186 | </section> |
| 187 | </>}</Show> |
| 188 | </>; |
| 189 | }} |
| 190 | </Loaded> |
| 191 | </div>; |
| 192 | } |