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
@@ -0,0 +1,46 @@
1
+ import { atomicWrite } from "../core/journal.js";
2
+ import { PRIVATE_FILE_MODE } from "../core/paths.js";
3
+ import { isRecord, readState } from "../core/state-file.js";
4
+ /**
5
+ * The client name (`clientInfo.name` from initialize) of each agent process, by PID, kept in a
6
+ * small state file so a session mcpd rebuilds after a restart still has its agent's
7
+ * name for the call log and `/status`. Entries of processes that are gone are dropped
8
+ * on the next write. Never holds anything but PIDs and printable names.
9
+ */
10
+ export class ClientNames {
11
+ file;
12
+ isAlive;
13
+ names = new Map();
14
+ constructor(file, isAlive) {
15
+ this.file = file;
16
+ this.isAlive = isAlive;
17
+ if (!file)
18
+ return;
19
+ // None yet, or unreadable: start empty.
20
+ for (const [pid, name] of Object.entries(readState(file, isRecord) ?? {})) {
21
+ const n = Number(pid);
22
+ if (Number.isInteger(n) && n > 0 && typeof name === "string")
23
+ this.names.set(n, name);
24
+ }
25
+ }
26
+ get(pid) {
27
+ return pid ? this.names.get(pid) : undefined;
28
+ }
29
+ set(pid, name) {
30
+ if (!pid || this.names.get(pid) === name)
31
+ return;
32
+ this.names.set(pid, name);
33
+ for (const p of this.names.keys())
34
+ if (!this.isAlive(p))
35
+ this.names.delete(p);
36
+ if (!this.file)
37
+ return;
38
+ try {
39
+ atomicWrite(this.file, `${JSON.stringify(Object.fromEntries(this.names))}\n`, PRIVATE_FILE_MODE);
40
+ }
41
+ catch {
42
+ /* a disk that refuses the write only costs the name after a restart */
43
+ }
44
+ }
45
+ }
46
+ //# sourceMappingURL=client-names.js.map
@@ -1,33 +1,187 @@
1
- import { createWriteStream, mkdirSync, statSync, renameSync } from "node:fs";
1
+ import { closeSync, fchmodSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync, } from "node:fs";
2
2
  import { join } from "node:path";
3
+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
3
4
  import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
4
5
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
5
6
  import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
6
- import { effectiveLaunch } from "../core/manifest.js";
7
- import { resolveRecord } from "../core/secrets.js";
8
- import { resolveCommand } from "./instances.js";
7
+ import { effectiveLaunch, effectiveStopGrace } from "../core/manifest.js";
8
+ import { PRIVATE_DIR_MODE, PRIVATE_FILE_MODE } from "../core/paths.js";
9
+ import { expandVars, resolveRecord, secretStore } from "../core/secrets.js";
10
+ import { isDirectory, resolveCommand } from "./instances.js";
11
+ import { OAuthSessions, oauthFetch } from "./oauth.js";
9
12
  const MAX_LOG = 5 * 1024 * 1024;
10
- function openLog(logsDir, name) {
11
- mkdirSync(logsDir, { recursive: true });
12
- const file = join(logsDir, `${name.replace(/[^A-Za-z0-9_.-]/g, "_")}.log`);
13
+ /** What the MCP SDK waits after closing stdin, and again after SIGTERM, in milliseconds. */
14
+ const SDK_STEP_MS = 2000;
15
+ /**
16
+ * Replaces the SDK's close of a stdio server (stdin closed, 2 s, SIGTERM, 2 s, SIGKILL) with one
17
+ * that waits `graceMs` before SIGTERM (`stop_grace`). The rest is the SDK's: SIGTERM,
18
+ * 2 s, SIGKILL. It reads the transport's child process, a private field of the SDK; if that
19
+ * ever changes, the SDK's own close runs unchanged.
20
+ */
21
+ function withStopGrace(transport, graceMs) {
22
+ const sdkClose = transport.close.bind(transport);
23
+ transport.close = async () => {
24
+ const proc = transport._process;
25
+ const running = () => proc !== undefined && proc.exitCode === null && proc.signalCode === null;
26
+ if (proc && running()) {
27
+ const closed = new Promise((resolve) => proc.once("close", () => resolve()));
28
+ const wait = (ms) => Promise.race([closed, new Promise((resolve) => setTimeout(resolve, ms).unref())]);
29
+ try {
30
+ proc.stdin?.end();
31
+ }
32
+ catch {
33
+ /* already closed */
34
+ }
35
+ await wait(graceMs);
36
+ if (running()) {
37
+ proc.kill("SIGTERM");
38
+ await wait(SDK_STEP_MS);
39
+ }
40
+ if (running()) {
41
+ proc.kill("SIGKILL");
42
+ await wait(1000);
43
+ }
44
+ }
45
+ // The process is gone (the SDK cleared it on "close"): this only resets the read buffer.
46
+ await sdkClose();
47
+ };
48
+ }
49
+ function logFileOf(logsDir, label) {
50
+ return join(logsDir, `${label.replace(/[^A-Za-z0-9_.-]/g, "_")}.log`);
51
+ }
52
+ /**
53
+ * Append-only log that rotates by size while it is written, not only when a server starts:
54
+ * a long-lived, chatty server would otherwise fill the disk.
55
+ */
56
+ export class RotatingLog {
57
+ file;
58
+ maxBytes;
59
+ fd;
60
+ size = 0;
61
+ constructor(file, maxBytes = MAX_LOG) {
62
+ this.file = file;
63
+ this.maxBytes = maxBytes;
64
+ mkdirSync(join(file, ".."), { recursive: true, mode: PRIVATE_DIR_MODE });
65
+ try {
66
+ this.size = statSync(file).size;
67
+ }
68
+ catch {
69
+ this.size = 0;
70
+ }
71
+ if (this.size > this.maxBytes)
72
+ this.rotate();
73
+ }
74
+ /** Synchronous, like mcpd.log: chunks are small and the order must survive a rotation. */
75
+ write(chunk) {
76
+ const buf = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
77
+ try {
78
+ if (this.size > 0 && this.size + buf.length > this.maxBytes)
79
+ this.rotate();
80
+ if (this.fd === undefined) {
81
+ // Server logs can quote tokens and file contents: 0600 like every file turno keeps.
82
+ this.fd = openSync(this.file, "a", PRIVATE_FILE_MODE);
83
+ if (fstatSync(this.fd).mode & 0o077)
84
+ fchmodSync(this.fd, PRIVATE_FILE_MODE);
85
+ }
86
+ writeSync(this.fd, buf);
87
+ this.size += buf.length;
88
+ }
89
+ catch {
90
+ /* a full or read-only disk must not take mcpd down */
91
+ }
92
+ }
93
+ end() {
94
+ if (this.fd === undefined)
95
+ return;
96
+ try {
97
+ closeSync(this.fd);
98
+ }
99
+ catch {
100
+ /* already closed */
101
+ }
102
+ delete this.fd;
103
+ }
104
+ rotate() {
105
+ this.end();
106
+ try {
107
+ renameSync(this.file, `${this.file}.1`);
108
+ }
109
+ catch {
110
+ /* nothing to rotate */
111
+ }
112
+ this.size = 0;
113
+ }
114
+ }
115
+ /** The last lines of a file (at most 16 KiB are read). */
116
+ function tailFile(file, lines = 10) {
117
+ let fd;
13
118
  try {
14
- if (statSync(file).size > MAX_LOG)
15
- renameSync(file, `${file}.1`);
119
+ const size = statSync(file).size;
120
+ const len = Math.min(size, 16 * 1024);
121
+ const buf = Buffer.alloc(len);
122
+ fd = openSync(file, "r");
123
+ readSync(fd, buf, 0, len, size - len);
124
+ const text = buf.toString("utf8").trimEnd();
125
+ return text ? text.split("\n").slice(-lines).join("\n") : undefined;
16
126
  }
17
127
  catch {
18
- /* new file */
128
+ return undefined;
129
+ }
130
+ finally {
131
+ if (fd !== undefined)
132
+ closeSync(fd);
19
133
  }
20
- return createWriteStream(file, { flags: "a" });
21
134
  }
22
135
  export class DefaultConnector {
23
136
  opts;
137
+ oauthSessions;
138
+ oauthLogs = new Map();
24
139
  constructor(opts) {
25
140
  this.opts = opts;
26
141
  }
142
+ sessions() {
143
+ this.oauthSessions ??=
144
+ this.opts.oauth ??
145
+ new OAuthSessions((name, variant) => ({
146
+ store: () => secretStore(this.opts.vars ?? process.env),
147
+ resolve: this.opts.resolve,
148
+ log: (line) => this.oauthLog(name, variant).write(`turno: ${line}\n`),
149
+ }));
150
+ return this.oauthSessions;
151
+ }
152
+ oauthLog(name, variant) {
153
+ const label = `${name}${variant === "default" ? "" : `@${variant}`}`;
154
+ let log = this.oauthLogs.get(label);
155
+ if (!log) {
156
+ log = new RotatingLog(logFileOf(this.opts.logsDir, label), this.opts.maxLogBytes ?? MAX_LOG);
157
+ this.oauthLogs.set(label, log);
158
+ }
159
+ return log;
160
+ }
161
+ forgetLogin(server, variants) {
162
+ this.oauthSessions?.forget(server, variants);
163
+ }
164
+ async flushLogins() {
165
+ return this.oauthSessions ? this.oauthSessions.flush() : [];
166
+ }
167
+ logTail(server, variant, lines = 10) {
168
+ return tailFile(logFileOf(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`), lines);
169
+ }
27
170
  async connect(server, variant, ctx, workdir) {
28
171
  if (server.kind === "remote") {
29
172
  const headers = await resolveRecord(server.headers, this.opts.resolve);
30
- const transport = new StreamableHTTPClientTransport(new URL(server.url), { requestInit: { headers } });
173
+ let fetchOpt = {};
174
+ if (server.oauth) {
175
+ // mcpd is the OAuth client. Asking for a token now fails fast with the login
176
+ // hint instead of a failed initialize; every request then gets a current token.
177
+ const session = this.sessions().get(server.name, variant);
178
+ await session.accessToken(server);
179
+ fetchOpt = { fetch: oauthFetch(session, () => server) };
180
+ }
181
+ // `type = "sse"` is the legacy HTTP+SSE transport; everything else is Streamable HTTP.
182
+ const transport = server.entry.type === "sse"
183
+ ? new SSEClientTransport(new URL(server.url), { requestInit: { headers }, ...fetchOpt })
184
+ : new StreamableHTTPClientTransport(new URL(server.url), { requestInit: { headers }, ...fetchOpt });
31
185
  return { transport };
32
186
  }
33
187
  if (server.builtin) {
@@ -40,24 +194,40 @@ export class DefaultConnector {
40
194
  return { transport: clientSide };
41
195
  }
42
196
  const launch = effectiveLaunch(server, variant);
197
+ const cwd = launch.cwd ?? workdir;
198
+ if (!isDirectory(cwd)) {
199
+ throw new Error(launch.cwd
200
+ ? `the folder ${cwd} (cwd in mcp.toml) does not exist`
201
+ : `the session folder ${cwd} does not exist or is not readable by mcpd`);
202
+ }
43
203
  const env = { ...(this.opts.baseEnv ?? {}), ...(await resolveRecord(launch.env, this.opts.resolve)) };
44
- const resolved = resolveCommand(server, launch.command, launch.args, this.opts.serversDir);
204
+ const vars = this.opts.vars;
205
+ const args = vars ? launch.args.map((a) => expandVars(a, vars)) : launch.args;
206
+ const resolved = resolveCommand(server, launch.command, args, this.opts.serversDir);
45
207
  const transport = new StdioClientTransport({
46
208
  command: resolved.command,
47
209
  args: resolved.args,
48
210
  env,
49
- cwd: launch.cwd ?? workdir,
211
+ cwd,
50
212
  stderr: "pipe",
51
213
  });
52
- const log = openLog(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`);
214
+ const graceMs = effectiveStopGrace(server, variant) * 1000;
215
+ if (graceMs !== SDK_STEP_MS)
216
+ withStopGrace(transport, graceMs);
217
+ const log = new RotatingLog(logFileOf(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`), this.opts.maxLogBytes ?? MAX_LOG);
218
+ const warnings = resolved.warning ? [resolved.warning] : [];
219
+ for (const w of warnings)
220
+ log.write(`turno: ${w}\n`);
53
221
  const origStart = transport.start.bind(transport);
54
222
  transport.start = async () => {
55
223
  await origStart();
56
224
  const err = transport.stderr;
57
- if (err)
58
- err.pipe(log);
225
+ if (err) {
226
+ err.on("data", (chunk) => log.write(chunk));
227
+ err.on("end", () => log.end());
228
+ }
59
229
  };
60
- return { transport, pid: () => transport.pid };
230
+ return { transport, pid: () => transport.pid, warnings };
61
231
  }
62
232
  }
63
233
  //# sourceMappingURL=connectors.js.map