turno-cli 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +335 -2
- package/dist/accounts/audit.js +54 -0
- package/dist/accounts/builtin.js +214 -0
- package/dist/accounts/delegate.js +185 -0
- package/dist/accounts/registry.js +190 -0
- package/dist/agents/index.js +101 -0
- package/dist/agents/snippets.js +64 -0
- package/dist/cli/args.js +47 -0
- package/dist/cli/commands/accounts.js +216 -0
- package/dist/cli/commands/auto.js +44 -0
- package/dist/cli/commands/available.js +18 -0
- package/dist/cli/commands/browser.js +175 -0
- package/dist/cli/commands/complete.js +112 -0
- package/dist/cli/commands/completion.js +57 -0
- package/dist/cli/commands/config.js +168 -0
- package/dist/cli/commands/doctor.js +324 -0
- package/dist/cli/commands/exhausted.js +40 -0
- package/dist/cli/commands/handoff.js +118 -0
- package/dist/cli/commands/hook.js +63 -0
- package/dist/cli/commands/identity.js +114 -0
- package/dist/cli/commands/limit.js +102 -0
- package/dist/cli/commands/mcp.js +350 -0
- package/dist/cli/commands/mcpd.js +161 -0
- package/dist/cli/commands/migrate.js +176 -0
- package/dist/cli/commands/open.js +60 -0
- package/dist/cli/commands/profile.js +259 -0
- package/dist/cli/commands/resolve.js +23 -0
- package/dist/cli/commands/run.js +15 -0
- package/dist/cli/commands/secret.js +123 -0
- package/dist/cli/commands/session.js +36 -0
- package/dist/cli/commands/sessions.js +27 -0
- package/dist/cli/commands/setup.js +224 -0
- package/dist/cli/commands/status.js +101 -0
- package/dist/cli/commands/statusline.js +275 -0
- package/dist/cli/commands/ui.js +276 -0
- package/dist/cli/commands/undo.js +67 -0
- package/dist/cli/commands/update-check.js +22 -0
- package/dist/cli/commands/upgrade.js +162 -0
- package/dist/cli/commands/usage.js +167 -0
- package/dist/cli/commands/version.js +27 -0
- package/dist/cli/commands/which.js +28 -0
- package/dist/cli/group.js +50 -0
- package/dist/cli/io.js +22 -0
- package/dist/cli/launch.js +98 -0
- package/dist/cli/launchers.js +200 -0
- package/dist/cli/main.js +94 -0
- package/dist/cli/registry.js +77 -0
- package/dist/core/config-edit.js +42 -0
- package/dist/core/config.js +137 -0
- package/dist/core/exhausted.js +30 -0
- package/dist/core/journal.js +185 -0
- package/dist/core/manifest.js +177 -0
- package/dist/core/paths.js +54 -0
- package/dist/core/profiles.js +61 -0
- package/dist/core/ratelimit.js +39 -0
- package/dist/core/resolve.js +78 -0
- package/dist/core/secrets.js +44 -0
- package/dist/core/sessions.js +173 -0
- package/dist/core/toml-edit.js +152 -0
- package/dist/core/update.js +102 -0
- package/dist/core/version.js +109 -0
- package/dist/mcpd/cache.js +41 -0
- package/dist/mcpd/connectors.js +63 -0
- package/dist/mcpd/daemon.js +163 -0
- package/dist/mcpd/gateway.js +308 -0
- package/dist/mcpd/importer.js +160 -0
- package/dist/mcpd/instances.js +303 -0
- package/dist/mcpd/jsonrpc.js +19 -0
- package/dist/mcpd/launchd.js +86 -0
- package/dist/mcpd/peerpid.js +25 -0
- package/dist/mcpd/sync.js +208 -0
- package/dist/mcpd/upstream.js +232 -0
- package/dist/statusline/cost.js +135 -0
- package/dist/statusline/render.js +246 -0
- package/dist/ui/cmap.js +57 -0
- package/dist/ui/fonts.js +107 -0
- package/dist/ui/settings.js +69 -0
- package/dist/ui/terminal-id.js +30 -0
- package/dist/ui/terminals.js +163 -0
- package/package.json +69 -3
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { randomUUID, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
4
|
+
import { LATEST_PROTOCOL_VERSION, SUPPORTED_PROTOCOL_VERSIONS } from "@modelcontextprotocol/sdk/types.js";
|
|
5
|
+
import { scopeOf } from "./instances.js";
|
|
6
|
+
import { ErrorCode, isNotification, isRequest, isResponse, rpcError, toResponse } from "./jsonrpc.js";
|
|
7
|
+
const LIST_METHODS = {
|
|
8
|
+
"tools/list": { cap: "tools", key: "tools", field: "tools" },
|
|
9
|
+
"prompts/list": { cap: "prompts", key: "prompts", field: "prompts" },
|
|
10
|
+
"resources/list": { cap: "resources", key: "resources", field: "resources" },
|
|
11
|
+
"resources/templates/list": { cap: "resources", key: "resourceTemplates", field: "resourceTemplates" },
|
|
12
|
+
};
|
|
13
|
+
function header(req, name) {
|
|
14
|
+
const v = req.headers[name];
|
|
15
|
+
return Array.isArray(v) ? v[0] : v;
|
|
16
|
+
}
|
|
17
|
+
function json(res, status, body) {
|
|
18
|
+
const text = JSON.stringify(body);
|
|
19
|
+
res.writeHead(status, { "content-type": "application/json", "content-length": Buffer.byteLength(text) });
|
|
20
|
+
res.end(text);
|
|
21
|
+
}
|
|
22
|
+
function safeEqual(a, b) {
|
|
23
|
+
const ab = Buffer.from(a);
|
|
24
|
+
const bb = Buffer.from(b);
|
|
25
|
+
return ab.length === bb.length && timingSafeEqual(ab, bb);
|
|
26
|
+
}
|
|
27
|
+
export function negotiateVersion(requested) {
|
|
28
|
+
return typeof requested === "string" && SUPPORTED_PROTOCOL_VERSIONS.includes(requested) ? requested : LATEST_PROTOCOL_VERSION;
|
|
29
|
+
}
|
|
30
|
+
/** One Claude Code MCP session against one server path. */
|
|
31
|
+
class ClientSession {
|
|
32
|
+
ctx;
|
|
33
|
+
transport;
|
|
34
|
+
gw;
|
|
35
|
+
id = "";
|
|
36
|
+
lastSeen = Date.now();
|
|
37
|
+
nextId = 1;
|
|
38
|
+
waiting = new Map();
|
|
39
|
+
constructor(ctx, transport, gw) {
|
|
40
|
+
this.ctx = ctx;
|
|
41
|
+
this.transport = transport;
|
|
42
|
+
this.gw = gw;
|
|
43
|
+
}
|
|
44
|
+
notify(method, params, related) {
|
|
45
|
+
const m = { jsonrpc: "2.0", method, ...(params ? { params } : {}) };
|
|
46
|
+
void this.transport.send(m, related !== undefined ? { relatedRequestId: related } : undefined).catch(() => { });
|
|
47
|
+
}
|
|
48
|
+
request(method, params, related) {
|
|
49
|
+
const id = `s${this.nextId++}`;
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
const timer = setTimeout(() => {
|
|
52
|
+
this.waiting.delete(id);
|
|
53
|
+
resolve(rpcError(ErrorCode.Internal, `client did not answer ${method}`));
|
|
54
|
+
}, this.gw.clientRequestTimeoutMs);
|
|
55
|
+
this.waiting.set(id, { resolve, timer });
|
|
56
|
+
const m = { jsonrpc: "2.0", id, method, ...(params ? { params } : {}) };
|
|
57
|
+
this.transport.send(m, related !== undefined ? { relatedRequestId: related } : undefined).catch((err) => {
|
|
58
|
+
clearTimeout(timer);
|
|
59
|
+
this.waiting.delete(id);
|
|
60
|
+
resolve(rpcError(ErrorCode.Internal, `cannot reach client: ${err.message}`));
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
close() {
|
|
65
|
+
for (const [, w] of this.waiting) {
|
|
66
|
+
clearTimeout(w.timer);
|
|
67
|
+
w.resolve(rpcError(ErrorCode.Internal, "client session closed"));
|
|
68
|
+
}
|
|
69
|
+
this.waiting.clear();
|
|
70
|
+
}
|
|
71
|
+
onMessage(m) {
|
|
72
|
+
this.lastSeen = Date.now();
|
|
73
|
+
if (isResponse(m)) {
|
|
74
|
+
const w = this.waiting.get(m.id);
|
|
75
|
+
if (w) {
|
|
76
|
+
clearTimeout(w.timer);
|
|
77
|
+
this.waiting.delete(m.id);
|
|
78
|
+
w.resolve(m.error ? { error: m.error } : { result: m.result ?? {} });
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (isRequest(m)) {
|
|
83
|
+
void this.handleRequest(m);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (isNotification(m))
|
|
87
|
+
this.handleNotification(m);
|
|
88
|
+
}
|
|
89
|
+
reply(id, outcome) {
|
|
90
|
+
void this.transport.send(toResponse(id, outcome), { relatedRequestId: id }).catch(() => { });
|
|
91
|
+
}
|
|
92
|
+
async handleRequest(m) {
|
|
93
|
+
const id = m.id;
|
|
94
|
+
const method = m.method;
|
|
95
|
+
const manager = this.gw.manager;
|
|
96
|
+
try {
|
|
97
|
+
if (method === "initialize") {
|
|
98
|
+
const caps = m.params?.capabilities;
|
|
99
|
+
if (caps)
|
|
100
|
+
this.ctx.clientCapabilities = caps;
|
|
101
|
+
const d = await manager.describe(this);
|
|
102
|
+
this.reply(id, {
|
|
103
|
+
result: {
|
|
104
|
+
protocolVersion: negotiateVersion(m.params?.protocolVersion),
|
|
105
|
+
capabilities: d.capabilities,
|
|
106
|
+
serverInfo: d.serverInfo,
|
|
107
|
+
...(d.instructions ? { instructions: d.instructions } : {}),
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (method === "ping") {
|
|
113
|
+
this.reply(id, { result: {} });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const list = LIST_METHODS[method];
|
|
117
|
+
if (list) {
|
|
118
|
+
const d = await manager.describe(this);
|
|
119
|
+
if (!d.capabilities[list.cap]) {
|
|
120
|
+
this.reply(id, rpcError(ErrorCode.MethodNotFound, `${this.ctx.server.name} has no ${list.cap}`));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.reply(id, { result: { [list.field]: d[list.key] ?? [] } });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (method === "logging/setLevel" && !manager.existing(this)) {
|
|
127
|
+
this.reply(id, { result: {} });
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const inst = await manager.acquire(this);
|
|
131
|
+
if ("error" in inst || "result" in inst) {
|
|
132
|
+
this.reply(id, inst);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.reply(id, await inst.upstream.call(method, m.params, this, id));
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
this.reply(id, rpcError(ErrorCode.Unavailable, `${this.ctx.server.name}: ${err.message}`));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
handleNotification(m) {
|
|
142
|
+
const method = m.method;
|
|
143
|
+
if (method === "notifications/initialized")
|
|
144
|
+
return;
|
|
145
|
+
const inst = this.gw.manager.existing(this);
|
|
146
|
+
if (!inst)
|
|
147
|
+
return;
|
|
148
|
+
if (method === "notifications/roots/list_changed" && scopeOf(this.ctx.server) === "shared")
|
|
149
|
+
return;
|
|
150
|
+
inst.upstream.notify(method, m.params, this);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The HTTP face of mcpd: one Streamable HTTP endpoint per server (/mcp/<name>), bearer
|
|
155
|
+
* token auth, localhost only, and per-session routing into the instance manager.
|
|
156
|
+
*/
|
|
157
|
+
export class Gateway {
|
|
158
|
+
opts;
|
|
159
|
+
manager;
|
|
160
|
+
clientRequestTimeoutMs;
|
|
161
|
+
sessions = new Map();
|
|
162
|
+
http;
|
|
163
|
+
constructor(opts) {
|
|
164
|
+
this.opts = opts;
|
|
165
|
+
this.manager = opts.manager;
|
|
166
|
+
this.clientRequestTimeoutMs = opts.clientRequestTimeoutMs ?? 10 * 60_000;
|
|
167
|
+
}
|
|
168
|
+
listen(port, host = "127.0.0.1") {
|
|
169
|
+
this.http = createServer((req, res) => {
|
|
170
|
+
this.handle(req, res).catch((err) => {
|
|
171
|
+
this.opts.log?.(`request failed: ${err.stack ?? err}`);
|
|
172
|
+
if (!res.headersSent)
|
|
173
|
+
json(res, 500, { error: "internal error" });
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
return new Promise((resolve, reject) => {
|
|
177
|
+
this.http?.once("error", reject);
|
|
178
|
+
this.http?.listen(port, host, () => {
|
|
179
|
+
const addr = this.http?.address();
|
|
180
|
+
resolve(typeof addr === "object" && addr ? addr.port : port);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
async close() {
|
|
185
|
+
for (const s of this.sessions.values()) {
|
|
186
|
+
s.close();
|
|
187
|
+
await s.transport.close().catch(() => { });
|
|
188
|
+
}
|
|
189
|
+
this.sessions.clear();
|
|
190
|
+
await new Promise((r) => (this.http ? this.http.close(() => r()) : r()));
|
|
191
|
+
this.http?.closeAllConnections?.();
|
|
192
|
+
}
|
|
193
|
+
sessionCount() {
|
|
194
|
+
return this.sessions.size;
|
|
195
|
+
}
|
|
196
|
+
/** Drop sessions whose Claude Code process is gone. */
|
|
197
|
+
pruneSessions(isAlive) {
|
|
198
|
+
for (const [id, s] of this.sessions) {
|
|
199
|
+
if (s.ctx.pid && !isAlive(s.ctx.pid))
|
|
200
|
+
this.dropSession(id);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
dropSession(id) {
|
|
204
|
+
const s = this.sessions.get(id);
|
|
205
|
+
if (!s)
|
|
206
|
+
return;
|
|
207
|
+
this.sessions.delete(id);
|
|
208
|
+
s.close();
|
|
209
|
+
this.manager.detach(s);
|
|
210
|
+
}
|
|
211
|
+
authorized(req) {
|
|
212
|
+
const addr = req.socket.remoteAddress ?? "";
|
|
213
|
+
if (!["127.0.0.1", "::1", "::ffff:127.0.0.1"].includes(addr))
|
|
214
|
+
return "not local";
|
|
215
|
+
const host = header(req, "host") ?? "";
|
|
216
|
+
if (!/^(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/.test(host))
|
|
217
|
+
return "bad host";
|
|
218
|
+
if (header(req, "origin") !== undefined)
|
|
219
|
+
return "browser origin";
|
|
220
|
+
const auth = header(req, "authorization") ?? "";
|
|
221
|
+
if (!safeEqual(auth, `Bearer ${this.opts.token}`))
|
|
222
|
+
return "bad token";
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
async handle(req, res) {
|
|
226
|
+
const denied = this.authorized(req);
|
|
227
|
+
if (denied) {
|
|
228
|
+
json(res, denied === "bad token" ? 401 : 403, { error: denied });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const url = new URL(req.url ?? "/", "http://127.0.0.1");
|
|
232
|
+
if (url.pathname === "/status" && req.method === "GET") {
|
|
233
|
+
json(res, 200, this.status());
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const m = /^\/mcp\/([^/]+)\/?$/.exec(url.pathname);
|
|
237
|
+
const name = m?.[1] ? decodeURIComponent(m[1]) : undefined;
|
|
238
|
+
const server = name ? this.opts.manifest().servers[name] : undefined;
|
|
239
|
+
if (!server || (server.kind === "remote" && !server.proxy)) {
|
|
240
|
+
json(res, 404, { error: `unknown server ${name ?? ""}` });
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const sid = header(req, "mcp-session-id");
|
|
244
|
+
if (sid) {
|
|
245
|
+
const s = this.sessions.get(sid);
|
|
246
|
+
if (!s || s.ctx.server.name !== server.name) {
|
|
247
|
+
json(res, 404, { jsonrpc: "2.0", error: { code: -32001, message: "Session not found" }, id: null });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
await s.transport.handleRequest(req, res);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
// New session: identity comes from headers; PID falls back to the TCP peer.
|
|
254
|
+
const profile = header(req, "x-turno-profile") || undefined;
|
|
255
|
+
let pid = Number.parseInt(header(req, "x-turno-pid") ?? "0", 10) || undefined;
|
|
256
|
+
if (!pid && this.opts.peerPid && req.socket.remotePort)
|
|
257
|
+
pid = await this.opts.peerPid(req.socket.remotePort);
|
|
258
|
+
const cwd = header(req, "x-turno-cwd") || undefined;
|
|
259
|
+
const ctx = {
|
|
260
|
+
server,
|
|
261
|
+
sessionId: "",
|
|
262
|
+
...(profile ? { profile } : {}),
|
|
263
|
+
...(pid ? { pid } : {}),
|
|
264
|
+
...(cwd ? { cwd } : {}),
|
|
265
|
+
};
|
|
266
|
+
let session;
|
|
267
|
+
const transport = new StreamableHTTPServerTransport({
|
|
268
|
+
sessionIdGenerator: () => randomUUID(),
|
|
269
|
+
onsessioninitialized: (id) => {
|
|
270
|
+
if (!session)
|
|
271
|
+
return;
|
|
272
|
+
session.id = id;
|
|
273
|
+
ctx.sessionId = id;
|
|
274
|
+
this.sessions.set(id, session);
|
|
275
|
+
this.manager.register(session);
|
|
276
|
+
},
|
|
277
|
+
onsessionclosed: (id) => this.dropSession(id),
|
|
278
|
+
});
|
|
279
|
+
session = new ClientSession(ctx, transport, this);
|
|
280
|
+
const s = session;
|
|
281
|
+
transport.onmessage = (msg) => s.onMessage(msg);
|
|
282
|
+
transport.onclose = () => {
|
|
283
|
+
if (s.id)
|
|
284
|
+
this.dropSession(s.id);
|
|
285
|
+
};
|
|
286
|
+
await transport.handleRequest(req, res);
|
|
287
|
+
if (!transport.sessionId)
|
|
288
|
+
await transport.close().catch(() => { });
|
|
289
|
+
}
|
|
290
|
+
status() {
|
|
291
|
+
return {
|
|
292
|
+
pid: process.pid,
|
|
293
|
+
sessions: this.sessions.size,
|
|
294
|
+
instances: this.manager.list().map((i) => ({
|
|
295
|
+
key: i.key,
|
|
296
|
+
server: i.server.name,
|
|
297
|
+
variant: i.variant,
|
|
298
|
+
pid: i.upstream.pid ?? null,
|
|
299
|
+
clients: i.clients.size,
|
|
300
|
+
holder: i.holder ? { profile: i.holder.ctx.profile ?? null, pid: i.holder.ctx.pid ?? null } : null,
|
|
301
|
+
idleSeconds: Math.round((Date.now() - i.upstream.lastActivity) / 1000),
|
|
302
|
+
inflight: i.upstream.inflight,
|
|
303
|
+
startedAt: i.upstream.startedAt,
|
|
304
|
+
})),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
//# sourceMappingURL=gateway.js.map
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { stringify } from "smol-toml";
|
|
4
|
+
/** Scope defaults for servers we know; unknown servers default to "project" (safe for folder-bound tools). */
|
|
5
|
+
const KNOWN = [
|
|
6
|
+
{ match: /chrome-devtools-mcp/, scope: "session", note: "drives its own browser per session" },
|
|
7
|
+
{ match: /@playwright\/mcp|playwright-mcp/, scope: "session", note: "drives its own browser per session" },
|
|
8
|
+
{ match: /browser-use/, scope: "session", note: "drives its own browser per session" },
|
|
9
|
+
{ match: /trigger\.dev/, scope: "project", note: "reads the project in its working folder" },
|
|
10
|
+
{ match: /codebase-memory-mcp/, scope: "shared", note: "one index for every repository" },
|
|
11
|
+
{ match: /@calcom\/cal-mcp/, scope: "shared", note: "stateless API client" },
|
|
12
|
+
];
|
|
13
|
+
const SECRET_KEY = /(key|token|secret|password|passwd|pat|auth|credential)/i;
|
|
14
|
+
function packageName(spec) {
|
|
15
|
+
const at = spec.lastIndexOf("@");
|
|
16
|
+
return at > 0 ? spec.slice(0, at) : spec;
|
|
17
|
+
}
|
|
18
|
+
/** Split `npx [-y] pkg[@ver] args...` into the package spec and the rest. */
|
|
19
|
+
export function parseNpx(args) {
|
|
20
|
+
const rest = [...args];
|
|
21
|
+
while (rest.length && rest[0].startsWith("-"))
|
|
22
|
+
rest.shift();
|
|
23
|
+
const pkg = rest.shift();
|
|
24
|
+
return pkg ? { pkg, rest } : { rest };
|
|
25
|
+
}
|
|
26
|
+
export function guessScope(text) {
|
|
27
|
+
for (const k of KNOWN)
|
|
28
|
+
if (k.match.test(text))
|
|
29
|
+
return { scope: k.scope, note: k.note };
|
|
30
|
+
return { scope: "project", note: "unknown server: started per project folder (change to shared if it does not use its folder)" };
|
|
31
|
+
}
|
|
32
|
+
export async function importServers(servers, opts = {}) {
|
|
33
|
+
const out = {};
|
|
34
|
+
const notes = [];
|
|
35
|
+
const secretsMoved = [];
|
|
36
|
+
const literalSecrets = [];
|
|
37
|
+
const secretRef = async (server, key, value) => {
|
|
38
|
+
if (value.startsWith("keychain:") || value.startsWith("env:") || /^\$\{.+\}$/.test(value))
|
|
39
|
+
return value;
|
|
40
|
+
if (!opts.moveSecrets || !opts.store) {
|
|
41
|
+
literalSecrets.push(`${server}.${key}`);
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
const service = `turno/${server}/${key.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
|
|
45
|
+
await opts.store(service, value);
|
|
46
|
+
secretsMoved.push(`${server}.${key} -> keychain:${service}`);
|
|
47
|
+
return `keychain:${service}`;
|
|
48
|
+
};
|
|
49
|
+
for (const [name, entry] of Object.entries(servers)) {
|
|
50
|
+
const type = entry.type ?? (entry.command ? "stdio" : "http");
|
|
51
|
+
if (type === "stdio") {
|
|
52
|
+
const command = String(entry.command ?? "");
|
|
53
|
+
const args = (entry.args ?? []).map(String);
|
|
54
|
+
const env = {};
|
|
55
|
+
for (const [k, v] of Object.entries(entry.env ?? {})) {
|
|
56
|
+
env[k] = SECRET_KEY.test(k) ? await secretRef(name, k, String(v)) : String(v);
|
|
57
|
+
}
|
|
58
|
+
const { scope, note } = guessScope(`${command} ${args.join(" ")}`);
|
|
59
|
+
const server = {};
|
|
60
|
+
if (command === "npx" || command.endsWith("/npx")) {
|
|
61
|
+
const { pkg, rest } = parseNpx(args);
|
|
62
|
+
if (pkg) {
|
|
63
|
+
const bin = (await opts.binOf?.(packageName(pkg))) ?? packageName(pkg).replace(/^@[^/]+\//, "");
|
|
64
|
+
server.command = bin;
|
|
65
|
+
server.package = pkg;
|
|
66
|
+
if (rest.length)
|
|
67
|
+
server.args = rest;
|
|
68
|
+
notes.push(`${name}: runs ${pkg} (turno mcp install pins it and drops the npx wrapper)`);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
server.command = command;
|
|
72
|
+
server.args = args;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
server.command = command;
|
|
77
|
+
if (args.length)
|
|
78
|
+
server.args = args;
|
|
79
|
+
}
|
|
80
|
+
if (Object.keys(env).length)
|
|
81
|
+
server.env = env;
|
|
82
|
+
server.scope = scope;
|
|
83
|
+
notes.push(`${name}: scope ${scope} (${note})`);
|
|
84
|
+
out[name] = server;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
// Remote server: keep it direct unless its headers carry secrets we moved to the Keychain.
|
|
88
|
+
const headers = {};
|
|
89
|
+
for (const [k, v] of Object.entries(entry.headers ?? {})) {
|
|
90
|
+
headers[k] = SECRET_KEY.test(k) || /^bearer\s/i.test(String(v)) ? await secretRef(name, k, String(v)) : String(v);
|
|
91
|
+
}
|
|
92
|
+
const server = { url: String(entry.url ?? "") };
|
|
93
|
+
if (type !== "http")
|
|
94
|
+
server.type = type;
|
|
95
|
+
if (Object.keys(headers).length)
|
|
96
|
+
server.headers = headers;
|
|
97
|
+
const proxied = Object.values(headers).some((v) => v.startsWith("keychain:"));
|
|
98
|
+
notes.push(`${name}: remote ${proxied ? "(proxied by mcpd, secrets in the Keychain)" : "(written directly into profiles)"}`);
|
|
99
|
+
out[name] = server;
|
|
100
|
+
}
|
|
101
|
+
const header = [
|
|
102
|
+
"# turno MCP manifest. Generated by `turno mcp import`; edit freely.",
|
|
103
|
+
"# scope: shared (one process for every session) | project (one per project folder)",
|
|
104
|
+
"# | session (one per Claude session). Servers start on first use and stop when idle.",
|
|
105
|
+
"# Docs: docs/mcp.md in the turno repository.",
|
|
106
|
+
"",
|
|
107
|
+
].join("\n");
|
|
108
|
+
return { toml: `${header}${stringify({ server: out })}\n`, notes, secretsMoved, literalSecrets };
|
|
109
|
+
}
|
|
110
|
+
/** Stdio servers provided by enabled plugins: they bypass mcpd and start in every session. */
|
|
111
|
+
export function pluginServers(hub) {
|
|
112
|
+
let enabled = {};
|
|
113
|
+
try {
|
|
114
|
+
const settings = JSON.parse(readFileSync(join(hub, "settings.json"), "utf8"));
|
|
115
|
+
enabled = settings.enabledPlugins ?? {};
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const found = [];
|
|
121
|
+
for (const [id, on] of Object.entries(enabled)) {
|
|
122
|
+
if (!on)
|
|
123
|
+
continue;
|
|
124
|
+
const [plugin, marketplace] = id.split("@");
|
|
125
|
+
if (!plugin || !marketplace)
|
|
126
|
+
continue;
|
|
127
|
+
const base = join(hub, "plugins", "cache", marketplace, plugin);
|
|
128
|
+
if (!existsSync(base))
|
|
129
|
+
continue;
|
|
130
|
+
const versions = readdirSync(base)
|
|
131
|
+
.map((v) => join(base, v))
|
|
132
|
+
.filter((d) => existsSync(join(d, ".mcp.json")))
|
|
133
|
+
.sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs);
|
|
134
|
+
const latest = versions[0];
|
|
135
|
+
if (!latest)
|
|
136
|
+
continue;
|
|
137
|
+
let mcp = {};
|
|
138
|
+
try {
|
|
139
|
+
mcp = JSON.parse(readFileSync(join(latest, ".mcp.json"), "utf8"));
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const servers = (mcp.mcpServers ?? mcp);
|
|
145
|
+
for (const [name, def] of Object.entries(servers)) {
|
|
146
|
+
if (typeof def !== "object" || def === null)
|
|
147
|
+
continue;
|
|
148
|
+
const stdio = typeof def.command === "string";
|
|
149
|
+
found.push({
|
|
150
|
+
plugin,
|
|
151
|
+
name,
|
|
152
|
+
toolPrefix: `mcp__plugin_${plugin}_${name}__`,
|
|
153
|
+
transport: stdio ? "stdio" : "remote",
|
|
154
|
+
...(stdio ? { command: `${def.command} ${(def.args ?? []).join(" ")}`.trim() } : {}),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return found;
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=importer.js.map
|