@aexol/spectral 0.2.5 → 0.2.6
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/dist/cli.js +10 -47
- package/dist/mcp/agent-dir.js +18 -0
- package/dist/mcp/app-bridge.bundle.js +67 -0
- package/dist/mcp/commands.js +263 -0
- package/dist/mcp/config.js +532 -0
- package/dist/mcp/consent-manager.js +59 -0
- package/dist/mcp/direct-tools.js +354 -0
- package/dist/mcp/errors.js +165 -0
- package/dist/mcp/glimpse-ui.js +67 -0
- package/dist/mcp/host-html-template.js +412 -0
- package/dist/mcp/index.js +291 -0
- package/dist/mcp/init.js +280 -0
- package/dist/mcp/lifecycle.js +79 -0
- package/dist/mcp/logger.js +130 -0
- package/dist/mcp/mcp-auth-flow.js +283 -0
- package/dist/mcp/mcp-auth.js +226 -0
- package/dist/mcp/mcp-callback-server.js +225 -0
- package/dist/mcp/mcp-oauth-provider.js +243 -0
- package/dist/mcp/mcp-panel.js +646 -0
- package/dist/mcp/mcp-setup-panel.js +485 -0
- package/dist/mcp/metadata-cache.js +158 -0
- package/dist/mcp/npx-resolver.js +385 -0
- package/dist/mcp/oauth-handler.js +54 -0
- package/dist/mcp/onboarding-state.js +56 -0
- package/dist/mcp/proxy-modes.js +714 -0
- package/dist/mcp/resource-tools.js +14 -0
- package/dist/mcp/sampling-handler.js +206 -0
- package/dist/mcp/server-manager.js +301 -0
- package/dist/mcp/state.js +1 -0
- package/dist/mcp/tool-metadata.js +128 -0
- package/dist/mcp/tool-registrar.js +43 -0
- package/dist/mcp/types.js +93 -0
- package/dist/mcp/ui-resource-handler.js +113 -0
- package/dist/mcp/ui-server.js +522 -0
- package/dist/mcp/ui-session.js +306 -0
- package/dist/mcp/ui-stream-types.js +58 -0
- package/dist/mcp/utils.js +104 -0
- package/dist/mcp/vitest.config.js +13 -0
- package/package.json +6 -3
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
// npx-resolver.ts - Resolve npx/npm exec binaries to avoid npm parent processes
|
|
2
|
+
import { existsSync, readFileSync, realpathSync, readdirSync, statSync, writeFileSync, renameSync, mkdirSync, openSync, readSync, closeSync } from "node:fs";
|
|
3
|
+
import { join, dirname, extname, resolve, sep } from "node:path";
|
|
4
|
+
import { getAgentPath } from "./agent-dir.js";
|
|
5
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
6
|
+
const CACHE_VERSION = 1;
|
|
7
|
+
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
8
|
+
export async function resolveNpxBinary(command, args) {
|
|
9
|
+
const parsed = command === "npx"
|
|
10
|
+
? parseNpxArgs(args)
|
|
11
|
+
: command === "npm"
|
|
12
|
+
? parseNpmExecArgs(args)
|
|
13
|
+
: null;
|
|
14
|
+
if (!parsed)
|
|
15
|
+
return null;
|
|
16
|
+
const cacheKey = JSON.stringify([command, ...args]);
|
|
17
|
+
const cache = loadCache();
|
|
18
|
+
const cached = cache?.entries?.[cacheKey];
|
|
19
|
+
if (cached && Date.now() - cached.resolvedAt < CACHE_TTL_MS && existsSync(cached.resolvedBin)) {
|
|
20
|
+
return { binPath: cached.resolvedBin, extraArgs: parsed.extraArgs, isJs: cached.isJs };
|
|
21
|
+
}
|
|
22
|
+
const resolved = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
|
|
23
|
+
if (resolved) {
|
|
24
|
+
saveCacheEntry(cacheKey, resolved);
|
|
25
|
+
return { binPath: resolved.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolved.isJs };
|
|
26
|
+
}
|
|
27
|
+
// Slow path: force npx cache population
|
|
28
|
+
await forceNpxCache(parsed.packageSpec);
|
|
29
|
+
const resolvedAfterInstall = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
|
|
30
|
+
if (resolvedAfterInstall) {
|
|
31
|
+
saveCacheEntry(cacheKey, resolvedAfterInstall);
|
|
32
|
+
return { binPath: resolvedAfterInstall.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolvedAfterInstall.isJs };
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
function parseNpxArgs(args) {
|
|
37
|
+
const separatorIndex = args.indexOf("--");
|
|
38
|
+
const before = separatorIndex >= 0 ? args.slice(0, separatorIndex) : args;
|
|
39
|
+
const after = separatorIndex >= 0 ? args.slice(separatorIndex + 1) : [];
|
|
40
|
+
const positionals = [];
|
|
41
|
+
let packageSpec;
|
|
42
|
+
let sawPackageFlag = false;
|
|
43
|
+
let foundFirstPositional = false;
|
|
44
|
+
for (let i = 0; i < before.length; i++) {
|
|
45
|
+
const arg = before[i];
|
|
46
|
+
if (foundFirstPositional) {
|
|
47
|
+
positionals.push(arg);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (arg === "-y" || arg === "--yes")
|
|
51
|
+
continue;
|
|
52
|
+
if (arg === "-p" || arg === "--package") {
|
|
53
|
+
const value = before[i + 1];
|
|
54
|
+
if (!value || value.startsWith("-"))
|
|
55
|
+
return null;
|
|
56
|
+
if (!packageSpec)
|
|
57
|
+
packageSpec = value;
|
|
58
|
+
sawPackageFlag = true;
|
|
59
|
+
i++;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (arg.startsWith("--package=")) {
|
|
63
|
+
const value = arg.slice("--package=".length);
|
|
64
|
+
if (!value)
|
|
65
|
+
return null;
|
|
66
|
+
if (!packageSpec)
|
|
67
|
+
packageSpec = value;
|
|
68
|
+
sawPackageFlag = true;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (arg.startsWith("-")) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
positionals.push(arg);
|
|
75
|
+
foundFirstPositional = true;
|
|
76
|
+
}
|
|
77
|
+
if (sawPackageFlag) {
|
|
78
|
+
const binName = positionals[0];
|
|
79
|
+
if (!packageSpec || !binName)
|
|
80
|
+
return null;
|
|
81
|
+
const extraArgs = positionals.slice(1).concat(after);
|
|
82
|
+
return { packageSpec, binName, extraArgs };
|
|
83
|
+
}
|
|
84
|
+
const packagePositional = positionals[0];
|
|
85
|
+
if (!packagePositional)
|
|
86
|
+
return null;
|
|
87
|
+
const extraArgs = positionals.slice(1).concat(after);
|
|
88
|
+
return { packageSpec: packagePositional, extraArgs };
|
|
89
|
+
}
|
|
90
|
+
function parseNpmExecArgs(args) {
|
|
91
|
+
if (args[0] !== "exec")
|
|
92
|
+
return null;
|
|
93
|
+
const execArgs = args.slice(1);
|
|
94
|
+
const separatorIndex = execArgs.indexOf("--");
|
|
95
|
+
if (separatorIndex < 0)
|
|
96
|
+
return null;
|
|
97
|
+
const before = execArgs.slice(0, separatorIndex);
|
|
98
|
+
const after = execArgs.slice(separatorIndex + 1);
|
|
99
|
+
let packageSpec;
|
|
100
|
+
for (let i = 0; i < before.length; i++) {
|
|
101
|
+
const arg = before[i];
|
|
102
|
+
if (arg === "-y" || arg === "--yes")
|
|
103
|
+
continue;
|
|
104
|
+
if (arg === "--package") {
|
|
105
|
+
const value = before[i + 1];
|
|
106
|
+
if (!value || value.startsWith("-"))
|
|
107
|
+
return null;
|
|
108
|
+
if (!packageSpec)
|
|
109
|
+
packageSpec = value;
|
|
110
|
+
i++;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (arg.startsWith("--package=")) {
|
|
114
|
+
const value = arg.slice("--package=".length);
|
|
115
|
+
if (!value)
|
|
116
|
+
return null;
|
|
117
|
+
if (!packageSpec)
|
|
118
|
+
packageSpec = value;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (arg.startsWith("-")) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const binName = after[0];
|
|
126
|
+
if (!packageSpec || !binName)
|
|
127
|
+
return null;
|
|
128
|
+
const extraArgs = after.slice(1);
|
|
129
|
+
return { packageSpec, binName, extraArgs };
|
|
130
|
+
}
|
|
131
|
+
function resolveFromNpmCache(packageSpec, binName) {
|
|
132
|
+
const cacheDir = getNpmCacheDir();
|
|
133
|
+
if (!cacheDir)
|
|
134
|
+
return null;
|
|
135
|
+
const packageName = extractPackageName(packageSpec);
|
|
136
|
+
if (!packageName)
|
|
137
|
+
return null;
|
|
138
|
+
const packageDir = findCachedPackageDir(cacheDir, packageName);
|
|
139
|
+
if (!packageDir)
|
|
140
|
+
return null;
|
|
141
|
+
const packageJsonPath = join(packageDir, "package.json");
|
|
142
|
+
if (!existsSync(packageJsonPath))
|
|
143
|
+
return null;
|
|
144
|
+
let pkg = null;
|
|
145
|
+
try {
|
|
146
|
+
pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
const binField = pkg?.bin;
|
|
152
|
+
if (!binField)
|
|
153
|
+
return null;
|
|
154
|
+
const candidates = buildBinCandidates(packageName, binName);
|
|
155
|
+
let chosenBinName;
|
|
156
|
+
let binRel;
|
|
157
|
+
if (typeof binField === "string") {
|
|
158
|
+
chosenBinName = defaultBinName(packageName);
|
|
159
|
+
binRel = binField;
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
for (const candidate of candidates) {
|
|
163
|
+
if (binField[candidate]) {
|
|
164
|
+
chosenBinName = candidate;
|
|
165
|
+
binRel = binField[candidate];
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (!binRel) {
|
|
170
|
+
const firstEntry = Object.entries(binField)[0];
|
|
171
|
+
if (firstEntry) {
|
|
172
|
+
chosenBinName = firstEntry[0];
|
|
173
|
+
binRel = firstEntry[1];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (!binRel)
|
|
178
|
+
return null;
|
|
179
|
+
const nodeModulesDir = findNodeModulesDir(packageDir);
|
|
180
|
+
const binLink = chosenBinName ? join(nodeModulesDir, ".bin", chosenBinName) : null;
|
|
181
|
+
let resolvedBin = binLink && existsSync(binLink) ? safeRealpath(binLink) : "";
|
|
182
|
+
if (!resolvedBin) {
|
|
183
|
+
resolvedBin = resolve(packageDir, binRel);
|
|
184
|
+
if (!existsSync(resolvedBin))
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const isJs = detectJsBinary(resolvedBin);
|
|
188
|
+
return {
|
|
189
|
+
resolvedBin,
|
|
190
|
+
resolvedAt: Date.now(),
|
|
191
|
+
packageVersion: pkg?.version,
|
|
192
|
+
isJs,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const FORCE_CACHE_TIMEOUT_MS = 30_000;
|
|
196
|
+
async function forceNpxCache(packageSpec) {
|
|
197
|
+
try {
|
|
198
|
+
await new Promise((resolve, reject) => {
|
|
199
|
+
const proc = spawn("npm", ["exec", "--yes", "--package", packageSpec, "--", "node", "-e", "1"], { stdio: "ignore" });
|
|
200
|
+
const timer = setTimeout(() => {
|
|
201
|
+
proc.kill();
|
|
202
|
+
reject(new Error("timeout"));
|
|
203
|
+
}, FORCE_CACHE_TIMEOUT_MS);
|
|
204
|
+
timer.unref();
|
|
205
|
+
proc.on("close", () => { clearTimeout(timer); resolve(); });
|
|
206
|
+
proc.on("error", (err) => { clearTimeout(timer); reject(err); });
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
// Ignore failures, resolution will fall back to original command
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
function buildBinCandidates(packageName, explicitBin) {
|
|
214
|
+
const candidates = [];
|
|
215
|
+
if (explicitBin)
|
|
216
|
+
candidates.push(explicitBin);
|
|
217
|
+
if (packageName.startsWith("@")) {
|
|
218
|
+
const namePart = packageName.split("/")[1] ?? "";
|
|
219
|
+
const scopePart = packageName.split("/")[0]?.replace("@", "") ?? "";
|
|
220
|
+
if (namePart)
|
|
221
|
+
candidates.push(namePart);
|
|
222
|
+
if (scopePart && namePart)
|
|
223
|
+
candidates.push(`${scopePart}-${namePart}`);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
candidates.push(packageName);
|
|
227
|
+
}
|
|
228
|
+
return [...new Set(candidates.filter(Boolean))];
|
|
229
|
+
}
|
|
230
|
+
function extractPackageName(spec) {
|
|
231
|
+
const trimmed = spec.trim();
|
|
232
|
+
if (!trimmed)
|
|
233
|
+
return null;
|
|
234
|
+
if (trimmed.startsWith("@")) {
|
|
235
|
+
const slashIndex = trimmed.indexOf("/");
|
|
236
|
+
if (slashIndex < 0)
|
|
237
|
+
return null;
|
|
238
|
+
const atIndex = trimmed.lastIndexOf("@");
|
|
239
|
+
if (atIndex > slashIndex) {
|
|
240
|
+
return trimmed.slice(0, atIndex);
|
|
241
|
+
}
|
|
242
|
+
return trimmed;
|
|
243
|
+
}
|
|
244
|
+
const atIndex = trimmed.indexOf("@");
|
|
245
|
+
return atIndex >= 0 ? trimmed.slice(0, atIndex) : trimmed;
|
|
246
|
+
}
|
|
247
|
+
function defaultBinName(packageName) {
|
|
248
|
+
if (packageName.startsWith("@")) {
|
|
249
|
+
const parts = packageName.split("/");
|
|
250
|
+
return parts[1] ?? packageName.replace("@", "").replace("/", "-");
|
|
251
|
+
}
|
|
252
|
+
return packageName;
|
|
253
|
+
}
|
|
254
|
+
function findCachedPackageDir(cacheDir, packageName) {
|
|
255
|
+
const npxDir = join(cacheDir, "_npx");
|
|
256
|
+
if (!existsSync(npxDir))
|
|
257
|
+
return null;
|
|
258
|
+
const packagePathParts = packageName.startsWith("@")
|
|
259
|
+
? packageName.split("/")
|
|
260
|
+
: [packageName];
|
|
261
|
+
const candidates = readdirSync(npxDir, { withFileTypes: true })
|
|
262
|
+
.filter(entry => entry.isDirectory())
|
|
263
|
+
.map(entry => {
|
|
264
|
+
const full = join(npxDir, entry.name);
|
|
265
|
+
const mtime = safeStatMtime(full);
|
|
266
|
+
return { name: entry.name, mtime };
|
|
267
|
+
})
|
|
268
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
269
|
+
for (const entry of candidates) {
|
|
270
|
+
const pkgDir = join(npxDir, entry.name, "node_modules", ...packagePathParts);
|
|
271
|
+
if (existsSync(join(pkgDir, "package.json"))) {
|
|
272
|
+
return pkgDir;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
function findNodeModulesDir(packageDir) {
|
|
278
|
+
const parts = packageDir.split(sep);
|
|
279
|
+
const idx = parts.lastIndexOf("node_modules");
|
|
280
|
+
if (idx >= 0) {
|
|
281
|
+
return parts.slice(0, idx + 1).join(sep);
|
|
282
|
+
}
|
|
283
|
+
return join(packageDir, "..");
|
|
284
|
+
}
|
|
285
|
+
function detectJsBinary(binPath) {
|
|
286
|
+
const ext = extname(binPath).toLowerCase();
|
|
287
|
+
if (ext === ".js" || ext === ".mjs" || ext === ".cjs")
|
|
288
|
+
return true;
|
|
289
|
+
try {
|
|
290
|
+
const fd = openSync(binPath, "r");
|
|
291
|
+
try {
|
|
292
|
+
const buf = Buffer.alloc(256);
|
|
293
|
+
readSync(fd, buf, 0, 256, 0);
|
|
294
|
+
const firstLine = buf.toString("utf-8").split("\n")[0] ?? "";
|
|
295
|
+
return firstLine.startsWith("#!") && firstLine.includes("node");
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
closeSync(fd);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
let npmCacheDirCached;
|
|
306
|
+
function getNpmCacheDir() {
|
|
307
|
+
if (npmCacheDirCached !== undefined)
|
|
308
|
+
return npmCacheDirCached;
|
|
309
|
+
if (process.env.NPM_CONFIG_CACHE) {
|
|
310
|
+
npmCacheDirCached = process.env.NPM_CONFIG_CACHE;
|
|
311
|
+
return npmCacheDirCached;
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
const result = spawnSync("npm", ["config", "get", "cache"], { encoding: "utf-8" });
|
|
315
|
+
if (result.status === 0) {
|
|
316
|
+
const path = String(result.stdout).trim();
|
|
317
|
+
npmCacheDirCached = path || null;
|
|
318
|
+
return npmCacheDirCached;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
npmCacheDirCached = null;
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
npmCacheDirCached = null;
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
function getNpxCachePath() {
|
|
329
|
+
return getAgentPath("mcp-npx-cache.json");
|
|
330
|
+
}
|
|
331
|
+
function loadCache() {
|
|
332
|
+
const cachePath = getNpxCachePath();
|
|
333
|
+
if (!existsSync(cachePath))
|
|
334
|
+
return null;
|
|
335
|
+
try {
|
|
336
|
+
const raw = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
337
|
+
if (!raw || typeof raw !== "object")
|
|
338
|
+
return null;
|
|
339
|
+
if (raw.version !== CACHE_VERSION)
|
|
340
|
+
return null;
|
|
341
|
+
if (!raw.entries || typeof raw.entries !== "object")
|
|
342
|
+
return null;
|
|
343
|
+
return raw;
|
|
344
|
+
}
|
|
345
|
+
catch {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
function saveCacheEntry(key, entry) {
|
|
350
|
+
const cachePath = getNpxCachePath();
|
|
351
|
+
const dir = dirname(cachePath);
|
|
352
|
+
mkdirSync(dir, { recursive: true });
|
|
353
|
+
let merged = { version: CACHE_VERSION, entries: {} };
|
|
354
|
+
try {
|
|
355
|
+
if (existsSync(cachePath)) {
|
|
356
|
+
const existing = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
357
|
+
if (existing && existing.version === CACHE_VERSION && existing.entries) {
|
|
358
|
+
merged.entries = { ...existing.entries };
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
// Ignore parse errors
|
|
364
|
+
}
|
|
365
|
+
merged.entries[key] = entry;
|
|
366
|
+
const tmpPath = `${cachePath}.${process.pid}.tmp`;
|
|
367
|
+
writeFileSync(tmpPath, JSON.stringify(merged, null, 2), "utf-8");
|
|
368
|
+
renameSync(tmpPath, cachePath);
|
|
369
|
+
}
|
|
370
|
+
function safeRealpath(path) {
|
|
371
|
+
try {
|
|
372
|
+
return realpathSync(path);
|
|
373
|
+
}
|
|
374
|
+
catch {
|
|
375
|
+
return "";
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
function safeStatMtime(path) {
|
|
379
|
+
try {
|
|
380
|
+
return statSync(path).mtimeMs;
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
return 0;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// oauth-handler.ts - OAuth token management for MCP servers
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { getAgentPath } from "./agent-dir.js";
|
|
5
|
+
// Token storage path for a server
|
|
6
|
+
function getTokensPath(serverName) {
|
|
7
|
+
const override = process.env.MCP_OAUTH_DIR?.trim();
|
|
8
|
+
const authDir = override ? override : getAgentPath("mcp-oauth");
|
|
9
|
+
return join(authDir, serverName, "tokens.json");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get stored OAuth tokens for a server (if any).
|
|
13
|
+
* Returns undefined if no tokens or tokens are expired.
|
|
14
|
+
*
|
|
15
|
+
* Token file location: $MCP_OAUTH_DIR/<server>/tokens.json when set,
|
|
16
|
+
* otherwise <Pi agent dir>/mcp-oauth/<server>/tokens.json
|
|
17
|
+
*
|
|
18
|
+
* Expected format:
|
|
19
|
+
* {
|
|
20
|
+
* "access_token": "...",
|
|
21
|
+
* "token_type": "bearer",
|
|
22
|
+
* "refresh_token": "...", // optional
|
|
23
|
+
* "expires_in": 3600, // optional, seconds
|
|
24
|
+
* "expiresAt": 1234567890 // optional, absolute timestamp ms
|
|
25
|
+
* }
|
|
26
|
+
*/
|
|
27
|
+
export function getStoredTokens(serverName) {
|
|
28
|
+
const tokensPath = getTokensPath(serverName);
|
|
29
|
+
if (!existsSync(tokensPath))
|
|
30
|
+
return undefined;
|
|
31
|
+
try {
|
|
32
|
+
const stored = JSON.parse(readFileSync(tokensPath, "utf-8"));
|
|
33
|
+
// Validate required field
|
|
34
|
+
if (!stored.access_token || typeof stored.access_token !== "string") {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
// Check expiration if expiresAt is set
|
|
38
|
+
if (stored.expiresAt && typeof stored.expiresAt === "number") {
|
|
39
|
+
if (Date.now() > stored.expiresAt) {
|
|
40
|
+
// Token expired
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
access_token: stored.access_token,
|
|
46
|
+
token_type: stored.token_type ?? "bearer",
|
|
47
|
+
refresh_token: stored.refresh_token,
|
|
48
|
+
expires_in: stored.expires_in,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { getAgentPath } from "./agent-dir.js";
|
|
4
|
+
const DEFAULT_STATE = {
|
|
5
|
+
version: 1,
|
|
6
|
+
sharedConfigHintShown: false,
|
|
7
|
+
setupCompleted: false,
|
|
8
|
+
};
|
|
9
|
+
export function getOnboardingStatePath() {
|
|
10
|
+
return getAgentPath("mcp-onboarding.json");
|
|
11
|
+
}
|
|
12
|
+
export function loadOnboardingState() {
|
|
13
|
+
const path = getOnboardingStatePath();
|
|
14
|
+
if (!existsSync(path))
|
|
15
|
+
return { ...DEFAULT_STATE };
|
|
16
|
+
try {
|
|
17
|
+
const raw = JSON.parse(readFileSync(path, "utf-8"));
|
|
18
|
+
if (!raw || typeof raw !== "object")
|
|
19
|
+
return { ...DEFAULT_STATE };
|
|
20
|
+
return {
|
|
21
|
+
version: 1,
|
|
22
|
+
sharedConfigHintShown: raw.sharedConfigHintShown === true,
|
|
23
|
+
setupCompleted: raw.setupCompleted === true,
|
|
24
|
+
lastDiscoveryFingerprint: typeof raw.lastDiscoveryFingerprint === "string" ? raw.lastDiscoveryFingerprint : undefined,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return { ...DEFAULT_STATE };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function saveOnboardingState(state) {
|
|
32
|
+
const path = getOnboardingStatePath();
|
|
33
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
34
|
+
const tmpPath = `${path}.${process.pid}.tmp`;
|
|
35
|
+
writeFileSync(tmpPath, `${JSON.stringify(state, null, 2)}\n`, "utf-8");
|
|
36
|
+
renameSync(tmpPath, path);
|
|
37
|
+
}
|
|
38
|
+
export function updateOnboardingState(updater) {
|
|
39
|
+
const next = updater(loadOnboardingState());
|
|
40
|
+
saveOnboardingState(next);
|
|
41
|
+
return next;
|
|
42
|
+
}
|
|
43
|
+
export function markSharedConfigHintShown(fingerprint) {
|
|
44
|
+
return updateOnboardingState((state) => ({
|
|
45
|
+
...state,
|
|
46
|
+
sharedConfigHintShown: true,
|
|
47
|
+
lastDiscoveryFingerprint: fingerprint ?? state.lastDiscoveryFingerprint,
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
export function markSetupCompleted(fingerprint) {
|
|
51
|
+
return updateOnboardingState((state) => ({
|
|
52
|
+
...state,
|
|
53
|
+
setupCompleted: true,
|
|
54
|
+
lastDiscoveryFingerprint: fingerprint ?? state.lastDiscoveryFingerprint,
|
|
55
|
+
}));
|
|
56
|
+
}
|