turno-cli 0.0.0-stage → 0.1.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/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +335 -2
- package/dist/accounts/audit.js +54 -0
- package/dist/accounts/builtin.js +214 -0
- package/dist/accounts/delegate.js +185 -0
- package/dist/accounts/registry.js +190 -0
- package/dist/agents/index.js +101 -0
- package/dist/agents/snippets.js +64 -0
- package/dist/cli/args.js +47 -0
- package/dist/cli/commands/accounts.js +216 -0
- package/dist/cli/commands/auto.js +44 -0
- package/dist/cli/commands/available.js +18 -0
- package/dist/cli/commands/browser.js +175 -0
- package/dist/cli/commands/complete.js +112 -0
- package/dist/cli/commands/completion.js +57 -0
- package/dist/cli/commands/config.js +168 -0
- package/dist/cli/commands/doctor.js +324 -0
- package/dist/cli/commands/exhausted.js +40 -0
- package/dist/cli/commands/handoff.js +118 -0
- package/dist/cli/commands/hook.js +63 -0
- package/dist/cli/commands/identity.js +114 -0
- package/dist/cli/commands/limit.js +102 -0
- package/dist/cli/commands/mcp.js +350 -0
- package/dist/cli/commands/mcpd.js +161 -0
- package/dist/cli/commands/migrate.js +176 -0
- package/dist/cli/commands/open.js +60 -0
- package/dist/cli/commands/profile.js +259 -0
- package/dist/cli/commands/resolve.js +23 -0
- package/dist/cli/commands/run.js +15 -0
- package/dist/cli/commands/secret.js +123 -0
- package/dist/cli/commands/session.js +36 -0
- package/dist/cli/commands/sessions.js +27 -0
- package/dist/cli/commands/setup.js +224 -0
- package/dist/cli/commands/status.js +101 -0
- package/dist/cli/commands/statusline.js +275 -0
- package/dist/cli/commands/ui.js +276 -0
- package/dist/cli/commands/undo.js +67 -0
- package/dist/cli/commands/update-check.js +22 -0
- package/dist/cli/commands/upgrade.js +162 -0
- package/dist/cli/commands/usage.js +167 -0
- package/dist/cli/commands/version.js +27 -0
- package/dist/cli/commands/which.js +28 -0
- package/dist/cli/group.js +50 -0
- package/dist/cli/io.js +22 -0
- package/dist/cli/launch.js +98 -0
- package/dist/cli/launchers.js +200 -0
- package/dist/cli/main.js +94 -0
- package/dist/cli/registry.js +77 -0
- package/dist/core/config-edit.js +42 -0
- package/dist/core/config.js +137 -0
- package/dist/core/exhausted.js +30 -0
- package/dist/core/journal.js +185 -0
- package/dist/core/manifest.js +177 -0
- package/dist/core/paths.js +54 -0
- package/dist/core/profiles.js +61 -0
- package/dist/core/ratelimit.js +39 -0
- package/dist/core/resolve.js +78 -0
- package/dist/core/secrets.js +44 -0
- package/dist/core/sessions.js +173 -0
- package/dist/core/toml-edit.js +152 -0
- package/dist/core/update.js +102 -0
- package/dist/core/version.js +109 -0
- package/dist/mcpd/cache.js +41 -0
- package/dist/mcpd/connectors.js +63 -0
- package/dist/mcpd/daemon.js +163 -0
- package/dist/mcpd/gateway.js +308 -0
- package/dist/mcpd/importer.js +160 -0
- package/dist/mcpd/instances.js +303 -0
- package/dist/mcpd/jsonrpc.js +19 -0
- package/dist/mcpd/launchd.js +86 -0
- package/dist/mcpd/peerpid.js +25 -0
- package/dist/mcpd/sync.js +208 -0
- package/dist/mcpd/upstream.js +232 -0
- package/dist/statusline/cost.js +135 -0
- package/dist/statusline/render.js +246 -0
- package/dist/ui/cmap.js +57 -0
- package/dist/ui/fonts.js +107 -0
- package/dist/ui/settings.js +69 -0
- package/dist/ui/terminal-id.js +30 -0
- package/dist/ui/terminals.js +163 -0
- package/package.json +69 -3
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { adapter, mcpFileFor } from "../../agents/index.js";
|
|
6
|
+
import { gatewaySnippet, isSnippetAgent, SNIPPET_AGENTS } from "../../agents/snippets.js";
|
|
7
|
+
import { loadConfig } from "../../core/config.js";
|
|
8
|
+
import { loadManifest, parseManifest } from "../../core/manifest.js";
|
|
9
|
+
import { Journal } from "../../core/journal.js";
|
|
10
|
+
import { paths, profileDir, tildify } from "../../core/paths.js";
|
|
11
|
+
import { agentOf, listProfiles } from "../../core/profiles.js";
|
|
12
|
+
import { fail, paint } from "../io.js";
|
|
13
|
+
const run = promisify(execFile);
|
|
14
|
+
const USAGE = `Usage:
|
|
15
|
+
turno mcp list [--json] servers in mcp.toml and where they run
|
|
16
|
+
turno mcp sync [profile...] [--dry-run] [--force]
|
|
17
|
+
write the gateway entries into each profile config
|
|
18
|
+
(Claude Code, Codex, Copilot and Cursor profiles)
|
|
19
|
+
turno mcp config --agent <agent> [--profile <p>]
|
|
20
|
+
print the gateway entries for ${SNIPPET_AGENTS.join(", ")}
|
|
21
|
+
turno mcp import [--write] [--force] [--move-secrets]
|
|
22
|
+
draft mcp.toml from the current ~/.claude.json servers
|
|
23
|
+
turno mcp install [server...] install declared packages (no npx at every start)
|
|
24
|
+
turno mcp refresh [server...] forget cached tool lists (relearned on next use)
|
|
25
|
+
turno mcp doctor find servers that bypass the gateway
|
|
26
|
+
turno mcp daemon <start|stop|restart|status|logs|run>
|
|
27
|
+
the gateway daemon (turno mcpd is the same)`;
|
|
28
|
+
async function list(io, json = false) {
|
|
29
|
+
const m = loadManifest(io.env);
|
|
30
|
+
const names = Object.keys(m.servers).sort();
|
|
31
|
+
if (json) {
|
|
32
|
+
io.out(JSON.stringify(names.map((n) => {
|
|
33
|
+
const s = m.servers[n];
|
|
34
|
+
return s.kind === "remote"
|
|
35
|
+
? { name: n, kind: "remote", url: s.url, proxy: Boolean(s.proxy), profiles: s.profiles }
|
|
36
|
+
: { name: n, kind: "stdio", command: s.builtin ? `builtin ${s.builtin}` : s.command, scope: s.scope, exclusive: Boolean(s.exclusive), idleSeconds: s.idle, profiles: s.profiles, package: s.package ?? null, variants: Object.keys(s.variants) };
|
|
37
|
+
}), null, 2));
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
40
|
+
if (!names.length) {
|
|
41
|
+
io.out(`no servers in ${tildify(paths(io.env).mcpFile, io.env)} (turno mcp import)`);
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
for (const name of names) {
|
|
45
|
+
const s = m.servers[name];
|
|
46
|
+
const where = s.profiles.includes("*") ? "all profiles" : s.profiles.join(", ");
|
|
47
|
+
if (s.kind === "remote") {
|
|
48
|
+
io.out(`${paint(io, name, "1")} remote ${s.proxy ? "via mcpd" : "direct"} ${s.url} (${where})`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const variants = Object.keys(s.variants);
|
|
52
|
+
const installed = s.package && existsSync(join(paths(io.env).serversDir, s.name, "node_modules"));
|
|
53
|
+
io.out(`${paint(io, name, "1")} ${s.builtin ? `builtin ${s.builtin}` : s.command} scope ${s.scope}${s.exclusive ? " exclusive" : ""} idle ${Math.round(s.idle / 60)}m (${where})` +
|
|
54
|
+
(variants.length ? ` variants: ${variants.join(", ")}` : "") +
|
|
55
|
+
(s.package ? ` package ${s.package}${installed ? "" : " (not installed)"}` : ""));
|
|
56
|
+
}
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
function targets(io, only) {
|
|
60
|
+
const p = paths(io.env);
|
|
61
|
+
const config = loadConfig(io.env);
|
|
62
|
+
const out = [];
|
|
63
|
+
let cursorDone = false;
|
|
64
|
+
for (const name of listProfiles(config, io.env)) {
|
|
65
|
+
if (only.length && !only.includes(name))
|
|
66
|
+
continue;
|
|
67
|
+
const agent = agentOf(name, config);
|
|
68
|
+
if (adapter(agent).sharedMcpFile) {
|
|
69
|
+
// One file for every Cursor profile: entries route by the launcher's environment.
|
|
70
|
+
if (cursorDone)
|
|
71
|
+
continue;
|
|
72
|
+
cursorDone = true;
|
|
73
|
+
out.push({ profile: "", file: mcpFileFor(name, agent, io.env), agent });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
out.push({ profile: name, file: agent === "claude" ? join(profileDir(name, io.env), ".claude.json") : mcpFileFor(name, agent, io.env), agent });
|
|
77
|
+
}
|
|
78
|
+
// The hub config belongs to the system profile (plain `claude`).
|
|
79
|
+
const hubProfile = config.system ?? "default";
|
|
80
|
+
if ((!only.length || only.includes(hubProfile) || only.includes("hub")) && existsSync(p.hubConfig)) {
|
|
81
|
+
out.push({ profile: hubProfile, file: p.hubConfig, agent: "claude" });
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
async function busySet(io) {
|
|
86
|
+
try {
|
|
87
|
+
const mod = (await import("../../core/sessions.js"));
|
|
88
|
+
const res = mod.busyProfiles?.(loadConfig(io.env), io.env) ?? [];
|
|
89
|
+
return new Set(res);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return new Set();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function sync(args, io) {
|
|
96
|
+
const dryRun = args.includes("--dry-run");
|
|
97
|
+
const force = args.includes("--force");
|
|
98
|
+
const only = args.filter((a) => !a.startsWith("--"));
|
|
99
|
+
const { profileServers, readManaged, syncCodexToml, syncConfigFile, syncJsonServers, writeManaged } = await import("../../mcpd/sync.js");
|
|
100
|
+
const { ensureToken, mcpdPort } = await import("../../mcpd/daemon.js");
|
|
101
|
+
const manifest = loadManifest(io.env);
|
|
102
|
+
const token = ensureToken(io.env);
|
|
103
|
+
const port = mcpdPort(io.env);
|
|
104
|
+
const p = paths(io.env);
|
|
105
|
+
const managed = readManaged(p.mcpdManaged);
|
|
106
|
+
const busy = await busySet(io);
|
|
107
|
+
let skipped = 0;
|
|
108
|
+
const journal = new Journal(io.env, `mcp sync ${args.join(" ")}`.trim(), dryRun);
|
|
109
|
+
for (const t of targets(io, only)) {
|
|
110
|
+
const label = `${t.profile || `${adapter(t.agent).label} profiles`} (${tildify(t.file, io.env)})`;
|
|
111
|
+
if (t.profile && busy.has(t.profile) && !force && !dryRun) {
|
|
112
|
+
io.out(`${paint(io, "skip", "33")} ${label}: has open sessions; close them or use --force`);
|
|
113
|
+
skipped++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const wanted = profileServers(manifest, t.profile, port, token, t.agent);
|
|
117
|
+
const before = journal.save(t.file);
|
|
118
|
+
const prev = managed[t.file] ?? [];
|
|
119
|
+
const res = t.agent === "codex"
|
|
120
|
+
? syncCodexToml(t.file, wanted, prev, { dryRun })
|
|
121
|
+
: t.agent === "claude"
|
|
122
|
+
? syncConfigFile(t.file, wanted, prev, { dryRun })
|
|
123
|
+
: syncJsonServers(t.file, "mcpServers", wanted, prev, { dryRun });
|
|
124
|
+
journal.settle(before);
|
|
125
|
+
if (!dryRun)
|
|
126
|
+
managed[t.file] = Object.keys(wanted);
|
|
127
|
+
const parts = [
|
|
128
|
+
res.added.length ? `added ${res.added.join(", ")}` : "",
|
|
129
|
+
res.updated.length ? `updated ${res.updated.join(", ")}` : "",
|
|
130
|
+
res.removed.length ? `removed ${res.removed.join(", ")}` : "",
|
|
131
|
+
res.foreign.length ? `left alone ${res.foreign.join(", ")}` : "",
|
|
132
|
+
].filter(Boolean);
|
|
133
|
+
io.out(`${paint(io, dryRun ? "plan" : res.changed ? "sync" : "same", res.changed ? "32" : "2")} ${label}${parts.length ? `: ${parts.join("; ")}` : ""}`);
|
|
134
|
+
if (t.agent === "cursor" && res.added.length && !dryRun) {
|
|
135
|
+
io.out(` Cursor asks to approve new servers once: cursor-agent mcp enable <name> (${res.added.join(", ")})`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!dryRun)
|
|
139
|
+
writeManaged(p.mcpdManaged, managed);
|
|
140
|
+
if (!dryRun)
|
|
141
|
+
io.out("New sessions pick this up; open sessions keep their old servers until restarted.");
|
|
142
|
+
const hint = journal.hint();
|
|
143
|
+
if (hint)
|
|
144
|
+
io.out(hint);
|
|
145
|
+
return skipped ? 1 : 0;
|
|
146
|
+
}
|
|
147
|
+
async function config(args, io) {
|
|
148
|
+
const { parseFlags, str } = await import("../args.js");
|
|
149
|
+
const p = parseFlags(args, { agent: { type: "string" }, profile: { type: "string" } }, 0);
|
|
150
|
+
const agent = str(p, "agent");
|
|
151
|
+
if (!agent || !isSnippetAgent(agent)) {
|
|
152
|
+
return fail(`usage: turno mcp config --agent <${SNIPPET_AGENTS.join("|")}> [--profile <p>]\n(Claude Code, Codex, Copilot and Cursor profiles get their entries from turno mcp sync)`);
|
|
153
|
+
}
|
|
154
|
+
const cfg = loadConfig(io.env);
|
|
155
|
+
const profile = str(p, "profile") ?? cfg.default ?? "default";
|
|
156
|
+
const manifest = loadManifest(io.env);
|
|
157
|
+
const { servesProfile } = await import("../../core/manifest.js");
|
|
158
|
+
const servers = Object.entries(manifest.servers)
|
|
159
|
+
.filter(([, def]) => servesProfile(def, profile) && !(def.kind === "remote" && !def.proxy))
|
|
160
|
+
.map(([name]) => name);
|
|
161
|
+
if (!servers.length)
|
|
162
|
+
return fail(`no gateway servers for profile ${profile} in mcp.toml`);
|
|
163
|
+
const { mcpdPort } = await import("../../mcpd/daemon.js");
|
|
164
|
+
const s = gatewaySnippet(agent, servers, mcpdPort(io.env), profile);
|
|
165
|
+
io.out(`# ${s.file}`);
|
|
166
|
+
for (const n of s.notes)
|
|
167
|
+
io.out(`# ${n}`);
|
|
168
|
+
io.out(s.text.trimEnd());
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
/** The bin npx would run: the only one, else the one named after the package. */
|
|
172
|
+
export function pickBin(pkg, bin) {
|
|
173
|
+
const base = pkg.replace(/^@[^/]+\//, "");
|
|
174
|
+
if (typeof bin === "string")
|
|
175
|
+
return base;
|
|
176
|
+
const names = Object.keys(bin);
|
|
177
|
+
if (names.length === 1)
|
|
178
|
+
return names[0];
|
|
179
|
+
return names.find((n) => n === base) ?? names.find((n) => n.includes("mcp"));
|
|
180
|
+
}
|
|
181
|
+
async function binOf(pkg) {
|
|
182
|
+
try {
|
|
183
|
+
const { stdout } = await run("npm", ["view", pkg, "bin", "--json"], { timeout: 20_000 });
|
|
184
|
+
const bin = JSON.parse(stdout || "null");
|
|
185
|
+
if (!bin)
|
|
186
|
+
return undefined;
|
|
187
|
+
return pickBin(pkg, bin);
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async function importCmd(args, io) {
|
|
194
|
+
const { importServers } = await import("../../mcpd/importer.js");
|
|
195
|
+
const { storeKeychainSecret } = await import("../../core/secrets.js");
|
|
196
|
+
const p = paths(io.env);
|
|
197
|
+
let hub = {};
|
|
198
|
+
try {
|
|
199
|
+
hub = JSON.parse(readFileSync(p.hubConfig, "utf8"));
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
return fail(`cannot read ${tildify(p.hubConfig, io.env)}`);
|
|
203
|
+
}
|
|
204
|
+
const servers = hub.mcpServers ?? {};
|
|
205
|
+
const report = await importServers(servers, {
|
|
206
|
+
moveSecrets: args.includes("--move-secrets"),
|
|
207
|
+
binOf,
|
|
208
|
+
store: (service, value) => storeKeychainSecret(service, value),
|
|
209
|
+
});
|
|
210
|
+
parseManifest(report.toml); // never write something we cannot read back
|
|
211
|
+
for (const n of report.notes)
|
|
212
|
+
io.err(` ${n}`);
|
|
213
|
+
for (const s of report.secretsMoved)
|
|
214
|
+
io.err(` moved ${s}`);
|
|
215
|
+
if (report.literalSecrets.length) {
|
|
216
|
+
io.err(` ${paint(io, "warning", "33")}: literal secrets kept (${report.literalSecrets.join(", ")}); rerun with --move-secrets to store them in the Keychain`);
|
|
217
|
+
}
|
|
218
|
+
if (!args.includes("--write")) {
|
|
219
|
+
io.out(report.toml);
|
|
220
|
+
return 0;
|
|
221
|
+
}
|
|
222
|
+
if (existsSync(p.mcpFile) && !args.includes("--force"))
|
|
223
|
+
return fail(`${tildify(p.mcpFile, io.env)} exists; use --force (it is backed up)`);
|
|
224
|
+
const journal = new Journal(io.env, "mcp import --write");
|
|
225
|
+
journal.write(p.mcpFile, report.toml, 0o600);
|
|
226
|
+
io.out(`wrote ${tildify(p.mcpFile, io.env)}`);
|
|
227
|
+
io.out(journal.hint());
|
|
228
|
+
return 0;
|
|
229
|
+
}
|
|
230
|
+
async function install(args, io) {
|
|
231
|
+
const m = loadManifest(io.env);
|
|
232
|
+
const p = paths(io.env);
|
|
233
|
+
const wanted = Object.values(m.servers).filter((s) => s.kind === "stdio" && Boolean(s.package) && (!args.length || args.includes(s.name)));
|
|
234
|
+
if (!wanted.length) {
|
|
235
|
+
io.out("nothing to install (no server declares a package)");
|
|
236
|
+
return 0;
|
|
237
|
+
}
|
|
238
|
+
let failed = 0;
|
|
239
|
+
for (const s of wanted) {
|
|
240
|
+
const dir = join(p.serversDir, s.name);
|
|
241
|
+
mkdirSync(dir, { recursive: true });
|
|
242
|
+
if (!existsSync(join(dir, "package.json")))
|
|
243
|
+
writeFileSync(join(dir, "package.json"), `{ "private": true }\n`);
|
|
244
|
+
io.out(`installing ${s.package} for ${s.name}...`);
|
|
245
|
+
try {
|
|
246
|
+
await run("npm", ["install", "--no-audit", "--no-fund", "--prefix", dir, s.package], { timeout: 300_000 });
|
|
247
|
+
const bin = join(dir, "node_modules", ".bin", s.command);
|
|
248
|
+
io.out(existsSync(bin) ? ` ok: ${tildify(bin, io.env)}` : ` ${paint(io, "warning", "33")}: installed but no bin named ${s.command}`);
|
|
249
|
+
}
|
|
250
|
+
catch (err) {
|
|
251
|
+
failed++;
|
|
252
|
+
io.err(` failed: ${err.message.split("\n")[0]}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return failed ? 1 : 0;
|
|
256
|
+
}
|
|
257
|
+
async function refresh(args, io) {
|
|
258
|
+
const dir = paths(io.env).mcpdCacheDir;
|
|
259
|
+
if (!existsSync(dir)) {
|
|
260
|
+
io.out("cache is empty");
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
const { readdirSync } = await import("node:fs");
|
|
264
|
+
let n = 0;
|
|
265
|
+
for (const f of readdirSync(dir)) {
|
|
266
|
+
if (!args.length || args.some((a) => f.startsWith(`${a}@`))) {
|
|
267
|
+
rmSync(join(dir, f), { force: true });
|
|
268
|
+
n++;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
io.out(`forgot ${n} cached descriptions; restart mcpd (turno mcpd restart) so it relearns them`);
|
|
272
|
+
return 0;
|
|
273
|
+
}
|
|
274
|
+
/** Local servers that do not go through mcpd. */
|
|
275
|
+
export async function bypassingServers(env) {
|
|
276
|
+
const { pluginServers } = await import("../../mcpd/importer.js");
|
|
277
|
+
const p = paths(env);
|
|
278
|
+
const manifest = loadManifest(env);
|
|
279
|
+
const plugins = pluginServers(p.hub)
|
|
280
|
+
.filter((s) => s.transport === "stdio")
|
|
281
|
+
.map((s) => ({ plugin: s.plugin, name: s.name, command: String(s.command), toolPrefix: s.toolPrefix }));
|
|
282
|
+
let hub = {};
|
|
283
|
+
try {
|
|
284
|
+
hub = JSON.parse(readFileSync(p.hubConfig, "utf8"));
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
/* no hub config */
|
|
288
|
+
}
|
|
289
|
+
const direct = Object.entries(hub.mcpServers ?? {})
|
|
290
|
+
.filter(([, e]) => typeof e.command === "string")
|
|
291
|
+
.map(([name]) => ({ name, inManifest: Boolean(manifest.servers[name]) }));
|
|
292
|
+
return { plugins, direct };
|
|
293
|
+
}
|
|
294
|
+
async function doctor(io) {
|
|
295
|
+
const { plugins, direct } = await bypassingServers(io.env);
|
|
296
|
+
if (plugins.length) {
|
|
297
|
+
io.out(paint(io, "Plugin servers that start in every session (they bypass mcpd):", "1"));
|
|
298
|
+
for (const s of plugins)
|
|
299
|
+
io.out(` ${s.plugin}/${s.name}: ${s.command} (tools ${s.toolPrefix}*)`);
|
|
300
|
+
io.out(" Disable the plugin, or keep it if you need it everywhere. To keep its tools through mcpd,");
|
|
301
|
+
io.out(` declare [server.plugin_<plugin>_<name>] in mcp.toml so tool names stay the same.`);
|
|
302
|
+
}
|
|
303
|
+
if (direct.length) {
|
|
304
|
+
io.out(paint(io, "Stdio servers still configured directly in the hub config:", "1"));
|
|
305
|
+
for (const d of direct)
|
|
306
|
+
io.out(` ${d.name}${d.inManifest ? " (in mcp.toml: run turno mcp sync)" : " (not in mcp.toml: turno mcp import)"}`);
|
|
307
|
+
}
|
|
308
|
+
if (!plugins.length && !direct.length)
|
|
309
|
+
io.out("Every local server goes through mcpd.");
|
|
310
|
+
return plugins.length + direct.length ? 1 : 0;
|
|
311
|
+
}
|
|
312
|
+
export const command = {
|
|
313
|
+
summary: "MCP servers behind the lazy gateway, and the gateway daemon itself",
|
|
314
|
+
usage: USAGE,
|
|
315
|
+
subcommands: {
|
|
316
|
+
list: { flags: ["--json"] },
|
|
317
|
+
sync: { args: ["profile"], flags: ["--dry-run", "--force"] },
|
|
318
|
+
config: { flags: ["--agent", "--profile"] },
|
|
319
|
+
import: { flags: ["--write", "--force", "--move-secrets"] },
|
|
320
|
+
install: { args: ["server"] },
|
|
321
|
+
refresh: { args: ["server"] },
|
|
322
|
+
doctor: {},
|
|
323
|
+
daemon: { args: ["any"] },
|
|
324
|
+
},
|
|
325
|
+
async run(args, io) {
|
|
326
|
+
const [sub, ...rest] = args;
|
|
327
|
+
switch (sub) {
|
|
328
|
+
case "list":
|
|
329
|
+
case undefined:
|
|
330
|
+
return list(io, rest.includes("--json"));
|
|
331
|
+
case "sync":
|
|
332
|
+
return sync(rest, io);
|
|
333
|
+
case "config":
|
|
334
|
+
return config(rest, io);
|
|
335
|
+
case "import":
|
|
336
|
+
return importCmd(rest, io);
|
|
337
|
+
case "install":
|
|
338
|
+
return install(rest, io);
|
|
339
|
+
case "refresh":
|
|
340
|
+
return refresh(rest, io);
|
|
341
|
+
case "daemon":
|
|
342
|
+
return (await import("./mcpd.js")).command.run(rest, io);
|
|
343
|
+
case "doctor":
|
|
344
|
+
return doctor(io);
|
|
345
|
+
default:
|
|
346
|
+
return fail(`unknown mcp command "${sub}"\n${USAGE}`);
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
//# sourceMappingURL=mcp.js.map
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
2
|
+
import { realpathSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { paths } from "../../core/paths.js";
|
|
6
|
+
import { fail, paint } from "../io.js";
|
|
7
|
+
const USAGE = `Usage:
|
|
8
|
+
turno mcpd run [--port N] run the gateway in the foreground
|
|
9
|
+
turno mcpd start install and start the LaunchAgent (keeps mcpd alive)
|
|
10
|
+
turno mcpd stop stop and unload the LaunchAgent
|
|
11
|
+
turno mcpd restart restart the LaunchAgent
|
|
12
|
+
turno mcpd status [--json] running servers, their clients and memory
|
|
13
|
+
turno mcpd logs [-n N] last lines of the mcpd log`;
|
|
14
|
+
/** GET /status from the running daemon. */
|
|
15
|
+
export async function fetchStatus(env) {
|
|
16
|
+
const { mcpdPort, readToken } = await import("../../mcpd/daemon.js");
|
|
17
|
+
const token = readToken(env);
|
|
18
|
+
if (!token)
|
|
19
|
+
return undefined;
|
|
20
|
+
try {
|
|
21
|
+
const res = await fetch(`http://127.0.0.1:${mcpdPort(env)}/status`, {
|
|
22
|
+
headers: { authorization: `Bearer ${token}` },
|
|
23
|
+
signal: AbortSignal.timeout(2000),
|
|
24
|
+
});
|
|
25
|
+
return res.ok ? (await res.json()) : undefined;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function rssOf(pids) {
|
|
32
|
+
const out = new Map();
|
|
33
|
+
if (!pids.length)
|
|
34
|
+
return out;
|
|
35
|
+
const { execFile } = await import("node:child_process");
|
|
36
|
+
const { promisify } = await import("node:util");
|
|
37
|
+
try {
|
|
38
|
+
const { stdout } = await promisify(execFile)("ps", ["-o", "pid=,rss=", "-p", pids.join(",")]);
|
|
39
|
+
for (const line of stdout.trim().split("\n")) {
|
|
40
|
+
const [pid, rss] = line.trim().split(/\s+/).map(Number);
|
|
41
|
+
if (pid && rss)
|
|
42
|
+
out.set(pid, rss * 1024);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
/* processes gone */
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
function mb(bytes) {
|
|
51
|
+
return `${Math.round(bytes / 1048576)} MB`;
|
|
52
|
+
}
|
|
53
|
+
async function status(args, io) {
|
|
54
|
+
const s = await fetchStatus(io.env);
|
|
55
|
+
if (!s) {
|
|
56
|
+
io.out("mcpd is not running (turno mcpd start)");
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
if (args.includes("--json")) {
|
|
60
|
+
io.out(JSON.stringify(s, null, 2));
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
const instances = s.instances ?? [];
|
|
64
|
+
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} client sessions`);
|
|
66
|
+
if (!instances.length) {
|
|
67
|
+
io.out(" no servers running (they start on first use)");
|
|
68
|
+
return 0;
|
|
69
|
+
}
|
|
70
|
+
let total = rss.get(Number(s.pid)) ?? 0;
|
|
71
|
+
for (const i of instances) {
|
|
72
|
+
const mem = rss.get(Number(i.pid)) ?? 0;
|
|
73
|
+
total += mem;
|
|
74
|
+
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} clients idle ${i.idleSeconds}s${holder ? ` held by ${holder.profile ?? "?"}/${holder.pid ?? "?"}` : ""}`);
|
|
76
|
+
}
|
|
77
|
+
io.out(` total ${mb(total)}`);
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
function entryScript() {
|
|
81
|
+
return realpathSync(join(fileURLToPath(new URL(".", import.meta.url)), "..", "main.js"));
|
|
82
|
+
}
|
|
83
|
+
async function logs(args, io) {
|
|
84
|
+
const n = Number(args[args.indexOf("-n") + 1]) || 40;
|
|
85
|
+
const file = join(paths(io.env).logsDir, "mcpd.log");
|
|
86
|
+
if (!existsSync(file)) {
|
|
87
|
+
io.out("no log yet");
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
const size = statSync(file).size;
|
|
91
|
+
const start = Math.max(0, size - 256 * 1024);
|
|
92
|
+
const chunks = [];
|
|
93
|
+
for await (const c of createReadStream(file, { start }))
|
|
94
|
+
chunks.push(c);
|
|
95
|
+
const lines = Buffer.concat(chunks).toString("utf8").trimEnd().split("\n");
|
|
96
|
+
for (const l of lines.slice(-n))
|
|
97
|
+
io.out(l);
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
export const command = {
|
|
101
|
+
summary: "run or manage the MCP gateway daemon",
|
|
102
|
+
usage: USAGE,
|
|
103
|
+
async run(args, io) {
|
|
104
|
+
const [sub, ...rest] = args;
|
|
105
|
+
switch (sub) {
|
|
106
|
+
case "run": {
|
|
107
|
+
const { startDaemon } = await import("../../mcpd/daemon.js");
|
|
108
|
+
const portArg = rest.indexOf("--port");
|
|
109
|
+
const d = await startDaemon({
|
|
110
|
+
env: io.env,
|
|
111
|
+
logToStdout: process.stdout.isTTY,
|
|
112
|
+
...(portArg >= 0 ? { port: Number(rest[portArg + 1]) } : {}),
|
|
113
|
+
});
|
|
114
|
+
io.err(`mcpd listening on 127.0.0.1:${d.port}`);
|
|
115
|
+
await new Promise((resolve) => {
|
|
116
|
+
const stop = () => {
|
|
117
|
+
void d.stop().then(resolve);
|
|
118
|
+
};
|
|
119
|
+
process.once("SIGINT", stop);
|
|
120
|
+
process.once("SIGTERM", stop);
|
|
121
|
+
});
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
case "start": {
|
|
125
|
+
const { install } = await import("../../mcpd/launchd.js");
|
|
126
|
+
const { ensureToken } = await import("../../mcpd/daemon.js");
|
|
127
|
+
ensureToken(io.env);
|
|
128
|
+
const file = await install({ node: process.execPath, script: entryScript(), env: io.env });
|
|
129
|
+
io.out(`LaunchAgent installed: ${file}`);
|
|
130
|
+
for (let i = 0; i < 20; i++) {
|
|
131
|
+
if (await fetchStatus(io.env)) {
|
|
132
|
+
io.out("mcpd is running");
|
|
133
|
+
return 0;
|
|
134
|
+
}
|
|
135
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
136
|
+
}
|
|
137
|
+
io.err("mcpd did not answer yet; see turno mcpd logs");
|
|
138
|
+
return 1;
|
|
139
|
+
}
|
|
140
|
+
case "stop": {
|
|
141
|
+
const { uninstall } = await import("../../mcpd/launchd.js");
|
|
142
|
+
await uninstall();
|
|
143
|
+
io.out("mcpd stopped");
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
case "restart": {
|
|
147
|
+
const { restart } = await import("../../mcpd/launchd.js");
|
|
148
|
+
await restart();
|
|
149
|
+
io.out("mcpd restarted");
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
case "status":
|
|
153
|
+
return status(rest, io);
|
|
154
|
+
case "logs":
|
|
155
|
+
return logs(rest, io);
|
|
156
|
+
default:
|
|
157
|
+
return fail(`unknown mcpd command "${sub ?? ""}"\n${USAGE}`);
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
//# sourceMappingURL=mcpd.js.map
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { isValidProfileName, parseConfig } from "../../core/config.js";
|
|
4
|
+
import { paths, tildify } from "../../core/paths.js";
|
|
5
|
+
import { fail } from "../io.js";
|
|
6
|
+
import { detectNerdTerminals } from "../../ui/terminals.js";
|
|
7
|
+
import { Journal } from "../../core/journal.js";
|
|
8
|
+
function lines(text) {
|
|
9
|
+
return text
|
|
10
|
+
.split("\n")
|
|
11
|
+
.map((raw) => raw.split("#", 1)[0].trim())
|
|
12
|
+
.filter(Boolean);
|
|
13
|
+
}
|
|
14
|
+
export function parseLegacy(proyectos, navegador) {
|
|
15
|
+
const out = { projects: [], browsers: {}, warnings: [] };
|
|
16
|
+
for (const line of lines(proyectos)) {
|
|
17
|
+
const space = line.search(/\s/);
|
|
18
|
+
const key = space < 0 ? line : line.slice(0, space);
|
|
19
|
+
const val = space < 0 ? "" : line.slice(space).trim();
|
|
20
|
+
if (key === "respaldo")
|
|
21
|
+
out.fallback = val;
|
|
22
|
+
else if (key === "por-defecto")
|
|
23
|
+
out.default = val;
|
|
24
|
+
else if (key === "sistema")
|
|
25
|
+
out.system = val;
|
|
26
|
+
else if (key === "iconos")
|
|
27
|
+
out.icons = val;
|
|
28
|
+
else if (!isValidProfileName(key) || !val)
|
|
29
|
+
out.warnings.push(`skipped line in proyectos: ${line}`);
|
|
30
|
+
else
|
|
31
|
+
out.projects.push({ profile: key, path: val });
|
|
32
|
+
}
|
|
33
|
+
for (const line of lines(navegador)) {
|
|
34
|
+
const [profile, mode] = line.split(/\s+/);
|
|
35
|
+
if (!profile || !isValidProfileName(profile) || (mode !== "predeterminado" && mode !== "chrome")) {
|
|
36
|
+
out.warnings.push(`skipped line in navegador: ${line}`);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
out.browsers[profile] = mode === "predeterminado" ? "default" : "chrome";
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
const q = (s) => JSON.stringify(s);
|
|
44
|
+
/** config.toml text for a legacy config. */
|
|
45
|
+
export function legacyToToml(legacy, chromeDir, nerdTerminals = []) {
|
|
46
|
+
const out = ["# Imported from the bash claudio by `turno migrate`."];
|
|
47
|
+
for (const key of ["system", "default", "fallback"]) {
|
|
48
|
+
const v = legacy[key];
|
|
49
|
+
if (v && isValidProfileName(v))
|
|
50
|
+
out.push(`${key} = ${q(v)}`);
|
|
51
|
+
else if (v)
|
|
52
|
+
legacy.warnings.push(`skipped ${key}: invalid profile name '${v}'`);
|
|
53
|
+
}
|
|
54
|
+
if (legacy.icons) {
|
|
55
|
+
if (["auto", "nerd", "unicode"].includes(legacy.icons))
|
|
56
|
+
out.push(`icons = ${q(legacy.icons)}`);
|
|
57
|
+
else
|
|
58
|
+
legacy.warnings.push(`skipped iconos '${legacy.icons}' (expected auto, nerd or unicode)`);
|
|
59
|
+
}
|
|
60
|
+
if (nerdTerminals.length)
|
|
61
|
+
out.push(`nerd_terminals = [${nerdTerminals.map(q).join(", ")}]`);
|
|
62
|
+
if (chromeDir)
|
|
63
|
+
out.push("", "[open]", `chrome_dir = ${q(chromeDir)}`);
|
|
64
|
+
for (const [name, browser] of Object.entries(legacy.browsers).sort()) {
|
|
65
|
+
out.push("", `[profiles.${name}]`, `browser = ${q(browser)}`);
|
|
66
|
+
}
|
|
67
|
+
for (const rule of legacy.projects) {
|
|
68
|
+
out.push("", "[[projects]]", `path = ${q(rule.path)}`, `profile = ${q(rule.profile)}`);
|
|
69
|
+
}
|
|
70
|
+
return `${out.join("\n")}\n`;
|
|
71
|
+
}
|
|
72
|
+
function read(file) {
|
|
73
|
+
try {
|
|
74
|
+
return readFileSync(file, "utf8");
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Copy files from a legacy state folder when missing or older at the destination. */
|
|
81
|
+
function copyState(from, to, filter, journal) {
|
|
82
|
+
let entries;
|
|
83
|
+
try {
|
|
84
|
+
entries = readdirSync(from);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
let copied = 0;
|
|
90
|
+
for (const name of entries) {
|
|
91
|
+
if (!filter(name))
|
|
92
|
+
continue;
|
|
93
|
+
const src = join(from, name);
|
|
94
|
+
const dst = join(to, name);
|
|
95
|
+
try {
|
|
96
|
+
if (!statSync(src).isFile())
|
|
97
|
+
continue;
|
|
98
|
+
if (existsSync(dst) && statSync(dst).mtimeMs >= statSync(src).mtimeMs)
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
journal.write(dst, readFileSync(src));
|
|
105
|
+
copied++;
|
|
106
|
+
}
|
|
107
|
+
return copied;
|
|
108
|
+
}
|
|
109
|
+
export function migrate(io, opts = {}) {
|
|
110
|
+
const p = paths(io.env);
|
|
111
|
+
const legacyDir = p.legacyConfig;
|
|
112
|
+
const prefix = opts.dryRun ? "(dry run) " : "";
|
|
113
|
+
const journal = new Journal(io.env, "migrate", Boolean(opts.dryRun));
|
|
114
|
+
if (!existsSync(legacyDir)) {
|
|
115
|
+
io.out(`Nothing to migrate: ${tildify(legacyDir, io.env)} does not exist.`);
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
const proyectos = read(join(legacyDir, "proyectos"));
|
|
119
|
+
const navegador = read(join(legacyDir, "navegador"));
|
|
120
|
+
if (proyectos === undefined && navegador === undefined) {
|
|
121
|
+
io.out(`No legacy proyectos or navegador in ${tildify(legacyDir, io.env)}; config.toml left as is.`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
const exists = existsSync(p.configFile);
|
|
125
|
+
if (exists && !opts.force) {
|
|
126
|
+
if (opts.dryRun)
|
|
127
|
+
io.out(`${prefix}! ${tildify(p.configFile, io.env)} exists; a real run needs --force to replace it`);
|
|
128
|
+
else
|
|
129
|
+
fail(`${tildify(p.configFile, io.env)} already exists; use --force to replace it (a backup is kept)`);
|
|
130
|
+
}
|
|
131
|
+
const legacy = parseLegacy(proyectos ?? "", navegador ?? "");
|
|
132
|
+
const legacyChrome = join(legacyDir, "chrome");
|
|
133
|
+
const chromeDir = existsSync(legacyChrome) ? tildify(legacyChrome, io.env) : undefined;
|
|
134
|
+
// The old status line drew Nerd icons in Warp; keep that wherever a Nerd Font is set.
|
|
135
|
+
const toml = legacyToToml(legacy, chromeDir, detectNerdTerminals(io.env));
|
|
136
|
+
parseConfig(toml, p.configFile, io.env); // never write a file turno cannot read back
|
|
137
|
+
for (const w of legacy.warnings)
|
|
138
|
+
io.out(` ! ${w}`);
|
|
139
|
+
journal.write(p.configFile, toml);
|
|
140
|
+
if (exists)
|
|
141
|
+
io.out(`${prefix}backed up config.toml (turno undo ${journal.stamp} puts it back)`);
|
|
142
|
+
const nBrowsers = Object.keys(legacy.browsers).length;
|
|
143
|
+
io.out(`${prefix}+ ${tildify(p.configFile, io.env)}: ${legacy.projects.length} project rules, ${nBrowsers} browser settings` +
|
|
144
|
+
(chromeDir ? `, Chrome profiles kept in ${chromeDir}` : ""));
|
|
145
|
+
if (opts.dryRun) {
|
|
146
|
+
io.out("");
|
|
147
|
+
io.out(toml.trimEnd());
|
|
148
|
+
io.out("");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const usage = copyState(join(legacyDir, "uso"), p.usageDir, (n) => n.endsWith(".json"), journal);
|
|
152
|
+
const exhausted = copyState(join(legacyDir, "agotadas"), p.exhaustedDir, isValidProfileName, journal);
|
|
153
|
+
const plan = copyState(join(legacyDir, "plan"), p.planDir, isValidProfileName, journal);
|
|
154
|
+
io.out(`${prefix}+ state: ${usage} usage snapshots, ${exhausted} exhausted marks, ${plan} plans copied to ${tildify(p.state, io.env)}/`);
|
|
155
|
+
io.out("Legacy files were left untouched.");
|
|
156
|
+
if (journal.used)
|
|
157
|
+
io.out(`undo with: turno undo ${journal.stamp}`);
|
|
158
|
+
return 0;
|
|
159
|
+
}
|
|
160
|
+
export const command = {
|
|
161
|
+
summary: "import the bash claudio config and state into the new layout",
|
|
162
|
+
usage: [
|
|
163
|
+
"Usage: turno migrate [--dry-run] [--force]",
|
|
164
|
+
"",
|
|
165
|
+
"Reads ~/.config/claudio/proyectos and navegador and writes config.toml (refuses to",
|
|
166
|
+
"replace an existing one unless --force, which backs it up first). Copies usage",
|
|
167
|
+
"snapshots, exhausted marks and plans to the state folder. Never changes legacy files.",
|
|
168
|
+
].join("\n"),
|
|
169
|
+
async run(args, io) {
|
|
170
|
+
for (const a of args)
|
|
171
|
+
if (a !== "--dry-run" && a !== "--force")
|
|
172
|
+
fail(`unknown option: ${a}`);
|
|
173
|
+
return migrate(io, { dryRun: args.includes("--dry-run"), force: args.includes("--force") });
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
//# sourceMappingURL=migrate.js.map
|