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.
Files changed (121) hide show
  1. package/CHANGELOG.md +432 -0
  2. package/README.md +228 -73
  3. package/dist/accounts/audit.js +154 -12
  4. package/dist/accounts/builtin.js +100 -25
  5. package/dist/accounts/delegate.js +199 -35
  6. package/dist/accounts/policy.js +79 -0
  7. package/dist/accounts/registry.js +39 -21
  8. package/dist/agents/adapter.js +29 -0
  9. package/dist/agents/claude.js +246 -0
  10. package/dist/agents/codex.js +186 -0
  11. package/dist/agents/copilot.js +56 -0
  12. package/dist/agents/cursor.js +58 -0
  13. package/dist/agents/index.js +26 -86
  14. package/dist/agents/snippets.js +43 -13
  15. package/dist/cli/args.js +38 -6
  16. package/dist/cli/commands/accounts.js +119 -52
  17. package/dist/cli/commands/auto.js +121 -33
  18. package/dist/cli/commands/browser-identity.js +514 -0
  19. package/dist/cli/commands/browser-session.js +102 -0
  20. package/dist/cli/commands/browser-vault.js +193 -0
  21. package/dist/cli/commands/browser.js +319 -138
  22. package/dist/cli/commands/complete.js +14 -3
  23. package/dist/cli/commands/completion.js +8 -2
  24. package/dist/cli/commands/config.js +186 -24
  25. package/dist/cli/commands/demo.js +321 -0
  26. package/dist/cli/commands/doctor.js +525 -123
  27. package/dist/cli/commands/handoff.js +33 -22
  28. package/dist/cli/commands/identity.js +192 -54
  29. package/dist/cli/commands/init.js +648 -0
  30. package/dist/cli/commands/limit.js +84 -16
  31. package/dist/cli/commands/mcp-call.js +240 -0
  32. package/dist/cli/commands/mcp-oauth.js +221 -0
  33. package/dist/cli/commands/mcp-observe.js +539 -0
  34. package/dist/cli/commands/mcp-share.js +201 -0
  35. package/dist/cli/commands/mcp.js +816 -187
  36. package/dist/cli/commands/mcpd.js +388 -76
  37. package/dist/cli/commands/migrate.js +6 -4
  38. package/dist/cli/commands/open.js +115 -27
  39. package/dist/cli/commands/profile.js +396 -78
  40. package/dist/cli/commands/resolve.js +14 -3
  41. package/dist/cli/commands/run.js +36 -6
  42. package/dist/cli/commands/secret.js +80 -76
  43. package/dist/cli/commands/session.js +17 -6
  44. package/dist/cli/commands/sessions.js +48 -12
  45. package/dist/cli/commands/setup.js +214 -164
  46. package/dist/cli/commands/status.js +227 -44
  47. package/dist/cli/commands/statusline.js +311 -114
  48. package/dist/cli/commands/ui.js +78 -68
  49. package/dist/cli/commands/undo.js +193 -18
  50. package/dist/cli/commands/uninstall.js +481 -0
  51. package/dist/cli/commands/upgrade.js +172 -79
  52. package/dist/cli/commands/usage.js +125 -51
  53. package/dist/cli/commands/version.js +21 -7
  54. package/dist/cli/commands/which.js +9 -24
  55. package/dist/cli/compile-cache.js +32 -0
  56. package/dist/cli/diagnostics.js +184 -0
  57. package/dist/cli/exit.js +65 -0
  58. package/dist/cli/group.js +24 -12
  59. package/dist/cli/help.js +62 -0
  60. package/dist/cli/highlight.js +236 -0
  61. package/dist/cli/hints.js +30 -0
  62. package/dist/cli/io.js +186 -1
  63. package/dist/cli/launch.js +52 -39
  64. package/dist/cli/launchers.js +161 -95
  65. package/dist/cli/main.js +201 -31
  66. package/dist/cli/optional.js +144 -0
  67. package/dist/cli/output.js +123 -0
  68. package/dist/cli/picker.js +459 -0
  69. package/dist/cli/registry.js +61 -6
  70. package/dist/cli/safe.js +87 -0
  71. package/dist/cli/secret-input.js +63 -0
  72. package/dist/cli/security.js +561 -0
  73. package/dist/cli/style.js +311 -0
  74. package/dist/cli/topics.js +142 -0
  75. package/dist/core/config.js +181 -23
  76. package/dist/core/exhausted.js +6 -8
  77. package/dist/core/fsutil.js +39 -0
  78. package/dist/core/journal.js +294 -16
  79. package/dist/core/manifest.js +153 -6
  80. package/dist/core/paths.js +14 -13
  81. package/dist/core/profiles.js +160 -10
  82. package/dist/core/resolve.js +139 -23
  83. package/dist/core/secrets.js +68 -23
  84. package/dist/core/sessions.js +13 -20
  85. package/dist/core/state-file.js +107 -0
  86. package/dist/core/update.js +31 -18
  87. package/dist/core/version.js +57 -10
  88. package/dist/demo/server.js +42 -0
  89. package/dist/mcpd/cache.js +23 -10
  90. package/dist/mcpd/calllog.js +254 -0
  91. package/dist/mcpd/client-names.js +46 -0
  92. package/dist/mcpd/connectors.js +188 -18
  93. package/dist/mcpd/daemon.js +472 -115
  94. package/dist/mcpd/folder-sets.js +42 -0
  95. package/dist/mcpd/gateway.js +473 -51
  96. package/dist/mcpd/health.js +313 -0
  97. package/dist/mcpd/importer.js +453 -36
  98. package/dist/mcpd/instances.js +453 -65
  99. package/dist/mcpd/jsonrpc.js +11 -1
  100. package/dist/mcpd/launchd.js +65 -48
  101. package/dist/mcpd/middleware.js +144 -0
  102. package/dist/mcpd/modern.js +686 -0
  103. package/dist/mcpd/oauth.js +845 -0
  104. package/dist/mcpd/peerpid.js +134 -6
  105. package/dist/mcpd/pressure.js +88 -0
  106. package/dist/mcpd/rss.js +166 -0
  107. package/dist/mcpd/service.js +191 -0
  108. package/dist/mcpd/share.js +217 -0
  109. package/dist/mcpd/state.js +183 -0
  110. package/dist/mcpd/sync.js +72 -87
  111. package/dist/mcpd/systemd.js +83 -0
  112. package/dist/mcpd/tokens.js +85 -0
  113. package/dist/mcpd/upstream.js +163 -46
  114. package/dist/statusline/cost.js +167 -44
  115. package/dist/statusline/input.js +78 -0
  116. package/dist/statusline/render.js +9 -3
  117. package/dist/ui/fonts.js +8 -3
  118. package/dist/ui/settings.js +33 -16
  119. package/npm-shrinkwrap.json +1102 -0
  120. package/package.json +10 -9
  121. package/dist/cli/commands/available.js +0 -18
@@ -2,6 +2,8 @@ export const isRequest = (m) => typeof m.method === "string" && m.id !== undefin
2
2
  export const isNotification = (m) => typeof m.method === "string" && (m.id === undefined || m.id === null);
3
3
  export const isResponse = (m) => m.method === undefined && m.id !== undefined && ("result" in m || "error" in m);
4
4
  export const ErrorCode = {
5
+ ParseError: -32700,
6
+ InvalidRequest: -32600,
5
7
  MethodNotFound: -32601,
6
8
  InvalidParams: -32602,
7
9
  Internal: -32603,
@@ -9,9 +11,17 @@ export const ErrorCode = {
9
11
  Unavailable: -32002,
10
12
  /** Server-defined: an exclusive server is held by another session. */
11
13
  Busy: -32003,
14
+ /** Server-defined: a server-to-client request could not be tied to one client session. */
15
+ Ambiguous: -32004,
16
+ /** Same code the SDK uses for a request that ran out of time. */
17
+ RequestTimeout: -32001,
18
+ /** The request was cancelled (by the client, or because the client went away). */
19
+ RequestCancelled: -32800,
12
20
  };
13
21
  export function toResponse(id, outcome) {
14
- return "error" in outcome ? { jsonrpc: "2.0", id, error: outcome.error } : { jsonrpc: "2.0", id, result: outcome.result };
22
+ return "error" in outcome
23
+ ? { jsonrpc: "2.0", id, error: outcome.error }
24
+ : { jsonrpc: "2.0", id, result: outcome.result };
15
25
  }
16
26
  export function rpcError(code, message, data) {
17
27
  return { error: data === undefined ? { code, message } : { code, message, data } };
@@ -1,9 +1,6 @@
1
- import { execFile } from "node:child_process";
2
- import { mkdirSync, writeFileSync } from "node:fs";
3
1
  import { join } from "node:path";
4
- import { promisify } from "node:util";
5
- import { paths } from "../core/paths.js";
6
- const run = promisify(execFile);
2
+ import { ensurePrivateDir, paths } from "../core/paths.js";
3
+ import { ServiceError, runTool, serviceArgs, serviceEnv } from "./service.js";
7
4
  export const LABEL = "dev.turno.mcpd";
8
5
  export function plistPath(env = process.env) {
9
6
  return join(paths(env).home, "Library", "LaunchAgents", `${LABEL}.plist`);
@@ -11,19 +8,22 @@ export function plistPath(env = process.env) {
11
8
  function xml(s) {
12
9
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
13
10
  }
14
- /** LaunchAgent that keeps `turno mcpd run` alive, with the caller's PATH (launchd's is minimal). */
15
- export function plist(opts) {
16
- const p = paths(opts.env);
17
- const vars = {
18
- PATH: opts.env.PATH ?? "/usr/bin:/bin:/usr/sbin:/sbin",
19
- HOME: p.home,
20
- };
21
- for (const k of ["XDG_CONFIG_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "LANG"])
22
- if (opts.env[k])
23
- vars[k] = opts.env[k];
24
- const envXml = Object.entries(vars)
11
+ function unxml(s) {
12
+ return s.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
13
+ }
14
+ /**
15
+ * LaunchAgent that keeps `turno mcp daemon run` alive. `Interactive` because mcpd answers
16
+ * calls the user is waiting for and starts browsers (a `Background` job gets throttled).
17
+ * KeepAlive only restarts it after a failure: a clean exit (another mcpd already serves) stays down.
18
+ */
19
+ export function plist(spec) {
20
+ const p = paths(spec.env);
21
+ const envXml = Object.entries(serviceEnv(spec, "launchd"))
25
22
  .map(([k, v]) => ` <key>${xml(k)}</key>\n <string>${xml(v)}</string>`)
26
23
  .join("\n");
24
+ const args = serviceArgs(spec)
25
+ .map((a) => ` <string>${xml(a)}</string>`)
26
+ .join("\n");
27
27
  return `<?xml version="1.0" encoding="UTF-8"?>
28
28
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
29
29
  <plist version="1.0">
@@ -32,10 +32,7 @@ export function plist(opts) {
32
32
  <string>${LABEL}</string>
33
33
  <key>ProgramArguments</key>
34
34
  <array>
35
- <string>${xml(opts.node)}</string>
36
- <string>${xml(opts.script)}</string>
37
- <string>mcpd</string>
38
- <string>run</string>
35
+ ${args}
39
36
  </array>
40
37
  <key>EnvironmentVariables</key>
41
38
  <dict>
@@ -44,9 +41,14 @@ ${envXml}
44
41
  <key>RunAtLoad</key>
45
42
  <true/>
46
43
  <key>KeepAlive</key>
47
- <true/>
44
+ <dict>
45
+ <key>SuccessfulExit</key>
46
+ <false/>
47
+ </dict>
48
+ <key>ThrottleInterval</key>
49
+ <integer>10</integer>
48
50
  <key>ProcessType</key>
49
- <string>Background</string>
51
+ <string>Interactive</string>
50
52
  <key>StandardOutPath</key>
51
53
  <string>${xml(join(p.logsDir, "mcpd.stdout.log"))}</string>
52
54
  <key>StandardErrorPath</key>
@@ -55,32 +57,47 @@ ${envXml}
55
57
  </plist>
56
58
  `;
57
59
  }
58
- function domain() {
59
- return `gui/${process.getuid?.() ?? 501}`;
60
- }
61
- export async function isLoaded() {
62
- try {
63
- await run("launchctl", ["print", `${domain()}/${LABEL}`], { timeout: 5000 });
64
- return true;
65
- }
66
- catch {
67
- return false;
68
- }
69
- }
70
- export async function install(opts) {
71
- const file = plistPath(opts.env);
72
- mkdirSync(join(paths(opts.env).home, "Library", "LaunchAgents"), { recursive: true });
73
- mkdirSync(paths(opts.env).logsDir, { recursive: true });
74
- writeFileSync(file, plist(opts));
75
- if (await isLoaded())
76
- await run("launchctl", ["bootout", `${domain()}/${LABEL}`], { timeout: 10_000 }).catch(() => { });
77
- await run("launchctl", ["bootstrap", domain(), file], { timeout: 10_000 });
78
- return file;
60
+ /** ProgramArguments of a plist written by `plist()` (or an earlier turno). */
61
+ export function plistProgram(text) {
62
+ const m = /<key>ProgramArguments<\/key>\s*<array>([\s\S]*?)<\/array>/.exec(text);
63
+ if (!m)
64
+ return [];
65
+ return [...m[1].matchAll(/<string>([\s\S]*?)<\/string>/g)].map((x) => unxml(x[1]));
79
66
  }
80
- export async function uninstall() {
81
- await run("launchctl", ["bootout", `${domain()}/${LABEL}`], { timeout: 10_000 });
82
- }
83
- export async function restart() {
84
- await run("launchctl", ["kickstart", "-k", `${domain()}/${LABEL}`], { timeout: 10_000 });
67
+ function domain() {
68
+ const uid = process.getuid?.();
69
+ if (uid === undefined)
70
+ throw new ServiceError("cannot tell the user id for launchctl");
71
+ return `gui/${uid}`;
85
72
  }
73
+ export const launchd = {
74
+ kind: "launchd",
75
+ noun: "LaunchAgent",
76
+ file: plistPath,
77
+ render: plist,
78
+ programOf: plistProgram,
79
+ async isLoaded(env) {
80
+ try {
81
+ await runTool("launchctl", ["print", `${domain()}/${LABEL}`], env, 5000);
82
+ return true;
83
+ }
84
+ catch (err) {
85
+ if (err instanceof ServiceError && /not found on PATH/.test(err.message))
86
+ throw err;
87
+ return false;
88
+ }
89
+ },
90
+ async load(env) {
91
+ ensurePrivateDir(paths(env).logsDir, env);
92
+ if (await launchd.isLoaded(env))
93
+ await runTool("launchctl", ["bootout", `${domain()}/${LABEL}`], env).catch(() => { });
94
+ await runTool("launchctl", ["bootstrap", domain(), plistPath(env)], env);
95
+ },
96
+ async unload(env) {
97
+ await runTool("launchctl", ["bootout", `${domain()}/${LABEL}`], env);
98
+ },
99
+ async restart(env) {
100
+ await runTool("launchctl", ["kickstart", "-k", `${domain()}/${LABEL}`], env);
101
+ },
102
+ };
86
103
  //# sourceMappingURL=launchd.js.map
@@ -0,0 +1,144 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { filtersTools, toolAllowed, toolFilterFor } from "../core/manifest.js";
3
+ import { ErrorCode, rpcError } from "./jsonrpc.js";
4
+ /**
5
+ * The call middleware. Every `tools/call` mcpd relays, from a 2025-11-25 session
6
+ * (gateway.ts) or a 2026-07-28 stateless request (modern.ts), goes through one pipeline of
7
+ * named stages before `InstanceManager.forward`:
8
+ *
9
+ * filter -> <opt-in stages, in order> -> calllog -> session -> forward
10
+ *
11
+ * - `filter` refuses a tool the profile's `tools.allow` / `tools.deny` hides. A
12
+ * refused call reaches no other stage and is not logged.
13
+ * - opt-in stages (none ship yet; later tool-output trimming, local telemetry counters, a cost
14
+ * meter) are off by default: each is a `CallStage` that something turns on explicitly. They
15
+ * see the call as the client sent it, and the outcome the client gets.
16
+ * - `calllog` times the call and hands one event to the call log when it is on.
17
+ * - `session` puts the client session id in the forwarded `_meta` (`io.turno/session`).
18
+ *
19
+ * Stage names are unique, lowercase, and never a built-in name.
20
+ */
21
+ /**
22
+ * The `_meta` key of the client session id. Typed by turno-browser's own constant (a type
23
+ * import, erased at build): if the two ever differ, the typecheck fails.
24
+ */
25
+ export const SESSION_META = "io.turno/session";
26
+ const BUILT_IN = ["filter", "calllog", "session"];
27
+ const STAGE_NAME = /^[a-z][a-z0-9-]*$/;
28
+ /** A profile's tool filter for a client's server, or undefined when it filters nothing. */
29
+ export function toolFilterOf(manifest, ctx) {
30
+ const server = manifest.servers[ctx.serverName];
31
+ if (!server)
32
+ return undefined;
33
+ const f = toolFilterFor(server, ctx.profile);
34
+ return filtersTools(f) ? f : undefined;
35
+ }
36
+ /** `filter`: a tool the profile's filter hides is refused here, before anything else runs. */
37
+ function filterStage(manifest) {
38
+ return {
39
+ name: "filter",
40
+ run(call, next) {
41
+ const filter = toolFilterOf(manifest(), call.client.ctx);
42
+ if (filter && !toolAllowed(filter, call.tool)) {
43
+ const { serverName, profile } = call.client.ctx;
44
+ return Promise.resolve(rpcError(ErrorCode.InvalidParams, `tool ${call.tool} of ${serverName} is not available to profile ${profile ?? "default"} (tools.allow / tools.deny in mcp.toml)`));
45
+ }
46
+ return next(call);
47
+ },
48
+ };
49
+ }
50
+ /** `calllog`: times the call and reports it to the call log, when one is on (read per call). */
51
+ function callLogStage(sink) {
52
+ return {
53
+ name: "calllog",
54
+ async run(call, next) {
55
+ const startedAt = Date.now();
56
+ const t0 = performance.now();
57
+ const params = call.params;
58
+ const outcome = await next(call);
59
+ const record = sink();
60
+ if (record) {
61
+ try {
62
+ record({
63
+ ctx: call.client.ctx,
64
+ method: "tools/call",
65
+ params,
66
+ ...(call.variant ? { variant: call.variant } : {}),
67
+ outcome,
68
+ startedAt,
69
+ durationMs: performance.now() - t0,
70
+ });
71
+ }
72
+ catch {
73
+ /* observing a call never fails it */
74
+ }
75
+ }
76
+ return outcome;
77
+ },
78
+ };
79
+ }
80
+ /**
81
+ * `session`: the client session id in `_meta[SESSION_META]`. A random UUID per client handle
82
+ * (one 2025-11-25 session, or one stateless client: profile, server, PID and folder), kept for
83
+ * the life of the handle. It is not the Mcp-Session-Id, so a server learns nothing it could use
84
+ * against mcpd. A value the client sent under the same key is replaced.
85
+ */
86
+ function sessionStage() {
87
+ const ids = new WeakMap();
88
+ return {
89
+ name: "session",
90
+ run(call, next) {
91
+ let id = ids.get(call.client);
92
+ if (!id) {
93
+ id = randomUUID();
94
+ ids.set(call.client, id);
95
+ }
96
+ const meta = typeof call.params._meta === "object" && call.params._meta !== null
97
+ ? call.params._meta
98
+ : {};
99
+ call.params = { ...call.params, _meta: { ...meta, [SESSION_META]: id } };
100
+ return next(call);
101
+ },
102
+ };
103
+ }
104
+ /** The ordered stages, and the relay through the instance manager at the end. */
105
+ export class CallPipeline {
106
+ manager;
107
+ stages;
108
+ constructor(manager, stages) {
109
+ this.manager = manager;
110
+ const seen = new Set();
111
+ for (const s of stages) {
112
+ if (!STAGE_NAME.test(s.name))
113
+ throw new Error(`call stage name "${s.name}" must match ${STAGE_NAME}`);
114
+ if (seen.has(s.name))
115
+ throw new Error(`call stage "${s.name}" is registered twice`);
116
+ seen.add(s.name);
117
+ }
118
+ this.stages = [...stages];
119
+ }
120
+ /** The stage names in the order a call meets them. */
121
+ names() {
122
+ return this.stages.map((s) => s.name);
123
+ }
124
+ /** Relay one tools/call through every stage, then `InstanceManager.forward`. */
125
+ call(client, params, originId, cancelled) {
126
+ const relay = (c) => this.manager.forward(c.client, "tools/call", c.params, c.originId, c.cancelled, (variant) => (c.variant = variant));
127
+ const run = this.stages.reduceRight((next, stage) => (c) => stage.run(c, next), relay);
128
+ return run({ client, tool: typeof params.name === "string" ? params.name : "", params, originId, cancelled });
129
+ }
130
+ }
131
+ /** mcpd's pipeline: the built-in stages around the opt-in ones. */
132
+ export function createCallPipeline(opts) {
133
+ const stages = opts.stages ?? [];
134
+ for (const s of stages)
135
+ if (BUILT_IN.includes(s.name))
136
+ throw new Error(`call stage "${s.name}" is registered twice`);
137
+ return new CallPipeline(opts.manager, [
138
+ filterStage(opts.manifest),
139
+ ...stages,
140
+ callLogStage(opts.callLog ?? (() => undefined)),
141
+ sessionStage(),
142
+ ]);
143
+ }
144
+ //# sourceMappingURL=middleware.js.map