@firefunc-agent/runner 0.7.1 → 0.9.3
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/LICENSE +7 -3
- package/README.md +296 -4
- package/dist/activity.d.ts +32 -0
- package/dist/activity.js +295 -0
- package/dist/api.d.ts +69 -1
- package/dist/api.js +114 -3
- package/dist/brief.d.ts +11 -0
- package/dist/brief.js +278 -0
- package/dist/build-info.json +4 -0
- package/dist/cli.js +59 -2
- package/dist/config.d.ts +7 -0
- package/dist/config.js +3 -0
- package/dist/cut.d.ts +45 -0
- package/dist/cut.js +105 -0
- package/dist/daemon.d.ts +16 -0
- package/dist/daemon.js +196 -7
- package/dist/declined.d.ts +11 -0
- package/dist/declined.js +27 -0
- package/dist/engines/antigravity.d.ts +2 -0
- package/dist/engines/antigravity.js +102 -0
- package/dist/engines/claude-budget.d.ts +1 -1
- package/dist/engines/claude-budget.js +2 -72
- package/dist/engines/claude.js +139 -2
- package/dist/engines/codex.js +10 -3
- package/dist/engines/copilot.d.ts +2 -0
- package/dist/engines/copilot.js +117 -0
- package/dist/engines/cursor.js +4 -0
- package/dist/engines/gemini.js +6 -1
- package/dist/engines/index.d.ts +2 -0
- package/dist/engines/index.js +22 -4
- package/dist/engines/types.d.ts +12 -1
- package/dist/exec.d.ts +3 -0
- package/dist/exec.js +48 -5
- package/dist/firefunc/agents.d.ts +2 -0
- package/dist/firefunc/agents.js +343 -0
- package/dist/firefunc/args.d.ts +18 -0
- package/dist/firefunc/args.js +82 -0
- package/dist/firefunc/auth.d.ts +21 -0
- package/dist/firefunc/auth.js +129 -0
- package/dist/firefunc/catalog.d.ts +10 -0
- package/dist/firefunc/catalog.js +201 -0
- package/dist/firefunc/cli.d.ts +2 -0
- package/dist/firefunc/cli.js +24 -0
- package/dist/firefunc/client.d.ts +36 -0
- package/dist/firefunc/client.js +177 -0
- package/dist/firefunc/command.d.ts +28 -0
- package/dist/firefunc/command.js +35 -0
- package/dist/firefunc/config.d.ts +29 -0
- package/dist/firefunc/config.js +111 -0
- package/dist/firefunc/configure.d.ts +6 -0
- package/dist/firefunc/configure.js +409 -0
- package/dist/firefunc/io.d.ts +25 -0
- package/dist/firefunc/io.js +14 -0
- package/dist/firefunc/main.d.ts +4 -0
- package/dist/firefunc/main.js +259 -0
- package/dist/firefunc/output.d.ts +29 -0
- package/dist/firefunc/output.js +131 -0
- package/dist/firefunc/prompt.d.ts +14 -0
- package/dist/firefunc/prompt.js +58 -0
- package/dist/firefunc/skill.d.ts +3 -0
- package/dist/firefunc/skill.js +80 -0
- package/dist/firefunc/tasks.d.ts +2 -0
- package/dist/firefunc/tasks.js +279 -0
- package/dist/firefunc/version.d.ts +2 -0
- package/dist/firefunc/version.js +8 -0
- package/dist/git-auth.d.ts +4 -1
- package/dist/git-auth.js +6 -2
- package/dist/held.d.ts +18 -0
- package/dist/held.js +95 -0
- package/dist/job.d.ts +3 -0
- package/dist/job.js +818 -102
- package/dist/keep-local.d.ts +12 -0
- package/dist/keep-local.js +651 -0
- package/dist/limit.d.ts +30 -0
- package/dist/limit.js +258 -0
- package/dist/locks.d.ts +14 -0
- package/dist/locks.js +164 -0
- package/dist/machine-logins.d.ts +19 -0
- package/dist/machine-logins.js +187 -0
- package/dist/mcp-login.d.ts +24 -0
- package/dist/mcp-login.js +993 -0
- package/dist/mcp-proxy.d.ts +21 -0
- package/dist/mcp-proxy.js +180 -0
- package/dist/mcp.d.ts +62 -0
- package/dist/mcp.js +611 -0
- package/dist/network.d.ts +20 -0
- package/dist/network.js +76 -0
- package/dist/orphans.d.ts +40 -0
- package/dist/orphans.js +235 -0
- package/dist/pending.d.ts +14 -0
- package/dist/pending.js +56 -0
- package/dist/pool.d.ts +7 -0
- package/dist/pool.js +57 -9
- package/dist/pr-title.d.ts +11 -0
- package/dist/pr-title.js +63 -0
- package/dist/recover.d.ts +10 -0
- package/dist/recover.js +104 -0
- package/dist/secret-scan.d.ts +19 -0
- package/dist/secret-scan.js +161 -0
- package/dist/session-stream.d.ts +9 -1
- package/dist/session-stream.js +20 -2
- package/dist/skills.d.ts +11 -0
- package/dist/skills.js +164 -0
- package/dist/stopped.d.ts +3 -0
- package/dist/stopped.js +10 -0
- package/dist/usage.d.ts +49 -0
- package/dist/usage.js +389 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +17 -0
- package/dist/viewer.js +7 -1
- package/dist/worktrees.d.ts +15 -0
- package/dist/worktrees.js +96 -0
- package/package.json +8 -5
- package/skill/firefunc/SKILL.md +222 -0
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
3
|
+
import { configPath } from './config.js';
|
|
4
|
+
import { CATALOG_ONLY, claudeCodeSignInFix, claudeCodeSignsFor } from './machine-logins.js';
|
|
5
|
+
import { execOut } from './exec.js';
|
|
6
|
+
import { startCredentialRelay } from './mcp-proxy.js';
|
|
7
|
+
const RELAYED_SERVERS = new Set(['github']);
|
|
8
|
+
const SERVER_ID_PATTERN = /^[a-z][a-z0-9_-]*$/;
|
|
9
|
+
const HEADER_NAME_PATTERN = /^[A-Za-z0-9!#$%&'*+.^_`|~-]+$/;
|
|
10
|
+
const HEADER_VALUE_PATTERN = /^[^\x00-\x1f\x7f]*$/;
|
|
11
|
+
const TOOL_NAME_PATTERN = /^[^\s\x00-\x1f\x7f]+$/;
|
|
12
|
+
const CONFIG_PATHS = {
|
|
13
|
+
claude: '.firefunc-mcp.json',
|
|
14
|
+
cursor: '.cursor/mcp.json',
|
|
15
|
+
gemini: '.gemini/settings.json',
|
|
16
|
+
codex: null,
|
|
17
|
+
copilot: '.firefunc-copilot-mcp.json',
|
|
18
|
+
};
|
|
19
|
+
export function mcpFormatForEngine(engine) {
|
|
20
|
+
switch (engine) {
|
|
21
|
+
case 'claude_code':
|
|
22
|
+
return 'claude';
|
|
23
|
+
case 'cursor_cli':
|
|
24
|
+
return 'cursor';
|
|
25
|
+
case 'gemini_cli':
|
|
26
|
+
return 'gemini';
|
|
27
|
+
case 'codex_cli':
|
|
28
|
+
return 'codex';
|
|
29
|
+
case 'copilot_cli':
|
|
30
|
+
return 'copilot';
|
|
31
|
+
default:
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function toolNamesFor(servers) {
|
|
36
|
+
const names = new Set();
|
|
37
|
+
for (const server of servers) {
|
|
38
|
+
for (const tool of server.allowedTools)
|
|
39
|
+
names.add(`mcp__${server.id}__${tool}`);
|
|
40
|
+
}
|
|
41
|
+
return [...names].sort();
|
|
42
|
+
}
|
|
43
|
+
function deniedNamesFor(servers) {
|
|
44
|
+
const names = new Set();
|
|
45
|
+
for (const server of servers) {
|
|
46
|
+
for (const tool of server.deniedTools ?? []) {
|
|
47
|
+
if (!TOOL_NAME_PATTERN.test(tool))
|
|
48
|
+
continue;
|
|
49
|
+
names.add(`mcp__${server.id}__${tool}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return [...names].sort();
|
|
53
|
+
}
|
|
54
|
+
function isRecord(v) {
|
|
55
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
56
|
+
}
|
|
57
|
+
function stringArray(v) {
|
|
58
|
+
if (!Array.isArray(v))
|
|
59
|
+
return null;
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const item of v) {
|
|
62
|
+
if (typeof item !== 'string' || !item)
|
|
63
|
+
return null;
|
|
64
|
+
out.push(item);
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function isAcceptableEndpoint(url) {
|
|
69
|
+
let parsed;
|
|
70
|
+
try {
|
|
71
|
+
parsed = new URL(url);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (parsed.protocol === 'https:')
|
|
77
|
+
return true;
|
|
78
|
+
return (parsed.protocol === 'http:' &&
|
|
79
|
+
(parsed.hostname === '127.0.0.1' ||
|
|
80
|
+
parsed.hostname === 'localhost' ||
|
|
81
|
+
parsed.hostname === '::1'));
|
|
82
|
+
}
|
|
83
|
+
function parseServer(raw) {
|
|
84
|
+
if (!isRecord(raw))
|
|
85
|
+
return { reject: 'it is not an object' };
|
|
86
|
+
const id = raw.id;
|
|
87
|
+
if (typeof id !== 'string' || !SERVER_ID_PATTERN.test(id) || id.includes('__')) {
|
|
88
|
+
return { reject: 'its id is not a lowercase slug without a double underscore' };
|
|
89
|
+
}
|
|
90
|
+
const transport = raw.transport;
|
|
91
|
+
if (transport !== 'stdio' && transport !== 'http' && transport !== 'sse') {
|
|
92
|
+
return { reject: `it declares an unknown transport (${String(transport)})` };
|
|
93
|
+
}
|
|
94
|
+
const auth = raw.auth;
|
|
95
|
+
if (auth !== 'minted' && auth !== 'oauth_local' && auth !== 'header' && auth !== 'none') {
|
|
96
|
+
return { reject: `it declares an unknown auth mode (${String(auth)})` };
|
|
97
|
+
}
|
|
98
|
+
const allowedTools = stringArray(raw.allowedTools);
|
|
99
|
+
if (!allowedTools)
|
|
100
|
+
return { reject: 'its allowedTools is not a list of tool names' };
|
|
101
|
+
const unusableName = allowedTools.find((name) => !TOOL_NAME_PATTERN.test(name));
|
|
102
|
+
if (unusableName !== undefined) {
|
|
103
|
+
return {
|
|
104
|
+
reject: `its allowedTools contain ${JSON.stringify(unusableName)}, which has whitespace or a control character in it — tool names are concatenated into the engine's allowlist argument, which is a command line`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const spec = { id, transport, auth, allowedTools };
|
|
108
|
+
if (raw.deniedTools !== undefined) {
|
|
109
|
+
const deniedTools = stringArray(raw.deniedTools);
|
|
110
|
+
if (!deniedTools)
|
|
111
|
+
return { reject: 'its deniedTools is not a list of tool names' };
|
|
112
|
+
const both = deniedTools.filter((name) => allowedTools.includes(name));
|
|
113
|
+
if (both.length) {
|
|
114
|
+
return {
|
|
115
|
+
reject: `its deniedTools name ${both.join(', ')}, which ${both.length === 1 ? 'is' : 'are'} also in its allowedTools — the server would offer and forbid the same tool`,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (deniedTools.length)
|
|
119
|
+
spec.deniedTools = deniedTools;
|
|
120
|
+
}
|
|
121
|
+
if (raw.writeTools !== undefined) {
|
|
122
|
+
const writeTools = stringArray(raw.writeTools);
|
|
123
|
+
if (!writeTools)
|
|
124
|
+
return { reject: 'its writeTools is not a list of tool names' };
|
|
125
|
+
const orphan = writeTools.filter((name) => !allowedTools.includes(name));
|
|
126
|
+
if (orphan.length) {
|
|
127
|
+
return {
|
|
128
|
+
reject: `its writeTools name ${orphan.join(', ')}, which ${orphan.length === 1 ? 'is' : 'are'} not in its allowedTools — the server would report a write it may not call`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
spec.writeTools = writeTools;
|
|
132
|
+
}
|
|
133
|
+
if (transport === 'stdio') {
|
|
134
|
+
if (typeof raw.command !== 'string' || !raw.command) {
|
|
135
|
+
return { reject: 'it is a stdio server with no command to launch' };
|
|
136
|
+
}
|
|
137
|
+
spec.command = raw.command;
|
|
138
|
+
if (raw.args !== undefined) {
|
|
139
|
+
const args = stringArray(raw.args);
|
|
140
|
+
if (!args)
|
|
141
|
+
return { reject: 'its args are not a list of strings' };
|
|
142
|
+
spec.args = args;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
if (typeof raw.url !== 'string' || !isAcceptableEndpoint(raw.url)) {
|
|
147
|
+
return {
|
|
148
|
+
reject: `it is an ${transport} server without an https endpoint (a bearer token may not be sent in the clear)`,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
spec.url = raw.url;
|
|
152
|
+
}
|
|
153
|
+
if (auth === 'header') {
|
|
154
|
+
if (transport === 'stdio') {
|
|
155
|
+
return {
|
|
156
|
+
reject: 'it is a stdio server declaring header auth, and a launched process has no request to put a header on',
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (typeof raw.headerName !== 'string' || !HEADER_NAME_PATTERN.test(raw.headerName)) {
|
|
160
|
+
return {
|
|
161
|
+
reject: 'it declares header auth without a valid `headerName` to send the credential under',
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
const hasRef = typeof raw.secretRef === 'string' && raw.secretRef.length > 0;
|
|
165
|
+
const hasInline = typeof raw.secret === 'string' && !!raw.secret && HEADER_VALUE_PATTERN.test(raw.secret);
|
|
166
|
+
if (!hasRef && !hasInline) {
|
|
167
|
+
return {
|
|
168
|
+
reject: 'it declares header auth without a `secretRef` to fetch the credential with, and without a usable inline `secret`',
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
spec.headerName = raw.headerName;
|
|
172
|
+
if (hasInline)
|
|
173
|
+
spec.secret = raw.secret;
|
|
174
|
+
if (hasRef)
|
|
175
|
+
spec.secretRef = raw.secretRef;
|
|
176
|
+
}
|
|
177
|
+
if (raw.headers !== undefined) {
|
|
178
|
+
if (!isRecord(raw.headers))
|
|
179
|
+
return { reject: 'its headers are not an object' };
|
|
180
|
+
const headers = {};
|
|
181
|
+
for (const [name, value] of Object.entries(raw.headers)) {
|
|
182
|
+
if (!HEADER_NAME_PATTERN.test(name))
|
|
183
|
+
return { reject: `header "${name}" is not a valid name` };
|
|
184
|
+
if (typeof value !== 'string' || !HEADER_VALUE_PATTERN.test(value)) {
|
|
185
|
+
return { reject: `header "${name}" has a value containing a control character` };
|
|
186
|
+
}
|
|
187
|
+
if (name.toLowerCase() === 'authorization')
|
|
188
|
+
continue;
|
|
189
|
+
headers[name] = value;
|
|
190
|
+
}
|
|
191
|
+
if (Object.keys(headers).length)
|
|
192
|
+
spec.headers = headers;
|
|
193
|
+
}
|
|
194
|
+
return { spec };
|
|
195
|
+
}
|
|
196
|
+
function parseRunConfig(raw, log) {
|
|
197
|
+
if (!isRecord(raw))
|
|
198
|
+
return null;
|
|
199
|
+
if (!Array.isArray(raw.servers))
|
|
200
|
+
return null;
|
|
201
|
+
const servers = [];
|
|
202
|
+
for (const entry of raw.servers) {
|
|
203
|
+
const parsed = parseServer(entry);
|
|
204
|
+
if ('reject' in parsed) {
|
|
205
|
+
log(`mcp: ignoring a server FireFunc sent — ${parsed.reject}.`);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
servers.push(parsed.spec);
|
|
209
|
+
}
|
|
210
|
+
const budget = raw.callBudget;
|
|
211
|
+
return {
|
|
212
|
+
servers,
|
|
213
|
+
callBudget: typeof budget === 'number' && Number.isInteger(budget) && budget > 0 ? budget : null,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
export function mcpLoginPath(server) {
|
|
217
|
+
return join(dirname(configPath()), 'mcp-logins', `${server}.json`);
|
|
218
|
+
}
|
|
219
|
+
const LOGIN_EXPIRY_SKEW_MS = 60_000;
|
|
220
|
+
function readCachedLogin(server, now) {
|
|
221
|
+
const file = mcpLoginPath(server);
|
|
222
|
+
if (!existsSync(file))
|
|
223
|
+
return { missing: 'none' };
|
|
224
|
+
let raw;
|
|
225
|
+
try {
|
|
226
|
+
raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return { missing: 'unreadable' };
|
|
230
|
+
}
|
|
231
|
+
if (!isRecord(raw))
|
|
232
|
+
return { missing: 'unreadable' };
|
|
233
|
+
const { accessToken, tokenType } = raw;
|
|
234
|
+
if (typeof accessToken !== 'string' || !accessToken)
|
|
235
|
+
return { missing: 'unreadable' };
|
|
236
|
+
if (typeof tokenType !== 'string' || !tokenType)
|
|
237
|
+
return { missing: 'unreadable' };
|
|
238
|
+
if (!HEADER_VALUE_PATTERN.test(accessToken) || !HEADER_VALUE_PATTERN.test(tokenType)) {
|
|
239
|
+
return { missing: 'unreadable' };
|
|
240
|
+
}
|
|
241
|
+
const expiresAt = typeof raw.expiresAt === 'string' ? raw.expiresAt : null;
|
|
242
|
+
const expiresMs = expiresAt === null ? Number.NaN : Date.parse(expiresAt);
|
|
243
|
+
if (!Number.isNaN(expiresMs) && expiresMs <= now + LOGIN_EXPIRY_SKEW_MS) {
|
|
244
|
+
return { missing: 'expired' };
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
login: {
|
|
248
|
+
server,
|
|
249
|
+
tokenType,
|
|
250
|
+
accessToken,
|
|
251
|
+
expiresAt,
|
|
252
|
+
accountLabel: typeof raw.accountLabel === 'string' ? raw.accountLabel : null,
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
const BOUNDABLE_ENGINES_FIX = 'Fix: run this project on Gemini CLI, whose config bounds each server to its declared tools (`includeTools`), or on Claude Code, where that bound is the `--allowedTools` flag rather than the config file';
|
|
257
|
+
function whyUnusable(spec, format) {
|
|
258
|
+
if (spec.allowedTools.length === 0) {
|
|
259
|
+
return 'no tools are declared for it, so it would connect and offer nothing';
|
|
260
|
+
}
|
|
261
|
+
const boundsTools = format !== 'cursor' && format !== 'codex';
|
|
262
|
+
if (spec.writeTools?.length && !boundsTools) {
|
|
263
|
+
return (`${format} has no per-tool allowlist, so every tool this server publishes would be callable — including the ${spec.writeTools.length} write tool(s) named in this spec, which is a mutation surface nothing here can bound. ` +
|
|
264
|
+
BOUNDABLE_ENGINES_FIX);
|
|
265
|
+
}
|
|
266
|
+
const unnarrowed = spec.auth === 'oauth_local' || spec.auth === 'header' || spec.auth === 'none';
|
|
267
|
+
if (unnarrowed && !boundsTools) {
|
|
268
|
+
if (spec.auth === 'none') {
|
|
269
|
+
return (`${format} has no per-tool allowlist, and this server takes no credential — so nothing on either side bounds which of its tools a run may call, and the ${spec.allowedTools.length} tool(s) named in its registration are advisory here. ` +
|
|
270
|
+
BOUNDABLE_ENGINES_FIX);
|
|
271
|
+
}
|
|
272
|
+
return (`${format} has no per-tool allowlist, so a server FireFunc did not narrow the credential for would expose whatever that account can do — only a read-only minted credential is injected here. ` +
|
|
273
|
+
BOUNDABLE_ENGINES_FIX);
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const EXCLUDE_HEADER = '# FireFunc: never commit an injected MCP config (it holds a live token)';
|
|
278
|
+
async function excludeFromGit(worktree, patterns, log) {
|
|
279
|
+
try {
|
|
280
|
+
const common = await execOut('git', ['-C', worktree, 'rev-parse', '--git-common-dir']);
|
|
281
|
+
if (!common)
|
|
282
|
+
return false;
|
|
283
|
+
const dir = isAbsolute(common) ? common : join(worktree, common);
|
|
284
|
+
const info = join(dir, 'info');
|
|
285
|
+
mkdirSync(info, { recursive: true });
|
|
286
|
+
const file = join(info, 'exclude');
|
|
287
|
+
const existing = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
288
|
+
const lines = existing.split('\n');
|
|
289
|
+
const missing = patterns.filter((p) => !lines.includes(p));
|
|
290
|
+
if (missing.length) {
|
|
291
|
+
const header = existing.includes(EXCLUDE_HEADER) ? '' : `\n${EXCLUDE_HEADER}\n`;
|
|
292
|
+
appendFileSync(file, `${header}${missing.join('\n')}\n`);
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
catch (e) {
|
|
297
|
+
log(`mcp: could not write the git exclude (${e.message}) — MCP is off for this run.`);
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function tomlString(value) {
|
|
302
|
+
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
303
|
+
}
|
|
304
|
+
function codexTokenVar(id) {
|
|
305
|
+
return `FIREFUNC_MCP_${id.toUpperCase().replace(/[^A-Z0-9]/g, '_')}_TOKEN`;
|
|
306
|
+
}
|
|
307
|
+
function credentialHeader(cred) {
|
|
308
|
+
if (!cred)
|
|
309
|
+
return null;
|
|
310
|
+
return cred.kind === 'bearer'
|
|
311
|
+
? { name: 'Authorization', value: `${cred.scheme} ${cred.token}` }
|
|
312
|
+
: { name: cred.name, value: cred.value };
|
|
313
|
+
}
|
|
314
|
+
function credentialHeaders(cred) {
|
|
315
|
+
const header = credentialHeader(cred);
|
|
316
|
+
return header ? { [header.name]: header.value } : {};
|
|
317
|
+
}
|
|
318
|
+
function claudeBody(resolved) {
|
|
319
|
+
const servers = {};
|
|
320
|
+
for (const { spec, cred } of resolved) {
|
|
321
|
+
servers[spec.id] =
|
|
322
|
+
spec.transport === 'stdio'
|
|
323
|
+
? { type: 'stdio', command: spec.command, ...(spec.args ? { args: spec.args } : {}) }
|
|
324
|
+
: {
|
|
325
|
+
type: spec.transport,
|
|
326
|
+
url: spec.url,
|
|
327
|
+
headers: { ...spec.headers, ...credentialHeaders(cred) },
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
return `${JSON.stringify({ mcpServers: servers }, null, 2)}\n`;
|
|
331
|
+
}
|
|
332
|
+
function cursorBody(resolved) {
|
|
333
|
+
const servers = {};
|
|
334
|
+
for (const { spec, cred } of resolved) {
|
|
335
|
+
servers[spec.id] =
|
|
336
|
+
spec.transport === 'stdio'
|
|
337
|
+
? { command: spec.command, ...(spec.args ? { args: spec.args } : {}) }
|
|
338
|
+
: {
|
|
339
|
+
url: spec.url,
|
|
340
|
+
headers: { ...spec.headers, ...credentialHeaders(cred) },
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
return `${JSON.stringify({ mcpServers: servers }, null, 2)}\n`;
|
|
344
|
+
}
|
|
345
|
+
function geminiBody(resolved) {
|
|
346
|
+
const servers = {};
|
|
347
|
+
for (const { spec, cred } of resolved) {
|
|
348
|
+
const headers = { ...spec.headers, ...credentialHeaders(cred) };
|
|
349
|
+
servers[spec.id] =
|
|
350
|
+
spec.transport === 'stdio'
|
|
351
|
+
? {
|
|
352
|
+
command: spec.command,
|
|
353
|
+
...(spec.args ? { args: spec.args } : {}),
|
|
354
|
+
includeTools: spec.allowedTools,
|
|
355
|
+
}
|
|
356
|
+
: {
|
|
357
|
+
...(spec.transport === 'http' ? { httpUrl: spec.url } : { url: spec.url }),
|
|
358
|
+
headers,
|
|
359
|
+
includeTools: spec.allowedTools,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
return `${JSON.stringify({ mcpServers: servers }, null, 2)}\n`;
|
|
363
|
+
}
|
|
364
|
+
function codexOverrides(resolved) {
|
|
365
|
+
const overrides = [];
|
|
366
|
+
const env = {};
|
|
367
|
+
let needsHttpClient = false;
|
|
368
|
+
for (const { spec, cred } of resolved) {
|
|
369
|
+
const base = `mcp_servers.${spec.id}`;
|
|
370
|
+
if (spec.transport === 'stdio') {
|
|
371
|
+
overrides.push(`${base}.command=${tomlString(spec.command ?? '')}`);
|
|
372
|
+
if (spec.args?.length) {
|
|
373
|
+
overrides.push(`${base}.args=[${spec.args.map(tomlString).join(',')}]`);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
needsHttpClient = true;
|
|
378
|
+
overrides.push(`${base}.url=${tomlString(spec.url ?? '')}`);
|
|
379
|
+
for (const [name, value] of Object.entries(spec.headers ?? {})) {
|
|
380
|
+
overrides.push(`${base}.http_headers.${name}=${tomlString(value)}`);
|
|
381
|
+
}
|
|
382
|
+
const header = credentialHeader(cred);
|
|
383
|
+
if (cred && header) {
|
|
384
|
+
const varName = codexTokenVar(spec.id);
|
|
385
|
+
if (cred.kind === 'bearer' && cred.scheme === 'Bearer') {
|
|
386
|
+
env[varName] = cred.token;
|
|
387
|
+
overrides.push(`${base}.bearer_token_env_var=${tomlString(varName)}`);
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
env[varName] = header.value;
|
|
391
|
+
overrides.push(`${base}.env_http_headers.${header.name}=${tomlString(varName)}`);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (needsHttpClient)
|
|
397
|
+
overrides.unshift('experimental_use_rmcp_client=true');
|
|
398
|
+
return { overrides, env };
|
|
399
|
+
}
|
|
400
|
+
const NO_LOG = () => { };
|
|
401
|
+
export async function prepareMcp(opts) {
|
|
402
|
+
const log = opts.log ?? NO_LOG;
|
|
403
|
+
const config = parseRunConfig(opts.raw, log);
|
|
404
|
+
if (!config || config.servers.length === 0)
|
|
405
|
+
return null;
|
|
406
|
+
const usable = [];
|
|
407
|
+
for (const spec of config.servers) {
|
|
408
|
+
const reason = whyUnusable(spec, opts.format);
|
|
409
|
+
if (reason) {
|
|
410
|
+
log(`mcp: skipping ${spec.id} on ${opts.format} — ${reason}.`);
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
usable.push(spec);
|
|
414
|
+
}
|
|
415
|
+
if (!usable.length)
|
|
416
|
+
return null;
|
|
417
|
+
const relative = CONFIG_PATHS[opts.format];
|
|
418
|
+
let target = null;
|
|
419
|
+
if (relative) {
|
|
420
|
+
target = join(opts.worktree, relative);
|
|
421
|
+
if (existsSync(target)) {
|
|
422
|
+
log(`mcp: ${relative} already exists in this repo, so MCP is off for this run — FireFunc will not overwrite a file the repo owns, and an exclude cannot protect a tracked one. ` +
|
|
423
|
+
`Fix: remove ${relative} from the repository, or run this project on an engine that reads a different path.`);
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
const excluded = await excludeFromGit(opts.worktree, [`/${relative}`], log);
|
|
427
|
+
if (!excluded) {
|
|
428
|
+
log('mcp: MCP is off for this run because the config could not be kept out of the commit. ' +
|
|
429
|
+
'Fix: check that the runner can write .git/info/exclude in the repository checkout.');
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const now = Date.now();
|
|
434
|
+
const resolved = [];
|
|
435
|
+
const relays = [];
|
|
436
|
+
const relayedIds = new Set();
|
|
437
|
+
const closeRelays = () => {
|
|
438
|
+
for (const r of relays)
|
|
439
|
+
r.close();
|
|
440
|
+
};
|
|
441
|
+
for (const spec of usable) {
|
|
442
|
+
if (spec.auth === 'none') {
|
|
443
|
+
resolved.push({ spec, cred: null });
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
if (spec.auth === 'header') {
|
|
447
|
+
if (!spec.headerName) {
|
|
448
|
+
log(`mcp: skipping ${spec.id} — its credential did not survive validation.`);
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
let value = spec.secret;
|
|
452
|
+
if (!value) {
|
|
453
|
+
const fetched = await opts.api.mcpCredential(opts.runId, spec.id).catch(() => null);
|
|
454
|
+
value = fetched?.token;
|
|
455
|
+
}
|
|
456
|
+
if (!value) {
|
|
457
|
+
log(`mcp: skipping ${spec.id} — FireFunc issued no credential for it. ` +
|
|
458
|
+
'Fix: check the server is still granted to this workspace and its secret is set, in Settings → Connections. ' +
|
|
459
|
+
'The run continues without it, on the context pack it already has.');
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
resolved.push({
|
|
463
|
+
spec,
|
|
464
|
+
cred: { kind: 'header', name: spec.headerName, value },
|
|
465
|
+
});
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
if (spec.auth === 'oauth_local') {
|
|
469
|
+
if (spec.transport === 'stdio') {
|
|
470
|
+
resolved.push({ spec, cred: null });
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
const found = readCachedLogin(spec.id, now);
|
|
474
|
+
if ('missing' in found &&
|
|
475
|
+
opts.format === 'claude' &&
|
|
476
|
+
spec.url &&
|
|
477
|
+
claudeCodeSignsFor(spec.id, spec.url, now)) {
|
|
478
|
+
log(`mcp: ${spec.id} — this machine is not logged in through the runner, but Claude Code holds its own sign-in for ${spec.url}; the run uses that.`);
|
|
479
|
+
resolved.push({ spec, cred: null });
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if ('missing' in found) {
|
|
483
|
+
const why = found.missing === 'expired'
|
|
484
|
+
? 'the login cached on this machine has expired'
|
|
485
|
+
: found.missing === 'unreadable'
|
|
486
|
+
? 'the login cached on this machine could not be read'
|
|
487
|
+
: 'this machine is not logged in to it';
|
|
488
|
+
log(`mcp: skipping ${spec.id} — ${why}. ` +
|
|
489
|
+
`Fix: run \`firefunc-runner mcp login ${spec.id}\` on this machine. ` +
|
|
490
|
+
(opts.format === 'claude' && CATALOG_ONLY.has(spec.id)
|
|
491
|
+
? `${spec.id} admits only clients in its own catalog, so the runner does not connect ` +
|
|
492
|
+
`there and that command has nothing to register. Claude Code is a catalog client: ` +
|
|
493
|
+
`${claudeCodeSignInFix(spec.id)}. Note it meters per seat, so runs here spend that ` +
|
|
494
|
+
`person's allowance. `
|
|
495
|
+
: '') +
|
|
496
|
+
'The run continues without it, on the context pack it already has.');
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
resolved.push({
|
|
500
|
+
spec,
|
|
501
|
+
cred: { kind: 'bearer', scheme: found.login.tokenType, token: found.login.accessToken },
|
|
502
|
+
});
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
const minted = await opts.api.mcpCredential(opts.runId, spec.id).catch(() => null);
|
|
506
|
+
if (!minted?.token) {
|
|
507
|
+
log(`mcp: skipping ${spec.id} — FireFunc issued no read-only credential for it. ` +
|
|
508
|
+
'Fix: the API log names the reason; a 403 usually means GitHub is not connected to this workspace, and a 503 that the GitHub App is not configured. The context pack still carries the repository context.');
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
if (opts.relay && RELAYED_SERVERS.has(spec.id) && spec.transport !== 'stdio' && spec.url) {
|
|
512
|
+
const relay = await startCredentialRelay({
|
|
513
|
+
upstream: spec.url,
|
|
514
|
+
name: spec.id,
|
|
515
|
+
initial: minted,
|
|
516
|
+
renew: () => opts.api.mcpCredential(opts.runId, spec.id).catch(() => null),
|
|
517
|
+
log,
|
|
518
|
+
}).catch(() => null);
|
|
519
|
+
if (relay) {
|
|
520
|
+
relays.push(relay);
|
|
521
|
+
relayedIds.add(spec.id);
|
|
522
|
+
resolved.push({ spec: { ...spec, url: relay.url }, cred: null });
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
log(`mcp: the ${spec.id} relay could not start — the token is written into the config instead, and ${spec.id} calls fail once it expires (an hour after it was issued).`);
|
|
526
|
+
}
|
|
527
|
+
resolved.push({ spec, cred: { kind: 'bearer', scheme: 'Bearer', token: minted.token } });
|
|
528
|
+
}
|
|
529
|
+
if (!resolved.length) {
|
|
530
|
+
closeRelays();
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
const written = resolved.map((r) => r.spec);
|
|
534
|
+
let inlineOverrides = [];
|
|
535
|
+
let env = {};
|
|
536
|
+
if (target) {
|
|
537
|
+
try {
|
|
538
|
+
mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
|
|
539
|
+
const body = opts.format === 'claude' || opts.format === 'copilot'
|
|
540
|
+
? claudeBody(resolved)
|
|
541
|
+
: opts.format === 'cursor'
|
|
542
|
+
? cursorBody(resolved)
|
|
543
|
+
: geminiBody(resolved);
|
|
544
|
+
writeFileSync(target, body, { mode: 0o600 });
|
|
545
|
+
}
|
|
546
|
+
catch (e) {
|
|
547
|
+
log(`mcp: could not write ${relative} (${e.message}) — MCP is off for this run.`);
|
|
548
|
+
closeRelays();
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
else {
|
|
553
|
+
const codex = codexOverrides(resolved);
|
|
554
|
+
inlineOverrides = codex.overrides;
|
|
555
|
+
env = codex.env;
|
|
556
|
+
}
|
|
557
|
+
let cleaned = false;
|
|
558
|
+
const writtenPath = target;
|
|
559
|
+
const cleanup = () => {
|
|
560
|
+
if (cleaned)
|
|
561
|
+
return;
|
|
562
|
+
cleaned = true;
|
|
563
|
+
closeRelays();
|
|
564
|
+
if (!writtenPath)
|
|
565
|
+
return;
|
|
566
|
+
try {
|
|
567
|
+
rmSync(writtenPath, { force: true });
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
const budget = config.callBudget === null
|
|
573
|
+
? 'no call budget sent'
|
|
574
|
+
: `call budget ${config.callBudget} (recorded — no CLI here can enforce it)`;
|
|
575
|
+
const logLine = `${written.length} server(s) — ${written
|
|
576
|
+
.map((s) => {
|
|
577
|
+
const total = s.allowedTools.length;
|
|
578
|
+
const writes = s.writeTools?.length ?? 0;
|
|
579
|
+
const tools = s.writeTools
|
|
580
|
+
? `${total - writes} read + ${writes} write`
|
|
581
|
+
: `${total} tool${total === 1 ? '' : 's'} (unclassified)`;
|
|
582
|
+
return `${s.id} (${s.transport}, ${s.auth}${relayedIds.has(s.id) ? ', through the renewing relay' : ''}, ${tools})`;
|
|
583
|
+
})
|
|
584
|
+
.join(', ')}; ${budget}`;
|
|
585
|
+
return {
|
|
586
|
+
format: opts.format,
|
|
587
|
+
configPath: target,
|
|
588
|
+
serverIds: written.map((s) => s.id),
|
|
589
|
+
toolNames: toolNamesFor(written),
|
|
590
|
+
writeToolNames: toolNamesFor(written.map((s) => ({ ...s, allowedTools: s.writeTools ?? [] }))),
|
|
591
|
+
deniedToolNames: deniedNamesFor(written),
|
|
592
|
+
env,
|
|
593
|
+
inlineOverrides,
|
|
594
|
+
callBudget: config.callBudget,
|
|
595
|
+
logLine,
|
|
596
|
+
cleanup,
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
export function mcpFromContext(ctx, format) {
|
|
600
|
+
const raw = ctx.mcp;
|
|
601
|
+
if (!isRecord(raw))
|
|
602
|
+
return null;
|
|
603
|
+
if (raw.format !== format)
|
|
604
|
+
return null;
|
|
605
|
+
if (!Array.isArray(raw.serverIds) || !Array.isArray(raw.toolNames))
|
|
606
|
+
return null;
|
|
607
|
+
if (!Array.isArray(raw.inlineOverrides))
|
|
608
|
+
return null;
|
|
609
|
+
return raw;
|
|
610
|
+
}
|
|
611
|
+
//# sourceMappingURL=mcp.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare function isNetworkError(input: unknown): boolean;
|
|
2
|
+
export declare function networkFailureHint(text: string): string | null;
|
|
3
|
+
export declare const netDeps: {
|
|
4
|
+
sleep: (ms: number) => Promise<void>;
|
|
5
|
+
now: () => number;
|
|
6
|
+
};
|
|
7
|
+
export type WaitOptions = {
|
|
8
|
+
probe: () => Promise<boolean>;
|
|
9
|
+
maxWaitMs: number;
|
|
10
|
+
log?: (line: string) => void;
|
|
11
|
+
initialDelayMs?: number;
|
|
12
|
+
maxDelayMs?: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function waitForNetwork(opts: WaitOptions): Promise<boolean>;
|
|
15
|
+
export declare function withNetworkRetry<T>(fn: () => Promise<T>, opts: {
|
|
16
|
+
label: string;
|
|
17
|
+
maxWaitMs: number;
|
|
18
|
+
probe: () => Promise<boolean>;
|
|
19
|
+
log?: (l: string) => void;
|
|
20
|
+
}): Promise<T>;
|