@yawlabs/caddy-mcp 2.0.0 → 2.1.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.
Files changed (2) hide show
  1. package/bin/caddy-mcp.mjs +175 -0
  2. package/package.json +3 -2
@@ -0,0 +1,175 @@
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
+ * SELECTION
32
+ * CADDY_MCP_RUNTIME=oam require oam; fail loudly if it is missing
33
+ * CADDY_MCP_RUNTIME=node never use oam
34
+ * CADDY_MCP_RUNTIME=auto prefer oam, silently fall back (default)
35
+ * OAM_BIN=/path/to/oam explicit binary, checked before any discovery
36
+ */
37
+
38
+ import { spawn } from "node:child_process";
39
+ import { existsSync } from "node:fs";
40
+ import { constants, homedir } from "node:os";
41
+ import { delimiter, join } from "node:path";
42
+ import { fileURLToPath } from "node:url";
43
+
44
+ // Two forms, deliberately. `import()` on Windows REJECTS a bare `C:\...` path
45
+ // with ERR_UNSUPPORTED_ESM_URL_SCHEME (it reads `c:` as a protocol), so the
46
+ // in-process fallback must use the file:// URL. spawn() needs a real path.
47
+ const SERVER_URL = new URL("../dist/index.js", import.meta.url);
48
+ const SERVER_ENTRY = fileURLToPath(SERVER_URL);
49
+ const isWin = process.platform === "win32";
50
+ const exe = isWin ? "oam.exe" : "oam";
51
+
52
+ /** Locate an oam binary, or null. Every branch is a stat, never a subprocess. */
53
+ function findOam() {
54
+ // 1. Explicit override wins and is never second-guessed.
55
+ const override = process.env.OAM_BIN;
56
+ if (override) return existsSync(override) ? override : null;
57
+
58
+ // 2. Installed locations, BEFORE PATH. Someone who develops oam itself
59
+ // usually has oam/target/release on PATH, and a build directory is the
60
+ // wrong thing for a user-facing launcher to bind to: cargo replaces the
61
+ // binary underneath running processes, and the dev build is not the
62
+ // release the user installed. Preferring the installed copy makes the
63
+ // default path "what a normal user has", and OAM_BIN remains the way to
64
+ // point deliberately at a dev build.
65
+ //
66
+ // Both forms are checked on Windows: the installer defaults to
67
+ // %LOCALAPPDATA%oamin there, but oam's docs name ~/.oam/bin first and
68
+ // OAM_INSTALL_DIR can pick either, so checking one silently misses a real
69
+ // install.
70
+ const installed = [join(homedir(), ".oam", "bin", exe)];
71
+ if (isWin) {
72
+ installed.unshift(join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), "oam", "bin", exe));
73
+ }
74
+ for (const candidate of installed) {
75
+ if (existsSync(candidate)) return candidate;
76
+ }
77
+
78
+ // 3. PATH, resolved manually rather than by spawning `which`/`where`, which
79
+ // would cost a subprocess on every launch just to decide whether to spawn.
80
+ const pathExt = isWin ? (process.env.PATHEXT ?? ".EXE").split(";").filter(Boolean) : [""];
81
+ for (const dir of (process.env.PATH ?? "").split(delimiter)) {
82
+ if (!dir) continue;
83
+ for (const ext of isWin ? pathExt : [""]) {
84
+ const candidate = join(dir, isWin ? `oam${ext.toLowerCase()}` : "oam");
85
+ if (existsSync(candidate)) return candidate;
86
+ }
87
+ }
88
+
89
+ return null;
90
+ }
91
+
92
+ /** Run the server in THIS process. The zero-overhead fallback. */
93
+ async function runInProcess() {
94
+ // A server may gate its bootstrap on being the process ENTRY POINT --
95
+ // `import.meta.url === pathToFileURL(process.argv[1]).href` -- so that its own
96
+ // test file can import the module for unit tests without connecting a stdio
97
+ // transport. aws-mcp does exactly this. Importing the server here would leave
98
+ // argv[1] pointing at THIS launcher, the guard would read false, and the
99
+ // server would load but never serve: the MCP handshake just hangs.
100
+ //
101
+ // Point argv[1] at the server first, so the in-process path is
102
+ // indistinguishable from having executed the file directly. The spawn path
103
+ // needs no equivalent -- there argv[1] is already the server.
104
+ process.argv[1] = SERVER_ENTRY;
105
+ await import(SERVER_URL.href);
106
+ }
107
+
108
+ const mode = (process.env.CADDY_MCP_RUNTIME ?? "auto").toLowerCase();
109
+
110
+ if (mode === "node") {
111
+ await runInProcess();
112
+ } else {
113
+ const oam = findOam();
114
+
115
+ if (!oam) {
116
+ if (mode === "oam") {
117
+ // Explicitly demanded, so this is a real misconfiguration. writeSync
118
+ // because stderr is async for TTYs/pipes on Windows and process.exit
119
+ // truncates pending writes.
120
+ const { writeSync } = await import("node:fs");
121
+ writeSync(
122
+ 2,
123
+ "caddy-mcp: CADDY_MCP_RUNTIME=oam but no oam binary was found.\n" +
124
+ "Install from https://oamjs.org, set OAM_BIN=/path/to/oam, or use CADDY_MCP_RUNTIME=node.\n",
125
+ );
126
+ process.exit(1);
127
+ }
128
+ await runInProcess();
129
+ } else {
130
+ // `--` separates oam's own flags from the script's argv, so `caddy-mcp
131
+ // --version` and any host-supplied flags survive the hop unchanged.
132
+ const child = spawn(oam, ["run", SERVER_ENTRY, "--", ...process.argv.slice(2)], {
133
+ // inherit keeps the SAME fds, so MCP's newline-delimited JSON framing on
134
+ // stdin/stdout is untouched and the host's stdin-close still reaches the
135
+ // server's shutdown path.
136
+ stdio: "inherit",
137
+ env: process.env,
138
+ windowsHide: true,
139
+ });
140
+
141
+ // If oam cannot be executed at all (deleted between the stat and the spawn,
142
+ // wrong arch, permission), fall back rather than failing the whole server.
143
+ // `spawned` prevents falling back AFTER the child started, which would
144
+ // double-start the server on the same stdio.
145
+ let spawned = false;
146
+ child.on("spawn", () => {
147
+ spawned = true;
148
+ });
149
+ child.on("error", (err) => {
150
+ if (spawned) return;
151
+ if (mode === "oam") {
152
+ process.stderr.write(`caddy-mcp: failed to launch oam (${err.message})\n`);
153
+ process.exit(1);
154
+ }
155
+ void runInProcess();
156
+ });
157
+
158
+ // Forward termination so the server's own shutdown path runs in the child
159
+ // rather than the child being orphaned. No-op on Windows, harmless to add.
160
+ for (const sig of ["SIGINT", "SIGTERM"]) {
161
+ process.on(sig, () => {
162
+ if (!child.killed) child.kill(sig);
163
+ });
164
+ }
165
+
166
+ child.on("exit", (code, signal) => {
167
+ // Mirror the child's fate: a signal death becomes 128+n so callers see a
168
+ // conventional shell exit status rather than a bare 0.
169
+ if (signal) {
170
+ process.exit(128 + (constants.signals[signal] ?? 15));
171
+ }
172
+ process.exit(code ?? 0);
173
+ });
174
+ }
175
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yawlabs/caddy-mcp",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "mcpName": "io.github.YawLabs/caddy-mcp",
5
5
  "description": "MCP server for managing Caddy web servers via the admin API",
6
6
  "license": "MIT",
@@ -15,9 +15,10 @@
15
15
  "main": "./dist/server.js",
16
16
  "types": "./dist/server.d.ts",
17
17
  "bin": {
18
- "caddy-mcp": "dist/index.js"
18
+ "caddy-mcp": "bin/caddy-mcp.mjs"
19
19
  },
20
20
  "files": [
21
+ "bin/caddy-mcp.mjs",
21
22
  "dist",
22
23
  "!dist/**/*.test.*",
23
24
  "README.md",