@yawlabs/ssh-mcp 0.12.0 → 0.14.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/bin/ssh-mcp.mjs +236 -0
- package/package.json +3 -2
package/bin/ssh-mcp.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runtime launcher for @yawlabs/ssh-mcp.
|
|
4
|
+
*
|
|
5
|
+
* Prefers the oam runtime (https://oamjs.org) and falls back to the Node
|
|
6
|
+
* process already running this file.
|
|
7
|
+
*
|
|
8
|
+
*
|
|
9
|
+
* WHY THE FALLBACK COSTS NOTHING
|
|
10
|
+
* npm has already started Node to run this launcher, so falling back is a
|
|
11
|
+
* plain `import()` of the server into THIS process: no extra spawn, no extra
|
|
12
|
+
* startup, byte-identical to invoking dist/index.js directly. Discovery is
|
|
13
|
+
* stat-only -- never a subprocess -- so the miss case stays sub-millisecond.
|
|
14
|
+
*
|
|
15
|
+
* WHAT THE OAM PATH COSTS
|
|
16
|
+
* Reaching oam through an npm `bin` means Node boots first and oam boots
|
|
17
|
+
* second, so the launcher is slower than either runtime alone. Measured on
|
|
18
|
+
* npmjs-mcp (windows-arm64, n=12 medians, spawn to first MCP initialize):
|
|
19
|
+
* oam 116ms, node 172ms, launcher 243ms. oam is the fastest runtime and the
|
|
20
|
+
* launcher is the slowest path -- it exists for `npx` convenience.
|
|
21
|
+
*
|
|
22
|
+
* For an MCP host config, point straight at oam and skip this file:
|
|
23
|
+
* { "command": "oam", "args": ["run", "<abs>/dist/index.js"] }
|
|
24
|
+
*
|
|
25
|
+
* NO SANDBOX HERE -- DELIBERATELY
|
|
26
|
+
* The purpose of this server is to open outbound SSH to hosts the caller names
|
|
27
|
+
* at run time and run commands there, so the net and child-process grants would
|
|
28
|
+
* both have to be unrestricted, and key material plus known_hosts need the
|
|
29
|
+
* filesystem. Nothing meaningful is left to deny, so `--permission` is not
|
|
30
|
+
* wired up here.
|
|
31
|
+
*
|
|
32
|
+
* MINIMUM OAM VERSION
|
|
33
|
+
* 0.9.0. Below it `child_process.execFile` ran its arguments through a SHELL,
|
|
34
|
+
* `exec` accepted `timeout` and ignored it, `spawnSync` truncated at
|
|
35
|
+
* `maxBuffer` while reporting success, and `stdio: 'inherit'`/`'ignore'` both
|
|
36
|
+
* behaved as `'pipe'`. This server shells out to a CLI on its
|
|
37
|
+
* main paths, so those were reachable bugs rather than theoretical ones: an
|
|
38
|
+
* argument containing shell metacharacters was re-split and executed.
|
|
39
|
+
* An older oam is not an error: the launcher falls back to Node and says so on
|
|
40
|
+
* stderr. Pinning the floor here is what makes that fallback automatic.
|
|
41
|
+
*
|
|
42
|
+
* SELECTION
|
|
43
|
+
* SSH_MCP_RUNTIME=oam require oam; fail loudly if it is missing
|
|
44
|
+
* SSH_MCP_RUNTIME=node never use oam
|
|
45
|
+
* SSH_MCP_RUNTIME=auto prefer oam, silently fall back (default)
|
|
46
|
+
* OAM_BIN=/path/to/oam explicit binary, checked before any discovery
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
50
|
+
import { existsSync } from "node:fs";
|
|
51
|
+
import { constants, homedir } from "node:os";
|
|
52
|
+
import { delimiter, join } from "node:path";
|
|
53
|
+
import { fileURLToPath } from "node:url";
|
|
54
|
+
|
|
55
|
+
/** Oldest oam whose `child_process` matches Node. See MINIMUM OAM VERSION above. */
|
|
56
|
+
const OAM_MIN = [0, 9, 0];
|
|
57
|
+
|
|
58
|
+
// Two forms, deliberately. `import()` on Windows REJECTS a bare `C:\...` path
|
|
59
|
+
// with ERR_UNSUPPORTED_ESM_URL_SCHEME (it reads `c:` as a protocol), so the
|
|
60
|
+
// in-process fallback must use the file:// URL. spawn() needs a real path.
|
|
61
|
+
const SERVER_URL = new URL("../dist/index.js", import.meta.url);
|
|
62
|
+
const SERVER_ENTRY = fileURLToPath(SERVER_URL);
|
|
63
|
+
const isWin = process.platform === "win32";
|
|
64
|
+
const exe = isWin ? "oam.exe" : "oam";
|
|
65
|
+
|
|
66
|
+
/** Locate an oam binary, or null. Every branch is a stat, never a subprocess. */
|
|
67
|
+
function findOam() {
|
|
68
|
+
// 1. Explicit override wins and is never second-guessed.
|
|
69
|
+
const override = process.env.OAM_BIN;
|
|
70
|
+
if (override) return existsSync(override) ? override : null;
|
|
71
|
+
|
|
72
|
+
// 2. Installed locations, BEFORE PATH. Someone who develops oam itself
|
|
73
|
+
// usually has oam/target/release on PATH, and a build directory is the
|
|
74
|
+
// wrong thing for a user-facing launcher to bind to: cargo replaces the
|
|
75
|
+
// binary underneath running processes, and the dev build is not the
|
|
76
|
+
// release the user installed. Preferring the installed copy makes the
|
|
77
|
+
// default path "what a normal user has", and OAM_BIN remains the way to
|
|
78
|
+
// point deliberately at a dev build.
|
|
79
|
+
//
|
|
80
|
+
// Both forms are checked on Windows: the installer defaults to
|
|
81
|
+
// %LOCALAPPDATA%oamin there, but oam's docs name ~/.oam/bin first and
|
|
82
|
+
// OAM_INSTALL_DIR can pick either, so checking one silently misses a real
|
|
83
|
+
// install.
|
|
84
|
+
const installed = [join(homedir(), ".oam", "bin", exe)];
|
|
85
|
+
if (isWin) {
|
|
86
|
+
installed.unshift(join(process.env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), "oam", "bin", exe));
|
|
87
|
+
}
|
|
88
|
+
for (const candidate of installed) {
|
|
89
|
+
if (existsSync(candidate)) return candidate;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 3. PATH, resolved manually rather than by spawning `which`/`where`, which
|
|
93
|
+
// would cost a subprocess on every launch just to decide whether to spawn.
|
|
94
|
+
const pathExt = isWin ? (process.env.PATHEXT ?? ".EXE").split(";").filter(Boolean) : [""];
|
|
95
|
+
for (const dir of (process.env.PATH ?? "").split(delimiter)) {
|
|
96
|
+
if (!dir) continue;
|
|
97
|
+
for (const ext of isWin ? pathExt : [""]) {
|
|
98
|
+
const candidate = join(dir, isWin ? `oam${ext.toLowerCase()}` : "oam");
|
|
99
|
+
if (existsSync(candidate)) return candidate;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* `oam --version` -> [major, minor, patch], or null when it cannot be read.
|
|
108
|
+
* A pre-release suffix (0.9.0-rc.1) truncates to its base version.
|
|
109
|
+
*/
|
|
110
|
+
function oamVersion(cmd) {
|
|
111
|
+
try {
|
|
112
|
+
const out = execFileSync(cmd, ["--version"], {
|
|
113
|
+
encoding: "utf-8",
|
|
114
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
115
|
+
});
|
|
116
|
+
const m = /(\d+)\.(\d+)\.(\d+)/.exec(out);
|
|
117
|
+
return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
|
|
118
|
+
} catch {
|
|
119
|
+
// Not executable, wrong arch, or deleted since the stat. Caller degrades.
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** True when `v` is at least `min`, comparing major/minor/patch in order. */
|
|
125
|
+
function atLeast(v, min) {
|
|
126
|
+
if (!v) return false;
|
|
127
|
+
for (let i = 0; i < min.length; i++) {
|
|
128
|
+
if (v[i] > min[i]) return true;
|
|
129
|
+
if (v[i] < min[i]) return false;
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Run the server in THIS process. The zero-overhead fallback. */
|
|
135
|
+
async function runInProcess() {
|
|
136
|
+
// A server may gate its bootstrap on being the process ENTRY POINT --
|
|
137
|
+
// `import.meta.url === pathToFileURL(process.argv[1]).href` -- so that its own
|
|
138
|
+
// test file can import the module for unit tests without connecting a stdio
|
|
139
|
+
// transport. aws-mcp does exactly this. Importing the server here would leave
|
|
140
|
+
// argv[1] pointing at THIS launcher, the guard would read false, and the
|
|
141
|
+
// server would load but never serve: the MCP handshake just hangs.
|
|
142
|
+
//
|
|
143
|
+
// Point argv[1] at the server first, so the in-process path is
|
|
144
|
+
// indistinguishable from having executed the file directly. The spawn path
|
|
145
|
+
// needs no equivalent -- there argv[1] is already the server.
|
|
146
|
+
process.argv[1] = SERVER_ENTRY;
|
|
147
|
+
await import(SERVER_URL.href);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const mode = (process.env.SSH_MCP_RUNTIME ?? "auto").toLowerCase();
|
|
151
|
+
|
|
152
|
+
if (mode === "node") {
|
|
153
|
+
await runInProcess();
|
|
154
|
+
} else {
|
|
155
|
+
const oam = findOam();
|
|
156
|
+
|
|
157
|
+
if (!oam) {
|
|
158
|
+
if (mode === "oam") {
|
|
159
|
+
// Explicitly demanded, so this is a real misconfiguration. writeSync
|
|
160
|
+
// because stderr is async for TTYs/pipes on Windows and process.exit
|
|
161
|
+
// truncates pending writes.
|
|
162
|
+
const { writeSync } = await import("node:fs");
|
|
163
|
+
writeSync(
|
|
164
|
+
2,
|
|
165
|
+
"ssh-mcp: SSH_MCP_RUNTIME=oam but no oam binary was found.\n" +
|
|
166
|
+
"Install from https://oamjs.org, set OAM_BIN=/path/to/oam, or use SSH_MCP_RUNTIME=node.\n",
|
|
167
|
+
);
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
await runInProcess();
|
|
171
|
+
} else if (!atLeast(oamVersion(oam), OAM_MIN)) {
|
|
172
|
+
// Discovery itself stays stat-only; this is the first subprocess, and it
|
|
173
|
+
// runs only once we have already decided to spawn oam anyway. Measured 26ms
|
|
174
|
+
// median (n=12, windows-arm64), paid once per MCP session.
|
|
175
|
+
const min = OAM_MIN.join(".");
|
|
176
|
+
if (mode === "oam") {
|
|
177
|
+
const { writeSync } = await import("node:fs");
|
|
178
|
+
writeSync(
|
|
179
|
+
2,
|
|
180
|
+
`ssh-mcp: SSH_MCP_RUNTIME=oam but ${oam} is older than oam ${min}.\n` +
|
|
181
|
+
`Run \`oam self-update\`, or use SSH_MCP_RUNTIME=node.\n`,
|
|
182
|
+
);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
}
|
|
185
|
+
// auto: an old oam is a reason to prefer Node, not to fail. Say so, because
|
|
186
|
+
// a silent downgrade is how someone keeps running an oam they meant to
|
|
187
|
+
// update. stderr is safe -- MCP frames travel on stdout.
|
|
188
|
+
process.stderr.write(`ssh-mcp: oam at ${oam} is older than ${min}; using Node instead.\n`);
|
|
189
|
+
await runInProcess();
|
|
190
|
+
} else {
|
|
191
|
+
// `--` separates oam's own flags from the script's argv, so `ssh-mcp
|
|
192
|
+
// --version` and any host-supplied flags survive the hop unchanged.
|
|
193
|
+
const child = spawn(oam, ["run", SERVER_ENTRY, "--", ...process.argv.slice(2)], {
|
|
194
|
+
// inherit keeps the SAME fds, so MCP's newline-delimited JSON framing on
|
|
195
|
+
// stdin/stdout is untouched and the host's stdin-close still reaches the
|
|
196
|
+
// server's shutdown path.
|
|
197
|
+
stdio: "inherit",
|
|
198
|
+
env: process.env,
|
|
199
|
+
windowsHide: true,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// If oam cannot be executed at all (deleted between the stat and the spawn,
|
|
203
|
+
// wrong arch, permission), fall back rather than failing the whole server.
|
|
204
|
+
// `spawned` prevents falling back AFTER the child started, which would
|
|
205
|
+
// double-start the server on the same stdio.
|
|
206
|
+
let spawned = false;
|
|
207
|
+
child.on("spawn", () => {
|
|
208
|
+
spawned = true;
|
|
209
|
+
});
|
|
210
|
+
child.on("error", (err) => {
|
|
211
|
+
if (spawned) return;
|
|
212
|
+
if (mode === "oam") {
|
|
213
|
+
process.stderr.write(`ssh-mcp: failed to launch oam (${err.message})\n`);
|
|
214
|
+
process.exit(1);
|
|
215
|
+
}
|
|
216
|
+
void runInProcess();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Forward termination so the server's own shutdown path runs in the child
|
|
220
|
+
// rather than the child being orphaned. No-op on Windows, harmless to add.
|
|
221
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
222
|
+
process.on(sig, () => {
|
|
223
|
+
if (!child.killed) child.kill(sig);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
child.on("exit", (code, signal) => {
|
|
228
|
+
// Mirror the child's fate: a signal death becomes 128+n so callers see a
|
|
229
|
+
// conventional shell exit status rather than a bare 0.
|
|
230
|
+
if (signal) {
|
|
231
|
+
process.exit(128 + (constants.signals[signal] ?? 15));
|
|
232
|
+
}
|
|
233
|
+
process.exit(code ?? 0);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yawlabs/ssh-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"mcpName": "io.github.YawLabs/ssh-mcp",
|
|
5
5
|
"description": "MCP server for SSH operations with built-in diagnostics",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
|
-
"ssh-mcp": "
|
|
8
|
+
"ssh-mcp": "bin/ssh-mcp.mjs"
|
|
9
9
|
},
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
+
"bin/ssh-mcp.mjs",
|
|
17
18
|
"dist",
|
|
18
19
|
"LICENSE",
|
|
19
20
|
"README.md"
|