turno-cli 0.1.1 → 0.3.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/CHANGELOG.md +432 -0
- package/README.md +228 -73
- package/dist/accounts/audit.js +154 -12
- package/dist/accounts/builtin.js +100 -25
- package/dist/accounts/delegate.js +199 -35
- package/dist/accounts/policy.js +79 -0
- package/dist/accounts/registry.js +39 -21
- package/dist/agents/adapter.js +29 -0
- package/dist/agents/claude.js +246 -0
- package/dist/agents/codex.js +186 -0
- package/dist/agents/copilot.js +56 -0
- package/dist/agents/cursor.js +58 -0
- package/dist/agents/index.js +26 -86
- package/dist/agents/snippets.js +43 -13
- package/dist/cli/args.js +38 -6
- package/dist/cli/commands/accounts.js +119 -52
- package/dist/cli/commands/auto.js +121 -33
- package/dist/cli/commands/browser-identity.js +514 -0
- package/dist/cli/commands/browser-session.js +102 -0
- package/dist/cli/commands/browser-vault.js +193 -0
- package/dist/cli/commands/browser.js +319 -138
- package/dist/cli/commands/complete.js +14 -3
- package/dist/cli/commands/completion.js +8 -2
- package/dist/cli/commands/config.js +186 -24
- package/dist/cli/commands/demo.js +321 -0
- package/dist/cli/commands/doctor.js +525 -123
- package/dist/cli/commands/handoff.js +33 -22
- package/dist/cli/commands/identity.js +192 -54
- package/dist/cli/commands/init.js +648 -0
- package/dist/cli/commands/limit.js +84 -16
- package/dist/cli/commands/mcp-call.js +240 -0
- package/dist/cli/commands/mcp-oauth.js +221 -0
- package/dist/cli/commands/mcp-observe.js +539 -0
- package/dist/cli/commands/mcp-share.js +201 -0
- package/dist/cli/commands/mcp.js +816 -187
- package/dist/cli/commands/mcpd.js +388 -76
- package/dist/cli/commands/migrate.js +6 -4
- package/dist/cli/commands/open.js +115 -27
- package/dist/cli/commands/profile.js +396 -78
- package/dist/cli/commands/resolve.js +14 -3
- package/dist/cli/commands/run.js +36 -6
- package/dist/cli/commands/secret.js +80 -76
- package/dist/cli/commands/session.js +17 -6
- package/dist/cli/commands/sessions.js +48 -12
- package/dist/cli/commands/setup.js +214 -164
- package/dist/cli/commands/status.js +227 -44
- package/dist/cli/commands/statusline.js +311 -114
- package/dist/cli/commands/ui.js +78 -68
- package/dist/cli/commands/undo.js +193 -18
- package/dist/cli/commands/uninstall.js +481 -0
- package/dist/cli/commands/upgrade.js +172 -79
- package/dist/cli/commands/usage.js +125 -51
- package/dist/cli/commands/version.js +21 -7
- package/dist/cli/commands/which.js +9 -24
- package/dist/cli/compile-cache.js +32 -0
- package/dist/cli/diagnostics.js +184 -0
- package/dist/cli/exit.js +65 -0
- package/dist/cli/group.js +24 -12
- package/dist/cli/help.js +62 -0
- package/dist/cli/highlight.js +236 -0
- package/dist/cli/hints.js +30 -0
- package/dist/cli/io.js +186 -1
- package/dist/cli/launch.js +52 -39
- package/dist/cli/launchers.js +161 -95
- package/dist/cli/main.js +201 -31
- package/dist/cli/optional.js +144 -0
- package/dist/cli/output.js +123 -0
- package/dist/cli/picker.js +459 -0
- package/dist/cli/registry.js +61 -6
- package/dist/cli/safe.js +87 -0
- package/dist/cli/secret-input.js +63 -0
- package/dist/cli/security.js +561 -0
- package/dist/cli/style.js +311 -0
- package/dist/cli/topics.js +142 -0
- package/dist/core/config.js +181 -23
- package/dist/core/exhausted.js +6 -8
- package/dist/core/fsutil.js +39 -0
- package/dist/core/journal.js +294 -16
- package/dist/core/manifest.js +153 -6
- package/dist/core/paths.js +14 -13
- package/dist/core/profiles.js +160 -10
- package/dist/core/resolve.js +139 -23
- package/dist/core/secrets.js +68 -23
- package/dist/core/sessions.js +13 -20
- package/dist/core/state-file.js +107 -0
- package/dist/core/update.js +31 -18
- package/dist/core/version.js +57 -10
- package/dist/demo/server.js +42 -0
- package/dist/mcpd/cache.js +23 -10
- package/dist/mcpd/calllog.js +254 -0
- package/dist/mcpd/client-names.js +46 -0
- package/dist/mcpd/connectors.js +188 -18
- package/dist/mcpd/daemon.js +472 -115
- package/dist/mcpd/folder-sets.js +42 -0
- package/dist/mcpd/gateway.js +473 -51
- package/dist/mcpd/health.js +313 -0
- package/dist/mcpd/importer.js +453 -36
- package/dist/mcpd/instances.js +453 -65
- package/dist/mcpd/jsonrpc.js +11 -1
- package/dist/mcpd/launchd.js +65 -48
- package/dist/mcpd/middleware.js +144 -0
- package/dist/mcpd/modern.js +686 -0
- package/dist/mcpd/oauth.js +845 -0
- package/dist/mcpd/peerpid.js +134 -6
- package/dist/mcpd/pressure.js +88 -0
- package/dist/mcpd/rss.js +166 -0
- package/dist/mcpd/service.js +191 -0
- package/dist/mcpd/share.js +217 -0
- package/dist/mcpd/state.js +183 -0
- package/dist/mcpd/sync.js +72 -87
- package/dist/mcpd/systemd.js +83 -0
- package/dist/mcpd/tokens.js +85 -0
- package/dist/mcpd/upstream.js +163 -46
- package/dist/statusline/cost.js +167 -44
- package/dist/statusline/input.js +78 -0
- package/dist/statusline/render.js +9 -3
- package/dist/ui/fonts.js +8 -3
- package/dist/ui/settings.js +33 -16
- package/npm-shrinkwrap.json +1102 -0
- package/package.json +10 -9
- package/dist/cli/commands/available.js +0 -18
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
2
|
-
import { realpathSync } from "node:fs";
|
|
1
|
+
import { createReadStream, existsSync, realpathSync, statSync } from "node:fs";
|
|
3
2
|
import { join } from "node:path";
|
|
4
3
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
turno mcpd logs [-n N] last lines of the mcpd log`;
|
|
4
|
+
import { Journal } from "../../core/journal.js";
|
|
5
|
+
import { paths, tildify } from "../../core/paths.js";
|
|
6
|
+
import { DRY_RUN, JSON_FLAG, TAIL, bool, count, parseFlags, str } from "../args.js";
|
|
7
|
+
import { Exit } from "../exit.js";
|
|
8
|
+
import { emit, jsonOutput } from "../output.js";
|
|
9
|
+
import { group } from "../group.js";
|
|
10
|
+
import { fail, paint, plural } from "../io.js";
|
|
11
|
+
import { duration, spinner, style } from "../style.js";
|
|
14
12
|
/** GET /status from the running daemon. */
|
|
15
13
|
export async function fetchStatus(env) {
|
|
16
|
-
const { mcpdPort, readToken } = await import("../../mcpd/
|
|
17
|
-
const token = readToken(env);
|
|
14
|
+
const { mcpdPort, readToken } = await import("../../mcpd/state.js");
|
|
15
|
+
const token = readToken(env, () => { });
|
|
18
16
|
if (!token)
|
|
19
17
|
return undefined;
|
|
20
18
|
try {
|
|
@@ -50,19 +48,67 @@ async function rssOf(pids) {
|
|
|
50
48
|
function mb(bytes) {
|
|
51
49
|
return `${Math.round(bytes / 1048576)} MB`;
|
|
52
50
|
}
|
|
51
|
+
/** Why mcpd is down, as far as the files and the port can tell (also shown by `turno status`). */
|
|
52
|
+
export async function downReasons(env) {
|
|
53
|
+
const { daemonFiles, loadConfigSafe, readLock, readStartError } = await import("../../mcpd/state.js");
|
|
54
|
+
const { describeOwner, portOwner } = await import("../../mcpd/peerpid.js");
|
|
55
|
+
const out = [];
|
|
56
|
+
const port = loadConfigSafe(env).config.mcpd.port;
|
|
57
|
+
const owner = await portOwner(port, { env });
|
|
58
|
+
if (owner)
|
|
59
|
+
out.push(`port ${port} is in use by ${describeOwner(owner)}`);
|
|
60
|
+
const e = readStartError(env);
|
|
61
|
+
if (e)
|
|
62
|
+
out.push(`last start failed at ${localTime(e.at)}: ${e.message}`);
|
|
63
|
+
const lock = readLock(env);
|
|
64
|
+
if (lock)
|
|
65
|
+
out.push(`stale lock from pid ${lock.pid} (${tildify(daemonFiles(env).lock, env)}); the next start clears it`);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/** `turno mcp daemon status --json`: mcpd's /status, or `{ running: false, problems }`. */
|
|
69
|
+
const STATUS_JSON = jsonOutput([
|
|
70
|
+
"running",
|
|
71
|
+
"problems",
|
|
72
|
+
"pid",
|
|
73
|
+
"version",
|
|
74
|
+
"port",
|
|
75
|
+
"startedAt",
|
|
76
|
+
"sessions",
|
|
77
|
+
"agents",
|
|
78
|
+
"memoryPressure",
|
|
79
|
+
"instances",
|
|
80
|
+
"manifest",
|
|
81
|
+
"config",
|
|
82
|
+
"callLog",
|
|
83
|
+
"serverRss",
|
|
84
|
+
]);
|
|
53
85
|
async function status(args, io) {
|
|
86
|
+
const p = parseFlags(args, JSON_FLAG, 0);
|
|
54
87
|
const s = await fetchStatus(io.env);
|
|
55
88
|
if (!s) {
|
|
56
|
-
|
|
57
|
-
|
|
89
|
+
const reasons = await downReasons(io.env);
|
|
90
|
+
if (bool(p, "json"))
|
|
91
|
+
emit(io, p, STATUS_JSON, { running: false, problems: reasons });
|
|
92
|
+
else {
|
|
93
|
+
io.out("mcpd is not running (turno mcp daemon start)");
|
|
94
|
+
for (const r of reasons)
|
|
95
|
+
io.out(` ${r}`);
|
|
96
|
+
}
|
|
97
|
+
return Exit.gatewayDown;
|
|
58
98
|
}
|
|
59
|
-
if (
|
|
60
|
-
io
|
|
99
|
+
if (bool(p, "json")) {
|
|
100
|
+
emit(io, p, STATUS_JSON, s);
|
|
61
101
|
return 0;
|
|
62
102
|
}
|
|
63
103
|
const instances = s.instances ?? [];
|
|
64
104
|
const rss = await rssOf([Number(s.pid), ...instances.map((i) => Number(i.pid)).filter(Boolean)]);
|
|
65
|
-
io.out(`${paint(io, "mcpd", "1")} pid ${s.pid} · ${mb(rss.get(Number(s.pid)) ?? 0)} · ${s.sessions
|
|
105
|
+
io.out(`${paint(io, "mcpd", "1")} pid ${s.pid}${s.port ? ` · port ${s.port}` : ""} · ${mb(rss.get(Number(s.pid)) ?? 0)} · ${plural(Number(s.sessions) || 0, "client session")}`);
|
|
106
|
+
const manifest = s.manifest;
|
|
107
|
+
if (manifest?.error)
|
|
108
|
+
io.out(` ${paint(io, "warn", "33")} mcp.toml is invalid, serving the ${manifest.source}: ${manifest.error}`);
|
|
109
|
+
const config = s.config;
|
|
110
|
+
if (config?.error)
|
|
111
|
+
io.out(` ${paint(io, "warn", "33")} config.toml is invalid, using defaults: ${config.error}`);
|
|
66
112
|
if (!instances.length) {
|
|
67
113
|
io.out(" no servers running (they start on first use)");
|
|
68
114
|
return 0;
|
|
@@ -72,16 +118,50 @@ async function status(args, io) {
|
|
|
72
118
|
const mem = rss.get(Number(i.pid)) ?? 0;
|
|
73
119
|
total += mem;
|
|
74
120
|
const holder = i.holder;
|
|
75
|
-
io.out(` ${String(i.server).padEnd(24)} ${String(i.variant).padEnd(10)} pid ${String(i.pid ?? "-").padEnd(7)} ${mb(mem).padStart(7)} ${i.clients}
|
|
121
|
+
io.out(` ${String(i.server).padEnd(24)} ${String(i.variant).padEnd(10)} pid ${String(i.pid ?? "-").padEnd(7)} ${mb(mem).padStart(7)} ${plural(Number(i.clients) || 0, "client")} idle ${i.idleSeconds}s${holder ? ` held by ${holder.profile ?? "?"}/${holder.pid ?? "?"}` : ""}`);
|
|
122
|
+
const busy = inflightText(i.inflightByClient);
|
|
123
|
+
if (busy)
|
|
124
|
+
io.out(` ${busy}`);
|
|
76
125
|
}
|
|
77
126
|
io.out(` total ${mb(total)}`);
|
|
78
127
|
return 0;
|
|
79
128
|
}
|
|
80
|
-
|
|
81
|
-
|
|
129
|
+
/** "2 calls in flight: 1f2e3d4c 1, 9a8b7c6d 1" from /status `inflightByClient` (client session ids). */
|
|
130
|
+
export function inflightText(byClient) {
|
|
131
|
+
if (!byClient || typeof byClient !== "object")
|
|
132
|
+
return undefined;
|
|
133
|
+
const rows = Object.entries(byClient).filter(([, n]) => Number(n) > 0);
|
|
134
|
+
if (!rows.length)
|
|
135
|
+
return undefined;
|
|
136
|
+
const total = rows.reduce((t, [, n]) => t + Number(n), 0);
|
|
137
|
+
return `${plural(total, "call")} in flight: ${rows.map(([id, n]) => `${id.slice(0, 8)} ${n}`).join(", ")}`;
|
|
138
|
+
}
|
|
139
|
+
/** The CLI entry the service runs when no stable `turno` is on PATH. */
|
|
140
|
+
export function entryScript() {
|
|
141
|
+
const dir = fileURLToPath(new URL("..", import.meta.url));
|
|
142
|
+
for (const f of ["main.js", "main.ts"]) {
|
|
143
|
+
const file = join(dir, f);
|
|
144
|
+
if (existsSync(file))
|
|
145
|
+
return realpathSync(file);
|
|
146
|
+
}
|
|
147
|
+
return join(dir, "main.js");
|
|
148
|
+
}
|
|
149
|
+
/** "2026-11-16 15:26:45" in the local time zone, from an ISO timestamp. */
|
|
150
|
+
export function localTime(iso) {
|
|
151
|
+
const d = new Date(iso);
|
|
152
|
+
if (Number.isNaN(d.getTime()))
|
|
153
|
+
return iso;
|
|
154
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
155
|
+
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
156
|
+
}
|
|
157
|
+
/** A log line with its leading UTC timestamp shown in local time. */
|
|
158
|
+
export function localizeLine(line) {
|
|
159
|
+
const m = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z) /.exec(line);
|
|
160
|
+
return m ? `${localTime(m[1])} ${line.slice(m[0].length)}` : line;
|
|
82
161
|
}
|
|
83
162
|
async function logs(args, io) {
|
|
84
|
-
const
|
|
163
|
+
const p = parseFlags(args, { ...TAIL, utc: { type: "boolean" } }, 0);
|
|
164
|
+
const n = count(p, "tail", 40);
|
|
85
165
|
const file = join(paths(io.env).logsDir, "mcpd.log");
|
|
86
166
|
if (!existsSync(file)) {
|
|
87
167
|
io.out("no log yet");
|
|
@@ -94,68 +174,300 @@ async function logs(args, io) {
|
|
|
94
174
|
chunks.push(c);
|
|
95
175
|
const lines = Buffer.concat(chunks).toString("utf8").trimEnd().split("\n");
|
|
96
176
|
for (const l of lines.slice(-n))
|
|
97
|
-
io.out(l);
|
|
177
|
+
io.out(bool(p, "utc") ? l : localizeLine(l));
|
|
98
178
|
return 0;
|
|
99
179
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
180
|
+
const isService = (env) => Boolean(env.TURNO_MCPD_SERVICE);
|
|
181
|
+
async function runDaemon(args, io) {
|
|
182
|
+
const p = parseFlags(args, { port: { type: "string" } }, 0);
|
|
183
|
+
const raw = str(p, "port");
|
|
184
|
+
let port;
|
|
185
|
+
if (raw !== undefined) {
|
|
186
|
+
port = Number(raw);
|
|
187
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535 || String(port) !== raw.trim())
|
|
188
|
+
fail("--port must be an integer between 0 and 65535");
|
|
189
|
+
}
|
|
190
|
+
const { AlreadyRunningError, PortBusyError, installCrashGuards, startDaemon } = await import("../../mcpd/daemon.js");
|
|
191
|
+
const service = isService(io.env);
|
|
192
|
+
let waiting = false;
|
|
193
|
+
for (;;) {
|
|
194
|
+
try {
|
|
195
|
+
const d = await startDaemon({
|
|
196
|
+
env: io.env,
|
|
197
|
+
logToStdout: Boolean(process.stdout.isTTY) && !service,
|
|
198
|
+
...(port !== undefined ? { port } : {}),
|
|
199
|
+
});
|
|
200
|
+
io.err(`mcpd listening on 127.0.0.1:${d.port}`);
|
|
201
|
+
const unguard = installCrashGuards(d.log, () => d.stop());
|
|
202
|
+
await new Promise((resolve) => {
|
|
203
|
+
const stop = () => {
|
|
204
|
+
void d.stop().then(resolve, resolve);
|
|
205
|
+
};
|
|
206
|
+
process.once("SIGINT", stop);
|
|
207
|
+
process.once("SIGTERM", stop);
|
|
208
|
+
});
|
|
209
|
+
unguard();
|
|
210
|
+
return 0;
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
if (err instanceof AlreadyRunningError) {
|
|
214
|
+
io.err(`turno: ${err.message}`);
|
|
215
|
+
// Under a service a clean exit is not restarted: the other mcpd serves.
|
|
216
|
+
return service ? 0 : 1;
|
|
123
217
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
await new Promise((r) => setTimeout(r, 250));
|
|
136
|
-
}
|
|
137
|
-
io.err("mcpd did not answer yet; see turno mcpd logs");
|
|
218
|
+
if (err instanceof PortBusyError && service) {
|
|
219
|
+
// Exiting would make the supervisor loop; wait for the port instead (doctor shows why).
|
|
220
|
+
if (!waiting)
|
|
221
|
+
io.err(`turno: ${err.message}; retrying every 30 s`);
|
|
222
|
+
waiting = true;
|
|
223
|
+
await new Promise((r) => setTimeout(r, 30_000));
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (err instanceof PortBusyError) {
|
|
227
|
+
io.err(`turno: ${err.message}`);
|
|
138
228
|
return 1;
|
|
139
229
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
230
|
+
throw err;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function backendOrReport(io) {
|
|
235
|
+
const { ServiceError, serviceBackend } = await import("../../mcpd/service.js");
|
|
236
|
+
try {
|
|
237
|
+
return await serviceBackend(io.env);
|
|
238
|
+
}
|
|
239
|
+
catch (err) {
|
|
240
|
+
if (err instanceof ServiceError) {
|
|
241
|
+
io.err(`turno: ${err.message}`);
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
throw err;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** Run a service step; a ServiceError is one line on stderr and exit 1, never a stack. */
|
|
248
|
+
async function serviceStep(io, step) {
|
|
249
|
+
const { ServiceError } = await import("../../mcpd/service.js");
|
|
250
|
+
try {
|
|
251
|
+
return await step();
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
if (err instanceof ServiceError) {
|
|
255
|
+
io.err(`turno: ${err.message}`);
|
|
256
|
+
return 1;
|
|
257
|
+
}
|
|
258
|
+
throw err;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async function waitFor(check, ms) {
|
|
262
|
+
for (let waited = 0; waited <= ms; waited += 250) {
|
|
263
|
+
if (await check())
|
|
264
|
+
return true;
|
|
265
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
266
|
+
}
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
/** Write (or rewrite) the service file and (re)load it. Shared by `start` and `upgrade --finish`. */
|
|
270
|
+
export async function installDaemonService(io, opts = {}) {
|
|
271
|
+
const backend = await backendOrReport(io);
|
|
272
|
+
if (!backend)
|
|
273
|
+
return 1;
|
|
274
|
+
return serviceStep(io, async () => {
|
|
275
|
+
const { chooseProgram, installService } = await import("../../mcpd/service.js");
|
|
276
|
+
const choice = chooseProgram({ env: io.env, execPath: process.execPath, script: entryScript() });
|
|
277
|
+
const journal = opts.journal ?? new Journal(io.env, opts.command ?? "mcp daemon start", Boolean(opts.dryRun));
|
|
278
|
+
if (!opts.dryRun) {
|
|
279
|
+
const { ensureToken } = await import("../../mcpd/state.js");
|
|
280
|
+
ensureToken(io.env, (m) => io.err(`turno: ${m}`));
|
|
281
|
+
}
|
|
282
|
+
const r = await installService(backend, { program: choice.program, env: io.env }, journal);
|
|
283
|
+
if (opts.dryRun) {
|
|
284
|
+
io.out(`would write ${tildify(r.file, io.env)}${r.changed ? "" : " (unchanged)"} and load the ${backend.noun}:`);
|
|
285
|
+
io.out("");
|
|
286
|
+
io.out(r.text.trimEnd());
|
|
287
|
+
}
|
|
288
|
+
else
|
|
289
|
+
io.out(style(io).note("ok", `${backend.noun} ${r.changed ? "written" : "unchanged"}: ${tildify(r.file, io.env)}`));
|
|
290
|
+
if (choice.warning)
|
|
291
|
+
io.err(style(io).note("warn", `warning: ${choice.warning}`));
|
|
292
|
+
const hint = opts.journal ? undefined : journal.hint();
|
|
293
|
+
if (hint)
|
|
294
|
+
io.out(hint);
|
|
295
|
+
return 0;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/** Install the service and wait until mcpd answers. Non-zero when either step fails. */
|
|
299
|
+
export async function startDaemonService(io, opts = {}) {
|
|
300
|
+
const code = await installDaemonService(io, opts);
|
|
301
|
+
if (code)
|
|
302
|
+
return code;
|
|
303
|
+
const st = style(io);
|
|
304
|
+
const spin = spinner(io, "waiting for mcpd to answer");
|
|
305
|
+
const up = await waitFor(async () => Boolean(await fetchStatus(io.env)), 5000);
|
|
306
|
+
const ms = spin.stop();
|
|
307
|
+
if (up) {
|
|
308
|
+
io.out(st.note("ok", `mcpd is running ${st.muted(`(${duration(ms)})`)}`));
|
|
309
|
+
return 0;
|
|
310
|
+
}
|
|
311
|
+
io.err(st.note("err", "mcpd did not answer yet; see turno mcp daemon status and turno mcp daemon logs"));
|
|
312
|
+
return 1;
|
|
313
|
+
}
|
|
314
|
+
async function start(args, io) {
|
|
315
|
+
const p = parseFlags(args, DRY_RUN, 0);
|
|
316
|
+
const dryRun = bool(p, "dry-run");
|
|
317
|
+
if (dryRun)
|
|
318
|
+
return installDaemonService(io, { dryRun });
|
|
319
|
+
return startDaemonService(io);
|
|
320
|
+
}
|
|
321
|
+
/** Stop the service and remove its file, so mcpd does not come back at login. A hand-started mcpd gets SIGTERM. */
|
|
322
|
+
async function stop(args, io) {
|
|
323
|
+
const p = parseFlags(args, DRY_RUN, 0);
|
|
324
|
+
const dryRun = bool(p, "dry-run");
|
|
325
|
+
const { installedBackend, removeService } = await import("../../mcpd/service.js");
|
|
326
|
+
const { liveLock } = await import("../../mcpd/state.js");
|
|
327
|
+
return serviceStep(io, async () => {
|
|
328
|
+
const backend = (await installedBackend(io.env)) ?? (await backendOrReport({ ...io, err: () => { } }));
|
|
329
|
+
const journal = new Journal(io.env, "mcp daemon stop", dryRun);
|
|
330
|
+
let did = false;
|
|
331
|
+
if (backend) {
|
|
332
|
+
const r = await removeService(backend, io.env, journal);
|
|
333
|
+
if (r.existed) {
|
|
334
|
+
io.out(`${dryRun ? "would remove" : "removed"} ${tildify(r.file, io.env)} (turno mcp daemon start installs it again)`);
|
|
335
|
+
did = true;
|
|
145
336
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
337
|
+
}
|
|
338
|
+
const lock = liveLock(io.env);
|
|
339
|
+
if (lock && lock.pid !== process.pid) {
|
|
340
|
+
if (dryRun)
|
|
341
|
+
io.out(`would stop mcpd (pid ${lock.pid})`);
|
|
342
|
+
else {
|
|
343
|
+
try {
|
|
344
|
+
process.kill(lock.pid, "SIGTERM");
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
/* gone already */
|
|
348
|
+
}
|
|
349
|
+
await waitFor(async () => !liveLock(io.env), 5000);
|
|
151
350
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
351
|
+
did = true;
|
|
352
|
+
}
|
|
353
|
+
if (!dryRun)
|
|
354
|
+
io.out(did ? "mcpd stopped" : "mcpd was not running");
|
|
355
|
+
const hint = journal.hint();
|
|
356
|
+
if (hint)
|
|
357
|
+
io.out(hint);
|
|
358
|
+
return 0;
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
async function restart(args, io) {
|
|
362
|
+
parseFlags(args, {}, 0);
|
|
363
|
+
const { installedBackend } = await import("../../mcpd/service.js");
|
|
364
|
+
return serviceStep(io, async () => {
|
|
365
|
+
const backend = await installedBackend(io.env);
|
|
366
|
+
if (!backend) {
|
|
367
|
+
io.err("turno: the mcpd service is not installed (turno mcp daemon start)");
|
|
368
|
+
return 1;
|
|
369
|
+
}
|
|
370
|
+
await backend.restart(io.env);
|
|
371
|
+
io.out("mcpd restarted; open sessions reconnect on their next call");
|
|
372
|
+
return 0;
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
/** `turno mcp token rotate`: a new master token, then mcpd picks it up and profiles need a sync. */
|
|
376
|
+
async function token(args, io) {
|
|
377
|
+
const p = parseFlags(args, DRY_RUN, 1);
|
|
378
|
+
const action = p.positionals[0];
|
|
379
|
+
if (action !== "rotate")
|
|
380
|
+
return fail(`usage: turno mcp token rotate [--dry-run]`);
|
|
381
|
+
const file = tildify(paths(io.env).mcpdToken, io.env);
|
|
382
|
+
if (bool(p, "dry-run")) {
|
|
383
|
+
io.out(`would write a new token to ${file} and restart mcpd`);
|
|
384
|
+
return 0;
|
|
385
|
+
}
|
|
386
|
+
const { liveLock, rotateToken } = await import("../../mcpd/state.js");
|
|
387
|
+
const { installedBackend } = await import("../../mcpd/service.js");
|
|
388
|
+
rotateToken(io.env);
|
|
389
|
+
io.out(`new gateway token written to ${file}`);
|
|
390
|
+
const code = await serviceStep(io, async () => {
|
|
391
|
+
const backend = await installedBackend(io.env);
|
|
392
|
+
if (backend && (await backend.isLoaded(io.env))) {
|
|
393
|
+
await backend.restart(io.env);
|
|
394
|
+
io.out("mcpd restarted with the new token");
|
|
395
|
+
return 0;
|
|
158
396
|
}
|
|
397
|
+
const lock = liveLock(io.env);
|
|
398
|
+
if (lock && lock.pid !== process.pid) {
|
|
399
|
+
process.kill(lock.pid, "SIGHUP");
|
|
400
|
+
io.out(`mcpd (pid ${lock.pid}) reloads the new token`);
|
|
401
|
+
}
|
|
402
|
+
else
|
|
403
|
+
io.out("mcpd is not running; it uses the new token when it starts");
|
|
404
|
+
return 0;
|
|
405
|
+
});
|
|
406
|
+
io.out("Every profile token derives from it and stopped working: run `turno mcp sync` so each profile gets its new one; open sessions need a restart.");
|
|
407
|
+
return code;
|
|
408
|
+
}
|
|
409
|
+
const SUBS = {
|
|
410
|
+
start: {
|
|
411
|
+
synopsis: "[--dry-run]",
|
|
412
|
+
summary: "install the service (LaunchAgent or systemd user unit) and start mcpd",
|
|
413
|
+
flags: ["--dry-run"],
|
|
414
|
+
run: start,
|
|
415
|
+
},
|
|
416
|
+
stop: {
|
|
417
|
+
synopsis: "[--dry-run]",
|
|
418
|
+
summary: "stop mcpd and remove the service, so it does not start at login",
|
|
419
|
+
flags: ["--dry-run"],
|
|
420
|
+
run: stop,
|
|
421
|
+
},
|
|
422
|
+
restart: { summary: "restart the service (open sessions reconnect)", run: restart },
|
|
423
|
+
status: {
|
|
424
|
+
synopsis: "[--json]",
|
|
425
|
+
summary: "running servers, their clients and memory; why mcpd is down (exit 5)",
|
|
426
|
+
flags: ["--json"],
|
|
427
|
+
json: STATUS_JSON,
|
|
428
|
+
run: status,
|
|
429
|
+
},
|
|
430
|
+
logs: {
|
|
431
|
+
synopsis: "[-n N] [--utc]",
|
|
432
|
+
summary: "last lines of the mcpd log, in local time",
|
|
433
|
+
flags: ["--tail", "--utc"],
|
|
434
|
+
run: logs,
|
|
435
|
+
},
|
|
436
|
+
run: {
|
|
437
|
+
synopsis: "[--port N]",
|
|
438
|
+
summary: "run the gateway in the foreground (what the service runs)",
|
|
439
|
+
flags: ["--port"],
|
|
440
|
+
run: runDaemon,
|
|
441
|
+
},
|
|
442
|
+
token: {
|
|
443
|
+
synopsis: "rotate [--dry-run]",
|
|
444
|
+
summary: "replace the gateway token (then turno mcp sync)",
|
|
445
|
+
args: ["any"],
|
|
446
|
+
flags: ["--dry-run"],
|
|
447
|
+
run: token,
|
|
448
|
+
},
|
|
449
|
+
};
|
|
450
|
+
export const tokenCommand = {
|
|
451
|
+
summary: "replace the gateway token",
|
|
452
|
+
usage: "Usage: turno mcp token rotate [--dry-run]\n\nWrites a new token (mode 600), restarts mcpd, and asks you to run turno mcp sync.\n\nExamples:\n $ turno mcp token rotate --dry-run\n $ turno mcp token rotate && turno mcp sync",
|
|
453
|
+
run: async (args, io) => {
|
|
454
|
+
if (!args.includes("--help") && !args.includes("-h"))
|
|
455
|
+
return token(args, io);
|
|
456
|
+
io.out(tokenCommand.usage);
|
|
457
|
+
return 0;
|
|
159
458
|
},
|
|
160
459
|
};
|
|
460
|
+
export const command = group("mcp daemon", "run or manage the MCP gateway daemon", SUBS, {
|
|
461
|
+
footer: "mcpd listens on 127.0.0.1 at mcpd.port (config.toml) and never moves to another port.\n" +
|
|
462
|
+
"start keeps it alive with a LaunchAgent on macOS or a systemd user unit on Linux; stop removes it.",
|
|
463
|
+
examples: [
|
|
464
|
+
"turno mcp daemon start",
|
|
465
|
+
"turno mcp daemon status --json",
|
|
466
|
+
"turno mcp daemon restart",
|
|
467
|
+
"turno mcp daemon logs -n 50",
|
|
468
|
+
"turno mcp daemon stop --dry-run",
|
|
469
|
+
"turno mcp daemon run --port 47900",
|
|
470
|
+
"turno mcp daemon token rotate",
|
|
471
|
+
],
|
|
472
|
+
});
|
|
161
473
|
//# sourceMappingURL=mcpd.js.map
|
|
@@ -2,6 +2,7 @@ import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { isValidProfileName, parseConfig } from "../../core/config.js";
|
|
4
4
|
import { paths, tildify } from "../../core/paths.js";
|
|
5
|
+
import { DRY_RUN, FORCE, bool, flagsOf, parseFlags } from "../args.js";
|
|
5
6
|
import { fail } from "../io.js";
|
|
6
7
|
import { detectNerdTerminals } from "../../ui/terminals.js";
|
|
7
8
|
import { Journal } from "../../core/journal.js";
|
|
@@ -157,8 +158,11 @@ export function migrate(io, opts = {}) {
|
|
|
157
158
|
io.out(`undo with: turno undo ${journal.stamp}`);
|
|
158
159
|
return 0;
|
|
159
160
|
}
|
|
161
|
+
const SPEC = { ...DRY_RUN, ...FORCE };
|
|
160
162
|
export const command = {
|
|
161
163
|
summary: "import the bash claudio config and state into the new layout",
|
|
164
|
+
hidden: true,
|
|
165
|
+
flags: flagsOf(SPEC),
|
|
162
166
|
usage: [
|
|
163
167
|
"Usage: turno migrate [--dry-run] [--force]",
|
|
164
168
|
"",
|
|
@@ -167,10 +171,8 @@ export const command = {
|
|
|
167
171
|
"snapshots, exhausted marks and plans to the state folder. Never changes legacy files.",
|
|
168
172
|
].join("\n"),
|
|
169
173
|
async run(args, io) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
fail(`unknown option: ${a}`);
|
|
173
|
-
return migrate(io, { dryRun: args.includes("--dry-run"), force: args.includes("--force") });
|
|
174
|
+
const p = parseFlags(args, SPEC, 0);
|
|
175
|
+
return migrate(io, { dryRun: bool(p, "dry-run"), force: bool(p, "force") });
|
|
174
176
|
},
|
|
175
177
|
};
|
|
176
178
|
//# sourceMappingURL=migrate.js.map
|