turno-cli 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +293 -0
- package/README.md +143 -75
- package/dist/accounts/audit.js +137 -7
- package/dist/accounts/builtin.js +68 -21
- package/dist/accounts/delegate.js +176 -35
- package/dist/accounts/policy.js +77 -0
- package/dist/accounts/registry.js +37 -21
- package/dist/agents/adapter.js +7 -0
- package/dist/agents/claude.js +195 -0
- package/dist/agents/codex.js +98 -0
- package/dist/agents/copilot.js +39 -0
- package/dist/agents/cursor.js +41 -0
- package/dist/agents/index.js +26 -86
- package/dist/agents/snippets.js +7 -4
- package/dist/cli/args.js +19 -4
- package/dist/cli/commands/accounts.js +96 -51
- package/dist/cli/commands/auto.js +82 -32
- package/dist/cli/commands/browser.js +216 -135
- package/dist/cli/commands/complete.js +2 -1
- package/dist/cli/commands/completion.js +4 -2
- package/dist/cli/commands/config.js +103 -18
- package/dist/cli/commands/demo.js +292 -0
- package/dist/cli/commands/doctor.js +387 -119
- package/dist/cli/commands/handoff.js +33 -22
- package/dist/cli/commands/identity.js +101 -50
- package/dist/cli/commands/init.js +616 -0
- package/dist/cli/commands/limit.js +25 -13
- package/dist/cli/commands/mcp-oauth.js +194 -0
- package/dist/cli/commands/mcp-observe.js +434 -0
- package/dist/cli/commands/mcp.js +653 -185
- package/dist/cli/commands/mcpd.js +323 -75
- package/dist/cli/commands/migrate.js +6 -4
- package/dist/cli/commands/open.js +92 -27
- package/dist/cli/commands/profile.js +205 -69
- package/dist/cli/commands/resolve.js +6 -2
- package/dist/cli/commands/run.js +15 -5
- package/dist/cli/commands/secret.js +61 -42
- package/dist/cli/commands/session.js +2 -2
- package/dist/cli/commands/sessions.js +42 -12
- package/dist/cli/commands/setup.js +136 -158
- package/dist/cli/commands/status.js +172 -41
- package/dist/cli/commands/statusline.js +305 -113
- package/dist/cli/commands/ui.js +64 -66
- package/dist/cli/commands/undo.js +161 -18
- package/dist/cli/commands/uninstall.js +462 -0
- package/dist/cli/commands/upgrade.js +75 -19
- package/dist/cli/commands/usage.js +117 -44
- package/dist/cli/commands/version.js +7 -5
- package/dist/cli/commands/which.js +7 -24
- package/dist/cli/compile-cache.js +32 -0
- package/dist/cli/group.js +10 -10
- package/dist/cli/help.js +57 -0
- package/dist/cli/highlight.js +236 -0
- package/dist/cli/io.js +170 -1
- package/dist/cli/launch.js +21 -37
- package/dist/cli/launchers.js +145 -94
- package/dist/cli/main.js +163 -27
- package/dist/cli/picker.js +447 -0
- package/dist/cli/registry.js +42 -5
- package/dist/cli/security.js +539 -0
- package/dist/cli/style.js +281 -0
- package/dist/core/config.js +145 -23
- package/dist/core/exhausted.js +6 -8
- package/dist/core/fsutil.js +47 -0
- package/dist/core/journal.js +281 -11
- package/dist/core/manifest.js +126 -6
- package/dist/core/paths.js +114 -7
- package/dist/core/profiles.js +139 -9
- package/dist/core/resolve.js +129 -23
- package/dist/core/secrets.js +215 -20
- package/dist/core/sessions.js +13 -20
- package/dist/core/update.js +18 -8
- package/dist/core/version.js +6 -0
- package/dist/demo/server.js +34 -0
- package/dist/mcpd/cache.js +18 -4
- package/dist/mcpd/calllog.js +253 -0
- package/dist/mcpd/client-names.js +51 -0
- package/dist/mcpd/connectors.js +148 -17
- package/dist/mcpd/daemon.js +452 -116
- package/dist/mcpd/folder-sets.js +39 -0
- package/dist/mcpd/gateway.js +441 -47
- package/dist/mcpd/health.js +269 -0
- package/dist/mcpd/importer.js +356 -36
- package/dist/mcpd/instances.js +425 -65
- package/dist/mcpd/jsonrpc.js +8 -0
- package/dist/mcpd/launchd.js +65 -48
- package/dist/mcpd/modern.js +648 -0
- package/dist/mcpd/oauth.js +791 -0
- package/dist/mcpd/peerpid.js +122 -6
- package/dist/mcpd/pressure.js +85 -0
- package/dist/mcpd/rss.js +151 -0
- package/dist/mcpd/service.js +167 -0
- package/dist/mcpd/state.js +183 -0
- package/dist/mcpd/sync.js +55 -80
- package/dist/mcpd/systemd.js +80 -0
- package/dist/mcpd/tokens.js +89 -0
- package/dist/mcpd/upstream.js +154 -44
- package/dist/statusline/cost.js +154 -44
- package/dist/statusline/input.js +66 -0
- package/dist/statusline/render.js +9 -3
- package/dist/ui/fonts.js +8 -3
- package/dist/ui/settings.js +27 -15
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +9 -9
- package/dist/cli/commands/available.js +0 -18
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readSync, renameSync, statSync, writeSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { ErrorCode } from "./jsonrpc.js";
|
|
4
|
+
/** Methods that get a record. */
|
|
5
|
+
const LOGGED = new Set(["tools/call"]);
|
|
6
|
+
const MAX_ERROR = 300;
|
|
7
|
+
/**
|
|
8
|
+
* Hide what looks like a credential in an error text: bearer and basic values, key=value
|
|
9
|
+
* pairs named like secrets, well-known token prefixes, and long opaque strings.
|
|
10
|
+
*/
|
|
11
|
+
export function redact(text) {
|
|
12
|
+
return text
|
|
13
|
+
.replace(/\b(Bearer|Basic|token)\s+[A-Za-z0-9._~+/=-]{8,}/gi, "$1 [redacted]")
|
|
14
|
+
.replace(/\b([A-Za-z_]*(?:token|secret|password|passwd|api[_-]?key|auth)[A-Za-z_]*)(["']?\s*[:=]\s*["']?)[^\s"'&,;]+/gi, "$1$2[redacted]")
|
|
15
|
+
.replace(/\b(?:sk|pk|rk)-[A-Za-z0-9_-]{8,}/g, "[redacted]")
|
|
16
|
+
.replace(/\b(?:ghp|gho|ghu|ghs|ghr|github_pat|xox[abpr]|glpat)[-_][A-Za-z0-9_-]{8,}/g, "[redacted]")
|
|
17
|
+
.replace(/[A-Za-z0-9+_-]{32,}={0,2}/g, (s) => (/\d/.test(s) && /[A-Za-z]/.test(s) ? "[redacted]" : s));
|
|
18
|
+
}
|
|
19
|
+
function outcomeOf(o) {
|
|
20
|
+
if ("error" in o) {
|
|
21
|
+
const code = o.error.code;
|
|
22
|
+
const outcome = code === ErrorCode.RequestCancelled ? "cancelled" : code === ErrorCode.RequestTimeout ? "timeout" : "error";
|
|
23
|
+
return { outcome, error: redact(String(o.error.message ?? "")).slice(0, MAX_ERROR) };
|
|
24
|
+
}
|
|
25
|
+
// A tool that failed answers with a result marked isError; its text is a result, so it stays out.
|
|
26
|
+
if (o.result.isError === true)
|
|
27
|
+
return { outcome: "error", error: "the tool reported an error" };
|
|
28
|
+
return { outcome: "ok" };
|
|
29
|
+
}
|
|
30
|
+
function bytes(v) {
|
|
31
|
+
try {
|
|
32
|
+
return Buffer.byteLength(JSON.stringify(v ?? {}));
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** A printable client name, at most 64 characters. */
|
|
39
|
+
export function cleanAgentName(name) {
|
|
40
|
+
if (typeof name !== "string")
|
|
41
|
+
return undefined;
|
|
42
|
+
const clean = name.replace(/[^\x20-\x7e]/g, "").trim().slice(0, 64);
|
|
43
|
+
return clean || undefined;
|
|
44
|
+
}
|
|
45
|
+
/** The record for one relayed request, or undefined when the method is not logged. */
|
|
46
|
+
export function toRecord(e) {
|
|
47
|
+
if (!LOGGED.has(e.method))
|
|
48
|
+
return undefined;
|
|
49
|
+
const tool = typeof e.params?.name === "string" ? e.params.name.slice(0, 200) : null;
|
|
50
|
+
const { outcome, error } = outcomeOf(e.outcome);
|
|
51
|
+
return {
|
|
52
|
+
time: new Date(e.startedAt).toISOString(),
|
|
53
|
+
agent: e.ctx.clientName ?? null,
|
|
54
|
+
profile: e.ctx.profile ?? null,
|
|
55
|
+
pid: e.ctx.pid ?? null,
|
|
56
|
+
server: e.ctx.serverName,
|
|
57
|
+
variant: e.variant ?? null,
|
|
58
|
+
tool,
|
|
59
|
+
durationMs: Math.max(0, Math.round(e.durationMs)),
|
|
60
|
+
outcome,
|
|
61
|
+
...(error !== undefined ? { error } : {}),
|
|
62
|
+
requestBytes: bytes(e.params),
|
|
63
|
+
responseBytes: bytes("error" in e.outcome ? e.outcome.error : e.outcome.result),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Append-only writer, synchronous like mcpd.log so records keep their order across a
|
|
68
|
+
* rotation: `calls.jsonl` becomes `calls.jsonl.1` at `maxBytes` and `keep` old files are kept.
|
|
69
|
+
* Mode 600. A disk that refuses writes never takes mcpd down.
|
|
70
|
+
*/
|
|
71
|
+
export function createCallLog(file, opts = {}) {
|
|
72
|
+
const maxBytes = opts.maxBytes ?? 5 * 1024 * 1024;
|
|
73
|
+
const keep = opts.keep ?? 2;
|
|
74
|
+
let fd;
|
|
75
|
+
let size = 0;
|
|
76
|
+
let reported = false;
|
|
77
|
+
const fail = (err) => {
|
|
78
|
+
if (reported)
|
|
79
|
+
return;
|
|
80
|
+
reported = true;
|
|
81
|
+
opts.onError?.(`cannot write ${file}: ${err.message}`);
|
|
82
|
+
};
|
|
83
|
+
const close = () => {
|
|
84
|
+
if (fd === undefined)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
closeSync(fd);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
/* already closed */
|
|
91
|
+
}
|
|
92
|
+
fd = undefined;
|
|
93
|
+
};
|
|
94
|
+
const open = () => {
|
|
95
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
96
|
+
fd = openSync(file, "a", 0o600);
|
|
97
|
+
size = fstatSync(fd).size;
|
|
98
|
+
};
|
|
99
|
+
const rotate = () => {
|
|
100
|
+
close();
|
|
101
|
+
for (let i = keep - 1; i >= 1; i--)
|
|
102
|
+
if (existsSync(`${file}.${i}`))
|
|
103
|
+
renameSync(`${file}.${i}`, `${file}.${i + 1}`);
|
|
104
|
+
renameSync(file, `${file}.1`);
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
record(e) {
|
|
108
|
+
try {
|
|
109
|
+
const r = toRecord(e);
|
|
110
|
+
if (!r)
|
|
111
|
+
return;
|
|
112
|
+
const line = Buffer.from(`${JSON.stringify(r)}\n`);
|
|
113
|
+
if (fd === undefined)
|
|
114
|
+
open();
|
|
115
|
+
if (size > 0 && size + line.length > maxBytes) {
|
|
116
|
+
rotate();
|
|
117
|
+
open();
|
|
118
|
+
}
|
|
119
|
+
writeSync(fd, line);
|
|
120
|
+
size += line.length;
|
|
121
|
+
reported = false;
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
close();
|
|
125
|
+
fail(err);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
close,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function parseLine(line) {
|
|
132
|
+
if (!line.trim())
|
|
133
|
+
return undefined;
|
|
134
|
+
try {
|
|
135
|
+
const r = JSON.parse(line);
|
|
136
|
+
return r && typeof r === "object" && typeof r.server === "string" && typeof r.time === "string" ? r : undefined;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function matches(r, f) {
|
|
143
|
+
return (f.server === undefined || r.server === f.server) && (f.profile === undefined || r.profile === f.profile);
|
|
144
|
+
}
|
|
145
|
+
function readText(file) {
|
|
146
|
+
let fd;
|
|
147
|
+
try {
|
|
148
|
+
fd = openSync(file, "r");
|
|
149
|
+
const size = fstatSync(fd).size;
|
|
150
|
+
const buf = Buffer.alloc(size);
|
|
151
|
+
let off = 0;
|
|
152
|
+
while (off < size) {
|
|
153
|
+
const n = readSync(fd, buf, off, size - off, off);
|
|
154
|
+
if (n <= 0)
|
|
155
|
+
break;
|
|
156
|
+
off += n;
|
|
157
|
+
}
|
|
158
|
+
return buf.subarray(0, off).toString("utf8");
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return "";
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
if (fd !== undefined)
|
|
165
|
+
closeSync(fd);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The last `tail` records that match, oldest first. Reads the current file, then the rotated
|
|
170
|
+
* ones only while more records are needed. Lines that do not parse are skipped.
|
|
171
|
+
*/
|
|
172
|
+
export function readCalls(file, tail, filter = {}, keep = 2) {
|
|
173
|
+
const out = [];
|
|
174
|
+
for (let i = 0; i <= keep && out.length < tail; i++) {
|
|
175
|
+
const f = i === 0 ? file : `${file}.${i}`;
|
|
176
|
+
if (!existsSync(f))
|
|
177
|
+
continue;
|
|
178
|
+
const found = readText(f)
|
|
179
|
+
.split("\n")
|
|
180
|
+
.map(parseLine)
|
|
181
|
+
.filter((r) => r !== undefined && matches(r, filter));
|
|
182
|
+
out.unshift(...found.slice(-(tail - out.length)));
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Follow the file like `tail -f`: report each new matching record until `signal` aborts.
|
|
188
|
+
* Polls (every `everyMs`), so it works on every platform and survives a rotation: a file that
|
|
189
|
+
* got smaller or was replaced is read again from its start.
|
|
190
|
+
*/
|
|
191
|
+
export async function followCalls(file, onRecord, signal, filter = {}, everyMs = 500) {
|
|
192
|
+
let offset = 0;
|
|
193
|
+
let ino;
|
|
194
|
+
try {
|
|
195
|
+
const st = statSync(file);
|
|
196
|
+
offset = st.size;
|
|
197
|
+
ino = st.ino;
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
/* not there yet */
|
|
201
|
+
}
|
|
202
|
+
let partial = "";
|
|
203
|
+
const poll = () => {
|
|
204
|
+
let st;
|
|
205
|
+
try {
|
|
206
|
+
st = statSync(file);
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (st.ino !== ino || st.size < offset) {
|
|
212
|
+
ino = st.ino;
|
|
213
|
+
offset = 0;
|
|
214
|
+
partial = "";
|
|
215
|
+
}
|
|
216
|
+
if (st.size === offset)
|
|
217
|
+
return;
|
|
218
|
+
let fd;
|
|
219
|
+
try {
|
|
220
|
+
fd = openSync(file, "r");
|
|
221
|
+
const len = Number(st.size) - offset;
|
|
222
|
+
const buf = Buffer.alloc(len);
|
|
223
|
+
const n = readSync(fd, buf, 0, len, offset);
|
|
224
|
+
offset += n;
|
|
225
|
+
const text = partial + buf.subarray(0, n).toString("utf8");
|
|
226
|
+
const lines = text.split("\n");
|
|
227
|
+
partial = lines.pop() ?? "";
|
|
228
|
+
for (const l of lines) {
|
|
229
|
+
const r = parseLine(l);
|
|
230
|
+
if (r && matches(r, filter))
|
|
231
|
+
onRecord(r);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
/* try again on the next tick */
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
if (fd !== undefined)
|
|
239
|
+
closeSync(fd);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
await new Promise((resolve) => {
|
|
243
|
+
if (signal.aborted)
|
|
244
|
+
return resolve();
|
|
245
|
+
const timer = setInterval(poll, everyMs);
|
|
246
|
+
signal.addEventListener("abort", () => {
|
|
247
|
+
clearInterval(timer);
|
|
248
|
+
poll();
|
|
249
|
+
resolve();
|
|
250
|
+
}, { once: true });
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=calllog.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { atomicWrite } from "../core/journal.js";
|
|
3
|
+
import { PRIVATE_FILE_MODE } from "../core/paths.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 (ADR 0016) still has its agent's
|
|
7
|
+
* name for the call log and `/status` (ADR 0026). 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
|
+
try {
|
|
20
|
+
const raw = JSON.parse(readFileSync(file, "utf8"));
|
|
21
|
+
for (const [pid, name] of Object.entries(raw)) {
|
|
22
|
+
const n = Number(pid);
|
|
23
|
+
if (Number.isInteger(n) && n > 0 && typeof name === "string")
|
|
24
|
+
this.names.set(n, name);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
/* none yet, or unreadable: start empty */
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
get(pid) {
|
|
32
|
+
return pid ? this.names.get(pid) : undefined;
|
|
33
|
+
}
|
|
34
|
+
set(pid, name) {
|
|
35
|
+
if (!pid || this.names.get(pid) === name)
|
|
36
|
+
return;
|
|
37
|
+
this.names.set(pid, name);
|
|
38
|
+
for (const p of this.names.keys())
|
|
39
|
+
if (!this.isAlive(p))
|
|
40
|
+
this.names.delete(p);
|
|
41
|
+
if (!this.file)
|
|
42
|
+
return;
|
|
43
|
+
try {
|
|
44
|
+
atomicWrite(this.file, `${JSON.stringify(Object.fromEntries(this.names))}\n`, PRIVATE_FILE_MODE);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
/* a disk that refuses the write only costs the name after a restart */
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=client-names.js.map
|
package/dist/mcpd/connectors.js
CHANGED
|
@@ -1,33 +1,151 @@
|
|
|
1
|
-
import {
|
|
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
7
|
import { effectiveLaunch } from "../core/manifest.js";
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
function logFileOf(logsDir, label) {
|
|
14
|
+
return join(logsDir, `${label.replace(/[^A-Za-z0-9_.-]/g, "_")}.log`);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Append-only log that rotates by size while it is written, not only when a server starts:
|
|
18
|
+
* a long-lived, chatty server would otherwise fill the disk.
|
|
19
|
+
*/
|
|
20
|
+
export class RotatingLog {
|
|
21
|
+
file;
|
|
22
|
+
maxBytes;
|
|
23
|
+
fd;
|
|
24
|
+
size = 0;
|
|
25
|
+
constructor(file, maxBytes = MAX_LOG) {
|
|
26
|
+
this.file = file;
|
|
27
|
+
this.maxBytes = maxBytes;
|
|
28
|
+
mkdirSync(join(file, ".."), { recursive: true, mode: PRIVATE_DIR_MODE });
|
|
29
|
+
try {
|
|
30
|
+
this.size = statSync(file).size;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
this.size = 0;
|
|
34
|
+
}
|
|
35
|
+
if (this.size > this.maxBytes)
|
|
36
|
+
this.rotate();
|
|
37
|
+
}
|
|
38
|
+
/** Synchronous, like mcpd.log: chunks are small and the order must survive a rotation. */
|
|
39
|
+
write(chunk) {
|
|
40
|
+
const buf = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
|
|
41
|
+
try {
|
|
42
|
+
if (this.size > 0 && this.size + buf.length > this.maxBytes)
|
|
43
|
+
this.rotate();
|
|
44
|
+
if (this.fd === undefined) {
|
|
45
|
+
// Server logs can quote tokens and file contents: 0600 like every file turno keeps.
|
|
46
|
+
this.fd = openSync(this.file, "a", PRIVATE_FILE_MODE);
|
|
47
|
+
if (fstatSync(this.fd).mode & 0o077)
|
|
48
|
+
fchmodSync(this.fd, PRIVATE_FILE_MODE);
|
|
49
|
+
}
|
|
50
|
+
writeSync(this.fd, buf);
|
|
51
|
+
this.size += buf.length;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
/* a full or read-only disk must not take mcpd down */
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
end() {
|
|
58
|
+
if (this.fd === undefined)
|
|
59
|
+
return;
|
|
60
|
+
try {
|
|
61
|
+
closeSync(this.fd);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
/* already closed */
|
|
65
|
+
}
|
|
66
|
+
delete this.fd;
|
|
67
|
+
}
|
|
68
|
+
rotate() {
|
|
69
|
+
this.end();
|
|
70
|
+
try {
|
|
71
|
+
renameSync(this.file, `${this.file}.1`);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
/* nothing to rotate */
|
|
75
|
+
}
|
|
76
|
+
this.size = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** The last lines of a file (at most 16 KiB are read). */
|
|
80
|
+
function tailFile(file, lines = 10) {
|
|
81
|
+
let fd;
|
|
13
82
|
try {
|
|
14
|
-
|
|
15
|
-
|
|
83
|
+
const size = statSync(file).size;
|
|
84
|
+
const len = Math.min(size, 16 * 1024);
|
|
85
|
+
const buf = Buffer.alloc(len);
|
|
86
|
+
fd = openSync(file, "r");
|
|
87
|
+
readSync(fd, buf, 0, len, size - len);
|
|
88
|
+
const text = buf.toString("utf8").trimEnd();
|
|
89
|
+
return text ? text.split("\n").slice(-lines).join("\n") : undefined;
|
|
16
90
|
}
|
|
17
91
|
catch {
|
|
18
|
-
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
if (fd !== undefined)
|
|
96
|
+
closeSync(fd);
|
|
19
97
|
}
|
|
20
|
-
return createWriteStream(file, { flags: "a" });
|
|
21
98
|
}
|
|
22
99
|
export class DefaultConnector {
|
|
23
100
|
opts;
|
|
101
|
+
oauthSessions;
|
|
102
|
+
oauthLogs = new Map();
|
|
24
103
|
constructor(opts) {
|
|
25
104
|
this.opts = opts;
|
|
26
105
|
}
|
|
106
|
+
sessions() {
|
|
107
|
+
this.oauthSessions ??=
|
|
108
|
+
this.opts.oauth ??
|
|
109
|
+
new OAuthSessions((name, variant) => ({
|
|
110
|
+
store: () => secretStore(this.opts.vars ?? process.env),
|
|
111
|
+
resolve: this.opts.resolve,
|
|
112
|
+
log: (line) => this.oauthLog(name, variant).write(`turno: ${line}\n`),
|
|
113
|
+
}));
|
|
114
|
+
return this.oauthSessions;
|
|
115
|
+
}
|
|
116
|
+
oauthLog(name, variant) {
|
|
117
|
+
const label = `${name}${variant === "default" ? "" : `@${variant}`}`;
|
|
118
|
+
let log = this.oauthLogs.get(label);
|
|
119
|
+
if (!log) {
|
|
120
|
+
log = new RotatingLog(logFileOf(this.opts.logsDir, label), this.opts.maxLogBytes ?? MAX_LOG);
|
|
121
|
+
this.oauthLogs.set(label, log);
|
|
122
|
+
}
|
|
123
|
+
return log;
|
|
124
|
+
}
|
|
125
|
+
forgetLogin(server, variants) {
|
|
126
|
+
this.oauthSessions?.forget(server, variants);
|
|
127
|
+
}
|
|
128
|
+
async flushLogins() {
|
|
129
|
+
return this.oauthSessions ? this.oauthSessions.flush() : [];
|
|
130
|
+
}
|
|
131
|
+
logTail(server, variant, lines = 10) {
|
|
132
|
+
return tailFile(logFileOf(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`), lines);
|
|
133
|
+
}
|
|
27
134
|
async connect(server, variant, ctx, workdir) {
|
|
28
135
|
if (server.kind === "remote") {
|
|
29
136
|
const headers = await resolveRecord(server.headers, this.opts.resolve);
|
|
30
|
-
|
|
137
|
+
let fetchOpt = {};
|
|
138
|
+
if (server.oauth) {
|
|
139
|
+
// mcpd is the OAuth client (ADR 0029). Asking for a token now fails fast with the login
|
|
140
|
+
// hint instead of a failed initialize; every request then gets a current token.
|
|
141
|
+
const session = this.sessions().get(server.name, variant);
|
|
142
|
+
await session.accessToken(server);
|
|
143
|
+
fetchOpt = { fetch: oauthFetch(session, () => server) };
|
|
144
|
+
}
|
|
145
|
+
// `type = "sse"` is the legacy HTTP+SSE transport; everything else is Streamable HTTP.
|
|
146
|
+
const transport = server.entry.type === "sse"
|
|
147
|
+
? new SSEClientTransport(new URL(server.url), { requestInit: { headers }, ...fetchOpt })
|
|
148
|
+
: new StreamableHTTPClientTransport(new URL(server.url), { requestInit: { headers }, ...fetchOpt });
|
|
31
149
|
return { transport };
|
|
32
150
|
}
|
|
33
151
|
if (server.builtin) {
|
|
@@ -40,24 +158,37 @@ export class DefaultConnector {
|
|
|
40
158
|
return { transport: clientSide };
|
|
41
159
|
}
|
|
42
160
|
const launch = effectiveLaunch(server, variant);
|
|
161
|
+
const cwd = launch.cwd ?? workdir;
|
|
162
|
+
if (!isDirectory(cwd)) {
|
|
163
|
+
throw new Error(launch.cwd
|
|
164
|
+
? `the folder ${cwd} (cwd in mcp.toml) does not exist`
|
|
165
|
+
: `the session folder ${cwd} does not exist or is not readable by mcpd`);
|
|
166
|
+
}
|
|
43
167
|
const env = { ...(this.opts.baseEnv ?? {}), ...(await resolveRecord(launch.env, this.opts.resolve)) };
|
|
44
|
-
const
|
|
168
|
+
const vars = this.opts.vars;
|
|
169
|
+
const args = vars ? launch.args.map((a) => expandVars(a, vars)) : launch.args;
|
|
170
|
+
const resolved = resolveCommand(server, launch.command, args, this.opts.serversDir);
|
|
45
171
|
const transport = new StdioClientTransport({
|
|
46
172
|
command: resolved.command,
|
|
47
173
|
args: resolved.args,
|
|
48
174
|
env,
|
|
49
|
-
cwd
|
|
175
|
+
cwd,
|
|
50
176
|
stderr: "pipe",
|
|
51
177
|
});
|
|
52
|
-
const log =
|
|
178
|
+
const log = new RotatingLog(logFileOf(this.opts.logsDir, `${server.name}${variant === "default" ? "" : `@${variant}`}`), this.opts.maxLogBytes ?? MAX_LOG);
|
|
179
|
+
const warnings = resolved.warning ? [resolved.warning] : [];
|
|
180
|
+
for (const w of warnings)
|
|
181
|
+
log.write(`turno: ${w}\n`);
|
|
53
182
|
const origStart = transport.start.bind(transport);
|
|
54
183
|
transport.start = async () => {
|
|
55
184
|
await origStart();
|
|
56
185
|
const err = transport.stderr;
|
|
57
|
-
if (err)
|
|
58
|
-
err.
|
|
186
|
+
if (err) {
|
|
187
|
+
err.on("data", (chunk) => log.write(chunk));
|
|
188
|
+
err.on("end", () => log.end());
|
|
189
|
+
}
|
|
59
190
|
};
|
|
60
|
-
return { transport, pid: () => transport.pid };
|
|
191
|
+
return { transport, pid: () => transport.pid, warnings };
|
|
61
192
|
}
|
|
62
193
|
}
|
|
63
194
|
//# sourceMappingURL=connectors.js.map
|