@yawlabs/caddy-mcp 2.2.0 → 2.3.1
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 +276 -246
- package/bin/caddy-mcp.mjs +334 -175
- package/dist/index.js +196 -54
- package/dist/server.js +196 -54
- package/dist/snapshots.d.ts +10 -0
- package/package.json +82 -82
package/bin/caddy-mcp.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Runtime launcher for @yawlabs/caddy-mcp.
|
|
4
|
-
*
|
|
5
|
-
* Prefers the oam runtime (https://oamjs.org) and falls back to the Node
|
|
6
|
-
* process already running this file.
|
|
7
|
-
*
|
|
8
|
-
* Unlike npmjs-mcp, this server is NOT a zero-dependency bundle -- dist/
|
|
9
|
-
* imports @modelcontextprotocol/sdk and zod from node_modules at runtime. That
|
|
10
|
-
* is fine on both paths: oam does npm resolution against an existing
|
|
11
|
-
* node_modules with CommonJS interop, and it was verified here before this
|
|
12
|
-
* launcher was written (`oam run dist/index.js -- --version` prints the same
|
|
13
|
-
* version Node does).
|
|
14
|
-
*
|
|
15
|
-
* WHY THE FALLBACK COSTS NOTHING
|
|
16
|
-
* npm has already started Node to run this launcher, so falling back is a
|
|
17
|
-
* plain `import()` of the server into THIS process: no extra spawn, no extra
|
|
18
|
-
* startup, byte-identical to invoking dist/index.js directly. Discovery is
|
|
19
|
-
* stat-only -- never a subprocess -- so the miss case stays sub-millisecond.
|
|
20
|
-
*
|
|
21
|
-
* WHAT THE OAM PATH COSTS
|
|
22
|
-
* Reaching oam through an npm `bin` means Node boots first and oam boots
|
|
23
|
-
* second, so the launcher is slower than either runtime alone. Measured on
|
|
24
|
-
* npmjs-mcp (windows-arm64, n=12 medians, spawn to first MCP initialize):
|
|
25
|
-
* oam 116ms, node 172ms, launcher 243ms. oam is the fastest runtime and the
|
|
26
|
-
* launcher is the slowest path -- it exists for `npx` convenience.
|
|
27
|
-
*
|
|
28
|
-
* For an MCP host config, point straight at oam and skip this file:
|
|
29
|
-
* { "command": "oam", "args": ["run", "<abs>/dist/index.js"] }
|
|
30
|
-
*
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runtime launcher for @yawlabs/caddy-mcp.
|
|
4
|
+
*
|
|
5
|
+
* Prefers the oam runtime (https://oamjs.org) and falls back to the Node
|
|
6
|
+
* process already running this file.
|
|
7
|
+
*
|
|
8
|
+
* Unlike npmjs-mcp, this server is NOT a zero-dependency bundle -- dist/
|
|
9
|
+
* imports @modelcontextprotocol/sdk and zod from node_modules at runtime. That
|
|
10
|
+
* is fine on both paths: oam does npm resolution against an existing
|
|
11
|
+
* node_modules with CommonJS interop, and it was verified here before this
|
|
12
|
+
* launcher was written (`oam run dist/index.js -- --version` prints the same
|
|
13
|
+
* version Node does).
|
|
14
|
+
*
|
|
15
|
+
* WHY THE FALLBACK COSTS NOTHING
|
|
16
|
+
* npm has already started Node to run this launcher, so falling back is a
|
|
17
|
+
* plain `import()` of the server into THIS process: no extra spawn, no extra
|
|
18
|
+
* startup, byte-identical to invoking dist/index.js directly. Discovery is
|
|
19
|
+
* stat-only -- never a subprocess -- so the miss case stays sub-millisecond.
|
|
20
|
+
*
|
|
21
|
+
* WHAT THE OAM PATH COSTS
|
|
22
|
+
* Reaching oam through an npm `bin` means Node boots first and oam boots
|
|
23
|
+
* second, so the launcher is slower than either runtime alone. Measured on
|
|
24
|
+
* npmjs-mcp (windows-arm64, n=12 medians, spawn to first MCP initialize):
|
|
25
|
+
* oam 116ms, node 172ms, launcher 243ms. oam is the fastest runtime and the
|
|
26
|
+
* launcher is the slowest path -- it exists for `npx` convenience.
|
|
27
|
+
*
|
|
28
|
+
* For an MCP host config, point straight at oam and skip this file:
|
|
29
|
+
* { "command": "oam", "args": ["run", "<abs>/dist/index.js"] }
|
|
30
|
+
*
|
|
31
31
|
* THE `--permission` SANDBOX (oam 0.9.0+, opt-in)
|
|
32
32
|
* `CADDY_MCP_SANDBOX=1` runs the server under oam's permission model.
|
|
33
33
|
*
|
|
@@ -52,71 +52,74 @@
|
|
|
52
52
|
* An older oam is not an error: the launcher falls back to Node and says so on
|
|
53
53
|
* stderr. Pinning the floor here is what makes that fallback automatic.
|
|
54
54
|
*
|
|
55
|
-
* SELECTION
|
|
56
|
-
* CADDY_MCP_RUNTIME=oam require oam; fail loudly if it is missing
|
|
57
|
-
* CADDY_MCP_RUNTIME=node never use oam
|
|
58
|
-
* CADDY_MCP_RUNTIME=auto prefer oam, silently fall back (default)
|
|
55
|
+
* SELECTION
|
|
56
|
+
* CADDY_MCP_RUNTIME=oam require oam; fail loudly if it is missing
|
|
57
|
+
* CADDY_MCP_RUNTIME=node never use oam
|
|
58
|
+
* CADDY_MCP_RUNTIME=auto prefer oam, silently fall back (default)
|
|
59
59
|
* CADDY_MCP_SANDBOX=1 run oam under --permission (oam 0.9.0+)
|
|
60
|
-
* OAM_BIN=/path/to/oam explicit binary, checked before any discovery
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
import { execFileSync, spawn } from "node:child_process";
|
|
64
|
-
import { existsSync } from "node:fs";
|
|
65
|
-
import { constants, homedir } from "node:os";
|
|
66
|
-
import { delimiter, join } from "node:path";
|
|
60
|
+
* OAM_BIN=/path/to/oam explicit binary, checked before any discovery
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
64
|
+
import { existsSync } from "node:fs";
|
|
65
|
+
import { constants, homedir } from "node:os";
|
|
66
|
+
import { delimiter, join } from "node:path";
|
|
67
67
|
import { fileURLToPath } from "node:url";
|
|
68
68
|
|
|
69
69
|
/** Oldest oam whose `child_process` matches Node. See MINIMUM OAM VERSION above. */
|
|
70
|
-
const OAM_MIN = [0, 9, 0];
|
|
71
|
-
|
|
72
|
-
// Two forms, deliberately. `import()` on Windows REJECTS a bare `C:\...` path
|
|
73
|
-
// with ERR_UNSUPPORTED_ESM_URL_SCHEME (it reads `c:` as a protocol), so the
|
|
74
|
-
// in-process fallback must use the file:// URL. spawn() needs a real path.
|
|
75
|
-
const SERVER_URL = new URL("../dist/index.js", import.meta.url);
|
|
76
|
-
const SERVER_ENTRY = fileURLToPath(SERVER_URL);
|
|
77
|
-
const isWin = process.platform === "win32";
|
|
78
|
-
const exe = isWin ? "oam.exe" : "oam";
|
|
79
|
-
|
|
80
|
-
/** Locate an oam binary, or null. Every branch is a stat, never a subprocess. */
|
|
81
|
-
function findOam() {
|
|
82
|
-
// 1. Explicit override wins and is never second-guessed.
|
|
83
|
-
const override = process.env.OAM_BIN;
|
|
84
|
-
if (override) return existsSync(override) ? override : null;
|
|
85
|
-
|
|
86
|
-
// 2. Installed locations, BEFORE PATH. Someone who develops oam itself
|
|
87
|
-
// usually has oam/target/release on PATH, and a build directory is the
|
|
88
|
-
// wrong thing for a user-facing launcher to bind to: cargo replaces the
|
|
89
|
-
// binary underneath running processes, and the dev build is not the
|
|
90
|
-
// release the user installed. Preferring the installed copy makes the
|
|
91
|
-
// default path "what a normal user has", and OAM_BIN remains the way to
|
|
92
|
-
// point deliberately at a dev build.
|
|
93
|
-
//
|
|
94
|
-
// Both forms are checked on Windows: the installer defaults to
|
|
95
|
-
// %LOCALAPPDATA
|
|
96
|
-
// OAM_INSTALL_DIR can pick either, so checking one silently misses a real
|
|
97
|
-
// install.
|
|
98
|
-
const installed = [join(homedir(), ".oam", "bin", exe)];
|
|
99
|
-
if (isWin) {
|
|
100
|
-
installed.unshift(join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), "oam", "bin", exe));
|
|
101
|
-
}
|
|
102
|
-
for (const candidate of installed) {
|
|
103
|
-
if (existsSync(candidate)) return candidate;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// 3. PATH, resolved manually rather than by spawning `which`/`where`, which
|
|
107
|
-
// would cost a subprocess on every launch just to decide whether to spawn.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
|
|
70
|
+
const OAM_MIN = [0, 9, 0];
|
|
71
|
+
|
|
72
|
+
// Two forms, deliberately. `import()` on Windows REJECTS a bare `C:\...` path
|
|
73
|
+
// with ERR_UNSUPPORTED_ESM_URL_SCHEME (it reads `c:` as a protocol), so the
|
|
74
|
+
// in-process fallback must use the file:// URL. spawn() needs a real path.
|
|
75
|
+
const SERVER_URL = new URL("../dist/index.js", import.meta.url);
|
|
76
|
+
const SERVER_ENTRY = fileURLToPath(SERVER_URL);
|
|
77
|
+
const isWin = process.platform === "win32";
|
|
78
|
+
const exe = isWin ? "oam.exe" : "oam";
|
|
79
|
+
|
|
80
|
+
/** Locate an oam binary, or null. Every branch is a stat, never a subprocess. */
|
|
81
|
+
function findOam() {
|
|
82
|
+
// 1. Explicit override wins and is never second-guessed.
|
|
83
|
+
const override = process.env.OAM_BIN;
|
|
84
|
+
if (override) return existsSync(override) ? override : null;
|
|
85
|
+
|
|
86
|
+
// 2. Installed locations, BEFORE PATH. Someone who develops oam itself
|
|
87
|
+
// usually has oam/target/release on PATH, and a build directory is the
|
|
88
|
+
// wrong thing for a user-facing launcher to bind to: cargo replaces the
|
|
89
|
+
// binary underneath running processes, and the dev build is not the
|
|
90
|
+
// release the user installed. Preferring the installed copy makes the
|
|
91
|
+
// default path "what a normal user has", and OAM_BIN remains the way to
|
|
92
|
+
// point deliberately at a dev build.
|
|
93
|
+
//
|
|
94
|
+
// Both forms are checked on Windows: the installer defaults to
|
|
95
|
+
// %LOCALAPPDATA%\oam\bin there, but oam's docs name ~/.oam/bin first and
|
|
96
|
+
// OAM_INSTALL_DIR can pick either, so checking one silently misses a real
|
|
97
|
+
// install.
|
|
98
|
+
const installed = [join(homedir(), ".oam", "bin", exe)];
|
|
99
|
+
if (isWin) {
|
|
100
|
+
installed.unshift(join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), "oam", "bin", exe));
|
|
101
|
+
}
|
|
102
|
+
for (const candidate of installed) {
|
|
103
|
+
if (existsSync(candidate)) return candidate;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 3. PATH, resolved manually rather than by spawning `which`/`where`, which
|
|
107
|
+
// would cost a subprocess on every launch just to decide whether to spawn.
|
|
108
|
+
// Windows: `.exe` ONLY -- deliberately narrower than PATHEXT. Node refuses to
|
|
109
|
+
// run a .cmd/.bat through execFile/spawn without `shell: true` (EINVAL, and
|
|
110
|
+
// for spawn it throws SYNCHRONOUSLY rather than emitting 'error'), so walking
|
|
111
|
+
// the full PATHEXT list would hand back a path this launcher cannot execute.
|
|
112
|
+
// Discovery has to agree with execution. A skipped shim is still reported --
|
|
113
|
+
// see findOamShim.
|
|
114
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
115
|
+
if (!dir) continue;
|
|
116
|
+
const candidate = join(dir, exe);
|
|
117
|
+
if (existsSync(candidate)) return candidate;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
120
123
|
/**
|
|
121
124
|
* `oam --version` -> [major, minor, patch], or null when it cannot be read.
|
|
122
125
|
* A pre-release suffix (0.9.0-rc.1) truncates to its base version.
|
|
@@ -183,87 +186,243 @@ function sandboxFlags() {
|
|
|
183
186
|
return flags;
|
|
184
187
|
}
|
|
185
188
|
|
|
186
|
-
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if (
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Write a diagnostic to stderr synchronously, so a following process.exit
|
|
191
|
+
* cannot truncate it.
|
|
192
|
+
*
|
|
193
|
+
* Not a bare writeSync: that call can short-write (it returns a byte count) and
|
|
194
|
+
* on macOS it can throw EAGAIN, because Node makes a piped stderr non-blocking
|
|
195
|
+
* there rather than blocking the write. Loop over the remaining bytes, and if
|
|
196
|
+
* stderr turns out to be unusable give up quietly -- failing to print a
|
|
197
|
+
* diagnostic is not worth crashing a stdio server over.
|
|
198
|
+
*/
|
|
199
|
+
async function errSync(message) {
|
|
200
|
+
const { writeSync } = await import("node:fs");
|
|
201
|
+
const buf = Buffer.from(message);
|
|
202
|
+
let off = 0;
|
|
203
|
+
for (let attempts = 0; off < buf.length && attempts < 1000; attempts++) {
|
|
204
|
+
try {
|
|
205
|
+
off += writeSync(2, buf, off, buf.length - off);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
if (err?.code !== "EAGAIN") return;
|
|
208
|
+
// Pipe is full and the reader has not drained yet -- retry.
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* An oam-named .cmd/.bat on PATH: a real install in a shape this launcher
|
|
215
|
+
* cannot spawn. Reported rather than ignored, because "no oam binary was found"
|
|
216
|
+
* reads as "install oam" -- the one thing that will not help. Windows only;
|
|
217
|
+
* there is no such shim concept on POSIX.
|
|
218
|
+
*/
|
|
219
|
+
function findOamShim() {
|
|
220
|
+
if (!isWin) return null;
|
|
221
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
222
|
+
if (!dir) continue;
|
|
223
|
+
for (const ext of [".cmd", ".bat"]) {
|
|
224
|
+
const candidate = join(dir, `oam${ext}`);
|
|
225
|
+
if (existsSync(candidate)) return candidate;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Run the server in THIS process. The zero-overhead fallback. */
|
|
232
|
+
async function runInProcess() {
|
|
233
|
+
// A server may gate its bootstrap on being the process ENTRY POINT --
|
|
234
|
+
// `import.meta.url === pathToFileURL(process.argv[1]).href` -- so that its own
|
|
235
|
+
// test file can import the module for unit tests without connecting a stdio
|
|
236
|
+
// transport. aws-mcp does exactly this. Importing the server here would leave
|
|
237
|
+
// argv[1] pointing at THIS launcher, the guard would read false, and the
|
|
238
|
+
// server would load but never serve: the MCP handshake just hangs.
|
|
239
|
+
//
|
|
240
|
+
// Point argv[1] at the server first, so the in-process path is
|
|
241
|
+
// indistinguishable from having executed the file directly. The spawn path
|
|
242
|
+
// needs no equivalent -- there argv[1] is already the server.
|
|
243
|
+
process.argv[1] = SERVER_ENTRY;
|
|
244
|
+
await import(SERVER_URL.href);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const mode = (process.env.CADDY_MCP_RUNTIME ?? "auto").toLowerCase();
|
|
248
|
+
|
|
249
|
+
if (mode === "node") {
|
|
250
|
+
await runInProcess();
|
|
251
|
+
} else {
|
|
252
|
+
const oam = findOam();
|
|
253
|
+
// Read the version ONCE, and only when discovery found something: the gate
|
|
254
|
+
// below has to tell "too old" apart from "could not be read at all", and
|
|
255
|
+
// re-probing inside the branch would cost a second subprocess.
|
|
256
|
+
//
|
|
257
|
+
// This is the first subprocess the launcher runs -- discovery itself is
|
|
258
|
+
// stat-only. Paid on every launch that finds an oam, including the ones
|
|
259
|
+
// that go on to fall back to Node.
|
|
260
|
+
const found = oam ? oamVersion(oam) : null;
|
|
261
|
+
|
|
262
|
+
if (!oam) {
|
|
263
|
+
// An oam-named .cmd/.bat on PATH is a real install in a shape this
|
|
264
|
+
// launcher cannot spawn. Naming it turns "no oam binary was found" --
|
|
265
|
+
// which reads as "install oam", the one thing that will not help --
|
|
266
|
+
// into something the user can act on.
|
|
267
|
+
const oamShim = findOamShim();
|
|
268
|
+
const shimNote = oamShim
|
|
269
|
+
? `Found ${oamShim}, but Node cannot execute a .cmd/.bat directly.\n` +
|
|
270
|
+
"Install the native oam binary, or point OAM_BIN at one.\n"
|
|
271
|
+
: "";
|
|
272
|
+
if (mode === "oam") {
|
|
273
|
+
// Explicitly demanded, so this is a real misconfiguration. writeSync
|
|
274
|
+
// because stderr is async for TTYs/pipes on Windows and process.exit
|
|
275
|
+
// truncates pending writes.
|
|
276
|
+
const { writeSync } = await import("node:fs");
|
|
277
|
+
writeSync(
|
|
278
|
+
2,
|
|
279
|
+
"caddy-mcp: CADDY_MCP_RUNTIME=oam but no runnable oam binary was found.\n" + shimNote +
|
|
280
|
+
"Install from https://oamjs.org, set OAM_BIN=/path/to/oam, or use CADDY_MCP_RUNTIME=node.\n",
|
|
281
|
+
);
|
|
282
|
+
process.exit(1);
|
|
283
|
+
}
|
|
284
|
+
// auto: falling back is correct, but silence is how someone never learns
|
|
285
|
+
// their oam install is a shape this launcher skips.
|
|
286
|
+
if (oamShim) await errSync(`caddy-mcp: ${shimNote}Using Node instead.\n`);
|
|
287
|
+
await runInProcess();
|
|
288
|
+
} else if (!atLeast(found, OAM_MIN)) {
|
|
289
|
+
const min = OAM_MIN.join(".");
|
|
290
|
+
// Two different causes reach this branch and they need different
|
|
291
|
+
// remedies. `found === null` is NOT "old": oamVersion returns null when
|
|
292
|
+
// the binary could not be run at all (not executable, wrong arch, a
|
|
293
|
+
// .cmd/.bat Node refuses, deleted between the stat and the probe) or
|
|
294
|
+
// when its --version output did not parse. Telling that user to
|
|
295
|
+
// `oam self-update` sends them after the one cause it definitely is not.
|
|
296
|
+
const detail = found
|
|
297
|
+
? `${oam} is oam ${found.join(".")}, older than ${min}`
|
|
298
|
+
: `${oam} could not be run, or did not report a version this launcher understands`;
|
|
299
|
+
const remedy = found
|
|
300
|
+
? "Run `oam self-update`, or use CADDY_MCP_RUNTIME=node.\n"
|
|
301
|
+
: "Check that it is an executable oam binary for this platform, or use CADDY_MCP_RUNTIME=node.\n";
|
|
302
|
+
if (mode === "oam") {
|
|
303
|
+
await errSync(`caddy-mcp: CADDY_MCP_RUNTIME=oam but ${detail}.\n${remedy}`);
|
|
304
|
+
process.exit(1);
|
|
305
|
+
}
|
|
306
|
+
// auto: neither cause is worth failing over -- prefer Node. Say so,
|
|
307
|
+
// because a silent downgrade is how someone keeps running an oam they
|
|
308
|
+
// meant to update, or never learns their oam is unexecutable.
|
|
309
|
+
await errSync(`caddy-mcp: ${detail}; using Node instead.\n`);
|
|
310
|
+
await runInProcess();
|
|
311
|
+
} else {
|
|
312
|
+
// `--` separates oam's own flags from the script's argv, so `caddy-mcp
|
|
313
|
+
// --version` and any host-supplied flags survive the hop unchanged.
|
|
314
|
+
// Every "oam could not be executed" outcome lands here: the synchronous
|
|
315
|
+
// throw from spawn() and the async 'error' event mean the same thing and
|
|
316
|
+
// must degrade the same way, so the handling lives in one place.
|
|
317
|
+
// errSync rather than process.stderr.write because stderr is async for
|
|
318
|
+
// TTYs and pipes on Windows and the process.exit below truncates pending
|
|
319
|
+
// writes.
|
|
320
|
+
const launchFailed = async (err) => {
|
|
321
|
+
if (mode === "oam") {
|
|
322
|
+
await errSync(`caddy-mcp: failed to launch oam (${err?.message ?? err})\n`);
|
|
323
|
+
process.exit(1);
|
|
324
|
+
}
|
|
325
|
+
await runInProcess();
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// ONE reporter shared by both launchFailed call sites, so the sync-throw
|
|
329
|
+
// path and the 'error'-event path cannot drift apart. Either can reject:
|
|
330
|
+
// runInProcess() is a bare import() that rejects when dist/index.js is
|
|
331
|
+
// missing, and at ESM top level an unhandled rejection is an uncaught
|
|
332
|
+
// exception -- the exact failure this handling exists to prevent.
|
|
333
|
+
const fallbackFailed = (e) => {
|
|
334
|
+
process.stderr.write(`caddy-mcp: fallback to Node failed (${e?.message ?? e})\n`);
|
|
335
|
+
process.exitCode = 1;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
let child = null;
|
|
339
|
+
try {
|
|
340
|
+
child = spawn(oam, [...sandboxFlags(), "run", SERVER_ENTRY, "--", ...process.argv.slice(2)], {
|
|
341
|
+
// inherit keeps the SAME fds, so MCP's newline-delimited JSON framing on
|
|
342
|
+
// stdin/stdout is untouched and the host's stdin-close still reaches the
|
|
343
|
+
// server's shutdown path.
|
|
344
|
+
stdio: "inherit",
|
|
345
|
+
env: process.env,
|
|
346
|
+
windowsHide: true,
|
|
347
|
+
});
|
|
348
|
+
} catch (err) {
|
|
349
|
+
// spawn() THROWS for some failures instead of emitting 'error', and the
|
|
350
|
+
// 'error' listener is registered AFTER this call, so it can never observe
|
|
351
|
+
// one -- an uncaught throw here kills the launcher with a raw stack trace
|
|
352
|
+
// instead of falling back to Node.
|
|
353
|
+
await launchFailed(err).catch(fallbackFailed);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (child) {
|
|
357
|
+
|
|
358
|
+
// If oam cannot be executed at all (deleted between the stat and the spawn,
|
|
359
|
+
// wrong arch, permission), fall back rather than failing the whole server.
|
|
360
|
+
// `spawned` prevents falling back AFTER the child started, which would
|
|
361
|
+
// double-start the server on the same stdio.
|
|
362
|
+
let spawned = false;
|
|
363
|
+
child.on("spawn", () => {
|
|
364
|
+
spawned = true;
|
|
365
|
+
});
|
|
366
|
+
child.on("error", (err) => {
|
|
367
|
+
if (spawned) return;
|
|
368
|
+
// Handle the rejection instead of discarding it: a failing in-process
|
|
369
|
+
// fallback would otherwise escape as an unhandled rejection, replacing
|
|
370
|
+
// this launcher's diagnostic with a raw stack trace.
|
|
371
|
+
launchFailed(err).catch(fallbackFailed);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
// Forward termination so the server's own shutdown path runs in the child
|
|
375
|
+
// rather than the child being orphaned.
|
|
376
|
+
//
|
|
377
|
+
// Registering ANY handler for these suppresses Node's default
|
|
378
|
+
// terminate-on-signal, so the parent's exit has to be arranged explicitly.
|
|
379
|
+
// `child.killed` only records that kill() was CALLED, never that the child
|
|
380
|
+
// is gone, so gating on it swallows every signal after the first and wedges
|
|
381
|
+
// the launcher with no escape hatch.
|
|
382
|
+
//
|
|
383
|
+
// Escalation is driven by a TIMER, not by counting signals. Counting is
|
|
384
|
+
// ambiguous: a supervisor routinely sends SIGINT then SIGTERM milliseconds
|
|
385
|
+
// apart, and a terminal Ctrl-C reaches the whole process group, so reading
|
|
386
|
+
// "a second signal" as impatience hard-kills a child that is already
|
|
387
|
+
// shutting down cleanly. A timer makes the count irrelevant -- ONE press is
|
|
388
|
+
// enough, and a wedged child dies on schedule. setTimeout is monotonic, so
|
|
389
|
+
// a wall-clock step cannot mis-gate the window either.
|
|
390
|
+
//
|
|
391
|
+
// POSIX vs Windows, and why we do NOT forward on Windows.
|
|
392
|
+
// On POSIX child.kill(sig) delivers a real, catchable signal, so forwarding
|
|
393
|
+
// is what lets the child run its shutdown. On Windows there are no POSIX
|
|
394
|
+
// signals: child.kill IGNORES the name and calls TerminateProcess -- an
|
|
395
|
+
// immediate hard kill (verified: a child with a SIGTERM handler never runs
|
|
396
|
+
// it and dies with code=null, signal=SIGTERM). Forwarding there ABORTS the
|
|
397
|
+
// graceful shutdown the console's own Ctrl-C just started, skipping the
|
|
398
|
+
// child's process.on("exit") cleanup. The console has already notified the
|
|
399
|
+
// child, so on Windows the timer below is the only kill we issue.
|
|
400
|
+
const ESCALATE_AFTER_MS = 2000;
|
|
401
|
+
let escalation = null;
|
|
402
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
403
|
+
process.on(sig, () => {
|
|
404
|
+
// No try/catch: kill() on an already-exited child returns false, it does
|
|
405
|
+
// not throw. It throws only for a signal the platform does not know,
|
|
406
|
+
// which SIGINT/SIGTERM/SIGKILL never are.
|
|
407
|
+
if (!isWin) child.kill(sig);
|
|
408
|
+
if (escalation) return; // already counting down; further signals are noise
|
|
409
|
+
escalation = setTimeout(() => {
|
|
410
|
+
// Still here after its grace window. Stop waiting on it.
|
|
411
|
+
child.kill("SIGKILL");
|
|
412
|
+
process.exit(128 + (constants.signals[sig] ?? 15));
|
|
413
|
+
}, ESCALATE_AFTER_MS);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
child.on("exit", (code, signal) => {
|
|
418
|
+
if (escalation) clearTimeout(escalation);
|
|
419
|
+
// Mirror the child's fate: a signal death becomes 128+n so callers see a
|
|
420
|
+
// conventional shell exit status rather than a bare 0.
|
|
421
|
+
if (signal) {
|
|
422
|
+
process.exit(128 + (constants.signals[signal] ?? 15));
|
|
423
|
+
}
|
|
424
|
+
process.exit(code ?? 0);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|