mcp-ssh-manager 3.8.7 → 4.0.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/.eslintignore +5 -0
- package/.gitattributes +5 -0
- package/CHANGELOG.md +371 -6
- package/CLAUDE.md +6 -0
- package/CODE_OF_CONDUCT.md +18 -0
- package/CONTRIBUTING.md +30 -0
- package/Formula/ssh-manager.rb +60 -0
- package/README.md +484 -964
- package/ROADMAP-V4.md +451 -0
- package/brand/README.md +43 -0
- package/brand/icon.svg +32 -0
- package/brand/mark.svg +18 -0
- package/brand/wordmark.svg +30 -0
- package/cli/app.js +230 -0
- package/cli/control.js +116 -0
- package/cli/lib/colors.sh +15 -0
- package/cli/lib/menu.sh +145 -96
- package/cli/portability.js +186 -0
- package/cli/ssh-manager +197 -91
- package/cli/ssh-manager.js +14 -0
- package/cli/style.js +17 -0
- package/cli/vault.js +411 -0
- package/desktop/ControlPlaneApp.swift +219 -0
- package/desktop/build.sh +58 -0
- package/desktop/electron/after-pack.cjs +39 -0
- package/desktop/electron/electron-builder.yml +148 -0
- package/desktop/electron/finalize-artifacts.mjs +83 -0
- package/desktop/electron/main.js +815 -0
- package/desktop/electron/package-lock.json +3656 -0
- package/desktop/electron/package.json +30 -0
- package/desktop/electron/prepare-engine.mjs +84 -0
- package/desktop/electron/resources/entitlements.mac.plist +22 -0
- package/desktop/electron/resources/icon.png +0 -0
- package/desktop/electron/resources/trayTemplate.png +0 -0
- package/desktop/electron/resources/trayTemplate@2x.png +0 -0
- package/desktop/electron/smoke-test.mjs +184 -0
- package/dist/ui/app.css +1 -0
- package/dist/ui/app.js +501 -0
- package/dist/ui/assets/Forum-Regular.woff2 +0 -0
- package/dist/ui/assets/JetBrainsMono-Bold.woff2 +0 -0
- package/dist/ui/assets/JetBrainsMono-Regular.woff2 +0 -0
- package/dist/ui/assets/Karla-Variable.woff2 +0 -0
- package/dist/ui/index.html +16 -0
- package/docs/DISTRIBUTION.md +136 -0
- package/docs/FINAL-TEST-V4.md +209 -0
- package/docs/MIGRATION.md +334 -0
- package/docs/TESTING-V4.md +119 -0
- package/docs/superpowers/specs/2026-10-08-desktop-from-npm-and-cli-design.md +187 -0
- package/docs/videos/control-plane.mp4 +0 -0
- package/package.json +61 -30
- package/scripts/build-interface.mjs +73 -0
- package/scripts/capture-screenshots.mjs +191 -0
- package/scripts/demo-env.mjs +461 -0
- package/scripts/frame-screenshots.py +176 -0
- package/scripts/macbook-mockup.py +296 -0
- package/scripts/make-tray-icon.py +61 -0
- package/scripts/preview-candidate.mjs +291 -0
- package/scripts/publish-desktop.mjs +46 -0
- package/scripts/record-demo.mjs +229 -0
- package/scripts/release-version.mjs +32 -0
- package/scripts/render-cli-menu.py +157 -0
- package/scripts/requirements-dev.in +12 -0
- package/scripts/requirements-dev.txt +424 -0
- package/scripts/run-ui-regression.mjs +48 -0
- package/scripts/setup-hooks.sh +7 -13
- package/scripts/smoke-desktop.mjs +80 -0
- package/scripts/test-drag-and-drop.mjs +114 -0
- package/scripts/test-first-run.mjs +5 -0
- package/scripts/test-import-ui.mjs +5 -0
- package/scripts/test-preferences-persist.mjs +5 -0
- package/scripts/test-upgrade-from-published.mjs +226 -0
- package/scripts/verify-mac-build.sh +153 -0
- package/scripts/verify-windows-build.ps1 +15 -0
- package/server.json +3 -3
- package/skills/README.md +42 -0
- package/skills/ssh-incident/SKILL.md +55 -0
- package/skills/ssh-operations/SKILL.md +62 -0
- package/skills/ssh-restricted/SKILL.md +57 -0
- package/src/agent-clients.js +482 -0
- package/src/approval.js +291 -0
- package/src/audit.js +11 -1
- package/src/command-log.js +162 -0
- package/src/config-loader.js +119 -4
- package/src/config-paths.js +101 -0
- package/src/control-plane.js +2730 -0
- package/src/desktop-install.js +747 -0
- package/src/index.js +179 -213
- package/src/live-stream.js +261 -0
- package/src/saved-commands.js +150 -0
- package/src/secret-store.js +573 -0
- package/src/server-aliases.js +8 -7
- package/src/server-config-manager.js +33 -7
- package/src/server-export.js +106 -0
- package/src/server-groups.js +82 -21
- package/src/server-import.js +477 -0
- package/src/shell-quote.js +9 -2
- package/src/ssh-connection.js +155 -0
- package/src/ssh-manager.js +186 -13
- package/src/tunnel-manager.js +79 -0
- package/src/vault-recovery.js +206 -0
- package/src/xlsx.js +322 -0
- package/docs/images/social-preview.png +0 -0
- package/docs/images/social-preview@2x.png +0 -0
- package/docs/images/ssh-manager-cli-menu.png +0 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ssh-incident
|
|
3
|
+
description: |
|
|
4
|
+
A diagnosis order for "the server is down", "the site is slow", "something
|
|
5
|
+
broke". Use when investigating a problem on a remote server through the ssh_*
|
|
6
|
+
tools, before changing anything.
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Diagnosing before touching
|
|
10
|
+
|
|
11
|
+
The instinct under pressure is to restart something. Resist it for four calls:
|
|
12
|
+
a restart that fixes the symptom destroys the evidence, and you will be back
|
|
13
|
+
tomorrow.
|
|
14
|
+
|
|
15
|
+
## The order, and why it is this order
|
|
16
|
+
|
|
17
|
+
**1. `ssh_health_check`** — one call, and it answers most incidents outright.
|
|
18
|
+
A full disk, memory exhaustion and a load spike all look like "the site is down"
|
|
19
|
+
from outside. Start here every time.
|
|
20
|
+
|
|
21
|
+
**2. `ssh_service_status`** on the services that matter — the web server, the
|
|
22
|
+
database, the app. "Active" with a recent start time means something restarted
|
|
23
|
+
it; that is a clue, not a resolution.
|
|
24
|
+
|
|
25
|
+
**3. `ssh_tail`** on the relevant log, with `follow: false` so you get the last
|
|
26
|
+
lines back rather than a stream. The error is usually in there, in plain text,
|
|
27
|
+
and reading it takes ten seconds. Guessing takes an hour.
|
|
28
|
+
|
|
29
|
+
**4. `ssh_process_manager`** to list processes when the health check pointed at
|
|
30
|
+
CPU or memory. Find what is eating the machine before deciding what to do about
|
|
31
|
+
it.
|
|
32
|
+
|
|
33
|
+
Only now consider changing something.
|
|
34
|
+
|
|
35
|
+
## Things that are true more often than they should be
|
|
36
|
+
|
|
37
|
+
- **A full disk presents as everything being broken.** Logs cannot be written,
|
|
38
|
+
databases refuse writes, uploads fail. Check `disk` in the health output first,
|
|
39
|
+
and look at `/var/log` before blaming the application.
|
|
40
|
+
- **"It worked yesterday" usually means something rotated, filled, or expired.**
|
|
41
|
+
A log that stopped rotating, a certificate, a token, a disk crossing 100%.
|
|
42
|
+
- **A service that is running is not a service that is working.** `active
|
|
43
|
+
(running)` and a port nobody answers on are entirely compatible.
|
|
44
|
+
- **Check whether it is only you.** A network path, a DNS entry or a firewall
|
|
45
|
+
rule can make one machine look dead from where you are and fine from elsewhere.
|
|
46
|
+
|
|
47
|
+
## What not to do
|
|
48
|
+
|
|
49
|
+
- Do not restart a service before reading its logs. You lose the reason.
|
|
50
|
+
- Do not clear a log file to free disk space without looking at it first; move it
|
|
51
|
+
aside if you must, and let the service reopen its handle.
|
|
52
|
+
- Do not run a fix on every server in a group because it worked on one. Confirm
|
|
53
|
+
the same symptom is there first.
|
|
54
|
+
- Do not report "fixed" when you restarted something and the symptom went away.
|
|
55
|
+
Say what you found and what you did — if you did not find a cause, say that too.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ssh-operations
|
|
3
|
+
description: |
|
|
4
|
+
How to work on remote servers through MCP SSH Manager without breaking them.
|
|
5
|
+
Use this whenever a task involves deploying, backing up, restarting services,
|
|
6
|
+
or running commands on a server through the ssh_* tools — especially on
|
|
7
|
+
anything named prod, production or live.
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Working on someone's servers
|
|
11
|
+
|
|
12
|
+
You have a shell on machines that matter. The person who gave you this access
|
|
13
|
+
cannot see what you are doing unless they have the control plane open. Behave
|
|
14
|
+
accordingly.
|
|
15
|
+
|
|
16
|
+
## Before you change anything
|
|
17
|
+
|
|
18
|
+
**Look first.** `ssh_health_check` returns CPU, memory, disk, uptime and an
|
|
19
|
+
overall status in one call. Reach for it before concluding that anything is
|
|
20
|
+
wrong — a "the site is down" report is often a full disk, and one call tells you.
|
|
21
|
+
|
|
22
|
+
**Back up before you overwrite.** `ssh_deploy` takes a backup by default; do not
|
|
23
|
+
turn that off to save time. Before a database import or a destructive migration,
|
|
24
|
+
`ssh_backup_create` first. A backup you did not need costs seconds; the one you
|
|
25
|
+
skipped costs the data.
|
|
26
|
+
|
|
27
|
+
**Check what you are about to replace.** `ssh_execute` with `ls -la`, `cat`, or
|
|
28
|
+
`systemctl status` before writing over a file or restarting a service. Read the
|
|
29
|
+
current state rather than assuming it.
|
|
30
|
+
|
|
31
|
+
## While you work
|
|
32
|
+
|
|
33
|
+
**One server at a time unless asked otherwise.** `ssh_execute_group` runs on
|
|
34
|
+
every member of a group; that is powerful and unforgiving. If the task says "the
|
|
35
|
+
web servers", confirm which machines that means before running anything on all
|
|
36
|
+
of them.
|
|
37
|
+
|
|
38
|
+
**Prefer the specific tool over a raw command.** `ssh_service_status` instead of
|
|
39
|
+
`systemctl status` parsed by hand, `ssh_db_query` instead of piping SQL into a
|
|
40
|
+
client, `ssh_tail` instead of `tail`. They parse the output properly, they are
|
|
41
|
+
covered by the security modes, and their arguments are shell-quoted.
|
|
42
|
+
|
|
43
|
+
**Never put a password in a command.** Servers can carry a configured sudo
|
|
44
|
+
password (`ssh_execute_sudo` uses it) and database credentials. Do not
|
|
45
|
+
interpolate secrets into a command line: they end up in the remote process list
|
|
46
|
+
and in logs.
|
|
47
|
+
|
|
48
|
+
## When something goes wrong
|
|
49
|
+
|
|
50
|
+
**Do not retry a destructive command that failed.** Find out why it failed
|
|
51
|
+
first. A failed `rm -rf` that you run again with `sudo` is how an incident
|
|
52
|
+
becomes an outage.
|
|
53
|
+
|
|
54
|
+
**Say what you did.** When you report back, name the servers you touched and the
|
|
55
|
+
commands that changed something. The person reading you may not have been
|
|
56
|
+
watching.
|
|
57
|
+
|
|
58
|
+
## What you cannot see
|
|
59
|
+
|
|
60
|
+
Servers can be in `readonly` or `restricted` mode, and actions can require a
|
|
61
|
+
human's approval. A refusal is not a bug and not something to work around — see
|
|
62
|
+
the `ssh-restricted` skill.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ssh-restricted
|
|
3
|
+
description: |
|
|
4
|
+
What to do when an ssh_* tool refuses to run: security modes (readonly,
|
|
5
|
+
restricted) and human approval. Use when a command is denied, refused, or
|
|
6
|
+
seems to hang waiting for something.
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# When a server says no
|
|
10
|
+
|
|
11
|
+
MCP SSH Manager lets an operator constrain what an agent may do, per server.
|
|
12
|
+
A refusal is the system working, not a bug to route around.
|
|
13
|
+
|
|
14
|
+
## The three answers you can get
|
|
15
|
+
|
|
16
|
+
**"Policy denied: Tool X is disabled (mode: readonly)"** — the server is
|
|
17
|
+
read-only. Mutating tools (`ssh_deploy`, `ssh_upload`, `ssh_execute_sudo`,
|
|
18
|
+
`ssh_backup_restore`, `ssh_db_import`…) are off entirely. Reading still works.
|
|
19
|
+
|
|
20
|
+
**"Policy denied: ... matches built-in destructive pattern"** or **"does not
|
|
21
|
+
match the allow patterns"** — the server is `restricted`. The operator listed
|
|
22
|
+
what may run there, and this is not on the list.
|
|
23
|
+
|
|
24
|
+
**"Refused by the operator"** — a human saw the command in the control plane and
|
|
25
|
+
said no. That is a person, not a rule.
|
|
26
|
+
|
|
27
|
+
## What to do about it
|
|
28
|
+
|
|
29
|
+
**Say what was refused and why, plainly.** The operator configured this on
|
|
30
|
+
purpose; they need to know their agent hit the wall, not that "something went
|
|
31
|
+
wrong".
|
|
32
|
+
|
|
33
|
+
**Offer the read-only equivalent.** Refused `systemctl restart nginx`? You can
|
|
34
|
+
still `ssh_service_status`, `ssh_tail` the error log, and report what you found
|
|
35
|
+
along with the exact command a human would need to run. That is usually more
|
|
36
|
+
useful than the restart would have been.
|
|
37
|
+
|
|
38
|
+
**Do not look for a way around it.** Do not:
|
|
39
|
+
|
|
40
|
+
- retry the same thing through `ssh_execute` because the specific tool was
|
|
41
|
+
blocked — the policy layer covers both;
|
|
42
|
+
- reach for another server that is less constrained to do the same work;
|
|
43
|
+
- ask the user to disable the mode so you can continue. If they want it off,
|
|
44
|
+
they will say so.
|
|
45
|
+
|
|
46
|
+
Working around a control someone deliberately put in place is worse than failing
|
|
47
|
+
the task, because they will believe it held.
|
|
48
|
+
|
|
49
|
+
## Approval, and waiting
|
|
50
|
+
|
|
51
|
+
When a server is set to `destructive` or `always` approval, the engine pauses and
|
|
52
|
+
asks a human before running. From your side the call simply takes longer — that
|
|
53
|
+
is expected, and it is what the operator wanted.
|
|
54
|
+
|
|
55
|
+
If it comes back **"Refused by the operator"**, do not run a variation of the
|
|
56
|
+
same command hoping it slips through. Report the refusal and ask what they would
|
|
57
|
+
prefer instead.
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
// Which engine your AI tools run, and switching it — from the interface.
|
|
2
|
+
//
|
|
3
|
+
// The engine an agent talks to can come from two places, and both are first
|
|
4
|
+
// class:
|
|
5
|
+
//
|
|
6
|
+
// npm the `mcp-ssh-manager` package, installed with npm. What everyone used
|
|
7
|
+
// before 4.0, and what anyone who likes it keeps using.
|
|
8
|
+
// app the copy of the same engine inside the desktop application, run by the
|
|
9
|
+
// application's own runtime (`ELECTRON_RUN_AS_NODE=1`). No Node.js, no
|
|
10
|
+
// npm: whoever installed only the application can still give their
|
|
11
|
+
// agents SSH. It updates with the application.
|
|
12
|
+
//
|
|
13
|
+
// Nothing here runs by itself. The interface shows what each tool is set to,
|
|
14
|
+
// and changes it only when somebody presses the button for that tool. The entry
|
|
15
|
+
// is always called `ssh-manager`, as the README has always told people to name
|
|
16
|
+
// it; its other settings (environment variables such as SSH_CONFIG_PATH) are
|
|
17
|
+
// kept when the engine changes.
|
|
18
|
+
//
|
|
19
|
+
// Three tools, three configuration files:
|
|
20
|
+
//
|
|
21
|
+
// Claude Code ~/.claude.json, user scope — through the `claude` command
|
|
22
|
+
// when it is installed, because Claude Code rewrites that
|
|
23
|
+
// file itself and an edit beside it can be lost
|
|
24
|
+
// Claude Desktop claude_desktop_config.json — refused while it runs: it
|
|
25
|
+
// writes its configuration back on quit and would undo ours
|
|
26
|
+
// Codex ~/.codex/config.toml — the one table replaced in place, the
|
|
27
|
+
// rest of the file (comments included) left as it is
|
|
28
|
+
|
|
29
|
+
import fs from 'fs';
|
|
30
|
+
import os from 'os';
|
|
31
|
+
import path from 'path';
|
|
32
|
+
import { spawn } from 'child_process';
|
|
33
|
+
import TOML from '@iarna/toml';
|
|
34
|
+
import { desktopStatus } from './desktop-install.js';
|
|
35
|
+
|
|
36
|
+
/** The name every tool knows the engine by. */
|
|
37
|
+
const ENTRY_NAME = 'ssh-manager';
|
|
38
|
+
|
|
39
|
+
/** The variable that makes the application's runtime behave as Node. */
|
|
40
|
+
const RUN_AS_NODE = 'ELECTRON_RUN_AS_NODE';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {Object} Engine
|
|
44
|
+
* @property {'app'|'npm'} kind
|
|
45
|
+
* @property {string} command - Absolute path of the program to start
|
|
46
|
+
* @property {string[]} args - The engine's entry point
|
|
47
|
+
* @property {Record<string, string>} env - Variables the engine needs to start
|
|
48
|
+
* @property {string|null} version
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @typedef {Object} ClientState
|
|
53
|
+
* @property {'claude-code'|'claude-desktop'|'codex'} id
|
|
54
|
+
* @property {string} name
|
|
55
|
+
* @property {boolean} installed - The tool seems to be on this machine
|
|
56
|
+
* @property {string} configPath
|
|
57
|
+
* @property {'app'|'npm'|'other'|null} engine - What its ssh-manager entry runs; null when there is none
|
|
58
|
+
* @property {string|null} version - The version of that engine, when it can be read
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @typedef {Object} Context
|
|
63
|
+
* @property {string} platform
|
|
64
|
+
* @property {NodeJS.ProcessEnv} env
|
|
65
|
+
* @property {string} home
|
|
66
|
+
* @property {(command: string, args: string[]) => Promise<{code: number, stdout: string, stderr: string}>} run
|
|
67
|
+
* @property {() => import('./desktop-install.js').DesktopStatus} desktop
|
|
68
|
+
* @property {(name: string) => Promise<string|null>} which - Resolves a command through the user's login shell
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/** @param {string} file */
|
|
72
|
+
function readText(file) {
|
|
73
|
+
try { return fs.readFileSync(file, 'utf8'); } catch { return null; }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @param {string} file */
|
|
77
|
+
function versionAt(file) {
|
|
78
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')).version || null; } catch { return null; }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A JSON settings file, `{}` when there is none yet. One that does not parse is
|
|
83
|
+
* refused rather than replaced: it holds everything else the tool keeps.
|
|
84
|
+
* @param {string} file
|
|
85
|
+
*/
|
|
86
|
+
function readJsonConfig(file) {
|
|
87
|
+
const text = readText(file);
|
|
88
|
+
if (text === null) return {};
|
|
89
|
+
try {
|
|
90
|
+
const parsed = JSON.parse(text);
|
|
91
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) return parsed;
|
|
92
|
+
} catch { /* refused below */ }
|
|
93
|
+
throw Object.assign(new Error(`${file} could not be read as JSON: change it by hand`), { status: 409 });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Write a file the way an editor would: to a temporary file beside it, then
|
|
98
|
+
* renamed over it, so a crash never leaves half a configuration behind.
|
|
99
|
+
* @param {string} file
|
|
100
|
+
* @param {string} text
|
|
101
|
+
*/
|
|
102
|
+
function writeAtomically(file, text) {
|
|
103
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
104
|
+
const temporary = `${file}.ssh-manager-${process.pid}.tmp`;
|
|
105
|
+
let mode = 0o600;
|
|
106
|
+
try { mode = fs.statSync(file).mode & 0o777; } catch { /* a new file: private */ }
|
|
107
|
+
fs.writeFileSync(temporary, text, { mode });
|
|
108
|
+
fs.renameSync(temporary, file);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The default runner: spawn, never a shell. */
|
|
112
|
+
function runCommand(command, args) {
|
|
113
|
+
return new Promise(resolve => {
|
|
114
|
+
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true });
|
|
115
|
+
let stdout = '';
|
|
116
|
+
let stderr = '';
|
|
117
|
+
child.stdout.on('data', chunk => { stdout += chunk; });
|
|
118
|
+
child.stderr.on('data', chunk => { stderr += chunk; });
|
|
119
|
+
child.once('error', error => resolve({ code: 127, stdout, stderr: error.message }));
|
|
120
|
+
child.once('close', code => resolve({ code: code ?? 1, stdout, stderr }));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The full path of a command as the user's own terminal would find it. An
|
|
126
|
+
* application started from the Dock or the Start menu does not have the PATH a
|
|
127
|
+
* terminal has, so `claude` or `node` installed with Homebrew or npm would be
|
|
128
|
+
* invisible to it without this.
|
|
129
|
+
* @param {string} name
|
|
130
|
+
* @param {{platform: string, env: NodeJS.ProcessEnv, run: Context['run']}} context
|
|
131
|
+
* @returns {Promise<string|null>}
|
|
132
|
+
*/
|
|
133
|
+
async function whichInLoginShell(name, { platform, env, run }) {
|
|
134
|
+
if (platform === 'win32') {
|
|
135
|
+
const { code, stdout } = await run('where.exe', [name]);
|
|
136
|
+
const found = code === 0 ? stdout.split(/\r?\n/).map(line => line.trim()).filter(Boolean) : [];
|
|
137
|
+
// npm puts an extensionless shell script first, then a .cmd: an .exe, when
|
|
138
|
+
// there is one, is the only kind that starts without a shell.
|
|
139
|
+
return found.find(file => /\.exe$/i.test(file)) || found[0] || null;
|
|
140
|
+
}
|
|
141
|
+
const shell = env.SHELL || (platform === 'darwin' ? '/bin/zsh' : '/bin/bash');
|
|
142
|
+
// A login shell first; an interactive one too when that finds nothing, for
|
|
143
|
+
// the PATH set in ~/.zshrc or ~/.bashrc (nvm, fnm, volta).
|
|
144
|
+
for (const flags of ['-lc', '-ilc']) {
|
|
145
|
+
const { code, stdout } = await run(shell, [flags, `command -v ${name}`]);
|
|
146
|
+
const found = code === 0 ? stdout.trim().split('\n').pop()?.trim() : '';
|
|
147
|
+
if (found && path.isAbsolute(found)) return found;
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The engine inside the installed desktop application, run by its own runtime.
|
|
154
|
+
* @param {Context} context
|
|
155
|
+
* @returns {Engine|null}
|
|
156
|
+
*/
|
|
157
|
+
export function appEngine(context) {
|
|
158
|
+
const status = context.desktop();
|
|
159
|
+
if (!status.installed || !status.path) return null;
|
|
160
|
+
// Started from its disk image, or moved aside by macOS because it was never
|
|
161
|
+
// copied to Applications: a path that is gone at the next start.
|
|
162
|
+
if (status.path.startsWith('/Volumes/') || status.path.includes('/AppTranslocation/')) return null;
|
|
163
|
+
let command;
|
|
164
|
+
let resources;
|
|
165
|
+
if (context.platform === 'darwin') {
|
|
166
|
+
command = path.join(status.path, 'Contents', 'MacOS', 'SSH Manager');
|
|
167
|
+
resources = path.join(status.path, 'Contents', 'Resources');
|
|
168
|
+
} else if (context.platform === 'win32') {
|
|
169
|
+
command = status.path;
|
|
170
|
+
resources = path.join(path.dirname(status.path), 'resources');
|
|
171
|
+
} else {
|
|
172
|
+
// The .deb puts a link on the PATH; the application lives where it points.
|
|
173
|
+
// An AppImage mounts itself somewhere new on every start, so nothing could
|
|
174
|
+
// point inside it for long: no engine is offered from one.
|
|
175
|
+
if (status.path.endsWith('.AppImage')) return null;
|
|
176
|
+
try { command = fs.realpathSync(status.path); } catch { return null; }
|
|
177
|
+
resources = path.join(path.dirname(command), 'resources');
|
|
178
|
+
}
|
|
179
|
+
const entry = path.join(resources, 'engine', 'src', 'index.js');
|
|
180
|
+
if (!fs.existsSync(entry)) return null;
|
|
181
|
+
return {
|
|
182
|
+
kind: 'app',
|
|
183
|
+
command,
|
|
184
|
+
args: [entry],
|
|
185
|
+
env: { [RUN_AS_NODE]: '1' },
|
|
186
|
+
version: versionAt(path.join(resources, 'engine', 'package.json')) || status.version,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* The engine installed with npm, started with the `node` it was installed for.
|
|
192
|
+
* @param {Context} context
|
|
193
|
+
* @returns {Promise<Engine|null>}
|
|
194
|
+
*/
|
|
195
|
+
async function npmEngine(context) {
|
|
196
|
+
const bin = await context.which('mcp-ssh-manager');
|
|
197
|
+
if (!bin) return null;
|
|
198
|
+
let entry;
|
|
199
|
+
try { entry = fs.realpathSync(bin); } catch { return null; }
|
|
200
|
+
if (!/[\\/]src[\\/]index\.js$/.test(entry)) {
|
|
201
|
+
// Windows has no links: a script beside the package, in the same prefix.
|
|
202
|
+
entry = path.join(path.dirname(bin), 'node_modules', 'mcp-ssh-manager', 'src', 'index.js');
|
|
203
|
+
if (!fs.existsSync(entry)) return null;
|
|
204
|
+
}
|
|
205
|
+
const node = await context.which('node');
|
|
206
|
+
if (!node) return null;
|
|
207
|
+
return {
|
|
208
|
+
kind: 'npm',
|
|
209
|
+
command: node,
|
|
210
|
+
args: [entry],
|
|
211
|
+
env: {},
|
|
212
|
+
version: versionAt(path.join(path.dirname(path.dirname(entry)), 'package.json')),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Which engine an existing entry runs.
|
|
218
|
+
* @param {{command?: string, args?: string[], env?: Record<string, string>}|null|undefined} entry
|
|
219
|
+
* @returns {{engine: 'app'|'npm'|'other'|null, version: string|null}}
|
|
220
|
+
*/
|
|
221
|
+
export function describeEntry(entry) {
|
|
222
|
+
if (!entry || typeof entry !== 'object') return { engine: null, version: null };
|
|
223
|
+
const parts = [entry.command, ...(Array.isArray(entry.args) ? entry.args : [])].filter(Boolean).map(String);
|
|
224
|
+
const script = parts.find(part => /[\\/]src[\\/]index\.js$/.test(part));
|
|
225
|
+
const root = script ? path.dirname(path.dirname(script)) : null;
|
|
226
|
+
const version = root ? versionAt(path.join(root, 'package.json')) : null;
|
|
227
|
+
if (entry.env?.[RUN_AS_NODE] === '1' && parts.some(part => /[\\/](resources|Resources)[\\/]engine[\\/]/.test(part))) {
|
|
228
|
+
return { engine: 'app', version };
|
|
229
|
+
}
|
|
230
|
+
if (parts.some(part => /mcp-ssh-manager/.test(part))) return { engine: 'npm', version };
|
|
231
|
+
return { engine: 'other', version };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The entry to write for an engine, keeping the settings of the one it replaces.
|
|
236
|
+
* @param {Engine} engine
|
|
237
|
+
* @param {{env?: Record<string, string>}|null|undefined} previous
|
|
238
|
+
*/
|
|
239
|
+
function entryFor(engine, previous) {
|
|
240
|
+
const env = { ...(previous?.env || {}) };
|
|
241
|
+
delete env[RUN_AS_NODE];
|
|
242
|
+
return { command: engine.command, args: engine.args, env: { ...env, ...engine.env } };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// --- Claude Code ------------------------------------------------------------
|
|
246
|
+
|
|
247
|
+
const claudeCode = {
|
|
248
|
+
id: /** @type {const} */ ('claude-code'),
|
|
249
|
+
name: 'Claude Code',
|
|
250
|
+
/** @param {Context} context */
|
|
251
|
+
configPath: context => path.join(context.home, '.claude.json'),
|
|
252
|
+
/** @param {Context} context */
|
|
253
|
+
read(context) {
|
|
254
|
+
const text = readText(this.configPath(context));
|
|
255
|
+
if (text === null) return { exists: false, entry: null };
|
|
256
|
+
try { return { exists: true, entry: JSON.parse(text).mcpServers?.[ENTRY_NAME] ?? null }; } catch { return { exists: true, entry: null }; }
|
|
257
|
+
},
|
|
258
|
+
/** @param {Context} context */
|
|
259
|
+
async installed(context) {
|
|
260
|
+
return this.read(context).exists || Boolean(await context.which('claude'));
|
|
261
|
+
},
|
|
262
|
+
/**
|
|
263
|
+
* @param {Context} context
|
|
264
|
+
* @param {Engine} engine
|
|
265
|
+
*/
|
|
266
|
+
async write(context, engine) {
|
|
267
|
+
const previous = this.read(context).entry;
|
|
268
|
+
const entry = { type: 'stdio', ...entryFor(engine, previous) };
|
|
269
|
+
const claude = await context.which('claude');
|
|
270
|
+
if (claude && (context.platform !== 'win32' || /\.exe$/i.test(claude))) {
|
|
271
|
+
// Claude Code's own command, so its own process is the one that writes
|
|
272
|
+
// the file it keeps rewriting.
|
|
273
|
+
await context.run(claude, ['mcp', 'remove', ENTRY_NAME, '--scope', 'user']);
|
|
274
|
+
const added = await context.run(claude, ['mcp', 'add-json', ENTRY_NAME, JSON.stringify(entry), '--scope', 'user']);
|
|
275
|
+
if (added.code !== 0) {
|
|
276
|
+
if (previous) await context.run(claude, ['mcp', 'add-json', ENTRY_NAME, JSON.stringify(previous), '--scope', 'user']);
|
|
277
|
+
throw new Error(`claude mcp add-json failed: ${(added.stderr || added.stdout).trim()}`);
|
|
278
|
+
}
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const file = this.configPath(context);
|
|
282
|
+
const config = readJsonConfig(file);
|
|
283
|
+
config.mcpServers = { ...(config.mcpServers || {}), [ENTRY_NAME]: entry };
|
|
284
|
+
writeAtomically(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// --- Claude Desktop ---------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
const claudeDesktop = {
|
|
291
|
+
id: /** @type {const} */ ('claude-desktop'),
|
|
292
|
+
name: 'Claude Desktop',
|
|
293
|
+
/** @param {Context} context */
|
|
294
|
+
configPath(context) {
|
|
295
|
+
if (context.platform === 'darwin') return path.join(context.home, 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json');
|
|
296
|
+
if (context.platform === 'win32') return path.join(context.env.APPDATA || path.join(context.home, 'AppData', 'Roaming'), 'Claude', 'claude_desktop_config.json');
|
|
297
|
+
return path.join(context.env.XDG_CONFIG_HOME || path.join(context.home, '.config'), 'Claude', 'claude_desktop_config.json');
|
|
298
|
+
},
|
|
299
|
+
/** @param {Context} context */
|
|
300
|
+
read(context) {
|
|
301
|
+
const text = readText(this.configPath(context));
|
|
302
|
+
if (text === null) return { exists: fs.existsSync(path.dirname(this.configPath(context))), entry: null };
|
|
303
|
+
try { return { exists: true, entry: JSON.parse(text).mcpServers?.[ENTRY_NAME] ?? null }; } catch { return { exists: true, entry: null }; }
|
|
304
|
+
},
|
|
305
|
+
/** @param {Context} context */
|
|
306
|
+
async installed(context) { return this.read(context).exists; },
|
|
307
|
+
/**
|
|
308
|
+
* Claude Desktop writes its configuration back when it quits: an edit made
|
|
309
|
+
* while it runs is undone. Asking first is the only reliable order.
|
|
310
|
+
* @param {Context} context
|
|
311
|
+
*/
|
|
312
|
+
async busy(context) {
|
|
313
|
+
const running = context.platform === 'win32'
|
|
314
|
+
? (await context.run('tasklist', ['/FO', 'CSV', '/NH', '/FI', 'IMAGENAME eq Claude.exe'])).stdout.includes('"Claude.exe"')
|
|
315
|
+
: (await context.run('ps', ['-A', '-o', 'command='])).stdout.split('\n').some(line =>
|
|
316
|
+
context.platform === 'darwin' ? line.includes('/Claude.app/Contents/MacOS/Claude') : /(^|\/)claude-desktop(\s|$)/.test(line));
|
|
317
|
+
return running ? 'Quit Claude Desktop first: it rewrites its settings when it closes and would undo this.' : null;
|
|
318
|
+
},
|
|
319
|
+
/**
|
|
320
|
+
* @param {Context} context
|
|
321
|
+
* @param {Engine} engine
|
|
322
|
+
*/
|
|
323
|
+
async write(context, engine) {
|
|
324
|
+
const file = this.configPath(context);
|
|
325
|
+
const config = readJsonConfig(file);
|
|
326
|
+
const previous = config.mcpServers?.[ENTRY_NAME];
|
|
327
|
+
config.mcpServers = { ...(config.mcpServers || {}), [ENTRY_NAME]: entryFor(engine, previous) };
|
|
328
|
+
writeAtomically(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
// --- Codex ------------------------------------------------------------------
|
|
333
|
+
|
|
334
|
+
/** A TOML string: JSON's escaping is a subset TOML's basic strings accept. */
|
|
335
|
+
const tomlString = value => JSON.stringify(String(value));
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The text with its `[mcp_servers.ssh-manager…]` tables replaced by `block`,
|
|
339
|
+
* where the first of them stood (at the end when there was none). Everything
|
|
340
|
+
* else is left as written, including the comments that introduce the next table.
|
|
341
|
+
* @param {string} text
|
|
342
|
+
* @param {string} block
|
|
343
|
+
*/
|
|
344
|
+
function replaceCodexEntry(text, block) {
|
|
345
|
+
const ours = /^\s*\[\s*mcp_servers\s*\.\s*(?:"ssh-manager"|ssh-manager)(?:\s*\.[^\]]*)?\s*\]\s*(?:#.*)?$/;
|
|
346
|
+
const table = /^\s*\[\[?[^[\]=]*\]\]?\s*(?:#.*)?$/;
|
|
347
|
+
const out = [];
|
|
348
|
+
/** @type {string[]} */
|
|
349
|
+
let held = []; // comments and blank lines at the end of our table: they may belong to the next one
|
|
350
|
+
let skipping = false;
|
|
351
|
+
let at = -1;
|
|
352
|
+
for (const line of text.split('\n')) {
|
|
353
|
+
if (table.test(line)) {
|
|
354
|
+
const mine = ours.test(line);
|
|
355
|
+
if (skipping && !mine) out.push(...held);
|
|
356
|
+
held = [];
|
|
357
|
+
skipping = mine;
|
|
358
|
+
if (mine && at < 0) at = out.length;
|
|
359
|
+
}
|
|
360
|
+
if (!skipping) out.push(line);
|
|
361
|
+
else if (/^\s*(?:#.*)?$/.test(line)) held.push(line);
|
|
362
|
+
else held = [];
|
|
363
|
+
}
|
|
364
|
+
if (at < 0) {
|
|
365
|
+
while (out.length && !out[out.length - 1].trim()) out.pop();
|
|
366
|
+
if (out.length) out.push('');
|
|
367
|
+
at = out.length;
|
|
368
|
+
}
|
|
369
|
+
out.splice(at, 0, ...block.split('\n'), '');
|
|
370
|
+
return `${out.join('\n').replace(/\n{3,}/g, '\n\n').replace(/\s+$/, '')}\n`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const codex = {
|
|
374
|
+
id: /** @type {const} */ ('codex'),
|
|
375
|
+
name: 'Codex',
|
|
376
|
+
/** @param {Context} context */
|
|
377
|
+
configPath: context => path.join(context.env.CODEX_HOME || path.join(context.home, '.codex'), 'config.toml'),
|
|
378
|
+
/** @param {Context} context */
|
|
379
|
+
read(context) {
|
|
380
|
+
const text = readText(this.configPath(context));
|
|
381
|
+
if (text === null) return { exists: fs.existsSync(path.dirname(this.configPath(context))), entry: null };
|
|
382
|
+
try {
|
|
383
|
+
const parsed = /** @type {any} */ (TOML.parse(text));
|
|
384
|
+
return { exists: true, entry: parsed.mcp_servers?.[ENTRY_NAME] ?? null };
|
|
385
|
+
} catch {
|
|
386
|
+
return { exists: true, entry: null };
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
/** @param {Context} context */
|
|
390
|
+
async installed(context) { return this.read(context).exists || Boolean(await context.which('codex')); },
|
|
391
|
+
/**
|
|
392
|
+
* @param {Context} context
|
|
393
|
+
* @param {Engine} engine
|
|
394
|
+
*/
|
|
395
|
+
async write(context, engine) {
|
|
396
|
+
const file = this.configPath(context);
|
|
397
|
+
const text = readText(file) ?? '';
|
|
398
|
+
const { command, args, env } = entryFor(engine, this.read(context).entry);
|
|
399
|
+
const block = [
|
|
400
|
+
`[mcp_servers.${ENTRY_NAME}]`,
|
|
401
|
+
`command = ${tomlString(command)}`,
|
|
402
|
+
`args = [${args.map(tomlString).join(', ')}]`,
|
|
403
|
+
...(Object.keys(env).length
|
|
404
|
+
? [`env = { ${Object.entries(env).map(([key, value]) => `${/^[A-Za-z0-9_-]+$/.test(key) ? key : tomlString(key)} = ${tomlString(value)}`).join(', ')} }`]
|
|
405
|
+
: []),
|
|
406
|
+
].join('\n');
|
|
407
|
+
const next = replaceCodexEntry(text, block);
|
|
408
|
+
try {
|
|
409
|
+
TOML.parse(next); // never write a file Codex could not read
|
|
410
|
+
} catch {
|
|
411
|
+
throw Object.assign(new Error(`${file} could not be changed safely (it does not parse, or names ssh-manager in a form this does not edit): change it by hand`), { status: 409 });
|
|
412
|
+
}
|
|
413
|
+
writeAtomically(file, next);
|
|
414
|
+
},
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const CLIENTS = [claudeCode, claudeDesktop, codex];
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* @param {Partial<Context>} [overrides] - Stand-ins for the tests
|
|
421
|
+
* @returns {Context}
|
|
422
|
+
*/
|
|
423
|
+
export function agentContext(overrides = {}) {
|
|
424
|
+
/** @type {Context} */
|
|
425
|
+
const context = {
|
|
426
|
+
platform: process.platform,
|
|
427
|
+
env: process.env,
|
|
428
|
+
home: os.homedir(),
|
|
429
|
+
run: runCommand,
|
|
430
|
+
desktop: () => desktopStatus(),
|
|
431
|
+
which: name => whichInLoginShell(name, context),
|
|
432
|
+
...overrides,
|
|
433
|
+
};
|
|
434
|
+
return context;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Every tool, what its ssh-manager entry runs, and which engines are on offer.
|
|
439
|
+
* @param {Context} [context]
|
|
440
|
+
* @returns {Promise<{engines: {app: Engine|null, npm: Engine|null}, clients: ClientState[]}>}
|
|
441
|
+
*/
|
|
442
|
+
export async function agentsStatus(context = agentContext()) {
|
|
443
|
+
const [app, npm] = [appEngine(context), await npmEngine(context)];
|
|
444
|
+
const clients = [];
|
|
445
|
+
for (const client of CLIENTS) {
|
|
446
|
+
const { entry } = client.read(context);
|
|
447
|
+
const { engine, version } = describeEntry(entry);
|
|
448
|
+
clients.push({
|
|
449
|
+
id: client.id,
|
|
450
|
+
name: client.name,
|
|
451
|
+
installed: await client.installed(context),
|
|
452
|
+
configPath: client.configPath(context),
|
|
453
|
+
engine,
|
|
454
|
+
version,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
return { engines: { app, npm }, clients };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Point one tool's ssh-manager entry at an engine.
|
|
462
|
+
* @param {string} clientId
|
|
463
|
+
* @param {'app'|'npm'} kind
|
|
464
|
+
* @param {Context} [context]
|
|
465
|
+
* @returns {Promise<ClientState>}
|
|
466
|
+
*/
|
|
467
|
+
export async function useEngine(clientId, kind, context = agentContext()) {
|
|
468
|
+
const client = CLIENTS.find(item => item.id === clientId);
|
|
469
|
+
if (!client) throw Object.assign(new Error(`Unknown tool: ${clientId}`), { status: 404 });
|
|
470
|
+
const engine = kind === 'app' ? appEngine(context) : kind === 'npm' ? await npmEngine(context) : null;
|
|
471
|
+
if (!engine) {
|
|
472
|
+
throw Object.assign(new Error(kind === 'app'
|
|
473
|
+
? 'The desktop application is not installed here, or its engine cannot be started from outside it'
|
|
474
|
+
: 'mcp-ssh-manager is not installed with npm here'), { status: 409 });
|
|
475
|
+
}
|
|
476
|
+
// Only Claude Desktop has a reason to refuse: it undoes edits made while it runs.
|
|
477
|
+
const busy = 'busy' in client ? await client.busy(context) : null;
|
|
478
|
+
if (busy) throw Object.assign(new Error(busy), { status: 409 });
|
|
479
|
+
await client.write(context, engine);
|
|
480
|
+
const status = await agentsStatus(context);
|
|
481
|
+
return /** @type {ClientState} */ (status.clients.find(item => item.id === clientId));
|
|
482
|
+
}
|