turno-cli 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +432 -0
- package/README.md +228 -73
- package/dist/accounts/audit.js +154 -12
- package/dist/accounts/builtin.js +100 -25
- package/dist/accounts/delegate.js +199 -35
- package/dist/accounts/policy.js +79 -0
- package/dist/accounts/registry.js +39 -21
- package/dist/agents/adapter.js +29 -0
- package/dist/agents/claude.js +246 -0
- package/dist/agents/codex.js +186 -0
- package/dist/agents/copilot.js +56 -0
- package/dist/agents/cursor.js +58 -0
- package/dist/agents/index.js +26 -86
- package/dist/agents/snippets.js +43 -13
- package/dist/cli/args.js +38 -6
- package/dist/cli/commands/accounts.js +119 -52
- package/dist/cli/commands/auto.js +121 -33
- package/dist/cli/commands/browser-identity.js +514 -0
- package/dist/cli/commands/browser-session.js +102 -0
- package/dist/cli/commands/browser-vault.js +193 -0
- package/dist/cli/commands/browser.js +319 -138
- package/dist/cli/commands/complete.js +14 -3
- package/dist/cli/commands/completion.js +8 -2
- package/dist/cli/commands/config.js +186 -24
- package/dist/cli/commands/demo.js +321 -0
- package/dist/cli/commands/doctor.js +525 -123
- package/dist/cli/commands/handoff.js +33 -22
- package/dist/cli/commands/identity.js +192 -54
- package/dist/cli/commands/init.js +648 -0
- package/dist/cli/commands/limit.js +84 -16
- package/dist/cli/commands/mcp-call.js +240 -0
- package/dist/cli/commands/mcp-oauth.js +221 -0
- package/dist/cli/commands/mcp-observe.js +539 -0
- package/dist/cli/commands/mcp-share.js +201 -0
- package/dist/cli/commands/mcp.js +816 -187
- package/dist/cli/commands/mcpd.js +388 -76
- package/dist/cli/commands/migrate.js +6 -4
- package/dist/cli/commands/open.js +115 -27
- package/dist/cli/commands/profile.js +396 -78
- package/dist/cli/commands/resolve.js +14 -3
- package/dist/cli/commands/run.js +36 -6
- package/dist/cli/commands/secret.js +80 -76
- package/dist/cli/commands/session.js +17 -6
- package/dist/cli/commands/sessions.js +48 -12
- package/dist/cli/commands/setup.js +214 -164
- package/dist/cli/commands/status.js +227 -44
- package/dist/cli/commands/statusline.js +311 -114
- package/dist/cli/commands/ui.js +78 -68
- package/dist/cli/commands/undo.js +193 -18
- package/dist/cli/commands/uninstall.js +481 -0
- package/dist/cli/commands/upgrade.js +172 -79
- package/dist/cli/commands/usage.js +125 -51
- package/dist/cli/commands/version.js +21 -7
- package/dist/cli/commands/which.js +9 -24
- package/dist/cli/compile-cache.js +32 -0
- package/dist/cli/diagnostics.js +184 -0
- package/dist/cli/exit.js +65 -0
- package/dist/cli/group.js +24 -12
- package/dist/cli/help.js +62 -0
- package/dist/cli/highlight.js +236 -0
- package/dist/cli/hints.js +30 -0
- package/dist/cli/io.js +186 -1
- package/dist/cli/launch.js +52 -39
- package/dist/cli/launchers.js +161 -95
- package/dist/cli/main.js +201 -31
- package/dist/cli/optional.js +144 -0
- package/dist/cli/output.js +123 -0
- package/dist/cli/picker.js +459 -0
- package/dist/cli/registry.js +61 -6
- package/dist/cli/safe.js +87 -0
- package/dist/cli/secret-input.js +63 -0
- package/dist/cli/security.js +561 -0
- package/dist/cli/style.js +311 -0
- package/dist/cli/topics.js +142 -0
- package/dist/core/config.js +181 -23
- package/dist/core/exhausted.js +6 -8
- package/dist/core/fsutil.js +39 -0
- package/dist/core/journal.js +294 -16
- package/dist/core/manifest.js +153 -6
- package/dist/core/paths.js +14 -13
- package/dist/core/profiles.js +160 -10
- package/dist/core/resolve.js +139 -23
- package/dist/core/secrets.js +68 -23
- package/dist/core/sessions.js +13 -20
- package/dist/core/state-file.js +107 -0
- package/dist/core/update.js +31 -18
- package/dist/core/version.js +57 -10
- package/dist/demo/server.js +42 -0
- package/dist/mcpd/cache.js +23 -10
- package/dist/mcpd/calllog.js +254 -0
- package/dist/mcpd/client-names.js +46 -0
- package/dist/mcpd/connectors.js +188 -18
- package/dist/mcpd/daemon.js +472 -115
- package/dist/mcpd/folder-sets.js +42 -0
- package/dist/mcpd/gateway.js +473 -51
- package/dist/mcpd/health.js +313 -0
- package/dist/mcpd/importer.js +453 -36
- package/dist/mcpd/instances.js +453 -65
- package/dist/mcpd/jsonrpc.js +11 -1
- package/dist/mcpd/launchd.js +65 -48
- package/dist/mcpd/middleware.js +144 -0
- package/dist/mcpd/modern.js +686 -0
- package/dist/mcpd/oauth.js +845 -0
- package/dist/mcpd/peerpid.js +134 -6
- package/dist/mcpd/pressure.js +88 -0
- package/dist/mcpd/rss.js +166 -0
- package/dist/mcpd/service.js +191 -0
- package/dist/mcpd/share.js +217 -0
- package/dist/mcpd/state.js +183 -0
- package/dist/mcpd/sync.js +72 -87
- package/dist/mcpd/systemd.js +83 -0
- package/dist/mcpd/tokens.js +85 -0
- package/dist/mcpd/upstream.js +163 -46
- package/dist/statusline/cost.js +167 -44
- package/dist/statusline/input.js +78 -0
- package/dist/statusline/render.js +9 -3
- package/dist/ui/fonts.js +8 -3
- package/dist/ui/settings.js +33 -16
- package/npm-shrinkwrap.json +1102 -0
- package/package.json +10 -9
- package/dist/cli/commands/available.js +0 -18
package/dist/mcpd/instances.js
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { existsSync, realpathSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { existsSync, readlinkSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { promisify } from "node:util";
|
|
4
6
|
import { effectiveLaunch, variantKey } from "../core/manifest.js";
|
|
5
|
-
import { resolveRecord } from "../core/secrets.js";
|
|
6
7
|
import { fingerprint } from "./cache.js";
|
|
7
8
|
import { ErrorCode, rpcError } from "./jsonrpc.js";
|
|
9
|
+
import { LoginRequiredError } from "./oauth.js";
|
|
10
|
+
import { effectiveIdle } from "./pressure.js";
|
|
8
11
|
import { Upstream } from "./upstream.js";
|
|
9
|
-
|
|
12
|
+
import { pidAlive } from "../core/fsutil.js";
|
|
13
|
+
const run = promisify(execFile);
|
|
14
|
+
export function isDirectory(p) {
|
|
10
15
|
try {
|
|
11
|
-
|
|
12
|
-
return true;
|
|
16
|
+
return statSync(p).isDirectory();
|
|
13
17
|
}
|
|
14
|
-
catch
|
|
15
|
-
return
|
|
18
|
+
catch {
|
|
19
|
+
return false;
|
|
16
20
|
}
|
|
17
21
|
}
|
|
18
|
-
/**
|
|
19
|
-
|
|
22
|
+
/** Results of slow lookups, keyed and shared by concurrent callers (a client opens many sessions at once). */
|
|
23
|
+
function memo(ttlMs, compute) {
|
|
24
|
+
const cache = new Map();
|
|
25
|
+
return (key, now = Date.now()) => {
|
|
26
|
+
const hit = cache.get(key);
|
|
27
|
+
if (hit && now - hit.at < ttlMs)
|
|
28
|
+
return hit.value;
|
|
29
|
+
if (cache.size > 500)
|
|
30
|
+
cache.clear();
|
|
31
|
+
const value = compute(key);
|
|
32
|
+
cache.set(key, { value, at: now });
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Git top level of a folder (worktree-specific), else the folder itself. Async, cached for 10 s. */
|
|
37
|
+
export const projectRoot = memo(10_000, async (cwd) => {
|
|
20
38
|
try {
|
|
21
|
-
|
|
39
|
+
const { stdout } = await run("git", ["-C", cwd, "rev-parse", "--show-toplevel"], {
|
|
22
40
|
encoding: "utf8",
|
|
23
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
24
41
|
timeout: 3000,
|
|
25
|
-
})
|
|
42
|
+
});
|
|
43
|
+
return stdout.trim() || cwd;
|
|
26
44
|
}
|
|
27
45
|
catch {
|
|
28
46
|
try {
|
|
@@ -32,9 +50,32 @@ export function projectRoot(cwd) {
|
|
|
32
50
|
return cwd;
|
|
33
51
|
}
|
|
34
52
|
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
53
|
+
});
|
|
54
|
+
/** Current folder of a process: /proc on Linux, lsof elsewhere. Cached per PID for a minute. */
|
|
55
|
+
export const processCwd = memo(60_000, async (pid) => {
|
|
56
|
+
if (existsSync("/proc/self/cwd")) {
|
|
57
|
+
try {
|
|
58
|
+
return readlinkSync(`/proc/${pid}/cwd`);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const { stdout } = await run("lsof", ["-a", "-p", String(pid), "-d", "cwd", "-Fn"], {
|
|
66
|
+
encoding: "utf8",
|
|
67
|
+
timeout: 3000,
|
|
68
|
+
});
|
|
69
|
+
const line = stdout.split("\n").find((l) => l.startsWith("n"));
|
|
70
|
+
return line ? line.slice(1) : undefined;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
/** Stdio servers and OAuth remotes have variants per profile; other remotes have only "default". */
|
|
77
|
+
function variantOf(server, profile) {
|
|
78
|
+
return variantKey(server, profile);
|
|
38
79
|
}
|
|
39
80
|
export function scopeOf(server) {
|
|
40
81
|
if (server.kind === "remote")
|
|
@@ -47,8 +88,35 @@ export function descriptionFingerprint(server, variant) {
|
|
|
47
88
|
if (server.kind === "remote")
|
|
48
89
|
return fingerprint({ url: server.url, headers: Object.keys(server.headers) });
|
|
49
90
|
const launch = effectiveLaunch(server, variant);
|
|
50
|
-
return fingerprint({
|
|
91
|
+
return fingerprint({
|
|
92
|
+
c: launch.command,
|
|
93
|
+
a: launch.args,
|
|
94
|
+
e: launch.env,
|
|
95
|
+
v: variant,
|
|
96
|
+
p: server.package ?? null,
|
|
97
|
+
b: server.builtin ?? null,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Fingerprint of the whole definition (launch, scope, cwd, exclusive, idle, lease, timeouts),
|
|
102
|
+
* except who gets it and which tools they see: tool filters apply per request, so a
|
|
103
|
+
* changed filter needs no new process.
|
|
104
|
+
*/
|
|
105
|
+
export function definitionFingerprint(server) {
|
|
106
|
+
const { tools: _tools, ...rest } = server;
|
|
107
|
+
const variants = rest.kind === "stdio"
|
|
108
|
+
? Object.fromEntries(Object.entries(rest.variants).map(([k, v]) => [k, { ...v, tools: null }]))
|
|
109
|
+
: undefined;
|
|
110
|
+
return fingerprint({ ...rest, profiles: null, ...(variants ? { variants } : {}) });
|
|
111
|
+
}
|
|
112
|
+
/** A server that is gone from the manifest (or no longer proxied) while sessions still use it. */
|
|
113
|
+
class ServerGoneError extends Error {
|
|
51
114
|
}
|
|
115
|
+
const LIST_NOTIFICATIONS = [
|
|
116
|
+
["tools", "notifications/tools/list_changed"],
|
|
117
|
+
["prompts", "notifications/prompts/list_changed"],
|
|
118
|
+
["resourceTemplates", "notifications/resources/list_changed"],
|
|
119
|
+
];
|
|
52
120
|
/**
|
|
53
121
|
* Owns every running upstream. Starts them lazily, shares or isolates them by scope,
|
|
54
122
|
* hands out exclusive leases, and stops them when their owner dies or they go idle.
|
|
@@ -59,48 +127,90 @@ export class InstanceManager {
|
|
|
59
127
|
starting = new Map();
|
|
60
128
|
describing = new Map();
|
|
61
129
|
clients = new Set();
|
|
130
|
+
breakers = new Map();
|
|
131
|
+
/** Starts per instance key since mcpd started; more than one means restarts. */
|
|
132
|
+
starts = new Map();
|
|
133
|
+
manifest;
|
|
62
134
|
isAlive;
|
|
63
135
|
now;
|
|
136
|
+
/** When this manager started: descriptions relisted since then were checked against a running server. */
|
|
137
|
+
bornAt;
|
|
138
|
+
/** The memory pressure seen by the last reap. */
|
|
139
|
+
pressureLevel = "normal";
|
|
140
|
+
/** When the last reap read the pressure (manager clock); undefined before that and while the feature is off. */
|
|
141
|
+
pressureCheckedAt;
|
|
64
142
|
constructor(opts) {
|
|
65
143
|
this.opts = opts;
|
|
66
|
-
this.isAlive = opts.isAlive ??
|
|
144
|
+
this.isAlive = opts.isAlive ?? pidAlive;
|
|
67
145
|
this.now = opts.now ?? Date.now;
|
|
146
|
+
this.bornAt = this.now();
|
|
147
|
+
if (opts.manifest)
|
|
148
|
+
this.manifest = opts.manifest;
|
|
149
|
+
}
|
|
150
|
+
/** Where definitions come from (the gateway passes its manifest getter). */
|
|
151
|
+
useManifest(manifest) {
|
|
152
|
+
this.manifest ??= manifest;
|
|
68
153
|
}
|
|
69
154
|
register(client) {
|
|
70
155
|
this.clients.add(client);
|
|
71
156
|
}
|
|
72
|
-
/** A client session ended: drop it from every instance and release its leases. */
|
|
157
|
+
/** A client session ended: cancel its calls, drop it from every instance and release its leases. */
|
|
73
158
|
detach(client) {
|
|
74
159
|
this.clients.delete(client);
|
|
75
160
|
for (const inst of this.instances.values()) {
|
|
76
|
-
|
|
77
|
-
|
|
161
|
+
const cancelled = inst.upstream.forgetClient(client);
|
|
162
|
+
if (cancelled)
|
|
163
|
+
this.opts.log?.(`cancelled ${cancelled} call(s) of a closed session on ${inst.upstream.label}`);
|
|
164
|
+
inst.clients.delete(client);
|
|
165
|
+
for (const [uri, subs] of inst.subscriptions) {
|
|
166
|
+
if (subs.delete(client) && subs.size === 0) {
|
|
167
|
+
inst.subscriptions.delete(uri);
|
|
168
|
+
void inst.upstream.call("resources/unsubscribe", { uri }, undefined, undefined, 30_000);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
78
171
|
if (inst.holder === client) {
|
|
79
172
|
delete inst.holder;
|
|
80
173
|
delete inst.holderSince;
|
|
81
174
|
}
|
|
82
175
|
}
|
|
83
176
|
}
|
|
177
|
+
/** The current definition of the server a session talks to. */
|
|
178
|
+
serverOf(ctx) {
|
|
179
|
+
const def = this.manifest?.().servers[ctx.serverName];
|
|
180
|
+
if (!def || (def.kind === "remote" && !def.proxy)) {
|
|
181
|
+
throw new ServerGoneError(`${ctx.serverName} is no longer served by mcpd (removed from mcp.toml); restart the session to drop it`);
|
|
182
|
+
}
|
|
183
|
+
return def;
|
|
184
|
+
}
|
|
84
185
|
keyOf(ctx) {
|
|
85
|
-
const s = ctx
|
|
186
|
+
const s = this.serverOf(ctx);
|
|
86
187
|
const variant = variantOf(s, ctx.profile);
|
|
87
188
|
const descKey = `${s.name}@${variant}`;
|
|
88
189
|
const scope = scopeOf(s);
|
|
190
|
+
const defFp = definitionFingerprint(s);
|
|
191
|
+
const base = { descKey, variant, server: s, scope, defFp };
|
|
192
|
+
const head = `${s.name}|${variant}|${defFp}`;
|
|
89
193
|
if (scope === "shared")
|
|
90
|
-
return { key:
|
|
194
|
+
return { ...base, key: head, workdir: this.opts.home, workdirKnown: false };
|
|
91
195
|
if (scope === "project") {
|
|
92
|
-
const root = ctx.
|
|
93
|
-
return {
|
|
196
|
+
const root = ctx.projectRoot ?? ctx.cwd;
|
|
197
|
+
return {
|
|
198
|
+
...base,
|
|
199
|
+
key: `${head}|root:${root ?? this.opts.home}`,
|
|
200
|
+
workdir: root ?? this.opts.home,
|
|
201
|
+
workdirKnown: root !== undefined,
|
|
202
|
+
};
|
|
94
203
|
}
|
|
95
|
-
const workdir = ctx.cwd
|
|
204
|
+
const workdir = ctx.cwd ?? this.opts.home;
|
|
205
|
+
const workdirKnown = ctx.cwd !== undefined;
|
|
96
206
|
if (ctx.pid)
|
|
97
|
-
return { key: `${
|
|
98
|
-
return { key: `${
|
|
207
|
+
return { ...base, key: `${head}|pid:${ctx.pid}`, workdir, workdirKnown, pid: ctx.pid };
|
|
208
|
+
return { ...base, key: `${head}|sid:${ctx.sessionId}`, workdir, workdirKnown };
|
|
99
209
|
}
|
|
100
210
|
/** Description for initialize and the list methods: cached, else from a (kept) running instance. */
|
|
101
211
|
async describe(client) {
|
|
102
|
-
const { descKey, variant } = this.keyOf(client.ctx);
|
|
103
|
-
const fp = descriptionFingerprint(
|
|
212
|
+
const { descKey, variant, server } = this.keyOf(client.ctx);
|
|
213
|
+
const fp = descriptionFingerprint(server, variant);
|
|
104
214
|
const cached = this.opts.cache.get(descKey, fp);
|
|
105
215
|
if (cached)
|
|
106
216
|
return cached;
|
|
@@ -108,13 +218,22 @@ export class InstanceManager {
|
|
|
108
218
|
if (!p) {
|
|
109
219
|
p = (async () => {
|
|
110
220
|
const inst = await this.instanceFor(client, false);
|
|
111
|
-
return this.
|
|
221
|
+
return inst.described ?? this.relist(inst);
|
|
112
222
|
})();
|
|
113
223
|
this.describing.set(descKey, p);
|
|
114
224
|
p.finally(() => this.describing.delete(descKey)).catch(() => { });
|
|
115
225
|
}
|
|
116
226
|
return p;
|
|
117
227
|
}
|
|
228
|
+
/** The cached description for this client's server, without starting anything. */
|
|
229
|
+
cachedDescription(client) {
|
|
230
|
+
const { descKey, variant, server } = this.keyOf(client.ctx);
|
|
231
|
+
return this.opts.cache.get(descKey, descriptionFingerprint(server, variant));
|
|
232
|
+
}
|
|
233
|
+
/** Whether a description was relisted from a running server since this manager started. */
|
|
234
|
+
verified(d) {
|
|
235
|
+
return d.updatedAt >= this.bornAt;
|
|
236
|
+
}
|
|
118
237
|
/** The instance that serves a real request from this client (starts it, checks leases). */
|
|
119
238
|
async acquire(client) {
|
|
120
239
|
let inst;
|
|
@@ -122,9 +241,11 @@ export class InstanceManager {
|
|
|
122
241
|
inst = await this.instanceFor(client, true);
|
|
123
242
|
}
|
|
124
243
|
catch (err) {
|
|
125
|
-
|
|
244
|
+
if (err instanceof ServerGoneError)
|
|
245
|
+
return rpcError(ErrorCode.Unavailable, err.message);
|
|
246
|
+
return rpcError(ErrorCode.Unavailable, `could not start ${client.ctx.serverName}: ${err.message}`);
|
|
126
247
|
}
|
|
127
|
-
const s =
|
|
248
|
+
const s = inst.server;
|
|
128
249
|
if (s.kind === "stdio" && s.exclusive) {
|
|
129
250
|
this.expireLease(inst);
|
|
130
251
|
if (inst.holder && inst.holder !== client) {
|
|
@@ -139,12 +260,67 @@ export class InstanceManager {
|
|
|
139
260
|
inst.clients.add(client);
|
|
140
261
|
return inst;
|
|
141
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Relay one client request: start or join the instance, apply the server's call_timeout,
|
|
265
|
+
* and retry once on a fresh instance when a remote server forgot our session. `cancelled`
|
|
266
|
+
* says whether the client cancelled the request while the instance was still starting: the
|
|
267
|
+
* upstream has no call to cancel yet, so the request is dropped here instead of sent.
|
|
268
|
+
* `onVariant` learns the variant of the instance that serves it (the call log's).
|
|
269
|
+
* A tools/call comes here through the call middleware (middleware.ts), never directly.
|
|
270
|
+
*/
|
|
271
|
+
async forward(client, method, params, originId, cancelled, onVariant) {
|
|
272
|
+
for (let attempt = 0;; attempt++) {
|
|
273
|
+
const inst = await this.acquire(client);
|
|
274
|
+
if ("error" in inst || "result" in inst)
|
|
275
|
+
return inst;
|
|
276
|
+
onVariant?.(inst.variant);
|
|
277
|
+
// Nothing awaits between this check and the call below, so a later cancellation finds it upstream.
|
|
278
|
+
if (cancelled?.())
|
|
279
|
+
return rpcError(ErrorCode.RequestCancelled, "cancelled by the client");
|
|
280
|
+
const timeout = inst.server.callTimeout ? inst.server.callTimeout * 1000 : undefined;
|
|
281
|
+
const out = await inst.upstream.call(method, params, client, originId, timeout);
|
|
282
|
+
if ("error" in out && inst.upstream.sessionLost && attempt === 0) {
|
|
283
|
+
this.opts.log?.(`${inst.upstream.label}: remote session expired; reconnecting and retrying ${method}`);
|
|
284
|
+
if (this.instances.get(inst.key) === inst)
|
|
285
|
+
this.instances.delete(inst.key);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
return out;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** resources/subscribe: one upstream subscription per URI and instance, many client subscribers. */
|
|
292
|
+
async subscribe(client, uri, originId) {
|
|
293
|
+
const inst = await this.acquire(client);
|
|
294
|
+
if ("error" in inst || "result" in inst)
|
|
295
|
+
return inst;
|
|
296
|
+
let subs = inst.subscriptions.get(uri);
|
|
297
|
+
if (!subs) {
|
|
298
|
+
const out = await inst.upstream.call("resources/subscribe", { uri }, client, originId);
|
|
299
|
+
if ("error" in out)
|
|
300
|
+
return out;
|
|
301
|
+
subs = inst.subscriptions.get(uri) ?? new Set();
|
|
302
|
+
inst.subscriptions.set(uri, subs);
|
|
303
|
+
}
|
|
304
|
+
subs.add(client);
|
|
305
|
+
return { result: {} };
|
|
306
|
+
}
|
|
307
|
+
async unsubscribe(client, uri, originId) {
|
|
308
|
+
const inst = this.existing(client);
|
|
309
|
+
const subs = inst?.subscriptions.get(uri);
|
|
310
|
+
if (!inst || !subs?.delete(client))
|
|
311
|
+
return { result: {} };
|
|
312
|
+
if (subs.size > 0)
|
|
313
|
+
return { result: {} };
|
|
314
|
+
inst.subscriptions.delete(uri);
|
|
315
|
+
return inst.upstream.call("resources/unsubscribe", { uri }, client, originId);
|
|
316
|
+
}
|
|
142
317
|
/** Instance already serving this client, if any (no start). */
|
|
143
318
|
existing(client) {
|
|
144
319
|
const inst = this.instances.get(this.keyOf(client.ctx).key);
|
|
145
320
|
return inst && !inst.upstream.isClosed ? inst : undefined;
|
|
146
321
|
}
|
|
147
|
-
|
|
322
|
+
/** List tools, prompts and resource templates again; store them and tell clients what changed. */
|
|
323
|
+
async relist(inst, quiet = []) {
|
|
148
324
|
const init = inst.upstream.init;
|
|
149
325
|
if (!init)
|
|
150
326
|
throw new Error(`${inst.server.name} is not initialized`);
|
|
@@ -160,13 +336,59 @@ export class InstanceManager {
|
|
|
160
336
|
d.tools = await inst.upstream.listAll("tools/list", "tools");
|
|
161
337
|
if (caps.prompts)
|
|
162
338
|
d.prompts = await inst.upstream.listAll("prompts/list", "prompts");
|
|
163
|
-
if (caps.resources)
|
|
164
|
-
d.
|
|
165
|
-
|
|
166
|
-
|
|
339
|
+
if (caps.resources)
|
|
340
|
+
d.resourceTemplates = await inst.upstream
|
|
341
|
+
.listAll("resources/templates/list", "resourceTemplates")
|
|
342
|
+
.catch(() => []);
|
|
343
|
+
const prev = this.opts.cache.peek(inst.descKey);
|
|
167
344
|
this.opts.cache.set(inst.descKey, d);
|
|
345
|
+
if (prev) {
|
|
346
|
+
const changed = LIST_NOTIFICATIONS.filter(([k, m]) => !quiet.includes(m) && JSON.stringify(prev[k] ?? null) !== JSON.stringify(d[k] ?? null));
|
|
347
|
+
if (changed.length) {
|
|
348
|
+
this.opts.log?.(`${inst.upstream.label}: ${changed.map(([k]) => k).join(", ")} changed; notifying clients`);
|
|
349
|
+
for (const c of this.clientsOf(inst.descKey))
|
|
350
|
+
for (const [, m] of changed)
|
|
351
|
+
c.notify(m, undefined);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
168
354
|
return d;
|
|
169
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* `POST /refresh/<server>`: relist running instances (clients get list_changed when it
|
|
358
|
+
* changed) and forget the cached description of variants that are not running.
|
|
359
|
+
*/
|
|
360
|
+
async refresh(name) {
|
|
361
|
+
const def = this.manifest?.().servers[name];
|
|
362
|
+
const variants = def ? ["default", ...Object.keys(def.variants)] : ["default"];
|
|
363
|
+
const out = { refreshed: [], forgotten: [], errors: {} };
|
|
364
|
+
for (const variant of variants) {
|
|
365
|
+
const descKey = `${name}@${variant}`;
|
|
366
|
+
const running = this.list().find((i) => i.descKey === descKey && !i.upstream.isClosed && i.upstream.init);
|
|
367
|
+
if (running) {
|
|
368
|
+
try {
|
|
369
|
+
await this.relist(running);
|
|
370
|
+
out.refreshed.push(descKey);
|
|
371
|
+
}
|
|
372
|
+
catch (err) {
|
|
373
|
+
out.errors[descKey] = err.message;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else if (this.opts.cache.forget(descKey)) {
|
|
377
|
+
out.forgotten.push(descKey);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return out;
|
|
381
|
+
}
|
|
382
|
+
clientsOf(descKey) {
|
|
383
|
+
return [...this.clients].filter((c) => {
|
|
384
|
+
try {
|
|
385
|
+
return this.keyOf(c.ctx).descKey === descKey;
|
|
386
|
+
}
|
|
387
|
+
catch {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|
|
170
392
|
async instanceFor(client, join) {
|
|
171
393
|
const k = this.keyOf(client.ctx);
|
|
172
394
|
const existing = this.instances.get(k.key);
|
|
@@ -177,7 +399,7 @@ export class InstanceManager {
|
|
|
177
399
|
}
|
|
178
400
|
let p = this.starting.get(k.key);
|
|
179
401
|
if (!p) {
|
|
180
|
-
p = this.
|
|
402
|
+
p = this.startGuarded(client.ctx, k);
|
|
181
403
|
this.starting.set(k.key, p);
|
|
182
404
|
p.finally(() => this.starting.delete(k.key)).catch(() => { });
|
|
183
405
|
}
|
|
@@ -186,46 +408,120 @@ export class InstanceManager {
|
|
|
186
408
|
inst.clients.add(client);
|
|
187
409
|
return inst;
|
|
188
410
|
}
|
|
411
|
+
/** Circuit breaker: after repeated start failures, answer at once with the log tail and back off. */
|
|
412
|
+
async startGuarded(ctx, k) {
|
|
413
|
+
const bkey = `${k.server.name}|${k.variant}|${k.defFp}`;
|
|
414
|
+
const b = this.breakers.get(bkey);
|
|
415
|
+
const now = this.now();
|
|
416
|
+
if (b && b.until > now)
|
|
417
|
+
throw new Error(this.breakerMessage(k, b, now));
|
|
418
|
+
try {
|
|
419
|
+
const inst = await this.start(ctx, k);
|
|
420
|
+
this.breakers.delete(bkey);
|
|
421
|
+
return inst;
|
|
422
|
+
}
|
|
423
|
+
catch (err) {
|
|
424
|
+
// A missing OAuth login is not a crash: no backoff, so the call after `turno mcp login` works.
|
|
425
|
+
if (err instanceof LoginRequiredError)
|
|
426
|
+
throw err;
|
|
427
|
+
const message = err.message;
|
|
428
|
+
const failures = (b?.failures ?? 0) + 1;
|
|
429
|
+
const threshold = this.opts.breakerThreshold ?? 3;
|
|
430
|
+
const base = this.opts.breakerBaseMs ?? 5000;
|
|
431
|
+
const until = failures >= threshold ? this.now() + Math.min(base * 2 ** (failures - threshold), 5 * 60_000) : 0;
|
|
432
|
+
this.breakers.set(bkey, { failures, until, lastError: message });
|
|
433
|
+
if (failures >= threshold)
|
|
434
|
+
this.opts.log?.(`${k.server.name}: ${failures} failed starts in a row; next try in ${Math.round((until - this.now()) / 1000)}s`);
|
|
435
|
+
throw err;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
breakerMessage(k, b, now) {
|
|
439
|
+
const tail = this.opts.connector.logTail?.(k.server, k.variant, 10);
|
|
440
|
+
return (`${k.server.name} failed to start ${b.failures} times in a row (last error: ${b.lastError}); ` +
|
|
441
|
+
`turno tries again in ${Math.max(1, Math.round((b.until - now) / 1000))}s.` +
|
|
442
|
+
(tail ? `\nLast lines of its log:\n${tail}` : ""));
|
|
443
|
+
}
|
|
444
|
+
/** Client capabilities to declare upstream: only what the client behind it can honor. */
|
|
445
|
+
upstreamCapabilities(ctx, scope) {
|
|
446
|
+
const cc = ctx.clientCapabilities;
|
|
447
|
+
const caps = {};
|
|
448
|
+
// Shared instances serve many folders: there is no single right answer to roots/list.
|
|
449
|
+
if (scope !== "shared")
|
|
450
|
+
caps.roots = { listChanged: true };
|
|
451
|
+
if (cc === undefined)
|
|
452
|
+
caps.elicitation = { form: {} };
|
|
453
|
+
else if (cc.elicitation)
|
|
454
|
+
caps.elicitation = cc.elicitation;
|
|
455
|
+
if (cc?.sampling)
|
|
456
|
+
caps.sampling = cc.sampling;
|
|
457
|
+
return caps;
|
|
458
|
+
}
|
|
189
459
|
async start(ctx, k) {
|
|
190
|
-
const server =
|
|
191
|
-
const { transport, pid } = await this.opts.connector.connect(server, k.variant, ctx, k.workdir);
|
|
460
|
+
const server = k.server;
|
|
461
|
+
const { transport, pid, warnings } = await this.opts.connector.connect(server, k.variant, ctx, k.workdir);
|
|
462
|
+
for (const w of warnings ?? [])
|
|
463
|
+
this.opts.log?.(`warning: ${w}`);
|
|
192
464
|
let inst;
|
|
193
465
|
const upstream = new Upstream({
|
|
194
466
|
label: `${server.name}${k.variant === "default" ? "" : `@${k.variant}`}`,
|
|
195
467
|
connect: async () => transport,
|
|
196
|
-
pid,
|
|
468
|
+
...(pid ? { pid } : {}),
|
|
469
|
+
capabilities: this.upstreamCapabilities(ctx, k.scope),
|
|
470
|
+
localRoots: () => inst && inst.scope !== "shared" && inst.workdirKnown
|
|
471
|
+
? { roots: [{ uri: pathToFileURL(inst.workdir).href, name: basename(inst.workdir) || inst.workdir }] }
|
|
472
|
+
: undefined,
|
|
473
|
+
attached: () => (inst ? [...inst.clients] : []),
|
|
197
474
|
onBroadcast: (method, params) => this.onBroadcast(inst, method, params),
|
|
198
475
|
onClose: (reason) => {
|
|
199
476
|
if (this.instances.get(k.key) === inst)
|
|
200
477
|
this.instances.delete(k.key);
|
|
201
478
|
this.opts.log?.(`stopped ${inst.upstream.label} (${reason})`);
|
|
202
479
|
},
|
|
480
|
+
...(this.opts.log ? { log: this.opts.log } : {}),
|
|
203
481
|
});
|
|
204
482
|
inst = {
|
|
205
483
|
key: k.key,
|
|
206
484
|
descKey: k.descKey,
|
|
207
485
|
server,
|
|
486
|
+
defFp: k.defFp,
|
|
487
|
+
scope: k.scope,
|
|
208
488
|
variant: k.variant,
|
|
209
489
|
upstream,
|
|
210
490
|
clients: new Set(),
|
|
211
491
|
workdir: k.workdir,
|
|
492
|
+
workdirKnown: k.workdirKnown,
|
|
493
|
+
subscriptions: new Map(),
|
|
212
494
|
...(k.pid ? { pid: k.pid } : {}),
|
|
213
495
|
};
|
|
214
496
|
await upstream.start();
|
|
215
497
|
this.instances.set(k.key, inst);
|
|
498
|
+
this.starts.set(k.key, (this.starts.get(k.key) ?? 0) + 1);
|
|
216
499
|
this.opts.log?.(`started ${upstream.label} pid=${upstream.pid ?? "-"} key=${k.key}`);
|
|
500
|
+
// Every real start relists: a server updated in place (npx, uvx, a new build) shows its new tools.
|
|
501
|
+
inst.described = this.relist(inst);
|
|
502
|
+
inst.described.catch((err) => this.opts.log?.(`relist ${upstream.label} failed: ${err.message}`));
|
|
217
503
|
return inst;
|
|
218
504
|
}
|
|
219
505
|
onBroadcast(inst, method, params) {
|
|
506
|
+
if (method === "notifications/resources/updated") {
|
|
507
|
+
const uri = typeof params?.uri === "string" ? params.uri : "";
|
|
508
|
+
for (const c of inst.subscriptions.get(uri) ?? [])
|
|
509
|
+
c.notify(method, params);
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
if (method === "notifications/resources/list_changed") {
|
|
513
|
+
// Resources are not cached: tell this instance's clients (and listen streams), then refresh the templates quietly.
|
|
514
|
+
const targets = new Set(inst.clients);
|
|
515
|
+
for (const c of this.clientsOf(inst.descKey))
|
|
516
|
+
if (c.listening)
|
|
517
|
+
targets.add(c);
|
|
518
|
+
for (const c of targets)
|
|
519
|
+
c.notify(method, params);
|
|
520
|
+
this.relist(inst, [method]).catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
220
523
|
if (method.endsWith("/list_changed")) {
|
|
221
|
-
this.
|
|
222
|
-
.then(() => {
|
|
223
|
-
for (const c of this.clients) {
|
|
224
|
-
if (this.keyOf(c.ctx).descKey === inst.descKey)
|
|
225
|
-
c.notify(method, params);
|
|
226
|
-
}
|
|
227
|
-
})
|
|
228
|
-
.catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
|
|
524
|
+
this.relist(inst).catch((err) => this.opts.log?.(`refresh ${inst.upstream.label} failed: ${err.message}`));
|
|
229
525
|
return;
|
|
230
526
|
}
|
|
231
527
|
for (const c of inst.clients)
|
|
@@ -237,29 +533,106 @@ export class InstanceManager {
|
|
|
237
533
|
return;
|
|
238
534
|
const h = inst.holder;
|
|
239
535
|
const dead = h.ctx.pid ? !this.isAlive(h.ctx.pid) : false;
|
|
240
|
-
|
|
536
|
+
// A long call keeps the lease: the holder is busy, not idle.
|
|
537
|
+
const idle = inst.upstream.inflightFor(h) === 0 && this.now() - h.lastSeen > s.lease * 1000;
|
|
241
538
|
if (dead || idle || !this.clients.has(h)) {
|
|
242
539
|
delete inst.holder;
|
|
243
540
|
delete inst.holderSince;
|
|
244
541
|
}
|
|
245
542
|
}
|
|
246
|
-
/**
|
|
543
|
+
/** Whether an instance still matches the manifest (false once its definition changed or went away). */
|
|
544
|
+
current(inst) {
|
|
545
|
+
if (!this.manifest)
|
|
546
|
+
return true;
|
|
547
|
+
const def = this.manifest().servers[inst.server.name];
|
|
548
|
+
return def !== undefined && definitionFingerprint(def) === inst.defFp;
|
|
549
|
+
}
|
|
550
|
+
/** `[mcpd] idle_under_pressure` changed in config.toml: applies from the next reap. */
|
|
551
|
+
setIdleUnderPressure(seconds) {
|
|
552
|
+
this.opts.idleUnderPressure = seconds;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* The memory pressure for /status and `turno mcp top`: the level the last reap
|
|
556
|
+
* read (null while the feature is off or before the first read), the configured limit, and
|
|
557
|
+
* the idle cap that applies now (null when servers keep their own limits).
|
|
558
|
+
*/
|
|
559
|
+
memoryPressure() {
|
|
560
|
+
const under = this.opts.idleUnderPressure ?? 0;
|
|
561
|
+
const watched = this.opts.pressure !== undefined && under > 0 && this.pressureCheckedAt !== undefined;
|
|
562
|
+
const level = watched ? this.pressureLevel : null;
|
|
563
|
+
const cap = level ? effectiveIdle(Number.POSITIVE_INFINITY, level, under) : Number.POSITIVE_INFINITY;
|
|
564
|
+
return {
|
|
565
|
+
level,
|
|
566
|
+
checkedAt: watched ? (this.pressureCheckedAt ?? null) : null,
|
|
567
|
+
idleUnderPressure: under,
|
|
568
|
+
idleCap: Number.isFinite(cap) ? cap : null,
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
/** Read memory pressure; a failing probe counts as normal. Logs when the level changes. */
|
|
572
|
+
async readPressure() {
|
|
573
|
+
let level = "normal";
|
|
574
|
+
if (this.opts.pressure && (this.opts.idleUnderPressure ?? 0) > 0) {
|
|
575
|
+
try {
|
|
576
|
+
level = await this.opts.pressure();
|
|
577
|
+
}
|
|
578
|
+
catch {
|
|
579
|
+
level = "normal";
|
|
580
|
+
}
|
|
581
|
+
this.pressureCheckedAt = this.now();
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
this.pressureCheckedAt = undefined;
|
|
585
|
+
}
|
|
586
|
+
if (level !== this.pressureLevel) {
|
|
587
|
+
const under = this.opts.idleUnderPressure ?? 0;
|
|
588
|
+
this.opts.log?.(level === "normal"
|
|
589
|
+
? "memory pressure is normal again: servers keep their own idle limits"
|
|
590
|
+
: `memory pressure is ${level}: idle servers stop after ${effectiveIdle(Number.POSITIVE_INFINITY, level, under)}s at most`);
|
|
591
|
+
this.pressureLevel = level;
|
|
592
|
+
}
|
|
593
|
+
return level;
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Stop instances whose owner died, whose definition changed, or that sat idle too long; forget
|
|
597
|
+
* dead clients. Under memory pressure the idle limit shrinks, and under critical
|
|
598
|
+
* pressure it applies to session servers too.
|
|
599
|
+
*/
|
|
247
600
|
async reap() {
|
|
248
601
|
const stopped = [];
|
|
602
|
+
const level = await this.readPressure();
|
|
249
603
|
for (const c of [...this.clients]) {
|
|
250
604
|
if (c.ctx.pid && !this.isAlive(c.ctx.pid))
|
|
251
605
|
this.detach(c);
|
|
252
606
|
}
|
|
253
607
|
for (const inst of [...this.instances.values()]) {
|
|
254
608
|
this.expireLease(inst);
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
const ownerDead = inst.pid !== undefined && !
|
|
258
|
-
const
|
|
259
|
-
|
|
609
|
+
const quiet = inst.upstream.inflight === 0;
|
|
610
|
+
const ownerAlive = inst.pid !== undefined && this.isAlive(inst.pid);
|
|
611
|
+
const ownerDead = inst.pid !== undefined && !ownerAlive;
|
|
612
|
+
const stale = quiet && !this.current(inst);
|
|
613
|
+
// A session server lives as long as its session: its state (a browser, a login) is the
|
|
614
|
+
// point. Under critical memory pressure a quiet one stops like any other; the next call
|
|
615
|
+
// starts it again.
|
|
616
|
+
const idleApplies = !(inst.scope === "session" && ownerAlive && level !== "critical");
|
|
617
|
+
const limit = effectiveIdle(inst.server.idle, level, this.opts.idleUnderPressure ?? 0);
|
|
618
|
+
const idleFor = this.now() - inst.upstream.lastActivity;
|
|
619
|
+
const idle = idleApplies && quiet && idleFor > limit * 1000;
|
|
620
|
+
if (ownerDead || stale || idle) {
|
|
621
|
+
// Only pressure stops a session server whose launcher lives, whatever its own limit.
|
|
622
|
+
const ownLimit = idleFor > inst.server.idle * 1000 && !(inst.scope === "session" && ownerAlive);
|
|
623
|
+
const why = ownerDead
|
|
624
|
+
? "owner exited"
|
|
625
|
+
: stale
|
|
626
|
+
? "definition changed"
|
|
627
|
+
: ownLimit
|
|
628
|
+
? "idle"
|
|
629
|
+
: "idle (memory pressure)";
|
|
260
630
|
this.instances.delete(inst.key);
|
|
631
|
+
// That key never comes back (dead PID, old definition): forget its start count.
|
|
632
|
+
if (ownerDead || stale)
|
|
633
|
+
this.starts.delete(inst.key);
|
|
261
634
|
await inst.upstream.close();
|
|
262
|
-
stopped.push(`${inst.upstream.label}: ${
|
|
635
|
+
stopped.push(`${inst.upstream.label}: ${why}`);
|
|
263
636
|
}
|
|
264
637
|
}
|
|
265
638
|
return stopped;
|
|
@@ -267,9 +640,21 @@ export class InstanceManager {
|
|
|
267
640
|
list() {
|
|
268
641
|
return [...this.instances.values()];
|
|
269
642
|
}
|
|
643
|
+
/** How many times the instance under `key` started again after its first start. */
|
|
644
|
+
restarts(key) {
|
|
645
|
+
return Math.max(0, (this.starts.get(key) ?? 0) - 1);
|
|
646
|
+
}
|
|
270
647
|
clientCount() {
|
|
271
648
|
return this.clients.size;
|
|
272
649
|
}
|
|
650
|
+
/** `turno mcp logout` (through `POST /logout/<server>`): the next request reads the secret store. */
|
|
651
|
+
forgetLogin(server, variants) {
|
|
652
|
+
this.opts.connector.forgetLogin?.(server, variants);
|
|
653
|
+
}
|
|
654
|
+
/** At shutdown: save OAuth tokens still only in memory; the labels whose tokens are lost. */
|
|
655
|
+
async flushLogins() {
|
|
656
|
+
return (await this.opts.connector.flushLogins?.()) ?? [];
|
|
657
|
+
}
|
|
273
658
|
async stopAll() {
|
|
274
659
|
const all = [...this.instances.values()];
|
|
275
660
|
this.instances.clear();
|
|
@@ -285,19 +670,22 @@ export class InstanceManager {
|
|
|
285
670
|
}
|
|
286
671
|
}
|
|
287
672
|
/**
|
|
288
|
-
* Resolve a command against an installed package (turno mcp install). A declared package
|
|
289
|
-
*
|
|
673
|
+
* Resolve a command against an installed package (turno mcp install). A declared package that
|
|
674
|
+
* is not installed still runs through `npx -y -p <package> <command>`, with a warning: `mcp
|
|
675
|
+
* import` writes `package` and `mcp install` is a separate step, so failing here would break
|
|
676
|
+
* imported setups.
|
|
290
677
|
*/
|
|
291
678
|
export function resolveCommand(server, command, args, serversDir) {
|
|
292
679
|
if (server.package) {
|
|
293
680
|
const local = join(serversDir, server.name, "node_modules", ".bin", command);
|
|
294
681
|
if (existsSync(local))
|
|
295
682
|
return { command: local, args };
|
|
296
|
-
return {
|
|
683
|
+
return {
|
|
684
|
+
command: "npx",
|
|
685
|
+
args: ["-y", "-p", server.package, command, ...args],
|
|
686
|
+
warning: `${server.name}: package ${server.package} is not installed, so npx resolves and runs it at every start. Run \`turno mcp install ${server.name}\` to install it once.`,
|
|
687
|
+
};
|
|
297
688
|
}
|
|
298
689
|
return { command, args };
|
|
299
690
|
}
|
|
300
|
-
export async function resolvedEnv(server, variant, resolve) {
|
|
301
|
-
return resolveRecord(effectiveLaunch(server, variant).env, resolve);
|
|
302
|
-
}
|
|
303
691
|
//# sourceMappingURL=instances.js.map
|