captain-tool 0.0.118 → 0.0.120
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 +28 -9
- package/bin/captain-tool +12 -0
- package/bin/captain-tool-setup +358 -96
- package/package.json +5 -5
- package/scripts/postinstall.js +72 -36
package/README.md
CHANGED
|
@@ -66,23 +66,36 @@ What does not carry over:
|
|
|
66
66
|
|
|
67
67
|
---
|
|
68
68
|
|
|
69
|
-
##
|
|
69
|
+
## Install
|
|
70
|
+
|
|
71
|
+
One command installs captain-tool and connects it to your AI client(s):
|
|
70
72
|
|
|
71
73
|
```bash
|
|
72
|
-
|
|
74
|
+
npx captain-tool@latest setup
|
|
73
75
|
```
|
|
74
76
|
|
|
75
|
-
|
|
77
|
+
It installs captain-tool globally (so your clients keep a path to it that npm does not clean
|
|
78
|
+
up), then asks which clients to configure, whether to auto-approve Captain's tools in Claude
|
|
79
|
+
Code, and turns on skill proof. If the global install needs permissions you don't have, it
|
|
80
|
+
stops, registers nothing, and prints the `npm install -g` command to run yourself.
|
|
76
81
|
|
|
77
|
-
|
|
82
|
+
### Already ran `npm install -g captain-tool`?
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
A global install registers captain with Claude Code on its own, if Claude Code is installed,
|
|
85
|
+
and prints a line saying so. Check with:
|
|
80
86
|
|
|
81
87
|
```bash
|
|
82
|
-
captain-tool
|
|
88
|
+
claude mcp list # shows: captain: …/captain-tool
|
|
83
89
|
```
|
|
84
90
|
|
|
85
|
-
|
|
91
|
+
Run `captain-tool setup` for everything else: auto-approval, skill proof, other clients, or
|
|
92
|
+
when the install didn't register (no Claude Code yet, a different server already named
|
|
93
|
+
`captain`, or a package manager or npm setting that skips install scripts, such as
|
|
94
|
+
`ignore-scripts`). After a local (project) or CI install, `captain-tool` isn't on your PATH:
|
|
95
|
+
run `npx captain-tool@latest setup` instead. Registration at install time writes only the server entry,
|
|
96
|
+
and only in `~/.claude.json`; it never changes your permissions. If your `captain` entry
|
|
97
|
+
pointed at a local build of captain-tool, a global install points it back at the installed
|
|
98
|
+
binary.
|
|
86
99
|
|
|
87
100
|
---
|
|
88
101
|
|
|
@@ -119,11 +132,17 @@ Restart Claude Desktop to activate.
|
|
|
119
132
|
|
|
120
133
|
### Claude Code (CLI)
|
|
121
134
|
|
|
135
|
+
**macOS / Linux:**
|
|
136
|
+
```bash
|
|
137
|
+
claude mcp add -s user captain -- captain-tool
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Windows** (requires `cmd /c` wrapper so the shell can resolve the `.cmd` shim):
|
|
122
141
|
```bash
|
|
123
|
-
claude mcp add captain -- cmd /c captain-tool
|
|
142
|
+
claude mcp add -s user captain -- cmd /c captain-tool
|
|
124
143
|
```
|
|
125
144
|
|
|
126
|
-
> Registers under the short name `captain` (tool prefix `mcp__captain__`). If you previously added it as `captain-tool`, run `claude mcp remove captain-tool`
|
|
145
|
+
> `-s user` registers it for every folder; without it Claude Code registers it for the current folder only. Registers under the short name `captain` (tool prefix `mcp__captain__`). If you previously added it as `captain-tool`, remove that entry to avoid a duplicate registration: `captain-tool setup` removes it from every folder at once, or run `claude mcp remove captain-tool` in each folder you added it in (a folder-scoped entry can only be removed from its own folder).
|
|
127
146
|
|
|
128
147
|
**Auto-approve captain tools (recommended).** Registration alone does not grant permissions — Claude Code will prompt "Allow captain to …?" on every tool call. The setup wizard (`npx captain-tool setup`) offers to fix this for you; to do it by hand, add one allow rule to `~/.claude/settings.json`:
|
|
129
148
|
|
package/bin/captain-tool
CHANGED
|
@@ -77,6 +77,18 @@ if (process.argv[2] === 'setup') {
|
|
|
77
77
|
console.error('Unexpected error:', err);
|
|
78
78
|
process.exit(1);
|
|
79
79
|
});
|
|
80
|
+
} else if (process.argv.length === 2 && process.stdin.isTTY) {
|
|
81
|
+
// A person typed `captain-tool` at a terminal. With no arguments the binary is the stdio
|
|
82
|
+
// MCP server an AI client starts, and it would sit waiting on their keyboard. An MCP
|
|
83
|
+
// client always pipes stdin, so this never fires for one. stderr: stdout is the protocol.
|
|
84
|
+
process.stderr.write(
|
|
85
|
+
'captain-tool is the MCP server your AI client starts; there is nothing to run here.\n' +
|
|
86
|
+
'To connect it to Claude Code or another client, run:\n' +
|
|
87
|
+
' npx captain-tool@latest setup\n'
|
|
88
|
+
);
|
|
89
|
+
// No process.exit(): stderr writes can be asynchronous (Windows terminals, pipes), and an
|
|
90
|
+
// exit would drop the hint. Nothing else is pending, so the process ends on its own.
|
|
91
|
+
process.exitCode = 0;
|
|
80
92
|
} else {
|
|
81
93
|
const binary = findBinary();
|
|
82
94
|
|
package/bin/captain-tool-setup
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
const fs = require('fs');
|
|
20
20
|
const path = require('path');
|
|
21
21
|
const os = require('os');
|
|
22
|
+
const crypto = require('crypto');
|
|
22
23
|
const readline = require('readline');
|
|
23
24
|
|
|
24
25
|
// ── Constants ─────────────────────────────────────────────────────────────────
|
|
@@ -61,10 +62,18 @@ const PLATFORMS = [
|
|
|
61
62
|
{ pkg: '@captain-tool/captain-tool-linux-x64', bin: 'captain-tool' },
|
|
62
63
|
];
|
|
63
64
|
|
|
64
|
-
|
|
65
|
+
/**
|
|
66
|
+
* The platform binary's absolute path, or null. `fromDir` resolves the platform package from
|
|
67
|
+
* another install of captain-tool (the global one, after setup installs it from npx) instead
|
|
68
|
+
* of this file's own.
|
|
69
|
+
*/
|
|
70
|
+
function findBinary(fromDir) {
|
|
65
71
|
for (const { pkg, bin } of PLATFORMS) {
|
|
66
72
|
try {
|
|
67
|
-
const pkgJsonPath = require.resolve(
|
|
73
|
+
const pkgJsonPath = require.resolve(
|
|
74
|
+
`${pkg}/package.json`,
|
|
75
|
+
fromDir ? { paths: [fromDir] } : undefined
|
|
76
|
+
);
|
|
68
77
|
const binPath = path.join(path.dirname(pkgJsonPath), 'bin', bin);
|
|
69
78
|
fs.accessSync(binPath, fs.constants.X_OK);
|
|
70
79
|
return binPath;
|
|
@@ -216,27 +225,6 @@ function buildEntryFormatB(binaryPath) {
|
|
|
216
225
|
};
|
|
217
226
|
}
|
|
218
227
|
|
|
219
|
-
// ── Atomic JSON file writer ───────────────────────────────────────────────────
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Atomically write `data` as formatted JSON to `filePath`.
|
|
223
|
-
*
|
|
224
|
-
* WHY atomic?
|
|
225
|
-
* Config files are read by GUI applications. If we write directly and the
|
|
226
|
-
* process crashes mid-write, the app sees a truncated/corrupt file and may
|
|
227
|
-
* fail to start. Writing to a .tmp file and then renaming is atomic on all
|
|
228
|
-
* supported OS (POSIX rename(2) and Windows MoveFileExW with MOVEFILE_REPLACE_EXISTING
|
|
229
|
-
* are both atomic at the filesystem level).
|
|
230
|
-
*/
|
|
231
|
-
function atomicWriteJson(filePath, data) {
|
|
232
|
-
const tmpPath = filePath + '.tmp';
|
|
233
|
-
const json = JSON.stringify(data, null, 2) + '\n';
|
|
234
|
-
// Ensure the parent directory exists (e.g. ~/.cursor/ may not exist yet).
|
|
235
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
236
|
-
fs.writeFileSync(tmpPath, json, 'utf8');
|
|
237
|
-
fs.renameSync(tmpPath, filePath);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
228
|
// ── Config read helper ────────────────────────────────────────────────────────
|
|
241
229
|
|
|
242
230
|
/**
|
|
@@ -244,19 +232,114 @@ function atomicWriteJson(filePath, data) {
|
|
|
244
232
|
* does not exist yet (first-time setup).
|
|
245
233
|
*/
|
|
246
234
|
function readJsonConfig(filePath) {
|
|
235
|
+
const raw = readRaw(filePath);
|
|
236
|
+
// A file that doesn't exist yet is fine: first-time setup creates it.
|
|
237
|
+
// Anything else (permission denied, malformed JSON) throws.
|
|
238
|
+
return raw === null ? {} : JSON.parse(raw);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** The file's text, or null when it does not exist. */
|
|
242
|
+
function readRaw(filePath) {
|
|
247
243
|
try {
|
|
248
|
-
|
|
249
|
-
return JSON.parse(raw);
|
|
244
|
+
return fs.readFileSync(filePath, 'utf8');
|
|
250
245
|
} catch (err) {
|
|
251
|
-
if (err.code === 'ENOENT')
|
|
252
|
-
// File doesn't exist yet — that's fine, we'll create it.
|
|
253
|
-
return {};
|
|
254
|
-
}
|
|
255
|
-
// Unexpected error (e.g. permission denied, malformed JSON).
|
|
246
|
+
if (err.code === 'ENOENT') return null;
|
|
256
247
|
throw err;
|
|
257
248
|
}
|
|
258
249
|
}
|
|
259
250
|
|
|
251
|
+
/** True for `{…}`: not an array, not null. */
|
|
252
|
+
function isPlainObject(v) {
|
|
253
|
+
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Read a JSON config, let `mutate` change it in place, and write it back atomically, the one
|
|
258
|
+
* read-modify-write every config change here goes through.
|
|
259
|
+
*
|
|
260
|
+
* WHY atomic: config files are read by GUI applications. A process that crashes mid-write
|
|
261
|
+
* would leave a truncated file the app may fail to start with. Writing a .tmp file and
|
|
262
|
+
* renaming it over the config is atomic on every supported OS (POSIX rename(2), Windows
|
|
263
|
+
* MoveFileExW with MOVEFILE_REPLACE_EXISTING).
|
|
264
|
+
*
|
|
265
|
+
* WHY the re-read before the rename: the agent may be running and rewriting the same file
|
|
266
|
+
* (Claude Code rewrites ~/.claude.json while it runs; an install can happen mid-session).
|
|
267
|
+
* Renaming our copy over a file that changed since we read it would drop that change. So
|
|
268
|
+
* the new content is written to a temp file first, the file is read again, and only an
|
|
269
|
+
* unchanged file is replaced; a changed one is merged again from the fresh copy, once, and a
|
|
270
|
+
* file that changes again is reported, not overwritten. A change landing between that
|
|
271
|
+
* re-read and the rename is still possible; the window is one rename.
|
|
272
|
+
*
|
|
273
|
+
* A symlinked config (a dotfiles repo) is written through to its target, so the link stays a
|
|
274
|
+
* link. A config whose JSON is not an object (`[]`, `null`, a string) is refused: setting a
|
|
275
|
+
* key on it would silently write nothing.
|
|
276
|
+
*
|
|
277
|
+
* `mutate` returns false to write nothing; a mutation that leaves the content equal is not
|
|
278
|
+
* written either, so a re-run never churns the file's bytes or mtime. Returns true when the
|
|
279
|
+
* file was written. `hooks.beforeCommit` is a test seam that runs between the temp write and
|
|
280
|
+
* the re-read.
|
|
281
|
+
*/
|
|
282
|
+
function updateJsonFile(filePath, mutate, hooks = {}) {
|
|
283
|
+
const target = resolveLink(filePath);
|
|
284
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
285
|
+
const raw = readRaw(target);
|
|
286
|
+
const data = raw === null ? {} : JSON.parse(raw);
|
|
287
|
+
if (!isPlainObject(data)) {
|
|
288
|
+
throw new Error(`${filePath} does not hold a JSON object, so captain-tool left it alone.`);
|
|
289
|
+
}
|
|
290
|
+
const before = JSON.stringify(data);
|
|
291
|
+
if (mutate(data) === false || JSON.stringify(data) === before) return false;
|
|
292
|
+
|
|
293
|
+
// A temp file of this call's own: two processes updating the same config must not
|
|
294
|
+
// write, and then rename, each other's prepared content.
|
|
295
|
+
const tmpPath = `${target}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
296
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
297
|
+
// The replacement keeps the config's permissions (~/.claude.json can be 0600 and holds
|
|
298
|
+
// account state); a new file starts private. `mode` applies only when the file is
|
|
299
|
+
// created, and umask can only narrow it, so chmod sets it exactly.
|
|
300
|
+
const mode = existingMode(target) ?? 0o600;
|
|
301
|
+
// Create it exclusively first; from then on it is ours, and every exit path, a failed
|
|
302
|
+
// write (a full disk) included, removes it. A failed create leaves nothing of ours.
|
|
303
|
+
const fd = fs.openSync(tmpPath, 'wx', mode);
|
|
304
|
+
try {
|
|
305
|
+
try {
|
|
306
|
+
fs.writeFileSync(fd, JSON.stringify(data, null, 2) + '\n', 'utf8');
|
|
307
|
+
} finally {
|
|
308
|
+
fs.closeSync(fd);
|
|
309
|
+
}
|
|
310
|
+
fs.chmodSync(tmpPath, mode);
|
|
311
|
+
if (hooks.beforeCommit) hooks.beforeCommit(attempt);
|
|
312
|
+
if (readRaw(target) !== raw) continue;
|
|
313
|
+
fs.renameSync(tmpPath, target);
|
|
314
|
+
return true;
|
|
315
|
+
} finally {
|
|
316
|
+
fs.rmSync(tmpPath, { force: true }); // gone already after a rename
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
throw new Error(
|
|
320
|
+
`${filePath} kept changing while captain-tool was updating it, so nothing was written. ` +
|
|
321
|
+
'Run `captain-tool setup` again.'
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** The permission bits of an existing file, or null when it doesn't exist. */
|
|
326
|
+
function existingMode(filePath) {
|
|
327
|
+
try {
|
|
328
|
+
return fs.statSync(filePath).mode & 0o777;
|
|
329
|
+
} catch (_) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** The file a symlink points at (followed to the end), or `filePath` itself. */
|
|
335
|
+
function resolveLink(filePath) {
|
|
336
|
+
try {
|
|
337
|
+
return fs.realpathSync(filePath);
|
|
338
|
+
} catch (_) {
|
|
339
|
+
return filePath; // doesn't exist yet: it is created where asked
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
260
343
|
// ── Legacy-registration cleanup ───────────────────────────────────────────────
|
|
261
344
|
|
|
262
345
|
/**
|
|
@@ -387,11 +470,11 @@ function applyPermissionRule(settings, { addAllowRule = true, sweepLegacy = true
|
|
|
387
470
|
* reformats the user's file or churns its mtime for a no-op.
|
|
388
471
|
*/
|
|
389
472
|
function configurePermissions(permissionsPath, opts) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
}
|
|
473
|
+
let result;
|
|
474
|
+
updateJsonFile(permissionsPath, (settings) => {
|
|
475
|
+
result = applyPermissionRule(settings, opts);
|
|
476
|
+
return result.added || result.removed > 0;
|
|
477
|
+
});
|
|
395
478
|
return result;
|
|
396
479
|
}
|
|
397
480
|
|
|
@@ -430,28 +513,121 @@ function hasUnrelatedLegacyServer(config) {
|
|
|
430
513
|
* Returns the absolute path that was written so the caller can print it.
|
|
431
514
|
*/
|
|
432
515
|
function configureClient(client, binaryPath) {
|
|
516
|
+
return writeClientEntry(client, binaryPath).configPath;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* configureClient's body. `onlyIfOursOrAbsent` (the install-time path) leaves a `captain`
|
|
521
|
+
* entry that is not ours untouched instead of replacing it. Returns
|
|
522
|
+
* { configPath, written, foreign }.
|
|
523
|
+
*/
|
|
524
|
+
function writeClientEntry(client, binaryPath, { onlyIfOursOrAbsent = false } = {}) {
|
|
433
525
|
const configPath = client.configPath(binaryPath);
|
|
526
|
+
const rootKey = client.format === 'A' ? (client.rootKey || 'mcpServers') : 'servers';
|
|
527
|
+
let foreign = false;
|
|
434
528
|
|
|
435
|
-
|
|
436
|
-
|
|
529
|
+
const written = updateJsonFile(configPath, (config) => {
|
|
530
|
+
if (config[rootKey] !== undefined && !isPlainObject(config[rootKey])) {
|
|
531
|
+
throw new Error(`${configPath}: "${rootKey}" is not a JSON object, so captain-tool left it alone.`);
|
|
532
|
+
}
|
|
533
|
+
const existing = config[rootKey] && config[rootKey][SERVER_NAME];
|
|
534
|
+
foreign = Boolean(existing) && !entryIsOurs(existing);
|
|
535
|
+
if (onlyIfOursOrAbsent && foreign) return false;
|
|
536
|
+
|
|
537
|
+
// Heal duplicate registrations in every scope (top-level + per-project) before
|
|
538
|
+
// writing the canonical entry.
|
|
539
|
+
removeLegacyEverywhere(config);
|
|
540
|
+
// Our own `captain` entries in per-folder scopes go too: the user-level one written below
|
|
541
|
+
// covers every folder, and a folder entry would override it there. The earlier README's
|
|
542
|
+
// `claude mcp add captain -- cmd /c captain-tool` (no `-s user`) left exactly such
|
|
543
|
+
// entries, broken outside Windows.
|
|
544
|
+
removeOursFromProjects(config, rootKey);
|
|
545
|
+
|
|
546
|
+
// Format A: { mcpServers: { "captain": { command, args } } }
|
|
547
|
+
// Format B: { servers: { "captain": { type, command, args } } }
|
|
548
|
+
if (!config[rootKey]) config[rootKey] = {};
|
|
549
|
+
config[rootKey][SERVER_NAME] = client.format === 'A'
|
|
550
|
+
? buildEntryFormatA(binaryPath)
|
|
551
|
+
: buildEntryFormatB(binaryPath);
|
|
552
|
+
return true;
|
|
553
|
+
});
|
|
554
|
+
return { configPath, written, foreign };
|
|
555
|
+
}
|
|
437
556
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
557
|
+
/**
|
|
558
|
+
* True when npm is installing this package globally. `-g` sets npm_config_global, but
|
|
559
|
+
* `--location=global` (or `global=true` in .npmrc) does not (verified, npm 11.19), so a
|
|
560
|
+
* package installed under npm's global node_modules counts too.
|
|
561
|
+
*/
|
|
562
|
+
function isGlobalInstall(env, packageDir) {
|
|
563
|
+
if (env.npm_config_global === 'true') return true;
|
|
564
|
+
if (!env.npm_config_global_prefix || !packageDir) return false;
|
|
565
|
+
const globalRoot = resolveLink(nodeModulesUnder(env.npm_config_global_prefix));
|
|
566
|
+
return path.dirname(resolveLink(packageDir)) === globalRoot;
|
|
567
|
+
}
|
|
441
568
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
569
|
+
/** npm's global node_modules under a global prefix. */
|
|
570
|
+
function nodeModulesUnder(prefix) {
|
|
571
|
+
return process.platform === 'win32'
|
|
572
|
+
? path.join(prefix, 'node_modules')
|
|
573
|
+
: path.join(prefix, 'lib', 'node_modules');
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Delete `captain` entries that are ours from every `projects[*]` scope. */
|
|
577
|
+
function removeOursFromProjects(config, rootKey) {
|
|
578
|
+
if (!isPlainObject(config.projects)) return;
|
|
579
|
+
for (const proj of Object.values(config.projects)) {
|
|
580
|
+
const servers = isPlainObject(proj) ? proj[rootKey] : undefined;
|
|
581
|
+
if (isPlainObject(servers) && entryIsOurs(servers[SERVER_NAME])) delete servers[SERVER_NAME];
|
|
451
582
|
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// ── Install-time registration (npm install -g) ────────────────────────────────
|
|
586
|
+
|
|
587
|
+
/** The Claude Code client definition. */
|
|
588
|
+
function claudeCodeClient() {
|
|
589
|
+
return CLIENTS.find((c) => c.rootKey === 'mcpServers' && c.permissionsPath);
|
|
590
|
+
}
|
|
452
591
|
|
|
453
|
-
|
|
454
|
-
|
|
592
|
+
/**
|
|
593
|
+
* Register captain with Claude Code from the package's install script. The fallback install
|
|
594
|
+
* path: `npx captain-tool@latest setup` is the primary one and asks the user; this one can't
|
|
595
|
+
* ask (npm runs install scripts without a terminal), so it writes only the MCP entry, for
|
|
596
|
+
* Claude Code only, and only when every guard holds. Returns { registered, reason }.
|
|
597
|
+
* Never throws: an install script that fails fails the user's whole `npm install`.
|
|
598
|
+
*
|
|
599
|
+
* Guards, each a `reason` when it stops us:
|
|
600
|
+
* not-global — a local `npm install`: the path would be inside someone's project.
|
|
601
|
+
* npx — `npx captain-tool`: the path is in npm's npx cache, which npm deletes.
|
|
602
|
+
* ci — CI and image builds have no user to use it.
|
|
603
|
+
* setup — setup is running this global install itself and registers afterwards.
|
|
604
|
+
* sudo — `sudo npm install -g`: we'd write a root-owned file into a home.
|
|
605
|
+
* no-binary — no platform binary for this machine.
|
|
606
|
+
* no-claude-code — ~/.claude.json doesn't exist: we never create it (Claude Code creates
|
|
607
|
+
* it on its first run).
|
|
608
|
+
* foreign — a `captain` server that isn't ours is registered; we leave it alone.
|
|
609
|
+
* unreadable — the config isn't JSON, or can't be read or written.
|
|
610
|
+
*/
|
|
611
|
+
function registerAtInstall({ env = process.env, binaryPath, packageDir } = {}) {
|
|
612
|
+
if (!isGlobalInstall(env, packageDir)) return { registered: false, reason: 'not-global' };
|
|
613
|
+
if (env.npm_command === 'exec') return { registered: false, reason: 'npx' };
|
|
614
|
+
if (env.CI && env.CI !== 'false' && env.CI !== '0') return { registered: false, reason: 'ci' };
|
|
615
|
+
if (env.CAPTAIN_TOOL_SETUP_RUNNING === '1') return { registered: false, reason: 'setup' };
|
|
616
|
+
if (typeof process.getuid === 'function' && process.getuid() === 0 && env.SUDO_USER) {
|
|
617
|
+
return { registered: false, reason: 'sudo' };
|
|
618
|
+
}
|
|
619
|
+
if (!binaryPath) return { registered: false, reason: 'no-binary' };
|
|
620
|
+
const client = claudeCodeClient();
|
|
621
|
+
if (!fs.existsSync(client.configPath(binaryPath))) {
|
|
622
|
+
return { registered: false, reason: 'no-claude-code' };
|
|
623
|
+
}
|
|
624
|
+
try {
|
|
625
|
+
const { foreign } = writeClientEntry(client, binaryPath, { onlyIfOursOrAbsent: true });
|
|
626
|
+
if (foreign) return { registered: false, reason: 'foreign' };
|
|
627
|
+
return { registered: true, reason: 'registered' };
|
|
628
|
+
} catch (_) {
|
|
629
|
+
return { registered: false, reason: 'unreadable' };
|
|
630
|
+
}
|
|
455
631
|
}
|
|
456
632
|
|
|
457
633
|
// ── Skill-proof hook (CAP-ADR-0042) ───────────────────────────────────────────
|
|
@@ -510,29 +686,24 @@ function parseFlags(argv) {
|
|
|
510
686
|
function uninstallClient(client, binaryPath) {
|
|
511
687
|
const configPath = client.configPath(binaryPath);
|
|
512
688
|
if (!fs.existsSync(configPath)) return false;
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
if (entryIsOurs(root[name])) {
|
|
519
|
-
delete root[name];
|
|
520
|
-
changed = true;
|
|
689
|
+
return updateJsonFile(configPath, (config) => {
|
|
690
|
+
const drop = (root) => {
|
|
691
|
+
if (!root || typeof root !== 'object') return;
|
|
692
|
+
for (const name of [SERVER_NAME, ...LEGACY_NAMES]) {
|
|
693
|
+
if (entryIsOurs(root[name])) delete root[name];
|
|
521
694
|
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
695
|
+
};
|
|
696
|
+
drop(config.mcpServers);
|
|
697
|
+
drop(config.servers);
|
|
698
|
+
if (config.projects && typeof config.projects === 'object') {
|
|
699
|
+
for (const proj of Object.values(config.projects)) {
|
|
700
|
+
if (proj && typeof proj === 'object') {
|
|
701
|
+
drop(proj.mcpServers);
|
|
702
|
+
drop(proj.servers);
|
|
703
|
+
}
|
|
531
704
|
}
|
|
532
705
|
}
|
|
533
|
-
}
|
|
534
|
-
if (changed) atomicWriteJson(configPath, config);
|
|
535
|
-
return changed;
|
|
706
|
+
});
|
|
536
707
|
}
|
|
537
708
|
|
|
538
709
|
/**
|
|
@@ -543,22 +714,24 @@ function uninstallClient(client, binaryPath) {
|
|
|
543
714
|
*/
|
|
544
715
|
function removePermissionRule(permissionsPath, { sweepLegacy = true } = {}) {
|
|
545
716
|
if (!fs.existsSync(permissionsPath)) return 0;
|
|
546
|
-
const settings = readJsonConfig(permissionsPath);
|
|
547
|
-
const allow = settings.permissions && Array.isArray(settings.permissions.allow)
|
|
548
|
-
? settings.permissions.allow
|
|
549
|
-
: null;
|
|
550
|
-
if (!allow) return 0;
|
|
551
717
|
const isOurs = (rule) =>
|
|
552
718
|
typeof rule === 'string' &&
|
|
553
719
|
[PERMISSION_RULE, ...(sweepLegacy ? LEGACY_PERMISSION_PREFIXES : [])].some(
|
|
554
720
|
(p) => rule === p || rule.startsWith(`${p}__`)
|
|
555
721
|
);
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
722
|
+
let removed = 0;
|
|
723
|
+
updateJsonFile(permissionsPath, (settings) => {
|
|
724
|
+
const allow = settings.permissions && Array.isArray(settings.permissions.allow)
|
|
725
|
+
? settings.permissions.allow
|
|
726
|
+
: null;
|
|
727
|
+
removed = 0;
|
|
728
|
+
if (!allow) return false;
|
|
729
|
+
const kept = allow.filter((r) => !isOurs(r));
|
|
730
|
+
removed = allow.length - kept.length;
|
|
731
|
+
if (removed === 0) return false;
|
|
559
732
|
settings.permissions.allow = kept;
|
|
560
|
-
|
|
561
|
-
}
|
|
733
|
+
return true;
|
|
734
|
+
});
|
|
562
735
|
return removed;
|
|
563
736
|
}
|
|
564
737
|
|
|
@@ -641,6 +814,69 @@ function abortNoInput() {
|
|
|
641
814
|
process.exit(1);
|
|
642
815
|
}
|
|
643
816
|
|
|
817
|
+
// ── npx → global install ──────────────────────────────────────────────────────
|
|
818
|
+
|
|
819
|
+
/** True when `dir` is inside npm's npx cache (`<cache>/_npx/<hash>/…`). */
|
|
820
|
+
function isNpxCacheDir(dir) {
|
|
821
|
+
return dir.split(/[\\/]/).includes('_npx');
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Install this version of captain-tool globally and return its platform binary.
|
|
826
|
+
*
|
|
827
|
+
* WHY: `npx captain-tool@latest setup` is the one command users run, but npx runs us from
|
|
828
|
+
* its cache, which `npm cache clean` deletes and a newer npx run never updates. Registering
|
|
829
|
+
* that path leaves the agent pointing at a binary that later vanishes. So setup installs
|
|
830
|
+
* the same version globally and registers the global copy. CAPTAIN_TOOL_SETUP_RUNNING tells
|
|
831
|
+
* that install's own install script not to register or print its hint: setup does both.
|
|
832
|
+
*
|
|
833
|
+
* Returns { ok: true, binaryPath } or { ok: false, reason, command }.
|
|
834
|
+
*/
|
|
835
|
+
function installGlobally({ version, env = process.env } = {}) {
|
|
836
|
+
const { spawnSync } = require('child_process');
|
|
837
|
+
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
838
|
+
const shell = process.platform === 'win32';
|
|
839
|
+
const spec = `captain-tool@${version}`;
|
|
840
|
+
const command = `npm install -g ${spec}`;
|
|
841
|
+
const install = spawnSync(npm, ['install', '-g', spec], {
|
|
842
|
+
env: { ...env, CAPTAIN_TOOL_SETUP_RUNNING: '1' },
|
|
843
|
+
stdio: ['ignore', 'inherit', 'inherit'],
|
|
844
|
+
shell,
|
|
845
|
+
});
|
|
846
|
+
const failed = (why) => ({ ok: false, reason: `Could not install captain-tool globally: ${why}`, command });
|
|
847
|
+
if (install.error) return failed(install.error.message);
|
|
848
|
+
if (install.status !== 0) return failed(`npm exited with code ${install.status} (see its output above)`);
|
|
849
|
+
const globalPkg = path.join(globalNodeModules({ env, npm, shell }), 'captain-tool');
|
|
850
|
+
const binaryPath = findBinary(globalPkg);
|
|
851
|
+
if (!binaryPath) {
|
|
852
|
+
// The install worked; re-running it won't help. A version manager that keeps global
|
|
853
|
+
// packages elsewhere (Volta and the like; unverified) lands here.
|
|
854
|
+
return {
|
|
855
|
+
ok: false,
|
|
856
|
+
reason: `captain-tool installed globally, but its binary is not under ${globalPkg}. ` +
|
|
857
|
+
'Run `captain-tool setup` from that install.',
|
|
858
|
+
command: null,
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
return { ok: true, binaryPath };
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/**
|
|
865
|
+
* npm's global node_modules directory.
|
|
866
|
+
*
|
|
867
|
+
* WHY the env var first: npx gives the processes it runs `npm_config_global_prefix`, and
|
|
868
|
+
* npm's own output redacts anything shaped like a UUID (`npm root -g` printed
|
|
869
|
+
* `/tmp/***\/lib/node_modules` for a prefix under a UUID-named folder, verified on npm
|
|
870
|
+
* 11.19), so parsing it can name a directory that doesn't exist. `npm root -g` is the
|
|
871
|
+
* fallback when setup was not started by npm.
|
|
872
|
+
*/
|
|
873
|
+
function globalNodeModules({ env, npm, shell }) {
|
|
874
|
+
if (env.npm_config_global_prefix) return nodeModulesUnder(env.npm_config_global_prefix);
|
|
875
|
+
const { spawnSync } = require('child_process');
|
|
876
|
+
const root = spawnSync(npm, ['root', '-g'], { env, encoding: 'utf8', shell });
|
|
877
|
+
return root.status === 0 ? String(root.stdout || '').trim() : '';
|
|
878
|
+
}
|
|
879
|
+
|
|
644
880
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
645
881
|
|
|
646
882
|
async function main() {
|
|
@@ -650,29 +886,17 @@ async function main() {
|
|
|
650
886
|
console.log('This wizard registers captain-tool as an MCP server in your AI agent config(s).');
|
|
651
887
|
console.log('');
|
|
652
888
|
|
|
889
|
+
const flags = parseFlags(process.argv);
|
|
890
|
+
|
|
653
891
|
// Resolve the absolute binary path — same logic as the runner.
|
|
654
892
|
// If no platform binary is installed we fall back to npx (null triggers the
|
|
655
893
|
// npx path in resolveCommand / buildEntry*).
|
|
656
|
-
|
|
657
|
-
if (binaryPath) {
|
|
658
|
-
console.log(`Binary path: ${binaryPath}`);
|
|
659
|
-
} else {
|
|
660
|
-
const fallback = process.platform === 'win32'
|
|
661
|
-
? 'cmd /c npx captain-tool'
|
|
662
|
-
: 'npx captain-tool';
|
|
663
|
-
console.warn(
|
|
664
|
-
`Warning: no pre-built binary found for this platform.\n` +
|
|
665
|
-
`Falling back to: ${fallback}\n` +
|
|
666
|
-
`For best performance, install on a supported platform: win32-x64, darwin-x64, darwin-arm64, linux-x64\n`
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
console.log('');
|
|
894
|
+
let binaryPath = findBinary();
|
|
670
895
|
|
|
671
896
|
// Non-interactive paths first. `--yes`/`-y` configures every client and
|
|
672
897
|
// enables auto-approval without prompting (for scripts/CI). Without the
|
|
673
898
|
// flag, a non-interactive stdin must be rejected up front — see prompt()
|
|
674
899
|
// for why EOF mid-question would otherwise end as a silent exit 0.
|
|
675
|
-
const flags = parseFlags(process.argv);
|
|
676
900
|
const assumeYes = flags.assumeYes;
|
|
677
901
|
|
|
678
902
|
// `--uninstall` (and `--hooks-only`, which removes only the skill-proof hook and records a
|
|
@@ -756,6 +980,39 @@ async function main() {
|
|
|
756
980
|
console.log('');
|
|
757
981
|
}
|
|
758
982
|
|
|
983
|
+
// Run through npx: install globally, then register that copy (see installGlobally). Only
|
|
984
|
+
// now, once we know what to register: a run that stops at a question installs nothing.
|
|
985
|
+
if (isNpxCacheDir(__dirname)) {
|
|
986
|
+
console.log('Installing captain-tool globally, so your AI clients keep a path to it that npm');
|
|
987
|
+
console.log('does not clean up (npx runs it from a temporary cache)...');
|
|
988
|
+
const global = installGlobally({ version: require('../package.json').version });
|
|
989
|
+
if (!global.ok) {
|
|
990
|
+
console.error(`\n${global.reason}`);
|
|
991
|
+
console.error('Nothing was registered.');
|
|
992
|
+
if (global.command) {
|
|
993
|
+
console.error('Run this yourself, then run `captain-tool setup`:');
|
|
994
|
+
console.error(` ${global.command}`);
|
|
995
|
+
}
|
|
996
|
+
process.exit(1);
|
|
997
|
+
}
|
|
998
|
+
binaryPath = global.binaryPath;
|
|
999
|
+
console.log('');
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
if (binaryPath) {
|
|
1003
|
+
console.log(`Binary path: ${binaryPath}`);
|
|
1004
|
+
} else {
|
|
1005
|
+
const fallback = process.platform === 'win32'
|
|
1006
|
+
? 'cmd /c npx captain-tool'
|
|
1007
|
+
: 'npx captain-tool';
|
|
1008
|
+
console.warn(
|
|
1009
|
+
`Warning: no pre-built binary found for this platform.\n` +
|
|
1010
|
+
`Falling back to: ${fallback}\n` +
|
|
1011
|
+
`For best performance, install on a supported platform: win32-x64, darwin-x64, darwin-arm64, linux-x64\n`
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
console.log('');
|
|
1015
|
+
|
|
759
1016
|
// Configure each selected client.
|
|
760
1017
|
let anyFailed = false;
|
|
761
1018
|
const succeeded = new Set();
|
|
@@ -878,6 +1135,11 @@ module.exports = {
|
|
|
878
1135
|
uninstallClient,
|
|
879
1136
|
removePermissionRule,
|
|
880
1137
|
uninstall,
|
|
1138
|
+
updateJsonFile,
|
|
1139
|
+
registerAtInstall,
|
|
1140
|
+
findBinary,
|
|
1141
|
+
isNpxCacheDir,
|
|
1142
|
+
installGlobally,
|
|
881
1143
|
// Exported so the npx runner (`captain-tool setup`) can launch the wizard:
|
|
882
1144
|
// the require.main gate above means a bare require() runs nothing.
|
|
883
1145
|
main,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "captain-tool",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.120",
|
|
4
4
|
"description": "MCP server connecting Claude Desktop and VS Code Copilot to Captain Cloud",
|
|
5
5
|
"bin": {
|
|
6
6
|
"captain-tool": "bin/captain-tool",
|
|
@@ -11,10 +11,10 @@
|
|
|
11
11
|
"test": "node test/setup.test.js"
|
|
12
12
|
},
|
|
13
13
|
"optionalDependencies": {
|
|
14
|
-
"@captain-tool/captain-tool-win32-x64": "0.0.
|
|
15
|
-
"@captain-tool/captain-tool-darwin-x64": "0.0.
|
|
16
|
-
"@captain-tool/captain-tool-darwin-arm64": "0.0.
|
|
17
|
-
"@captain-tool/captain-tool-linux-x64": "0.0.
|
|
14
|
+
"@captain-tool/captain-tool-win32-x64": "0.0.120",
|
|
15
|
+
"@captain-tool/captain-tool-darwin-x64": "0.0.120",
|
|
16
|
+
"@captain-tool/captain-tool-darwin-arm64": "0.0.120",
|
|
17
|
+
"@captain-tool/captain-tool-linux-x64": "0.0.120"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
20
|
"bin/",
|
package/scripts/postinstall.js
CHANGED
|
@@ -4,51 +4,87 @@
|
|
|
4
4
|
/**
|
|
5
5
|
* postinstall.js — runs automatically after `npm install captain-tool`.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
7
|
+
* On a global install (`npm install -g captain-tool`) it registers captain with Claude Code,
|
|
8
|
+
* so the agent finds the server without a second command, and tells the person what it did.
|
|
9
|
+
* This is the fallback install path: `npx captain-tool@latest setup` is the primary one,
|
|
10
|
+
* because it can ask (which clients, auto-approval, skill proof) and this script cannot.
|
|
11
|
+
* What it registers, and when it declines to, is setup's registerAtInstall.
|
|
9
12
|
*
|
|
10
|
-
* WHY exit 0 on failure?
|
|
13
|
+
* WHY exit 0 on every failure?
|
|
11
14
|
* If postinstall exits non-zero, npm marks the entire install as failed.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* We warn and let them proceed.
|
|
15
|
+
* Not registering is never worth failing the install: the person can still run
|
|
16
|
+
* `captain-tool setup`. We warn and let them proceed.
|
|
15
17
|
*/
|
|
16
18
|
|
|
17
|
-
const
|
|
18
|
-
const fs = require('fs');
|
|
19
|
+
const fs = require('fs');
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Show `text` to the person running npm.
|
|
23
|
+
*
|
|
24
|
+
* WHY /dev/tty: npm runs install scripts with their output hidden (foreground-scripts is off
|
|
25
|
+
* by default), so stdout reaches no one. The terminal itself still does. With no terminal
|
|
26
|
+
* (CI, scripts) opening it fails and we fall back to stdout, which npm shows only with
|
|
27
|
+
* --foreground-scripts. Windows has no /dev/tty.
|
|
28
|
+
*/
|
|
29
|
+
function tellUser(text) {
|
|
30
|
+
if (process.platform !== 'win32') {
|
|
29
31
|
try {
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const fd = fs.openSync('/dev/tty', 'w');
|
|
33
|
+
try {
|
|
34
|
+
fs.writeSync(fd, text);
|
|
35
|
+
} finally {
|
|
36
|
+
fs.closeSync(fd);
|
|
37
|
+
}
|
|
38
|
+
return;
|
|
34
39
|
} catch (_) {}
|
|
35
40
|
}
|
|
36
|
-
|
|
41
|
+
process.stdout.write(text);
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
const
|
|
44
|
+
const SETUP_HINT = 'Run `captain-tool setup` to auto-approve Captain\'s tools, turn on skill proof, or set up other AI clients.';
|
|
40
45
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
let result = { registered: false, reason: 'error' };
|
|
47
|
+
try {
|
|
48
|
+
const path = require('path');
|
|
49
|
+
const setup = require('../bin/captain-tool-setup');
|
|
50
|
+
const binaryPath = setup.findBinary();
|
|
51
|
+
if (!binaryPath) {
|
|
52
|
+
// Every install, local or global, hears about this: nothing works without the binary.
|
|
53
|
+
tellUser(
|
|
54
|
+
'\ncaptain-tool: WARNING — no pre-built binary was found for this platform.\n' +
|
|
55
|
+
'Supported platforms: win32-x64, darwin-x64, darwin-arm64, linux-x64\n' +
|
|
56
|
+
'If you believe this is an error, please open an issue:\n' +
|
|
57
|
+
' https://github.com/your-org/captain-tool/issues\n\n'
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
result = setup.registerAtInstall({ binaryPath, packageDir: path.join(__dirname, '..') });
|
|
61
|
+
} catch (_) {
|
|
62
|
+
// Fall through: the person is pointed at setup below.
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
switch (result.reason) {
|
|
66
|
+
case 'registered':
|
|
67
|
+
tellUser(`\ncaptain-tool: registered with Claude Code. ${SETUP_HINT}\n\n`);
|
|
68
|
+
break;
|
|
69
|
+
case 'no-binary':
|
|
70
|
+
break; // already told, above
|
|
71
|
+
case 'foreign':
|
|
72
|
+
tellUser(
|
|
73
|
+
'\ncaptain-tool: installed. Claude Code already has a different server named "captain",\n' +
|
|
74
|
+
'so it was left alone. Run `captain-tool setup` to register captain-tool instead.\n\n'
|
|
75
|
+
);
|
|
76
|
+
break;
|
|
77
|
+
case 'no-claude-code':
|
|
78
|
+
case 'unreadable':
|
|
79
|
+
case 'sudo':
|
|
80
|
+
case 'error':
|
|
81
|
+
tellUser('\ncaptain-tool: installed. Run `captain-tool setup` to connect it to your AI client.\n\n');
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
// not-global, npx, ci, setup: nobody to tell, or setup is talking to them already.
|
|
85
|
+
break;
|
|
54
86
|
}
|
|
87
|
+
|
|
88
|
+
// Exit 0 intentionally — see module comment above. exitCode, not process.exit(): the stdout
|
|
89
|
+
// fallback can be asynchronous (pipes, Windows), and an exit would drop the message.
|
|
90
|
+
process.exitCode = 0;
|