@supabeast/mcp 0.1.1 → 0.2.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/README.md +10 -2
- package/package.json +5 -2
- package/skills/supabeast/SKILL.md +1 -1
- package/src/api.mjs +383 -0
- package/src/browser.mjs +195 -0
- package/src/check.mjs +17 -14
- package/src/clients.mjs +311 -72
- package/src/constants.mjs +11 -2
- package/src/credentials.mjs +109 -0
- package/src/index.mjs +1316 -110
- package/src/local-project.mjs +110 -0
- package/src/prompt.mjs +56 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @supabeast/mcp
|
|
2
2
|
|
|
3
|
-
Zero-dependency CLI to connect your
|
|
3
|
+
Zero-dependency CLI to sign in to SupaBeast (press Enter to open your default browser or copy/paste the portable link into any local or remote browser), connect your Supabase project, optionally link GitHub, enable the free, read-only **SupaBeast Remote MCP** (`https://app.supabeast.com/mcp`), configure coding agents, and install the companion `SKILL.md` workflow.
|
|
4
4
|
|
|
5
5
|
## Quick Start
|
|
6
6
|
|
|
@@ -14,13 +14,21 @@ Or specify your client explicitly:
|
|
|
14
14
|
npx @supabeast/mcp --client claude
|
|
15
15
|
npx @supabeast/mcp --client cursor
|
|
16
16
|
npx @supabeast/mcp --client codex
|
|
17
|
+
npx @supabeast/mcp --client gemini
|
|
18
|
+
npx @supabeast/mcp --client antigravity
|
|
19
|
+
npx @supabeast/mcp --client t3
|
|
17
20
|
npx @supabeast/mcp --client opencode
|
|
18
21
|
```
|
|
19
22
|
|
|
20
|
-
|
|
23
|
+
Manage your workspace connection and check setup:
|
|
21
24
|
|
|
22
25
|
```bash
|
|
26
|
+
npx @supabeast/mcp login
|
|
27
|
+
npx @supabeast/mcp connect supabase
|
|
28
|
+
npx @supabeast/mcp connect github
|
|
29
|
+
npx @supabeast/mcp status
|
|
23
30
|
npx @supabeast/mcp --check
|
|
31
|
+
npx @supabeast/mcp logout
|
|
24
32
|
```
|
|
25
33
|
|
|
26
34
|
See the full documentation and Read-Only Security FAQ at [SupaBeast-Org/supabeast-mcp](https://github.com/SupaBeast-Org/supabeast-mcp).
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supabeast/mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Configure the read-only SupaBeast Remote MCP and companion coding-agent skill for Claude Code, Cursor, Codex, and OpenCode.",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Configure the read-only SupaBeast Remote MCP and companion coding-agent skill for Claude Code, Cursor, Codex, Gemini CLI, Antigravity, T3 Code, and OpenCode.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/SupaBeast-Org/supabeast-mcp#readme",
|
|
@@ -23,6 +23,9 @@
|
|
|
23
23
|
"claude-code",
|
|
24
24
|
"cursor",
|
|
25
25
|
"codex",
|
|
26
|
+
"gemini-cli",
|
|
27
|
+
"antigravity",
|
|
28
|
+
"t3-code",
|
|
26
29
|
"opencode",
|
|
27
30
|
"supabeast"
|
|
28
31
|
],
|
|
@@ -73,4 +73,4 @@ After the post-deploy observation window accumulates samples:
|
|
|
73
73
|
|
|
74
74
|
## Setup If Not Connected
|
|
75
75
|
|
|
76
|
-
If SupaBeast MCP tools are unavailable in this session, run `npx @supabeast/mcp` (or `npx supabeast init`), connect your Supabase project at `https://app.supabeast.com
|
|
76
|
+
If SupaBeast MCP tools are unavailable in this session, run `npx @supabeast/mcp` (or `npx supabeast init`) in your terminal to sign in via browser PKCE, connect your Supabase project (and automatically enable Remote MCP), optionally link your GitHub repository, and configure your coding agent. For T3 Code (Antigravity) and Antigravity, `npx @supabeast/mcp` also writes a revocable 30-day read-only Bearer token into user-scoped `mcp_config.json` automatically (or you can generate one at `https://app.supabeast.com/connections`). Check your setup anytime with `npx supabeast status` or `npx supabeast --check`.
|
package/src/api.mjs
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import { isHeadlessOrRemote, openBrowser, startLoopbackCallbackServer } from "./browser.mjs";
|
|
4
|
+
|
|
5
|
+
const CLI_REQUEST_TIMEOUT_MS = 30_000;
|
|
6
|
+
const DEFAULT_POLL_INTERVAL_MS = 2_500;
|
|
7
|
+
const DEFAULT_FLOW_TIMEOUT_MS = 300_000;
|
|
8
|
+
const MAX_CONSECUTIVE_POLL_ERRORS = 5;
|
|
9
|
+
const RETRYABLE_POLL_STATUSES = new Set([429, 502, 503, 504, 520, 521, 522, 523, 524]);
|
|
10
|
+
|
|
11
|
+
function cancelledError() {
|
|
12
|
+
return Object.assign(Error("Cancelled."), { code: "ECANCELLED" });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseRetryAfterMs(headers) {
|
|
16
|
+
const raw = headers?.get?.("Retry-After") ?? headers?.get?.("retry-after");
|
|
17
|
+
if (typeof raw !== "string") return null;
|
|
18
|
+
const trimmed = raw.trim();
|
|
19
|
+
if (!/^\d+$/.test(trimmed)) return null;
|
|
20
|
+
const seconds = Number.parseInt(trimmed, 10);
|
|
21
|
+
return Number.isFinite(seconds) ? seconds * 1_000 : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Generate an RFC 7636 PKCE verifier + S256 challenge pair. */
|
|
25
|
+
export function createPkcePair() {
|
|
26
|
+
const verifier = randomBytes(32).toString("base64url");
|
|
27
|
+
const challenge = createHash("sha256").update(verifier).digest("base64url");
|
|
28
|
+
return { verifier, challenge };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Send an authenticated or anonymous JSON request to the SupaBeast CLI HTTP API. */
|
|
32
|
+
export async function cliRequest(options) {
|
|
33
|
+
const {
|
|
34
|
+
appOrigin,
|
|
35
|
+
path,
|
|
36
|
+
method = "POST",
|
|
37
|
+
token,
|
|
38
|
+
body,
|
|
39
|
+
fetchImpl = fetch,
|
|
40
|
+
timeoutMs = CLI_REQUEST_TIMEOUT_MS,
|
|
41
|
+
} = options;
|
|
42
|
+
|
|
43
|
+
const headers = {
|
|
44
|
+
Accept: "application/json",
|
|
45
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
46
|
+
...(body !== undefined ? { "Content-Type": "application/json" } : {}),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const response = await fetchImpl(`${appOrigin}${path}`, {
|
|
50
|
+
method,
|
|
51
|
+
headers,
|
|
52
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
53
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const retryAfterMs = parseRetryAfterMs(response.headers);
|
|
57
|
+
let data = null;
|
|
58
|
+
let text = "";
|
|
59
|
+
try {
|
|
60
|
+
if (typeof response.text === "function") {
|
|
61
|
+
const raw = await response.text();
|
|
62
|
+
text = raw ? raw.slice(0, 240).trim() : "";
|
|
63
|
+
if (raw) {
|
|
64
|
+
data = JSON.parse(raw);
|
|
65
|
+
}
|
|
66
|
+
} else if (typeof response.json === "function") {
|
|
67
|
+
data = await response.json();
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
data = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { ok: response.ok, status: response.status, data, text, retryAfterMs };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function sleep(ms, signal) {
|
|
77
|
+
if (signal?.aborted) return Promise.resolve();
|
|
78
|
+
return new Promise((resolve) => {
|
|
79
|
+
const onAbort = () => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
resolve();
|
|
82
|
+
};
|
|
83
|
+
const timer = setTimeout(() => {
|
|
84
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
85
|
+
resolve();
|
|
86
|
+
}, ms);
|
|
87
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Listen for Enter (open browser) or Ctrl+C (cancel) on `stdin`, always restoring terminal state. */
|
|
92
|
+
export function waitForBrowserTrigger(stdin, signal) {
|
|
93
|
+
if (!stdin || signal?.aborted) {
|
|
94
|
+
return Promise.resolve("aborted");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
const useRaw = Boolean(stdin.isTTY && typeof stdin.setRawMode === "function");
|
|
99
|
+
const wasRaw = useRaw ? Boolean(stdin.isRaw) : false;
|
|
100
|
+
let settled = false;
|
|
101
|
+
|
|
102
|
+
const cleanup = () => {
|
|
103
|
+
stdin.off?.("data", onData);
|
|
104
|
+
stdin.off?.("end", onEnd);
|
|
105
|
+
signal?.removeEventListener?.("abort", onAbort);
|
|
106
|
+
if (useRaw) {
|
|
107
|
+
try {
|
|
108
|
+
stdin.setRawMode(wasRaw);
|
|
109
|
+
} catch {
|
|
110
|
+
/* Ignore raw mode restoration errors during stream teardown. */
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
stdin.pause?.();
|
|
115
|
+
} catch {
|
|
116
|
+
/* Ignore pause errors on custom stream mocks. */
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const finish = (outcome) => {
|
|
121
|
+
if (settled) return;
|
|
122
|
+
settled = true;
|
|
123
|
+
cleanup();
|
|
124
|
+
resolve(outcome);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const onAbort = () => finish("aborted");
|
|
128
|
+
const onEnd = () => finish("aborted");
|
|
129
|
+
const onData = (chunk) => {
|
|
130
|
+
const text = typeof chunk === "string" ? chunk : chunk.toString("utf8");
|
|
131
|
+
if (text.includes("\x03")) {
|
|
132
|
+
finish("cancel");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (text.includes("\r") || text.includes("\n")) {
|
|
136
|
+
finish("open");
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (useRaw) {
|
|
141
|
+
try {
|
|
142
|
+
stdin.setRawMode(true);
|
|
143
|
+
} catch {
|
|
144
|
+
/* Fall back to line-buffered input if raw mode cannot be enabled. */
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
stdin.resume?.();
|
|
149
|
+
} catch {
|
|
150
|
+
/* Ignore resume errors on custom stream mocks. */
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
stdin.on("data", onData);
|
|
154
|
+
stdin.once?.("end", onEnd);
|
|
155
|
+
signal?.addEventListener?.("abort", onAbort, { once: true });
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function buildStartErrorMessage(appOrigin, startPath, startRes) {
|
|
160
|
+
if (startPath === "/api/cli/auth/start" && (startRes.status === 401 || startRes.status === 404 || !startRes.data)) {
|
|
161
|
+
const detail = startRes.data?.error || (startRes.text && !startRes.text.startsWith("<") ? startRes.text : "");
|
|
162
|
+
return `CLI authentication at ${appOrigin} failed (HTTP ${startRes.status}${detail ? `: ${detail}` : ""}). Confirm ${appOrigin} has CLI onboarding deployed, or pass --url to target another host.`;
|
|
163
|
+
}
|
|
164
|
+
return startRes.data?.error || `Failed to start authorization (${startRes.status})`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Run a PKCE-bound browser authorization flow supporting Enter-to-open, copy/paste to any browser, and polling backoff. */
|
|
168
|
+
export async function runBrowserFlow(options) {
|
|
169
|
+
const {
|
|
170
|
+
appOrigin,
|
|
171
|
+
startPath,
|
|
172
|
+
pollPath,
|
|
173
|
+
startBody = {},
|
|
174
|
+
token,
|
|
175
|
+
noBrowser = false,
|
|
176
|
+
openImmediately = false,
|
|
177
|
+
interactive = false,
|
|
178
|
+
useCallbackServer = false,
|
|
179
|
+
flowLabel = "continue",
|
|
180
|
+
deviceName = `SupaBeast CLI (${os.hostname()})`,
|
|
181
|
+
stdin = process.stdin,
|
|
182
|
+
stdout,
|
|
183
|
+
env = process.env,
|
|
184
|
+
platform = process.platform,
|
|
185
|
+
fetchImpl = fetch,
|
|
186
|
+
openBrowserImpl = openBrowser,
|
|
187
|
+
startCallbackServerImpl = startLoopbackCallbackServer,
|
|
188
|
+
pollIntervalMs,
|
|
189
|
+
timeoutMs = DEFAULT_FLOW_TIMEOUT_MS,
|
|
190
|
+
json = false,
|
|
191
|
+
} = options;
|
|
192
|
+
|
|
193
|
+
const pkce = createPkcePair();
|
|
194
|
+
const state = randomBytes(24).toString("base64url");
|
|
195
|
+
const callbackServer = useCallbackServer && !noBrowser ? await startCallbackServerImpl(state) : null;
|
|
196
|
+
const abortController = new AbortController();
|
|
197
|
+
let cancelled = false;
|
|
198
|
+
let triggerPromise = null;
|
|
199
|
+
|
|
200
|
+
const onSigint = () => {
|
|
201
|
+
cancelled = true;
|
|
202
|
+
abortController.abort();
|
|
203
|
+
};
|
|
204
|
+
process.once("SIGINT", onSigint);
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
const startRes = await cliRequest({
|
|
208
|
+
appOrigin,
|
|
209
|
+
path: startPath,
|
|
210
|
+
method: "POST",
|
|
211
|
+
token,
|
|
212
|
+
fetchImpl,
|
|
213
|
+
body: {
|
|
214
|
+
...startBody,
|
|
215
|
+
challenge: pkce.challenge,
|
|
216
|
+
device_name: deviceName,
|
|
217
|
+
...(callbackServer ? { redirect_uri: callbackServer.redirectUri, state } : {}),
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
if (!startRes.ok || !startRes.data?.flow_id || !startRes.data?.auth_url) {
|
|
221
|
+
throw Error(buildStartErrorMessage(appOrigin, startPath, startRes));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const { flow_id: flowId, auth_url: authUrl } = startRes.data;
|
|
225
|
+
const serverPollMs =
|
|
226
|
+
typeof startRes.data.poll_interval_ms === "number" && startRes.data.poll_interval_ms > 0
|
|
227
|
+
? startRes.data.poll_interval_ms
|
|
228
|
+
: DEFAULT_POLL_INTERVAL_MS;
|
|
229
|
+
const basePollIntervalMs = pollIntervalMs ?? serverPollMs;
|
|
230
|
+
const headless = isHeadlessOrRemote({ env, platform });
|
|
231
|
+
|
|
232
|
+
if (!json && stdout) {
|
|
233
|
+
stdout.write(`\nTo ${flowLabel}, open this URL in any browser (local or remote):\n\n ${authUrl}\n\n`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let browserOpened = false;
|
|
237
|
+
if (noBrowser || (headless && !openImmediately)) {
|
|
238
|
+
if (!json && stdout) {
|
|
239
|
+
if (!noBrowser && headless) {
|
|
240
|
+
stdout.write(
|
|
241
|
+
"Headless or remote environment detected — waiting for authorization from your browser... (Press Ctrl+C to cancel)\n",
|
|
242
|
+
);
|
|
243
|
+
} else {
|
|
244
|
+
stdout.write("Waiting for authorization in your browser... (Press Ctrl+C to cancel)\n");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} else if (openImmediately || !interactive) {
|
|
248
|
+
browserOpened = true;
|
|
249
|
+
const launched = await openBrowserImpl(authUrl, { env, platform });
|
|
250
|
+
if (!launched.launched && !json && stdout) {
|
|
251
|
+
stdout.write("Could not open a browser automatically. Open the URL above manually.\n");
|
|
252
|
+
}
|
|
253
|
+
} else {
|
|
254
|
+
if (!json && stdout) {
|
|
255
|
+
stdout.write(
|
|
256
|
+
"Press Enter to open in your default browser, or copy/paste the URL above into another browser.\n" +
|
|
257
|
+
"Waiting for authorization... (Ctrl+C to cancel)\n",
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
triggerPromise = waitForBrowserTrigger(stdin, abortController.signal).then(async (outcome) => {
|
|
261
|
+
if (outcome === "cancel") {
|
|
262
|
+
cancelled = true;
|
|
263
|
+
abortController.abort();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (outcome === "open" && !browserOpened && !abortController.signal.aborted) {
|
|
267
|
+
browserOpened = true;
|
|
268
|
+
const launched = await openBrowserImpl(authUrl, { env, platform });
|
|
269
|
+
if (!json && stdout) {
|
|
270
|
+
stdout.write(
|
|
271
|
+
launched.launched
|
|
272
|
+
? "Opened default browser. Waiting for confirmation...\n"
|
|
273
|
+
: "Could not open a browser automatically. Open the URL above manually.\n",
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const deadline = Date.now() + Math.min(timeoutMs, (startRes.data.expires_in ?? 600) * 1_000);
|
|
281
|
+
const callbackPromise = callbackServer ? callbackServer.waitForCallback(deadline - Date.now()) : null;
|
|
282
|
+
let callbackDone = false;
|
|
283
|
+
let consecutiveErrors = 0;
|
|
284
|
+
|
|
285
|
+
while (Date.now() < deadline) {
|
|
286
|
+
if (cancelled) {
|
|
287
|
+
throw cancelledError();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let pollRes;
|
|
291
|
+
try {
|
|
292
|
+
pollRes = await cliRequest({
|
|
293
|
+
appOrigin,
|
|
294
|
+
path: pollPath,
|
|
295
|
+
method: "POST",
|
|
296
|
+
token,
|
|
297
|
+
fetchImpl,
|
|
298
|
+
body: { flow_id: flowId, code_verifier: pkce.verifier },
|
|
299
|
+
});
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (cancelled) {
|
|
302
|
+
throw cancelledError();
|
|
303
|
+
}
|
|
304
|
+
consecutiveErrors++;
|
|
305
|
+
if (consecutiveErrors > MAX_CONSECUTIVE_POLL_ERRORS) {
|
|
306
|
+
throw Error(
|
|
307
|
+
error instanceof Error
|
|
308
|
+
? `Lost connection while waiting for authorization: ${error.message}`
|
|
309
|
+
: "Lost connection while waiting for authorization.",
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
const remaining = Math.max(50, deadline - Date.now());
|
|
313
|
+
const backoffMs = Math.min(10_000, basePollIntervalMs * 2 ** (consecutiveErrors - 1));
|
|
314
|
+
await sleep(Math.min(backoffMs, remaining), abortController.signal);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (cancelled) {
|
|
319
|
+
throw cancelledError();
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const status = pollRes.data?.status;
|
|
323
|
+
if (pollRes.ok && status === "ready") {
|
|
324
|
+
return {
|
|
325
|
+
flowId,
|
|
326
|
+
verifier: pkce.verifier,
|
|
327
|
+
startData: startRes.data,
|
|
328
|
+
data: pollRes.data,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
if (status === "failed" || status === "expired" || status === "forbidden") {
|
|
332
|
+
throw Error(pollRes.data?.error || "Authorization did not succeed.");
|
|
333
|
+
}
|
|
334
|
+
if (RETRYABLE_POLL_STATUSES.has(pollRes.status)) {
|
|
335
|
+
consecutiveErrors++;
|
|
336
|
+
if (consecutiveErrors > MAX_CONSECUTIVE_POLL_ERRORS) {
|
|
337
|
+
throw Error(pollRes.data?.error || `Authorization check failed (${pollRes.status})`);
|
|
338
|
+
}
|
|
339
|
+
const remaining = Math.max(50, deadline - Date.now());
|
|
340
|
+
const waitMs =
|
|
341
|
+
pollRes.retryAfterMs !== null
|
|
342
|
+
? Math.min(30_000, pollRes.retryAfterMs)
|
|
343
|
+
: Math.min(10_000, basePollIntervalMs * 2 ** (consecutiveErrors - 1));
|
|
344
|
+
await sleep(Math.min(waitMs, remaining), abortController.signal);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
if (!pollRes.ok && pollRes.status !== 200) {
|
|
348
|
+
throw Error(pollRes.data?.error || `Authorization check failed (${pollRes.status})`);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
consecutiveErrors = 0;
|
|
352
|
+
|
|
353
|
+
if (callbackDone) {
|
|
354
|
+
await sleep(250, abortController.signal);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const remaining = Math.max(50, deadline - Date.now());
|
|
359
|
+
const waitMs = Math.min(basePollIntervalMs, remaining);
|
|
360
|
+
if (callbackPromise) {
|
|
361
|
+
const result = await Promise.race([
|
|
362
|
+
callbackPromise.then((cb) => ({ kind: "callback", cb })),
|
|
363
|
+
sleep(waitMs, abortController.signal).then(() => ({ kind: "tick" })),
|
|
364
|
+
]);
|
|
365
|
+
if (result.kind === "callback") {
|
|
366
|
+
callbackDone = true;
|
|
367
|
+
}
|
|
368
|
+
} else {
|
|
369
|
+
await sleep(waitMs, abortController.signal);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (cancelled) {
|
|
374
|
+
throw cancelledError();
|
|
375
|
+
}
|
|
376
|
+
throw Error("Timed out waiting for browser authorization. Run the command again to retry.");
|
|
377
|
+
} finally {
|
|
378
|
+
process.off("SIGINT", onSigint);
|
|
379
|
+
abortController.abort();
|
|
380
|
+
await triggerPromise;
|
|
381
|
+
await callbackServer?.close();
|
|
382
|
+
}
|
|
383
|
+
}
|
package/src/browser.mjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
|
|
4
|
+
const CALLBACK_PAGE_OK = `<!doctype html>
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<title>SupaBeast CLI</title>
|
|
7
|
+
<body style="font-family:system-ui,-apple-system,sans-serif;max-width:520px;margin:64px auto;padding:0 20px;line-height:1.5">
|
|
8
|
+
<h1>Authorization received</h1>
|
|
9
|
+
<p>You can close this browser tab and return to your terminal to finish setup.</p>
|
|
10
|
+
</body>`;
|
|
11
|
+
|
|
12
|
+
const CALLBACK_PAGE_ERROR = `<!doctype html>
|
|
13
|
+
<meta charset="utf-8">
|
|
14
|
+
<title>SupaBeast CLI</title>
|
|
15
|
+
<body style="font-family:system-ui,-apple-system,sans-serif;max-width:520px;margin:64px auto;padding:0 20px;line-height:1.5">
|
|
16
|
+
<h1>Authorization not completed</h1>
|
|
17
|
+
<p>You can close this browser tab and check your terminal.</p>
|
|
18
|
+
</body>`;
|
|
19
|
+
|
|
20
|
+
function isWsl(env = process.env) {
|
|
21
|
+
return Boolean(env?.WSL_DISTRO_NAME?.trim() || env?.WSL_INTEROP?.trim());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Detect SSH, CI, container, or headless environments without a usable local browser. */
|
|
25
|
+
export function isHeadlessOrRemote(options = {}) {
|
|
26
|
+
const { env = process.env, platform = process.platform } = options;
|
|
27
|
+
if (typeof env?.BROWSER === "string" && env.BROWSER.trim().toLowerCase() === "none") {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
const remoteFlags = [
|
|
31
|
+
"SSH_CONNECTION",
|
|
32
|
+
"SSH_CLIENT",
|
|
33
|
+
"SSH_TTY",
|
|
34
|
+
"CI",
|
|
35
|
+
"GITHUB_ACTIONS",
|
|
36
|
+
"CODESPACES",
|
|
37
|
+
"CLOUD_SHELL",
|
|
38
|
+
];
|
|
39
|
+
for (const key of remoteFlags) {
|
|
40
|
+
if (typeof env?.[key] === "string" && env[key].trim()) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (platform === "linux") {
|
|
45
|
+
const hasDisplay = Boolean(env?.DISPLAY?.trim() || env?.WAYLAND_DISPLAY?.trim());
|
|
46
|
+
if (!hasDisplay && !isWsl(env)) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Best-effort launch of the system browser (or Windows host browser under WSL) for an auth URL. */
|
|
54
|
+
export function openBrowser(url, options = {}) {
|
|
55
|
+
const { platform = process.platform, env = process.env, spawnImpl = spawn } = options;
|
|
56
|
+
let parsed;
|
|
57
|
+
try {
|
|
58
|
+
parsed = new URL(url);
|
|
59
|
+
} catch {
|
|
60
|
+
return Promise.resolve({ launched: false });
|
|
61
|
+
}
|
|
62
|
+
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
|
|
63
|
+
return Promise.resolve({ launched: false });
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
let command = "xdg-open";
|
|
68
|
+
let args = [url];
|
|
69
|
+
const spawnOpts = { stdio: "ignore", detached: true };
|
|
70
|
+
if (platform === "darwin") {
|
|
71
|
+
command = "open";
|
|
72
|
+
args = [url];
|
|
73
|
+
} else if (platform === "win32") {
|
|
74
|
+
command = "cmd";
|
|
75
|
+
args = ["/c", "start", "", url];
|
|
76
|
+
spawnOpts.windowsHide = true;
|
|
77
|
+
} else if (
|
|
78
|
+
platform === "linux" &&
|
|
79
|
+
!env?.DISPLAY?.trim() &&
|
|
80
|
+
!env?.WAYLAND_DISPLAY?.trim() &&
|
|
81
|
+
isWsl(env)
|
|
82
|
+
) {
|
|
83
|
+
command = "cmd.exe";
|
|
84
|
+
args = ["/c", "start", "", url];
|
|
85
|
+
spawnOpts.windowsHide = true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
const child = spawnImpl(command, args, spawnOpts);
|
|
90
|
+
let settled = false;
|
|
91
|
+
child.once("error", () => {
|
|
92
|
+
if (!settled) {
|
|
93
|
+
settled = true;
|
|
94
|
+
resolve({ launched: false });
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
child.once("spawn", () => {
|
|
98
|
+
if (!settled) {
|
|
99
|
+
settled = true;
|
|
100
|
+
try {
|
|
101
|
+
child.unref();
|
|
102
|
+
} catch {
|
|
103
|
+
/* Ignore unref failures on mocked child processes. */
|
|
104
|
+
}
|
|
105
|
+
resolve({ launched: true });
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
} catch {
|
|
109
|
+
resolve({ launched: false });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Start a loopback HTTP server on `127.0.0.1` to receive an explicit browser callback redirect. */
|
|
115
|
+
export async function startLoopbackCallbackServer(expectedState, options = {}) {
|
|
116
|
+
const { host = "127.0.0.1", port = 0 } = options;
|
|
117
|
+
let settled = false;
|
|
118
|
+
let resolveCallback;
|
|
119
|
+
const callbackPromise = new Promise((resolve) => {
|
|
120
|
+
resolveCallback = resolve;
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const server = http.createServer((req, res) => {
|
|
124
|
+
try {
|
|
125
|
+
const reqUrl = new URL(req.url ?? "/", `http://${host}`);
|
|
126
|
+
if (reqUrl.pathname !== "/callback") {
|
|
127
|
+
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|
|
128
|
+
res.end("Not found");
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const state = reqUrl.searchParams.get("state");
|
|
132
|
+
if (state !== expectedState) {
|
|
133
|
+
res.writeHead(400, { "Content-Type": "text/plain; charset=utf-8" });
|
|
134
|
+
res.end("Invalid state parameter.");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const status = reqUrl.searchParams.get("status") ?? "ok";
|
|
138
|
+
const flow = reqUrl.searchParams.get("flow") ?? "";
|
|
139
|
+
const error = reqUrl.searchParams.get("error") ?? "";
|
|
140
|
+
const ok = status === "ok";
|
|
141
|
+
res.writeHead(ok ? 200 : 400, {
|
|
142
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
143
|
+
"Cache-Control": "no-store",
|
|
144
|
+
});
|
|
145
|
+
res.end(ok ? CALLBACK_PAGE_OK : CALLBACK_PAGE_ERROR);
|
|
146
|
+
if (!settled) {
|
|
147
|
+
settled = true;
|
|
148
|
+
resolveCallback({ status, flow, error });
|
|
149
|
+
}
|
|
150
|
+
} catch {
|
|
151
|
+
res.writeHead(400, { "Content-Type": "text/plain; charset=utf-8" });
|
|
152
|
+
res.end("Bad request");
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
await new Promise((resolve, reject) => {
|
|
157
|
+
server.once("error", reject);
|
|
158
|
+
server.listen(port, host, () => {
|
|
159
|
+
server.removeListener("error", reject);
|
|
160
|
+
resolve();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const address = server.address();
|
|
165
|
+
if (!address || typeof address === "string") {
|
|
166
|
+
server.close();
|
|
167
|
+
throw Error("Failed to bind loopback callback port.");
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
redirectUri: `http://${host}:${address.port}/callback`,
|
|
172
|
+
waitForCallback(timeoutMs = 300_000) {
|
|
173
|
+
let timer;
|
|
174
|
+
return Promise.race([
|
|
175
|
+
callbackPromise,
|
|
176
|
+
new Promise((resolve) => {
|
|
177
|
+
timer = setTimeout(() => {
|
|
178
|
+
if (!settled) {
|
|
179
|
+
settled = true;
|
|
180
|
+
resolve({ status: "timeout", flow: "", error: "Timed out waiting for browser authorization." });
|
|
181
|
+
}
|
|
182
|
+
}, timeoutMs);
|
|
183
|
+
timer.unref?.();
|
|
184
|
+
}),
|
|
185
|
+
]).finally(() => {
|
|
186
|
+
if (timer) clearTimeout(timer);
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
close() {
|
|
190
|
+
return new Promise((resolve) => {
|
|
191
|
+
server.close(() => resolve());
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
package/src/check.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import { inspectClientConfig,
|
|
4
|
+
import { inspectClientConfig, resolveClientTargets } from "./clients.mjs";
|
|
5
5
|
import {
|
|
6
6
|
AUTH_SERVER_PATH,
|
|
7
7
|
DEFAULT_MCP_URL,
|
|
@@ -124,19 +124,22 @@ export function inspectLocalState(options = {}) {
|
|
|
124
124
|
|
|
125
125
|
for (const clientId of SUPPORTED_CLIENTS) {
|
|
126
126
|
for (const scope of ["user", "project"]) {
|
|
127
|
-
const
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
127
|
+
const targets = resolveClientTargets(clientId, { scope, homeDir, cwd, env, pathApi, url });
|
|
128
|
+
for (const target of targets) {
|
|
129
|
+
const configState = inspectClientConfig(target);
|
|
130
|
+
skillPaths.add(target.skillPath);
|
|
131
|
+
clients.push({
|
|
132
|
+
clientId,
|
|
133
|
+
label: target.label,
|
|
134
|
+
scope,
|
|
135
|
+
configPath: target.configPath,
|
|
136
|
+
configStatus: configState.status,
|
|
137
|
+
configuredUrl: configState.configuredUrl,
|
|
138
|
+
authMode: configState.authMode,
|
|
139
|
+
skillPath: target.skillPath,
|
|
140
|
+
skillInstalled: existsSync(target.skillPath),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
140
143
|
}
|
|
141
144
|
}
|
|
142
145
|
|