@yawlabs/mcp 0.74.1 → 0.74.3
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/CHANGELOG.md +133 -1
- package/README.md +3 -0
- package/dist/chunk-VHPGG3GN.js +759 -0
- package/dist/index.js +2536 -2148
- package/dist/oam-spawn-Z5CASEBY.js +71 -0
- package/package.json +1 -1
|
@@ -0,0 +1,759 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/oam-spawn.ts
|
|
4
|
+
import { spawn } from "child_process";
|
|
5
|
+
import { accessSync, constants, existsSync, readdirSync, readFileSync, statSync } from "fs";
|
|
6
|
+
import { homedir as homedir2 } from "os";
|
|
7
|
+
import { dirname, isAbsolute, join, sep } from "path";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
|
|
10
|
+
// src/logger.ts
|
|
11
|
+
var LOG_LEVELS = { debug: 0, info: 1, warn: 2, error: 3 };
|
|
12
|
+
var minLevel = LOG_LEVELS[process.env.LOG_LEVEL?.toLowerCase()] ?? LOG_LEVELS.info;
|
|
13
|
+
function log(level, msg, data) {
|
|
14
|
+
if (LOG_LEVELS[level] < minLevel) return;
|
|
15
|
+
const entry = JSON.stringify({ ...data, level, msg, ts: (/* @__PURE__ */ new Date()).toISOString() });
|
|
16
|
+
process.stderr.write(`${entry}
|
|
17
|
+
`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/paths.ts
|
|
21
|
+
import { access } from "fs/promises";
|
|
22
|
+
import { homedir } from "os";
|
|
23
|
+
import path from "path";
|
|
24
|
+
function cacheDir() {
|
|
25
|
+
if (process.platform === "win32") {
|
|
26
|
+
const localAppData = process.env.LOCALAPPDATA;
|
|
27
|
+
const base = localAppData && localAppData.length > 0 ? localAppData : path.join(homedir(), "AppData", "Local");
|
|
28
|
+
return path.join(base, "yaw-mcp", "Cache");
|
|
29
|
+
}
|
|
30
|
+
if (process.platform === "darwin") {
|
|
31
|
+
return path.join(homedir(), "Library", "Caches", "yaw-mcp");
|
|
32
|
+
}
|
|
33
|
+
const xdg = process.env.XDG_CACHE_HOME;
|
|
34
|
+
return path.join(xdg && xdg.length > 0 ? xdg : path.join(homedir(), ".cache"), "yaw-mcp");
|
|
35
|
+
}
|
|
36
|
+
var CONFIG_DIRNAME = ".yaw-mcp";
|
|
37
|
+
var SIDECARS_DIRNAME = "sidecars";
|
|
38
|
+
function sidecarsRoot(home = homedir()) {
|
|
39
|
+
return path.join(userConfigDir(home), SIDECARS_DIRNAME);
|
|
40
|
+
}
|
|
41
|
+
function sidecarsNodeModules(home = homedir()) {
|
|
42
|
+
return path.join(sidecarsRoot(home), "node_modules");
|
|
43
|
+
}
|
|
44
|
+
function userConfigDir(home = homedir()) {
|
|
45
|
+
return path.join(home, CONFIG_DIRNAME);
|
|
46
|
+
}
|
|
47
|
+
function normalizeForCompare(p) {
|
|
48
|
+
return process.platform === "win32" ? p.toLowerCase() : p;
|
|
49
|
+
}
|
|
50
|
+
function isUnderHome(dir, homeResolved) {
|
|
51
|
+
const dirKey = normalizeForCompare(dir);
|
|
52
|
+
const homeKey = normalizeForCompare(homeResolved);
|
|
53
|
+
if (dirKey === homeKey) return false;
|
|
54
|
+
const rel = path.relative(homeResolved, dir);
|
|
55
|
+
if (rel === "" || path.isAbsolute(rel)) return false;
|
|
56
|
+
const relNorm = normalizeForCompare(rel);
|
|
57
|
+
return relNorm !== ".." && !relNorm.startsWith(`..${path.sep}`);
|
|
58
|
+
}
|
|
59
|
+
async function findProjectConfigDir(start, home = homedir()) {
|
|
60
|
+
const homeFallback = home && home.length > 0 ? home : process.env.USERPROFILE || homedir();
|
|
61
|
+
const homeResolved = path.resolve(homeFallback);
|
|
62
|
+
let dir = path.resolve(start);
|
|
63
|
+
let prev = "";
|
|
64
|
+
while (dir !== prev) {
|
|
65
|
+
if (!isUnderHome(dir, homeResolved)) return null;
|
|
66
|
+
const candidate = path.join(dir, CONFIG_DIRNAME);
|
|
67
|
+
try {
|
|
68
|
+
await access(candidate);
|
|
69
|
+
return candidate;
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
prev = dir;
|
|
73
|
+
dir = path.dirname(dir);
|
|
74
|
+
}
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
var GUIDE_FILENAME = "YAW-MCP.md";
|
|
78
|
+
function guidePath(configDir) {
|
|
79
|
+
return path.join(configDir, GUIDE_FILENAME);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/oam-spawn.ts
|
|
83
|
+
function packageName(spec) {
|
|
84
|
+
const start = spec.startsWith("@") ? 1 : 0;
|
|
85
|
+
const at = spec.indexOf("@", start);
|
|
86
|
+
return at === -1 ? spec : spec.slice(0, at);
|
|
87
|
+
}
|
|
88
|
+
function specSuffix(spec) {
|
|
89
|
+
const start = spec.startsWith("@") ? 1 : 0;
|
|
90
|
+
const at = spec.indexOf("@", start);
|
|
91
|
+
return at === -1 ? null : spec.slice(at + 1);
|
|
92
|
+
}
|
|
93
|
+
var EXACT_VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
94
|
+
function specConstraint(spec) {
|
|
95
|
+
const suffix = specSuffix(spec);
|
|
96
|
+
if (suffix === null || suffix === "") return { kind: "any" };
|
|
97
|
+
if (EXACT_VERSION.test(suffix)) return { kind: "exact", version: suffix };
|
|
98
|
+
const versionish = /^[v=<>^~*\d]/.test(suffix) || suffix.includes("||");
|
|
99
|
+
return versionish ? { kind: "range", raw: suffix } : { kind: "any" };
|
|
100
|
+
}
|
|
101
|
+
function isRegistrySpec(spec) {
|
|
102
|
+
if (spec.includes(":") || /^[./~\\]/.test(spec)) return false;
|
|
103
|
+
return /^(@[^/@\s]+\/)?[^./_@\s][^/@\s]*$/.test(packageName(spec));
|
|
104
|
+
}
|
|
105
|
+
function npxSpecIndex(args) {
|
|
106
|
+
return args.findIndex((a) => a !== "-y" && a !== "--yes");
|
|
107
|
+
}
|
|
108
|
+
function npxSpec(args) {
|
|
109
|
+
const idx = npxSpecIndex(args);
|
|
110
|
+
if (idx === -1) return null;
|
|
111
|
+
const spec = args[idx];
|
|
112
|
+
return spec.startsWith("-") ? null : spec;
|
|
113
|
+
}
|
|
114
|
+
var MIN_OAM_VERSION = "0.9.0";
|
|
115
|
+
var OAM_INSTALL_SH = "curl -fsSL https://oamjs.org/install.sh | sh";
|
|
116
|
+
var OAM_INSTALL_PS1 = "irm https://oamjs.org/install.ps1 | iex";
|
|
117
|
+
function oamInstallCommand(os) {
|
|
118
|
+
return os === "windows" ? OAM_INSTALL_PS1 : OAM_INSTALL_SH;
|
|
119
|
+
}
|
|
120
|
+
var warnedOamUnavailable = false;
|
|
121
|
+
function oamFailureLabel(failure) {
|
|
122
|
+
if (failure === "timeout") return "`oam --version` did not answer in time";
|
|
123
|
+
if (failure === "exit") return "`oam --version` exited non-zero";
|
|
124
|
+
return "the binary could not be executed";
|
|
125
|
+
}
|
|
126
|
+
var oamProbeCache;
|
|
127
|
+
function parseOamVersion(out) {
|
|
128
|
+
const m = /\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?/.exec(out);
|
|
129
|
+
return m ? m[0] : null;
|
|
130
|
+
}
|
|
131
|
+
function parseSemver(s) {
|
|
132
|
+
const m = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-.]+))?/.exec(s);
|
|
133
|
+
if (!m) return null;
|
|
134
|
+
const pre = m[4] === void 0 ? [] : m[4].split(".").map((id) => /^\d+$/.test(id) ? Number(id) : id);
|
|
135
|
+
return { release: [Number(m[1]), Number(m[2]), Number(m[3])], pre };
|
|
136
|
+
}
|
|
137
|
+
function comparePre(a, b) {
|
|
138
|
+
if (a.length === 0 || b.length === 0) {
|
|
139
|
+
if (a.length === b.length) return 0;
|
|
140
|
+
return a.length === 0 ? 1 : -1;
|
|
141
|
+
}
|
|
142
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
143
|
+
const x = a[i];
|
|
144
|
+
const y = b[i];
|
|
145
|
+
if (x === y) continue;
|
|
146
|
+
if (typeof x === "number" && typeof y === "number") return x < y ? -1 : 1;
|
|
147
|
+
if (typeof x === "number") return -1;
|
|
148
|
+
if (typeof y === "number") return 1;
|
|
149
|
+
return x < y ? -1 : 1;
|
|
150
|
+
}
|
|
151
|
+
if (a.length === b.length) return 0;
|
|
152
|
+
return a.length < b.length ? -1 : 1;
|
|
153
|
+
}
|
|
154
|
+
function compareSemver(a, b) {
|
|
155
|
+
for (let i = 0; i < 3; i++) {
|
|
156
|
+
if (a.release[i] !== b.release[i]) return a.release[i] < b.release[i] ? -1 : 1;
|
|
157
|
+
}
|
|
158
|
+
return comparePre(a.pre, b.pre);
|
|
159
|
+
}
|
|
160
|
+
function compareVersions(a, b) {
|
|
161
|
+
const pa = parseSemver(a);
|
|
162
|
+
const pb = parseSemver(b);
|
|
163
|
+
if (!pa || !pb) return 0;
|
|
164
|
+
return compareSemver(pa, pb);
|
|
165
|
+
}
|
|
166
|
+
function satisfiesExactPin(declared, want) {
|
|
167
|
+
if (declared === null) return false;
|
|
168
|
+
const d = parseSemver(declared);
|
|
169
|
+
const w = parseSemver(want);
|
|
170
|
+
if (d && w) return compareSemver(d, w) === 0;
|
|
171
|
+
return declared === want;
|
|
172
|
+
}
|
|
173
|
+
function winNormalize(p, platform = process.platform) {
|
|
174
|
+
return platform === "win32" ? p.replace(/\//g, "\\") : p;
|
|
175
|
+
}
|
|
176
|
+
function isExecutableFile(candidate, platform) {
|
|
177
|
+
try {
|
|
178
|
+
if (!statSync(candidate).isFile()) return false;
|
|
179
|
+
if (platform !== "win32") accessSync(candidate, constants.X_OK);
|
|
180
|
+
return true;
|
|
181
|
+
} catch {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
function resolveBinAbsolute(bin, env = process.env, platform = process.platform) {
|
|
186
|
+
if (isAbsolute(bin)) return isExecutableFile(bin, platform) ? winNormalize(bin, platform) : null;
|
|
187
|
+
const pathVar = env.PATH ?? env.Path ?? env.path ?? "";
|
|
188
|
+
if (!pathVar) return null;
|
|
189
|
+
const dirs = pathVar.split(platform === "win32" ? ";" : ":").filter(Boolean);
|
|
190
|
+
const exts = platform === "win32" ? ["", ...(env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean)] : [""];
|
|
191
|
+
for (const dir of dirs) {
|
|
192
|
+
const clean = dir.replace(/^"|"$/g, "");
|
|
193
|
+
for (const ext of exts) {
|
|
194
|
+
const candidate = join(clean, bin + ext);
|
|
195
|
+
if (!isAbsolute(candidate)) continue;
|
|
196
|
+
if (isExecutableFile(candidate, platform)) return winNormalize(candidate, platform);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
function classifyProbeFailure(err) {
|
|
202
|
+
const code = err?.code;
|
|
203
|
+
if (code === "ETIMEDOUT") return "timeout";
|
|
204
|
+
if (code === "EOAMEXIT") return "exit";
|
|
205
|
+
return "spawn";
|
|
206
|
+
}
|
|
207
|
+
var OAM_PROBE_TIMEOUT_MS = 3e3;
|
|
208
|
+
var OAM_PROBE_KILL_SIGNAL = "SIGKILL";
|
|
209
|
+
var OAM_PROBE_MAX_OUTPUT = 8 * 1024;
|
|
210
|
+
var VERSION_CARRY = 32;
|
|
211
|
+
function createProbeCollector(max = OAM_PROBE_MAX_OUTPUT) {
|
|
212
|
+
let head = "";
|
|
213
|
+
let carry = "";
|
|
214
|
+
let found = null;
|
|
215
|
+
return {
|
|
216
|
+
push(chunk) {
|
|
217
|
+
if (found !== null) return;
|
|
218
|
+
found = parseOamVersion(carry + chunk);
|
|
219
|
+
carry = (carry + chunk).slice(-VERSION_CARRY);
|
|
220
|
+
if (head.length >= max) return;
|
|
221
|
+
head += chunk.slice(0, max - head.length);
|
|
222
|
+
},
|
|
223
|
+
/** The version if one appeared anywhere, else the capped head (which
|
|
224
|
+
* parses to null either way, so the caller's contract is unchanged). */
|
|
225
|
+
result() {
|
|
226
|
+
return found ?? head;
|
|
227
|
+
},
|
|
228
|
+
/** Test hook: bytes actually retained. */
|
|
229
|
+
retainedLength() {
|
|
230
|
+
return head.length;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function spawnVersionProbe(bin) {
|
|
235
|
+
return new Promise((resolve, reject) => {
|
|
236
|
+
let settled = false;
|
|
237
|
+
let timer;
|
|
238
|
+
const settle = (fn) => {
|
|
239
|
+
if (settled) return;
|
|
240
|
+
settled = true;
|
|
241
|
+
if (timer) clearTimeout(timer);
|
|
242
|
+
fn();
|
|
243
|
+
};
|
|
244
|
+
let child;
|
|
245
|
+
try {
|
|
246
|
+
child = spawn(bin, ["--version"], {
|
|
247
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
248
|
+
windowsHide: process.platform === "win32"
|
|
249
|
+
});
|
|
250
|
+
} catch (err) {
|
|
251
|
+
reject(err);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const collector = createProbeCollector();
|
|
255
|
+
child.stdout?.setEncoding("utf8");
|
|
256
|
+
child.stdout?.on("error", () => {
|
|
257
|
+
});
|
|
258
|
+
child.stdout?.on("data", (chunk) => collector.push(chunk));
|
|
259
|
+
child.on("error", (err) => settle(() => reject(err)));
|
|
260
|
+
child.on(
|
|
261
|
+
"close",
|
|
262
|
+
(code, signal) => settle(() => {
|
|
263
|
+
if (code === 0) {
|
|
264
|
+
resolve(collector.result());
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const err = new Error(`oam exited ${code ?? signal}`);
|
|
268
|
+
err.code = "EOAMEXIT";
|
|
269
|
+
reject(err);
|
|
270
|
+
})
|
|
271
|
+
);
|
|
272
|
+
timer = setTimeout(() => {
|
|
273
|
+
settle(() => {
|
|
274
|
+
try {
|
|
275
|
+
child.kill(OAM_PROBE_KILL_SIGNAL);
|
|
276
|
+
} catch {
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
child.stdout?.destroy();
|
|
280
|
+
child.unref();
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
const err = new Error(`oam --version exceeded ${OAM_PROBE_TIMEOUT_MS}ms`);
|
|
284
|
+
err.code = "ETIMEDOUT";
|
|
285
|
+
reject(err);
|
|
286
|
+
});
|
|
287
|
+
}, OAM_PROBE_TIMEOUT_MS);
|
|
288
|
+
timer.unref?.();
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
var oamProbeInFlight;
|
|
292
|
+
var oamProbeGeneration = 0;
|
|
293
|
+
async function probeOam(run = spawnVersionProbe) {
|
|
294
|
+
if (oamProbeCache !== void 0) return oamProbeCache;
|
|
295
|
+
if (oamProbeInFlight !== void 0) return oamProbeInFlight;
|
|
296
|
+
const generation = oamProbeGeneration;
|
|
297
|
+
oamProbeInFlight = probeOamUncached(run, generation).finally(() => {
|
|
298
|
+
if (generation === oamProbeGeneration) oamProbeInFlight = void 0;
|
|
299
|
+
});
|
|
300
|
+
return oamProbeInFlight;
|
|
301
|
+
}
|
|
302
|
+
async function probeOamUncached(run, generation) {
|
|
303
|
+
if (oamProbeCache !== void 0) return oamProbeCache;
|
|
304
|
+
const bin = winNormalize(process.env.OAM_BIN || (process.platform === "win32" ? "oam.exe" : "oam"));
|
|
305
|
+
const publish = (probe) => {
|
|
306
|
+
if (generation === oamProbeGeneration) oamProbeCache = probe;
|
|
307
|
+
return probe;
|
|
308
|
+
};
|
|
309
|
+
try {
|
|
310
|
+
const version = parseOamVersion(await run(bin));
|
|
311
|
+
if (version !== null && compareVersions(version, MIN_OAM_VERSION) < 0) {
|
|
312
|
+
log("warn", "oam is installed but below the minimum supported version; falling back to node", {
|
|
313
|
+
// The full token, prerelease suffix included: a build reporting
|
|
314
|
+
// "0.8.3-rc.1" against a 0.8.3 floor lands here deliberately (semver
|
|
315
|
+
// ranks a prerelease below its release), and naming it as "0.8.3" would
|
|
316
|
+
// make this line look like a comparator bug.
|
|
317
|
+
oamVersion: version,
|
|
318
|
+
minVersion: MIN_OAM_VERSION,
|
|
319
|
+
// The floor tracks the latest release, so below-min always means out
|
|
320
|
+
// of date, and oam updates itself in place.
|
|
321
|
+
updateWith: "oam self-update",
|
|
322
|
+
// ...but updating alone changes nothing here. oamProbeCache is written
|
|
323
|
+
// once per process and only ever cleared by a test hook, so every
|
|
324
|
+
// opted-in server keeps landing on node until the broker restarts --
|
|
325
|
+
// and an MCP broker under a desktop client lives for days. Without this
|
|
326
|
+
// the user runs the update, sees no further log line, and reads a
|
|
327
|
+
// working fix as a fix that did not work. The timeout and generic
|
|
328
|
+
// failure warns say "for this process" for the same reason.
|
|
329
|
+
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
330
|
+
});
|
|
331
|
+
return publish({ bin: null, binPath: null, version, belowMin: true, failure: null, failureDetail: null });
|
|
332
|
+
}
|
|
333
|
+
return publish({
|
|
334
|
+
bin,
|
|
335
|
+
// Resolved once, alongside the probe that proved the name spawns, so the
|
|
336
|
+
// install path never has to re-derive it (or spawn `where`/`which`).
|
|
337
|
+
binPath: resolveBinAbsolute(bin),
|
|
338
|
+
version,
|
|
339
|
+
belowMin: false,
|
|
340
|
+
failure: null,
|
|
341
|
+
failureDetail: null
|
|
342
|
+
});
|
|
343
|
+
} catch (err) {
|
|
344
|
+
const code = err?.code;
|
|
345
|
+
if (code === "ETIMEDOUT") {
|
|
346
|
+
log("warn", "oam did not respond to --version in time; falling back to node for this process", {
|
|
347
|
+
timeoutMs: OAM_PROBE_TIMEOUT_MS,
|
|
348
|
+
bin
|
|
349
|
+
});
|
|
350
|
+
} else if (code !== "ENOENT") {
|
|
351
|
+
log("warn", "oam --version failed; falling back to node for this process", {
|
|
352
|
+
bin,
|
|
353
|
+
error: err instanceof Error ? err.message : String(err)
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
return publish({
|
|
357
|
+
bin: null,
|
|
358
|
+
binPath: null,
|
|
359
|
+
version: null,
|
|
360
|
+
belowMin: false,
|
|
361
|
+
failure: code === "ENOENT" ? null : classifyProbeFailure(err),
|
|
362
|
+
failureDetail: code === "ENOENT" ? null : err instanceof Error ? err.message : String(err)
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function resetOamBinCache() {
|
|
367
|
+
warnedOamUnavailable = false;
|
|
368
|
+
oamProbeCache = void 0;
|
|
369
|
+
oamProbeInFlight = void 0;
|
|
370
|
+
oamProbeGeneration++;
|
|
371
|
+
pinnedReported.clear();
|
|
372
|
+
}
|
|
373
|
+
var WIN_EXE_EXT = /\.(?:exe|cmd|bat)$/i;
|
|
374
|
+
function nodeLaunchKind(command) {
|
|
375
|
+
const base = (command.split(/[\\/]/).pop() ?? command).replace(WIN_EXE_EXT, "").toLowerCase();
|
|
376
|
+
if (base === "node") return "node";
|
|
377
|
+
if (base === "npx") return "npx";
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
function rewriteForOam(command, args, deps) {
|
|
381
|
+
const bin = deps.oamBin;
|
|
382
|
+
if (!bin) return { command, args };
|
|
383
|
+
const toOam = (entry, rest) => ({
|
|
384
|
+
command: bin,
|
|
385
|
+
args: rest.length > 0 ? ["run", entry, "--", ...rest] : ["run", entry]
|
|
386
|
+
});
|
|
387
|
+
const kind = nodeLaunchKind(command);
|
|
388
|
+
if (kind === "node") {
|
|
389
|
+
const [entry, ...rest] = args;
|
|
390
|
+
if (!entry) return { command, args };
|
|
391
|
+
if (entry.startsWith("-")) return { command, args };
|
|
392
|
+
return toOam(entry, rest);
|
|
393
|
+
}
|
|
394
|
+
if (kind === "npx") {
|
|
395
|
+
const specIdx = npxSpecIndex(args);
|
|
396
|
+
const spec = specIdx === -1 ? void 0 : args[specIdx];
|
|
397
|
+
if (!spec) return { command, args };
|
|
398
|
+
if (spec.startsWith("-")) {
|
|
399
|
+
log("debug", "npx launch carries flags yaw-mcp does not parse; staying on npx instead of oam", {
|
|
400
|
+
flag: spec,
|
|
401
|
+
args
|
|
402
|
+
});
|
|
403
|
+
return { command, args };
|
|
404
|
+
}
|
|
405
|
+
if (!isRegistrySpec(spec)) {
|
|
406
|
+
log("debug", "npx spec is a git/path target, not a registry package; staying on npx instead of oam", { spec });
|
|
407
|
+
return { command, args };
|
|
408
|
+
}
|
|
409
|
+
const pkg = packageName(spec);
|
|
410
|
+
const constraint = specConstraint(spec);
|
|
411
|
+
if (constraint.kind === "range") {
|
|
412
|
+
log("debug", "npx spec constrains the version with a range yaw-mcp cannot evaluate; staying on npx", {
|
|
413
|
+
package: pkg,
|
|
414
|
+
range: constraint.raw
|
|
415
|
+
});
|
|
416
|
+
return { command, args };
|
|
417
|
+
}
|
|
418
|
+
const wantVersion = constraint.kind === "exact" ? constraint.version : null;
|
|
419
|
+
const entry = deps.resolveEntry(pkg, wantVersion);
|
|
420
|
+
if (!entry) {
|
|
421
|
+
if (wantVersion !== null) {
|
|
422
|
+
log("debug", "no on-disk copy declares the pinned version; staying on npx so the pin is honoured", {
|
|
423
|
+
package: pkg,
|
|
424
|
+
version: wantVersion
|
|
425
|
+
});
|
|
426
|
+
} else {
|
|
427
|
+
log("debug", "npx package has no on-disk entry; staying on npx instead of oam", { package: pkg });
|
|
428
|
+
}
|
|
429
|
+
return { command, args };
|
|
430
|
+
}
|
|
431
|
+
return toOam(entry, args.slice(specIdx + 1));
|
|
432
|
+
}
|
|
433
|
+
return { command, args };
|
|
434
|
+
}
|
|
435
|
+
var NPX_CACHE_MARKER = `${sep}_npx${sep}`;
|
|
436
|
+
function npxCacheNodeModules(fromUrl = import.meta.url) {
|
|
437
|
+
let here;
|
|
438
|
+
try {
|
|
439
|
+
here = fileURLToPath(fromUrl);
|
|
440
|
+
} catch {
|
|
441
|
+
return [];
|
|
442
|
+
}
|
|
443
|
+
const idx = here.indexOf(NPX_CACHE_MARKER);
|
|
444
|
+
if (idx === -1) return [];
|
|
445
|
+
const npxRoot = here.slice(0, idx + NPX_CACHE_MARKER.length - sep.length);
|
|
446
|
+
try {
|
|
447
|
+
return readdirSync(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => join(npxRoot, e.name, "node_modules"));
|
|
448
|
+
} catch {
|
|
449
|
+
return [];
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
function npmrcValue(raw) {
|
|
453
|
+
const v = raw.trim();
|
|
454
|
+
const quoted = v.length > 1 && (v.startsWith('"') && v.endsWith('"') || v.startsWith("'") && v.endsWith("'"));
|
|
455
|
+
if (quoted) {
|
|
456
|
+
if (v.startsWith('"')) {
|
|
457
|
+
try {
|
|
458
|
+
return JSON.parse(v);
|
|
459
|
+
} catch {
|
|
460
|
+
return v.slice(1, -1);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return v.slice(1, -1);
|
|
464
|
+
}
|
|
465
|
+
let out = "";
|
|
466
|
+
let esc = false;
|
|
467
|
+
for (const c of v) {
|
|
468
|
+
if (esc) {
|
|
469
|
+
out += ";#\\".includes(c) ? c : `\\${c}`;
|
|
470
|
+
esc = false;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
if (c === ";" || c === "#") break;
|
|
474
|
+
if (c === "\\") {
|
|
475
|
+
esc = true;
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
out += c;
|
|
479
|
+
}
|
|
480
|
+
if (esc) out += "\\";
|
|
481
|
+
return out.trim();
|
|
482
|
+
}
|
|
483
|
+
function npmrcCache(file) {
|
|
484
|
+
let text;
|
|
485
|
+
try {
|
|
486
|
+
text = readFileSync(file, "utf8");
|
|
487
|
+
} catch {
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
for (const line of text.split(/\r?\n/)) {
|
|
491
|
+
const m = /^\s*cache\s*=(.*)$/.exec(line);
|
|
492
|
+
if (!m) continue;
|
|
493
|
+
const value = npmrcValue(m[1]);
|
|
494
|
+
if (!value) continue;
|
|
495
|
+
return value.startsWith("~/") ? join(homedir2(), value.slice(2)) : value;
|
|
496
|
+
}
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
var npmCacheDirCache = /* @__PURE__ */ new Map();
|
|
500
|
+
function npmCacheDir(fromUrl = import.meta.url) {
|
|
501
|
+
const cached = npmCacheDirCache.get(fromUrl);
|
|
502
|
+
if (cached !== void 0) return cached;
|
|
503
|
+
const resolved = resolveNpmCacheDir(fromUrl);
|
|
504
|
+
npmCacheDirCache.set(fromUrl, resolved);
|
|
505
|
+
return resolved;
|
|
506
|
+
}
|
|
507
|
+
function resetNpmCacheDir() {
|
|
508
|
+
npmCacheDirCache.clear();
|
|
509
|
+
}
|
|
510
|
+
function resolveNpmCacheDir(fromUrl) {
|
|
511
|
+
const fromEnv = process.env.npm_config_cache;
|
|
512
|
+
if (fromEnv) return fromEnv;
|
|
513
|
+
const candidates = [join(homedir2(), ".npmrc")];
|
|
514
|
+
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
515
|
+
candidates.push(
|
|
516
|
+
join(dirname(nodeModules), "etc", "npmrc"),
|
|
517
|
+
// <prefix>/node_modules (Windows)
|
|
518
|
+
join(dirname(dirname(nodeModules)), "etc", "npmrc"),
|
|
519
|
+
// <prefix>/lib/node_modules (POSIX)
|
|
520
|
+
join(nodeModules, "npm", "npmrc")
|
|
521
|
+
// builtin, beside npm itself
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
const nodeDir = dirname(process.execPath);
|
|
525
|
+
candidates.push(
|
|
526
|
+
join(nodeDir, "node_modules", "npm", "npmrc"),
|
|
527
|
+
// Windows layout
|
|
528
|
+
join(nodeDir, "..", "lib", "node_modules", "npm", "npmrc")
|
|
529
|
+
// POSIX layout
|
|
530
|
+
);
|
|
531
|
+
for (const file of candidates) {
|
|
532
|
+
const cache = npmrcCache(file);
|
|
533
|
+
if (cache) return cache;
|
|
534
|
+
}
|
|
535
|
+
const fallback = process.platform === "win32" ? process.env.LOCALAPPDATA && join(process.env.LOCALAPPDATA, "npm-cache") : join(homedir2(), ".npm");
|
|
536
|
+
return fallback && existsSync(fallback) ? fallback : null;
|
|
537
|
+
}
|
|
538
|
+
function npmCacheNpxNodeModules(cacheDir2) {
|
|
539
|
+
if (!cacheDir2) return [];
|
|
540
|
+
const npxRoot = join(cacheDir2, "_npx");
|
|
541
|
+
try {
|
|
542
|
+
return readdirSync(npxRoot, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => join(npxRoot, e.name, "node_modules"));
|
|
543
|
+
} catch {
|
|
544
|
+
return [];
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function ownNodeModules(fromUrl = import.meta.url) {
|
|
548
|
+
let here;
|
|
549
|
+
try {
|
|
550
|
+
here = fileURLToPath(fromUrl);
|
|
551
|
+
} catch {
|
|
552
|
+
return [];
|
|
553
|
+
}
|
|
554
|
+
const seg = `${sep}node_modules${sep}`;
|
|
555
|
+
const idx = here.lastIndexOf(seg);
|
|
556
|
+
if (idx === -1) return [];
|
|
557
|
+
return [here.slice(0, idx + seg.length - sep.length)];
|
|
558
|
+
}
|
|
559
|
+
function packageEntry(pkgDir, pkg) {
|
|
560
|
+
const pjPath = join(pkgDir, "package.json");
|
|
561
|
+
if (!existsSync(pjPath)) return null;
|
|
562
|
+
let j;
|
|
563
|
+
try {
|
|
564
|
+
j = JSON.parse(readFileSync(pjPath, "utf8"));
|
|
565
|
+
} catch {
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
let rel;
|
|
569
|
+
if (typeof j.bin === "string") {
|
|
570
|
+
rel = j.bin;
|
|
571
|
+
} else if (j.bin && typeof j.bin === "object") {
|
|
572
|
+
const unscoped = pkg.slice(pkg.lastIndexOf("/") + 1);
|
|
573
|
+
rel = j.bin[unscoped] ?? (j.name ? j.bin[j.name] : void 0) ?? Object.values(j.bin)[0];
|
|
574
|
+
}
|
|
575
|
+
if (!rel && typeof j.main === "string") rel = j.main;
|
|
576
|
+
if (!rel) return null;
|
|
577
|
+
const entry = isAbsolute(rel) ? rel : join(pkgDir, rel);
|
|
578
|
+
if (!existsSync(entry)) return null;
|
|
579
|
+
return {
|
|
580
|
+
entry,
|
|
581
|
+
version: typeof j.version === "string" ? j.version : null
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
function resolveNpmEntry(pkg, fromUrl = import.meta.url, npmCache = npmCacheDir(fromUrl), managedRoot = sidecarsNodeModules(), wantVersion = null) {
|
|
585
|
+
const parts = pkg.split("/");
|
|
586
|
+
const durable = [
|
|
587
|
+
...managedRoot ? [{ nodeModules: managedRoot, source: "managed" }] : [],
|
|
588
|
+
// The broker's OWN node_modules sits UNDER _npx whenever yaw-mcp was
|
|
589
|
+
// itself launched via `npx -y @yawlabs/mcp` -- the common install shape,
|
|
590
|
+
// not an edge case. Calling that "durable" would advertise `npm install
|
|
591
|
+
// <pkg>@latest`, which cannot refresh a content-hashed cache directory.
|
|
592
|
+
// resolveStableNpmEntry draws the same distinction for the same reason.
|
|
593
|
+
...ownNodeModules(fromUrl).map((nodeModules) => ({
|
|
594
|
+
nodeModules,
|
|
595
|
+
source: nodeModules.includes(NPX_CACHE_MARKER) ? "npx-cache" : "durable"
|
|
596
|
+
}))
|
|
597
|
+
];
|
|
598
|
+
for (const { nodeModules, source } of durable) {
|
|
599
|
+
const hit = packageEntry(join(nodeModules, ...parts), pkg);
|
|
600
|
+
if (!hit) continue;
|
|
601
|
+
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
602
|
+
notePinnedSidecar(pkg, hit.version, source, nodeModules);
|
|
603
|
+
return hit.entry;
|
|
604
|
+
}
|
|
605
|
+
const roots = /* @__PURE__ */ new Set([...npxCacheNodeModules(fromUrl), ...npmCacheNpxNodeModules(npmCache)]);
|
|
606
|
+
let best = null;
|
|
607
|
+
let bestRoot = null;
|
|
608
|
+
let bestParsed = null;
|
|
609
|
+
for (const nodeModules of roots) {
|
|
610
|
+
const hit = packageEntry(join(nodeModules, ...parts), pkg);
|
|
611
|
+
if (!hit) continue;
|
|
612
|
+
if (wantVersion !== null && !satisfiesExactPin(hit.version, wantVersion)) continue;
|
|
613
|
+
const parsed = hit.version === null ? null : parseSemver(hit.version);
|
|
614
|
+
const wins = best === null || parsed !== null && (bestParsed === null || compareSemver(parsed, bestParsed) > 0);
|
|
615
|
+
if (wins) {
|
|
616
|
+
best = hit;
|
|
617
|
+
bestRoot = nodeModules;
|
|
618
|
+
bestParsed = parsed;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (best !== null && bestRoot !== null) notePinnedSidecar(pkg, best.version, "npx-cache", bestRoot);
|
|
622
|
+
return best?.entry ?? null;
|
|
623
|
+
}
|
|
624
|
+
var REFRESH_COMMAND = {
|
|
625
|
+
managed: () => "yaw-mcp sidecars install",
|
|
626
|
+
durable: (pkg) => `npm install ${pkg}@latest`,
|
|
627
|
+
"npx-cache": (pkg) => `npx -y ${pkg}@latest --help`
|
|
628
|
+
};
|
|
629
|
+
var pinnedReported = /* @__PURE__ */ new Set();
|
|
630
|
+
function resetPinnedSidecarLog() {
|
|
631
|
+
pinnedReported.clear();
|
|
632
|
+
}
|
|
633
|
+
function notePinnedSidecar(pkg, version, source, from) {
|
|
634
|
+
if (pinnedReported.has(pkg)) return;
|
|
635
|
+
pinnedReported.add(pkg);
|
|
636
|
+
log(
|
|
637
|
+
source === "managed" ? "debug" : "info",
|
|
638
|
+
"hosting sidecar on oam from an on-disk copy; it will not self-update the way npx does",
|
|
639
|
+
{
|
|
640
|
+
package: pkg,
|
|
641
|
+
version: version ?? "unknown",
|
|
642
|
+
source,
|
|
643
|
+
// The node_modules the entry was actually resolved out of. refreshWith
|
|
644
|
+
// cannot name it -- a durable tree may be global or project-local, and
|
|
645
|
+
// the command differs -- so this is what makes the advice actionable
|
|
646
|
+
// rather than something to run in whatever cwd the user happens to be.
|
|
647
|
+
from,
|
|
648
|
+
refreshWith: REFRESH_COMMAND[source](pkg)
|
|
649
|
+
}
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
function resolveStableNpmEntry(pkg, fromUrl = import.meta.url) {
|
|
653
|
+
for (const nodeModules of ownNodeModules(fromUrl)) {
|
|
654
|
+
if (nodeModules.includes(NPX_CACHE_MARKER)) continue;
|
|
655
|
+
const hit = packageEntry(join(nodeModules, ...pkg.split("/")), pkg);
|
|
656
|
+
if (hit) return hit.entry;
|
|
657
|
+
}
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
async function resolveOamSpawn(command, args, optedIn = true) {
|
|
661
|
+
const probe = await probeOam();
|
|
662
|
+
if (optedIn && probe.bin === null && !probe.belowMin && !warnedOamUnavailable) {
|
|
663
|
+
warnedOamUnavailable = true;
|
|
664
|
+
if (probe.failure !== null) {
|
|
665
|
+
log("warn", "a server opted in to oam but oam is installed and unusable; running it on node instead", {
|
|
666
|
+
reason: oamFailureLabel(probe.failure),
|
|
667
|
+
// The raw error behind the label. It is the part a support ticket needs
|
|
668
|
+
// and the part the plain-English line deliberately does not carry.
|
|
669
|
+
detail: probe.failureDetail,
|
|
670
|
+
// No install commands: the install is already there. Pointing OAM_BIN
|
|
671
|
+
// at a working copy is the actionable move, and the probe is cached for
|
|
672
|
+
// the process lifetime, so repairing it needs a restart to take effect.
|
|
673
|
+
overrideWith: "OAM_BIN",
|
|
674
|
+
thenRestart: "restart yaw-mcp; this probe is cached for the process lifetime"
|
|
675
|
+
});
|
|
676
|
+
} else {
|
|
677
|
+
log("warn", "a server opted in to oam but oam is not installed; running it on node instead", {
|
|
678
|
+
// Both, not oamInstallCommand(CURRENT_OS): this is a structured log
|
|
679
|
+
// line read off a server's stderr, which is routinely a different
|
|
680
|
+
// machine from the one reading it. The rendered REPORTS (doctor,
|
|
681
|
+
// install) are the surfaces that know which platform they are about.
|
|
682
|
+
install: OAM_INSTALL_SH,
|
|
683
|
+
installWindows: OAM_INSTALL_PS1,
|
|
684
|
+
overrideWith: "OAM_BIN"
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return rewriteForOam(command, args, {
|
|
689
|
+
oamBin: probe.bin,
|
|
690
|
+
// The three injectables (fromUrl, npmCache, managedRoot) keep their
|
|
691
|
+
// production defaults -- explicit `undefined` rather than a reorder,
|
|
692
|
+
// because they are positional and every test call site passes them.
|
|
693
|
+
resolveEntry: (pkg, wantVersion) => resolveNpmEntry(pkg, void 0, void 0, void 0, wantVersion)
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
function isOamCommand(command) {
|
|
697
|
+
const base = command.split(/[\\/]/).pop() ?? command;
|
|
698
|
+
return /^oam(\.exe)?$/i.test(base);
|
|
699
|
+
}
|
|
700
|
+
function isOamLaunch(command, args = []) {
|
|
701
|
+
if (isOamCommand(command)) return true;
|
|
702
|
+
const base = command.split(/[\\/]/).pop() ?? command;
|
|
703
|
+
if (/^cmd(\.exe)?$/i.test(base)) {
|
|
704
|
+
const first = args.find((a) => !/^\/[a-z]$/i.test(a));
|
|
705
|
+
return first !== void 0 && isOamCommand(first);
|
|
706
|
+
}
|
|
707
|
+
if (/^(sh|bash|zsh|dash)$/i.test(base)) {
|
|
708
|
+
const dashC = args.indexOf("-c");
|
|
709
|
+
const payload = dashC >= 0 ? args[dashC + 1] : args[0];
|
|
710
|
+
if (payload === void 0) return false;
|
|
711
|
+
const firstToken = payload.trim().split(/\s+/)[0]?.replace(/^["']|["']$/g, "");
|
|
712
|
+
return firstToken !== void 0 && firstToken.length > 0 && isOamCommand(firstToken);
|
|
713
|
+
}
|
|
714
|
+
return false;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
export {
|
|
718
|
+
log,
|
|
719
|
+
cacheDir,
|
|
720
|
+
CONFIG_DIRNAME,
|
|
721
|
+
sidecarsRoot,
|
|
722
|
+
sidecarsNodeModules,
|
|
723
|
+
userConfigDir,
|
|
724
|
+
findProjectConfigDir,
|
|
725
|
+
guidePath,
|
|
726
|
+
packageName,
|
|
727
|
+
specConstraint,
|
|
728
|
+
isRegistrySpec,
|
|
729
|
+
npxSpecIndex,
|
|
730
|
+
npxSpec,
|
|
731
|
+
MIN_OAM_VERSION,
|
|
732
|
+
OAM_INSTALL_SH,
|
|
733
|
+
OAM_INSTALL_PS1,
|
|
734
|
+
oamInstallCommand,
|
|
735
|
+
oamFailureLabel,
|
|
736
|
+
parseOamVersion,
|
|
737
|
+
compareVersions,
|
|
738
|
+
winNormalize,
|
|
739
|
+
resolveBinAbsolute,
|
|
740
|
+
OAM_PROBE_TIMEOUT_MS,
|
|
741
|
+
OAM_PROBE_KILL_SIGNAL,
|
|
742
|
+
OAM_PROBE_MAX_OUTPUT,
|
|
743
|
+
createProbeCollector,
|
|
744
|
+
probeOam,
|
|
745
|
+
resetOamBinCache,
|
|
746
|
+
nodeLaunchKind,
|
|
747
|
+
rewriteForOam,
|
|
748
|
+
npxCacheNodeModules,
|
|
749
|
+
npmCacheDir,
|
|
750
|
+
resetNpmCacheDir,
|
|
751
|
+
npmCacheNpxNodeModules,
|
|
752
|
+
ownNodeModules,
|
|
753
|
+
resolveNpmEntry,
|
|
754
|
+
resetPinnedSidecarLog,
|
|
755
|
+
resolveStableNpmEntry,
|
|
756
|
+
resolveOamSpawn,
|
|
757
|
+
isOamCommand,
|
|
758
|
+
isOamLaunch
|
|
759
|
+
};
|