turno-cli 0.1.0 → 0.2.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +293 -0
  2. package/README.md +143 -75
  3. package/dist/accounts/audit.js +137 -7
  4. package/dist/accounts/builtin.js +68 -21
  5. package/dist/accounts/delegate.js +176 -35
  6. package/dist/accounts/policy.js +77 -0
  7. package/dist/accounts/registry.js +37 -21
  8. package/dist/agents/adapter.js +7 -0
  9. package/dist/agents/claude.js +195 -0
  10. package/dist/agents/codex.js +98 -0
  11. package/dist/agents/copilot.js +39 -0
  12. package/dist/agents/cursor.js +41 -0
  13. package/dist/agents/index.js +26 -86
  14. package/dist/agents/snippets.js +7 -4
  15. package/dist/cli/args.js +19 -4
  16. package/dist/cli/commands/accounts.js +96 -51
  17. package/dist/cli/commands/auto.js +82 -32
  18. package/dist/cli/commands/browser.js +216 -135
  19. package/dist/cli/commands/complete.js +2 -1
  20. package/dist/cli/commands/completion.js +4 -2
  21. package/dist/cli/commands/config.js +103 -18
  22. package/dist/cli/commands/demo.js +292 -0
  23. package/dist/cli/commands/doctor.js +387 -119
  24. package/dist/cli/commands/handoff.js +33 -22
  25. package/dist/cli/commands/identity.js +101 -50
  26. package/dist/cli/commands/init.js +616 -0
  27. package/dist/cli/commands/limit.js +25 -13
  28. package/dist/cli/commands/mcp-oauth.js +194 -0
  29. package/dist/cli/commands/mcp-observe.js +434 -0
  30. package/dist/cli/commands/mcp.js +653 -185
  31. package/dist/cli/commands/mcpd.js +323 -75
  32. package/dist/cli/commands/migrate.js +6 -4
  33. package/dist/cli/commands/open.js +92 -27
  34. package/dist/cli/commands/profile.js +205 -69
  35. package/dist/cli/commands/resolve.js +6 -2
  36. package/dist/cli/commands/run.js +15 -5
  37. package/dist/cli/commands/secret.js +61 -42
  38. package/dist/cli/commands/session.js +2 -2
  39. package/dist/cli/commands/sessions.js +42 -12
  40. package/dist/cli/commands/setup.js +136 -158
  41. package/dist/cli/commands/status.js +172 -41
  42. package/dist/cli/commands/statusline.js +305 -113
  43. package/dist/cli/commands/ui.js +64 -66
  44. package/dist/cli/commands/undo.js +161 -18
  45. package/dist/cli/commands/uninstall.js +462 -0
  46. package/dist/cli/commands/upgrade.js +75 -19
  47. package/dist/cli/commands/usage.js +117 -44
  48. package/dist/cli/commands/version.js +7 -5
  49. package/dist/cli/commands/which.js +7 -24
  50. package/dist/cli/compile-cache.js +32 -0
  51. package/dist/cli/group.js +10 -10
  52. package/dist/cli/help.js +57 -0
  53. package/dist/cli/highlight.js +236 -0
  54. package/dist/cli/io.js +170 -1
  55. package/dist/cli/launch.js +21 -37
  56. package/dist/cli/launchers.js +145 -94
  57. package/dist/cli/main.js +163 -27
  58. package/dist/cli/picker.js +447 -0
  59. package/dist/cli/registry.js +42 -5
  60. package/dist/cli/security.js +539 -0
  61. package/dist/cli/style.js +281 -0
  62. package/dist/core/config.js +145 -23
  63. package/dist/core/exhausted.js +6 -8
  64. package/dist/core/fsutil.js +47 -0
  65. package/dist/core/journal.js +281 -11
  66. package/dist/core/manifest.js +126 -6
  67. package/dist/core/paths.js +114 -7
  68. package/dist/core/profiles.js +139 -9
  69. package/dist/core/resolve.js +129 -23
  70. package/dist/core/secrets.js +215 -20
  71. package/dist/core/sessions.js +13 -20
  72. package/dist/core/update.js +18 -8
  73. package/dist/core/version.js +6 -0
  74. package/dist/demo/server.js +34 -0
  75. package/dist/mcpd/cache.js +18 -4
  76. package/dist/mcpd/calllog.js +253 -0
  77. package/dist/mcpd/client-names.js +51 -0
  78. package/dist/mcpd/connectors.js +148 -17
  79. package/dist/mcpd/daemon.js +452 -116
  80. package/dist/mcpd/folder-sets.js +39 -0
  81. package/dist/mcpd/gateway.js +441 -47
  82. package/dist/mcpd/health.js +269 -0
  83. package/dist/mcpd/importer.js +356 -36
  84. package/dist/mcpd/instances.js +425 -65
  85. package/dist/mcpd/jsonrpc.js +8 -0
  86. package/dist/mcpd/launchd.js +65 -48
  87. package/dist/mcpd/modern.js +648 -0
  88. package/dist/mcpd/oauth.js +791 -0
  89. package/dist/mcpd/peerpid.js +122 -6
  90. package/dist/mcpd/pressure.js +85 -0
  91. package/dist/mcpd/rss.js +151 -0
  92. package/dist/mcpd/service.js +167 -0
  93. package/dist/mcpd/state.js +183 -0
  94. package/dist/mcpd/sync.js +55 -80
  95. package/dist/mcpd/systemd.js +80 -0
  96. package/dist/mcpd/tokens.js +89 -0
  97. package/dist/mcpd/upstream.js +154 -44
  98. package/dist/statusline/cost.js +154 -44
  99. package/dist/statusline/input.js +66 -0
  100. package/dist/statusline/render.js +9 -3
  101. package/dist/ui/fonts.js +8 -3
  102. package/dist/ui/settings.js +27 -15
  103. package/npm-shrinkwrap.json +1093 -0
  104. package/package.json +9 -9
  105. package/dist/cli/commands/available.js +0 -18
@@ -1,13 +1,21 @@
1
- import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
1
+ import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, writeFileSync } from "node:fs";
2
2
  import { createHash } from "node:crypto";
3
3
  import { basename, dirname, join } from "node:path";
4
- import { paths } from "./paths.js";
4
+ import { PRIVATE_FILE_MODE, ensurePrivateDir, isPrivatePath, paths } from "./paths.js";
5
5
  /** Local "YYYYMMDD-HHMMSS". */
6
6
  export function stamp(d = new Date()) {
7
7
  const p = (n) => String(n).padStart(2, "0");
8
8
  return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
9
9
  }
10
10
  const MANIFEST = "journal.json";
11
+ /** Automatic pruning when a new backup is written: older than 30 days, newest 10 always kept. */
12
+ export const PRUNE_DEFAULT_AGE_MS = 30 * 24 * 60 * 60 * 1000;
13
+ export const PRUNE_DEFAULT_KEEP = 10;
14
+ function writeManifest(dir, m) {
15
+ const file = join(dir, MANIFEST);
16
+ writeFileSync(file, `${JSON.stringify(m, null, 2)}\n`, { mode: PRIVATE_FILE_MODE });
17
+ chmodSync(file, PRIVATE_FILE_MODE);
18
+ }
11
19
  function digest(file) {
12
20
  try {
13
21
  return createHash("sha256").update(readFileSync(file)).digest("hex");
@@ -30,6 +38,23 @@ function real(p) {
30
38
  }
31
39
  }
32
40
  }
41
+ /** `p` with its folder resolved but not `p` itself: the path of a link, not of what it points to. */
42
+ function realParent(p) {
43
+ try {
44
+ return join(realpathSync(dirname(p)), basename(p));
45
+ }
46
+ catch {
47
+ return p;
48
+ }
49
+ }
50
+ function lstatOf(p) {
51
+ try {
52
+ return lstatSync(p);
53
+ }
54
+ catch {
55
+ return undefined;
56
+ }
57
+ }
33
58
  /** Write text atomically, keeping the mode of the file it replaces. */
34
59
  export function atomicWrite(file, data, mode) {
35
60
  mkdirSync(dirname(file), { recursive: true });
@@ -42,15 +67,19 @@ export function atomicWrite(file, data, mode) {
42
67
  export class Journal {
43
68
  env;
44
69
  dryRun;
70
+ protect;
45
71
  stamp;
46
72
  dir;
47
73
  manifest;
48
74
  seen = new Set();
49
75
  constructor(env, command,
50
76
  /** Dry runs record nothing and write nothing. */
51
- dryRun = false) {
77
+ dryRun = false,
78
+ /** Stamps the automatic pruning must not touch (the journal an undo reads from). */
79
+ protect = []) {
52
80
  this.env = env;
53
81
  this.dryRun = dryRun;
82
+ this.protect = protect;
54
83
  const root = paths(env).backupsDir;
55
84
  let s = stamp();
56
85
  for (let n = 2; existsSync(join(root, s)); n++)
@@ -75,8 +104,11 @@ export class Journal {
75
104
  throw new Error(`${target} is not a regular file; turno only journals files`);
76
105
  entry.mode = st.mode & 0o777;
77
106
  entry.backup = join("files", `${this.manifest.entries.length}-${basename(target)}`);
78
- mkdirSync(join(this.dir, "files"), { recursive: true });
79
- copyFileSync(target, join(this.dir, entry.backup));
107
+ ensurePrivateDir(join(this.dir, "files"), this.env);
108
+ // The copy may hold secrets (a config with a literal token): 0600 inside 0700 folders.
109
+ const copy = join(this.dir, entry.backup);
110
+ copyFileSync(target, copy);
111
+ chmodSync(copy, PRIVATE_FILE_MODE);
80
112
  }
81
113
  this.manifest.entries.push(entry);
82
114
  this.flush();
@@ -93,16 +125,27 @@ export class Journal {
93
125
  entry.wrote = digest(target);
94
126
  this.flush();
95
127
  }
128
+ flushed = false;
96
129
  flush() {
97
- mkdirSync(this.dir, { recursive: true });
98
- writeFileSync(join(this.dir, MANIFEST), `${JSON.stringify(this.manifest, null, 2)}\n`);
130
+ ensurePrivateDir(this.dir, this.env);
131
+ writeManifest(this.dir, this.manifest);
132
+ if (!this.flushed) {
133
+ this.flushed = true;
134
+ try {
135
+ pruneJournals(this.env, { exclude: [this.stamp, ...this.protect] });
136
+ }
137
+ catch {
138
+ /* pruning is housekeeping: never let it break the command that writes */
139
+ }
140
+ }
99
141
  }
100
142
  /** Save, then write atomically through symlinks so linked profiles keep linking. */
101
143
  write(file, data, mode) {
102
144
  if (this.dryRun)
103
145
  return;
104
146
  const target = this.save(file);
105
- let keep = mode;
147
+ // turno's own files (config.toml, mcp.toml, accounts.toml) are private whatever mode they had.
148
+ let keep = mode ?? (isPrivatePath(target, this.env) ? PRIVATE_FILE_MODE : undefined);
106
149
  if (keep === undefined) {
107
150
  try {
108
151
  keep = statSync(target).mode & 0o777;
@@ -111,6 +154,8 @@ export class Journal {
111
154
  keep = undefined;
112
155
  }
113
156
  }
157
+ if (isPrivatePath(target, this.env))
158
+ ensurePrivateDir(dirname(target), this.env);
114
159
  atomicWrite(target, data, keep);
115
160
  this.settle(target);
116
161
  }
@@ -121,13 +166,78 @@ export class Journal {
121
166
  rmSync(target, { force: true });
122
167
  this.settle(target);
123
168
  }
169
+ record(entry) {
170
+ this.manifest.entries.push(entry);
171
+ this.flush();
172
+ }
173
+ /** Create a link at `path` to `target`. */
174
+ symlink(target, path) {
175
+ if (this.dryRun)
176
+ return;
177
+ const at = realParent(path);
178
+ symlinkSync(target, at);
179
+ this.record({ kind: "symlink", path: at, target, existed: false });
180
+ }
181
+ /** Remove the link at `path` (never what it points to). */
182
+ unlink(path) {
183
+ if (this.dryRun)
184
+ return;
185
+ const at = realParent(path);
186
+ const target = readlinkSync(at);
187
+ rmSync(at);
188
+ this.record({ kind: "unlink", path: at, target, existed: true });
189
+ }
190
+ /** Rename `from` to `to` (a file, a folder or a link). */
191
+ move(from, to) {
192
+ if (this.dryRun)
193
+ return;
194
+ const src = realParent(from);
195
+ const dst = realParent(to);
196
+ renameSync(src, dst);
197
+ this.record({ kind: "move", path: src, to: dst, existed: true });
198
+ }
199
+ /** Create the folder `path` and any missing parents, recording each one it creates. */
200
+ mkdir(path, mode) {
201
+ if (this.dryRun)
202
+ return;
203
+ const missing = [];
204
+ for (let p = path; !lstatOf(p); p = dirname(p)) {
205
+ missing.unshift(p);
206
+ if (dirname(p) === p)
207
+ break;
208
+ }
209
+ for (const p of missing) {
210
+ mkdirSync(p, mode !== undefined ? { mode } : undefined);
211
+ this.record({ kind: "mkdir", path: realParent(p), existed: false, ...(mode !== undefined ? { mode } : {}) });
212
+ }
213
+ }
214
+ /** Remove the empty folder `path`. */
215
+ rmdir(path) {
216
+ if (this.dryRun)
217
+ return;
218
+ const at = realParent(path);
219
+ const mode = statSync(at).mode & 0o777;
220
+ rmdirSync(at);
221
+ this.record({ kind: "rmdir", path: at, existed: true, mode });
222
+ }
223
+ /** Set the permission bits of `path`, recording the old ones when they change. */
224
+ chmod(path, mode) {
225
+ if (this.dryRun)
226
+ return;
227
+ const at = realParent(path);
228
+ const old = statSync(at).mode & 0o777;
229
+ if (old === mode)
230
+ return;
231
+ chmodSync(at, mode);
232
+ this.record({ kind: "chmod", path: at, existed: true, mode: old, newMode: mode });
233
+ }
124
234
  /** The `turno undo` hint to print after a command that changed something. */
125
235
  hint() {
126
236
  return this.used ? `undo with: turno undo ${this.stamp}` : undefined;
127
237
  }
128
238
  }
129
239
  /** Backup folders, newest first. */
130
- export function listJournals(env = process.env) {
240
+ export function listJournals(env) {
131
241
  const root = paths(env).backupsDir;
132
242
  let names = [];
133
243
  try {
@@ -150,6 +260,102 @@ export function listJournals(env = process.env) {
150
260
  }
151
261
  });
152
262
  }
263
+ function isEmptyDir(p) {
264
+ try {
265
+ return readdirSync(p).length === 0;
266
+ }
267
+ catch {
268
+ return false;
269
+ }
270
+ }
271
+ /** Undo one link, move or folder entry through `journal` (which records the inverse). */
272
+ function undoTreeEntry(e, journal) {
273
+ const st = lstatOf(e.path);
274
+ switch (e.kind) {
275
+ case "symlink": {
276
+ if (!st)
277
+ return undefined;
278
+ if (!st.isSymbolicLink() || readlinkSync(e.path) !== e.target)
279
+ return { path: e.path, action: "skip", reason: "no longer the link turno made; left as it is" };
280
+ journal.unlink(e.path);
281
+ return { path: e.path, action: "unlink" };
282
+ }
283
+ case "unlink": {
284
+ if (st)
285
+ return { path: e.path, action: "skip", reason: `something else is there now; it linked to ${e.target}` };
286
+ journal.symlink(e.target, e.path);
287
+ return { path: e.path, action: "link" };
288
+ }
289
+ case "move": {
290
+ if (st)
291
+ return { path: e.path, action: "skip", reason: `something is there now; the moved copy stays at ${e.to}` };
292
+ if (!lstatOf(e.to))
293
+ return { path: e.path, action: "skip", reason: `${e.to} is gone` };
294
+ journal.move(e.to, e.path);
295
+ return { path: e.path, action: "move", from: e.to };
296
+ }
297
+ case "mkdir": {
298
+ if (!st)
299
+ return undefined;
300
+ if (!st.isDirectory() || !isEmptyDir(e.path))
301
+ return { path: e.path, action: "keep", reason: "not empty (it holds files made since)" };
302
+ journal.rmdir(e.path);
303
+ return { path: e.path, action: "rmdir" };
304
+ }
305
+ case "rmdir": {
306
+ if (st)
307
+ return undefined;
308
+ journal.mkdir(e.path, e.mode);
309
+ return { path: e.path, action: "mkdir" };
310
+ }
311
+ case "chmod": {
312
+ const step = chmodStep(e, st);
313
+ if (step?.action === "chmod")
314
+ journal.chmod(e.path, e.mode);
315
+ return step;
316
+ }
317
+ default:
318
+ return undefined;
319
+ }
320
+ }
321
+ /** Undoing a mode change: set the old mode again, unless the file is gone or its mode changed since. */
322
+ function chmodStep(e, st) {
323
+ if (!st || e.mode === undefined)
324
+ return undefined;
325
+ const now = st.mode & 0o777;
326
+ if (now === e.mode)
327
+ return undefined;
328
+ if (now !== e.newMode)
329
+ return { path: e.path, action: "skip", reason: `its mode changed since (now ${now.toString(8)}); left as it is` };
330
+ return { path: e.path, action: "chmod", mode: e.mode };
331
+ }
332
+ /** What undoing a tree entry would do, without doing it (for --dry-run). */
333
+ function planTreeEntry(e) {
334
+ const st = lstatOf(e.path);
335
+ switch (e.kind) {
336
+ case "symlink":
337
+ if (!st)
338
+ return undefined;
339
+ return st.isSymbolicLink() && readlinkSync(e.path) === e.target ? { path: e.path, action: "unlink" } : { path: e.path, action: "skip", reason: "no longer the link turno made; left as it is" };
340
+ case "unlink":
341
+ return st ? { path: e.path, action: "skip", reason: `something else is there now; it linked to ${e.target}` } : { path: e.path, action: "link" };
342
+ case "move":
343
+ if (st)
344
+ return { path: e.path, action: "skip", reason: `something is there now; the moved copy stays at ${e.to}` };
345
+ return lstatOf(e.to) ? { path: e.path, action: "move", from: e.to } : { path: e.path, action: "skip", reason: `${e.to} is gone` };
346
+ case "mkdir":
347
+ if (!st)
348
+ return undefined;
349
+ // In a dry run nothing inside was removed yet: report the folder as removable.
350
+ return st.isDirectory() ? { path: e.path, action: "rmdir" } : { path: e.path, action: "keep", reason: "not a folder any more" };
351
+ case "rmdir":
352
+ return st ? undefined : { path: e.path, action: "mkdir" };
353
+ case "chmod":
354
+ return chmodStep(e, st);
355
+ default:
356
+ return undefined;
357
+ }
358
+ }
153
359
  /**
154
360
  * Put back every file of a journal. The current contents are journaled first, so an undo can
155
361
  * itself be undone.
@@ -159,9 +365,15 @@ export function undoJournal(info, env, opts = {}) {
159
365
  const m = info.manifest;
160
366
  if (!m)
161
367
  throw new Error(`${info.stamp} was written before journals existed; restore it by hand from ${info.dir}`);
162
- const journal = new Journal(env, `undo ${info.stamp}`, dryRun);
368
+ const journal = new Journal(env, `undo ${info.stamp}`, dryRun, [info.stamp]);
163
369
  const steps = [];
164
370
  for (const e of [...m.entries].reverse()) {
371
+ if (e.kind && e.kind !== "file") {
372
+ const step = dryRun ? planTreeEntry(e) : undoTreeEntry(e, journal);
373
+ if (step)
374
+ steps.push(step);
375
+ continue;
376
+ }
165
377
  if (!opts.force && e.wrote && digest(e.path) !== e.wrote) {
166
378
  steps.push({ path: e.path, action: "skip", reason: "changed since; --force puts the old copy back anyway" });
167
379
  continue;
@@ -178,8 +390,66 @@ export function undoJournal(info, env, opts = {}) {
178
390
  }
179
391
  if (!dryRun) {
180
392
  m.undoneBy = journal.stamp;
181
- writeFileSync(join(info.dir, MANIFEST), `${JSON.stringify(m, null, 2)}\n`);
393
+ writeManifest(info.dir, m);
182
394
  }
183
395
  return { steps, journal };
184
396
  }
397
+ /** When a journal was written: its manifest, else its stamp (local time), else the folder mtime. */
398
+ function journalTime(info) {
399
+ const created = info.manifest ? Date.parse(info.manifest.created) : Number.NaN;
400
+ if (!Number.isNaN(created))
401
+ return created;
402
+ const m = /^(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})/.exec(info.stamp);
403
+ if (m)
404
+ return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTime();
405
+ try {
406
+ return statSync(info.dir).mtimeMs;
407
+ }
408
+ catch {
409
+ return Date.now();
410
+ }
411
+ }
412
+ /**
413
+ * The first file a journal can still put back cleanly: not undone, and the file holds exactly
414
+ * what the command left there. Its backup is then the only copy of what was there before.
415
+ */
416
+ function liveEntry(info) {
417
+ const m = info.manifest;
418
+ if (!m || m.undoneBy)
419
+ return undefined;
420
+ return m.entries.find((e) => (e.kind ?? "file") === "file" && e.wrote !== undefined && digest(e.path) === e.wrote);
421
+ }
422
+ /**
423
+ * Delete old backup folders. The newest `keep` stay, and so does any journal that is still the
424
+ * live undo point of a file (see liveEntry): pruning it would lose the only copy of what that
425
+ * file held before a change the user has not undone.
426
+ */
427
+ export function pruneJournals(env, opts = {}) {
428
+ const olderThan = opts.olderThanMs ?? PRUNE_DEFAULT_AGE_MS;
429
+ const keep = opts.keep ?? PRUNE_DEFAULT_KEEP;
430
+ const now = opts.now ?? Date.now();
431
+ const exclude = new Set(opts.exclude ?? []);
432
+ const result = { pruned: [], kept: [] };
433
+ for (const info of listJournals(env).slice(keep)) {
434
+ if (exclude.has(info.stamp) || now - journalTime(info) < olderThan)
435
+ continue;
436
+ const live = liveEntry(info);
437
+ if (live) {
438
+ result.kept.push({ stamp: info.stamp, path: live.path });
439
+ continue;
440
+ }
441
+ if (!opts.dryRun)
442
+ rmSync(info.dir, { recursive: true, force: true });
443
+ result.pruned.push(info.stamp);
444
+ }
445
+ return result;
446
+ }
447
+ /** Parse an age such as 30d, 12h, 2w, 90m or a bare number of days. */
448
+ export function parseAge(text) {
449
+ const m = /^(\d+)\s*([smhdw]?)$/.exec(text.trim());
450
+ if (!m)
451
+ throw new Error(`invalid age ${JSON.stringify(text)}: use a number with s, m, h, d or w (30d)`);
452
+ const unit = { s: 1, m: 60, h: 3600, d: 86400, w: 604800, "": 86400 }[m[2]];
453
+ return Number(m[1]) * unit * 1000;
454
+ }
185
455
  //# sourceMappingURL=journal.js.map
@@ -3,12 +3,12 @@ import { parse } from "smol-toml";
3
3
  import { ConfigError, isValidProfileName } from "./config.js";
4
4
  import { paths } from "./paths.js";
5
5
  /** A value resolved at spawn time: "keychain:<service>" or "env:<NAME>". */
6
- export function isSecretRef(v) {
6
+ function isSecretRef(v) {
7
7
  return v.startsWith("keychain:") || v.startsWith("env:");
8
8
  }
9
9
  const SERVER_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
10
10
  /** "90s", "15m", "2h" or a number of seconds. */
11
- export function parseDuration(v, file, key) {
11
+ function parseDuration(v, file, key) {
12
12
  if (typeof v === "number" && v >= 0)
13
13
  return v;
14
14
  if (typeof v === "string") {
@@ -45,6 +45,71 @@ function optStr(v, file, key) {
45
45
  throw new ConfigError(file, `${key} must be a non-empty string`);
46
46
  return v;
47
47
  }
48
+ const TOOL_PATTERN = /^[^\s]{1,128}$/;
49
+ /** `tools = { allow = [...], deny = [...] }`: a table with only those keys, each a list of patterns. */
50
+ function toolFilterOf(v, file, key) {
51
+ if (v === undefined)
52
+ return undefined;
53
+ if (typeof v !== "object" || v === null || Array.isArray(v))
54
+ throw new ConfigError(file, `${key} must be a table with allow and/or deny`);
55
+ const out = {};
56
+ for (const [k, val] of Object.entries(v)) {
57
+ if (k !== "allow" && k !== "deny")
58
+ throw new ConfigError(file, `${key}.${k} is not a tool filter key (allow, deny)`);
59
+ const list = strArray(val, file, `${key}.${k}`);
60
+ for (const p of list)
61
+ if (!TOOL_PATTERN.test(p))
62
+ throw new ConfigError(file, `${key}.${k}: invalid tool pattern ${JSON.stringify(p)}`);
63
+ out[k] = list;
64
+ }
65
+ return out;
66
+ }
67
+ const OAUTH_KEYS = {
68
+ client_id: "clientId",
69
+ client_secret: "clientSecret",
70
+ client_metadata_url: "clientMetadataUrl",
71
+ scope: "scope",
72
+ callback_port: "callbackPort",
73
+ authorization_server: "authorizationServer",
74
+ };
75
+ /** `auth = "oauth"` and the optional `[server.<name>.oauth]` table; undefined without OAuth. */
76
+ function oauthOf(v, file, key, headers) {
77
+ if (v.auth === undefined) {
78
+ if (v.oauth !== undefined)
79
+ throw new ConfigError(file, `${key}.oauth needs auth = "oauth"`);
80
+ return undefined;
81
+ }
82
+ if (v.auth !== "oauth")
83
+ throw new ConfigError(file, `${key}.auth must be "oauth"`);
84
+ if (Object.keys(headers).some((h) => h.toLowerCase() === "authorization")) {
85
+ throw new ConfigError(file, `${key}: auth = "oauth" sets the Authorization header itself; remove headers.Authorization`);
86
+ }
87
+ const raw = v.oauth ?? {};
88
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw))
89
+ throw new ConfigError(file, `${key}.oauth must be a table`);
90
+ const out = {};
91
+ for (const [k, val] of Object.entries(raw)) {
92
+ const field = OAUTH_KEYS[k];
93
+ if (!field)
94
+ throw new ConfigError(file, `${key}.oauth.${k} is not an OAuth setting (${Object.keys(OAUTH_KEYS).join(", ")})`);
95
+ if (field === "callbackPort") {
96
+ if (typeof val !== "number" || !Number.isInteger(val) || val < 1024 || val > 65535) {
97
+ throw new ConfigError(file, `${key}.oauth.callback_port must be a port number from 1024 to 65535`);
98
+ }
99
+ out.callbackPort = val;
100
+ continue;
101
+ }
102
+ const s = optStr(val, file, `${key}.oauth.${k}`);
103
+ if (field === "clientSecret" && !isSecretRef(s)) {
104
+ throw new ConfigError(file, `${key}.oauth.client_secret must be a keychain: or env: reference (turno secret set <name>), not the secret itself`);
105
+ }
106
+ if ((field === "clientMetadataUrl" || field === "authorizationServer") && !/^https?:\/\/\S+$/.test(s)) {
107
+ throw new ConfigError(file, `${key}.oauth.${k} must be a URL`);
108
+ }
109
+ out[field] = s;
110
+ }
111
+ return out;
112
+ }
48
113
  function profilesOf(v, file, key) {
49
114
  const list = v === undefined ? ["*"] : strArray(v, file, key);
50
115
  for (const p of list)
@@ -71,11 +136,29 @@ export function parseManifest(text, file = "mcp.toml") {
71
136
  if (v.url !== undefined) {
72
137
  if (typeof v.url !== "string")
73
138
  throw new ConfigError(file, `${key}.url must be a string`);
74
- const { profiles: _p, idle: _i, ...rest } = v;
139
+ const { profiles: _p, idle: _i, call_timeout: _t, tools: _f, auth: _a, oauth: _o, variant: _v, ...rest } = v;
75
140
  const entry = { type: "http", ...rest };
76
141
  const headers = strRecord(v.headers, file, `${key}.headers`);
77
- const proxy = Object.values(headers).some(isSecretRef);
78
- servers[name] = {
142
+ const oauth = oauthOf(v, file, key, headers);
143
+ const proxy = oauth !== undefined || Object.values(headers).some(isSecretRef);
144
+ const tools = toolFilterOf(v.tools, file, `${key}.tools`);
145
+ if (tools && !proxy) {
146
+ throw new ConfigError(file, `${key}.tools only works for servers that go through mcpd; a remote server does when a header uses keychain: or env:, or with auth = "oauth"`);
147
+ }
148
+ const remoteVariants = {};
149
+ for (const [prof, vv] of Object.entries((v.variant ?? {}))) {
150
+ if (!oauth)
151
+ throw new ConfigError(file, `${key}.variant: a remote server has variants only with auth = "oauth" (one login per variant)`);
152
+ if (!isValidProfileName(prof))
153
+ throw new ConfigError(file, `${key}.variant: invalid profile "${prof}"`);
154
+ const o = (vv ?? {});
155
+ const variant = {};
156
+ const vtools = toolFilterOf(o.tools, file, `${key}.variant.${prof}.tools`);
157
+ if (vtools)
158
+ variant.tools = vtools;
159
+ remoteVariants[prof] = variant;
160
+ }
161
+ const remote = {
79
162
  kind: "remote",
80
163
  name,
81
164
  entry,
@@ -84,7 +167,15 @@ export function parseManifest(text, file = "mcp.toml") {
84
167
  url: v.url,
85
168
  headers,
86
169
  idle: v.idle === undefined ? 30 * 60 : parseDuration(v.idle, file, `${key}.idle`),
170
+ variants: remoteVariants,
87
171
  };
172
+ if (oauth)
173
+ remote.oauth = oauth;
174
+ if (v.call_timeout !== undefined)
175
+ remote.callTimeout = parseDuration(v.call_timeout, file, `${key}.call_timeout`);
176
+ if (tools)
177
+ remote.tools = tools;
178
+ servers[name] = remote;
88
179
  continue;
89
180
  }
90
181
  const builtin = optStr(v.builtin, file, `${key}.builtin`);
@@ -114,6 +205,9 @@ export function parseManifest(text, file = "mcp.toml") {
114
205
  const vcwd = optStr(o.cwd, file, `${key}.variant.${prof}.cwd`);
115
206
  if (vcwd)
116
207
  variant.cwd = vcwd;
208
+ const vtools = toolFilterOf(o.tools, file, `${key}.variant.${prof}.tools`);
209
+ if (vtools)
210
+ variant.tools = vtools;
117
211
  variants[prof] = variant;
118
212
  }
119
213
  const server = {
@@ -137,11 +231,16 @@ export function parseManifest(text, file = "mcp.toml") {
137
231
  server.package = pkg;
138
232
  if (builtin)
139
233
  server.builtin = builtin;
234
+ if (v.call_timeout !== undefined)
235
+ server.callTimeout = parseDuration(v.call_timeout, file, `${key}.call_timeout`);
236
+ const tools = toolFilterOf(v.tools, file, `${key}.tools`);
237
+ if (tools)
238
+ server.tools = tools;
140
239
  servers[name] = server;
141
240
  }
142
241
  return { servers };
143
242
  }
144
- export function loadManifest(env = process.env) {
243
+ export function loadManifest(env) {
145
244
  const file = paths(env).mcpFile;
146
245
  let text = "";
147
246
  try {
@@ -174,4 +273,25 @@ export function effectiveLaunch(server, variant) {
174
273
  out.cwd = cwd;
175
274
  return out;
176
275
  }
276
+ /** The tool filter a profile's sessions get: the server's, with the variant's keys in place of its own. */
277
+ export function toolFilterFor(server, profile) {
278
+ const base = server.tools ?? {};
279
+ const v = server.variants[variantKey(server, profile)]?.tools;
280
+ return v ? { ...base, ...v } : base;
281
+ }
282
+ function globRegExp(pattern) {
283
+ const body = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
284
+ return new RegExp(`^${body}$`);
285
+ }
286
+ /** Whether a tool passes a filter. */
287
+ export function toolAllowed(filter, tool) {
288
+ const hit = (patterns) => patterns.some((p) => globRegExp(p).test(tool));
289
+ if (filter.allow && !hit(filter.allow))
290
+ return false;
291
+ return !(filter.deny && hit(filter.deny));
292
+ }
293
+ /** Whether a filter can hide anything. */
294
+ export function filtersTools(filter) {
295
+ return filter.allow !== undefined || filter.deny !== undefined;
296
+ }
177
297
  //# sourceMappingURL=manifest.js.map