@yolo-labs/yolobridge 0.19.0 → 0.20.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/dist/cli.js +12 -0
- package/dist/local-mcp-tools.js +268 -0
- package/dist/mcp-proxy.js +144 -10
- package/dist/share-cmd.js +8 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -349,6 +349,18 @@ async function cmdAttach(args) {
|
|
|
349
349
|
// guess which studio_list_tiles row is itself — and a backwards
|
|
350
350
|
// guess sends the prompt into its OWN input.
|
|
351
351
|
callerTileId: tileId,
|
|
352
|
+
// Tools the proxy serves ITSELF (local-mcp-tools.ts). A cloud tool
|
|
353
|
+
// cannot read this machine's disk, so sharing a local file is the
|
|
354
|
+
// one thing that has to be answered here.
|
|
355
|
+
//
|
|
356
|
+
// `implicitRoots` is the directory the daemon was launched in — the
|
|
357
|
+
// project the agent is already working in and can read anyway.
|
|
358
|
+
// Anything outside needs `yolo-bridge allow`.
|
|
359
|
+
localTools: {
|
|
360
|
+
workspaceId,
|
|
361
|
+
implicitRoots: [process.cwd()],
|
|
362
|
+
commonApiBaseUrl: apiUrl(),
|
|
363
|
+
},
|
|
352
364
|
log: (line) => process.stdout.write(`${line}\n`),
|
|
353
365
|
});
|
|
354
366
|
// Command-line MCP configuration, never a file in the project
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tools the daemon serves ITSELF, instead of forwarding to the cloud MCP.
|
|
3
|
+
*
|
|
4
|
+
* WHY ANY TOOL IS LOCAL. `mcp-proxy.ts` forwards the attached agent's MCP
|
|
5
|
+
* traffic to the cloud `yolo-studio-mcp`, and the agent already has full
|
|
6
|
+
* workspace-wide scope there. But a cloud tool executes in the cloud and cannot
|
|
7
|
+
* read the operator's disk. Sharing a local file is therefore the one thing the
|
|
8
|
+
* proxy has to answer on its own.
|
|
9
|
+
*
|
|
10
|
+
* TWO PROPERTIES THIS FILE EXISTS TO KEEP:
|
|
11
|
+
*
|
|
12
|
+
* 1. A local call is NEVER FORWARDED, so the operator's file never becomes a
|
|
13
|
+
* cloud request, and no delegated cloud token is minted for it. The
|
|
14
|
+
* interception in `mcp-proxy.ts` runs BEFORE `getToken()` for exactly this
|
|
15
|
+
* reason — see the call site.
|
|
16
|
+
* 2. The upload opens the RESOLVED path the approval check returned, never
|
|
17
|
+
* the string the agent passed. Checking one path and opening another is
|
|
18
|
+
* how symlink containment gets defeated: an in-tree link can be repointed
|
|
19
|
+
* between the two.
|
|
20
|
+
*
|
|
21
|
+
* ⚠️ THE APPROVAL LIST IS NOT A SECURITY BOUNDARY. The agent runs as the same
|
|
22
|
+
* OS user with shell access, so it can widen the list itself, and it could
|
|
23
|
+
* already `curl -T` a file out without us. What this buys is that ACCIDENTS are
|
|
24
|
+
* prevented, the credentialed path stays deliberate, and grants are auditable.
|
|
25
|
+
* Do not write a message here claiming more than that.
|
|
26
|
+
*/
|
|
27
|
+
import { checkPathApproved } from './approved-paths.js';
|
|
28
|
+
import { runShare, describeShareFailure } from './share-cmd.js';
|
|
29
|
+
/** Namespaced so it can never collide with a forwarded cloud tool name. */
|
|
30
|
+
export const SHARE_FILE_TOOL = 'yolobridge_share_file';
|
|
31
|
+
export const LOCAL_TOOL_DEFINITIONS = [
|
|
32
|
+
{
|
|
33
|
+
name: SHARE_FILE_TOOL,
|
|
34
|
+
description: 'Send a file from the LOCAL machine this agent is running on up to the cloud workspace, '
|
|
35
|
+
+ 'so a cloud agent can see it. Push only — this cannot read paths the operator has not '
|
|
36
|
+
+ 'approved, and nothing in the cloud can pull files from this machine.',
|
|
37
|
+
inputSchema: {
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {
|
|
40
|
+
path: {
|
|
41
|
+
type: 'string',
|
|
42
|
+
description: 'Path to the local file to send. Must be inside an approved directory.',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
required: ['path'],
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
function toolResult(id, text, isError = false) {
|
|
51
|
+
return {
|
|
52
|
+
jsonrpc: '2.0',
|
|
53
|
+
id,
|
|
54
|
+
result: { content: [{ type: 'text', text }], ...(isError ? { isError: true } : {}) },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Is this parsed JSON-RPC message a call to one of our local tools? */
|
|
58
|
+
export function isLocalToolCall(msg) {
|
|
59
|
+
const m = msg;
|
|
60
|
+
if (!m || m.method !== 'tools/call')
|
|
61
|
+
return false;
|
|
62
|
+
return typeof m.params?.name === 'string'
|
|
63
|
+
&& LOCAL_TOOL_DEFINITIONS.some((t) => t.name === m.params.name);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Run one local tool call and produce its JSON-RPC response.
|
|
67
|
+
*
|
|
68
|
+
* Errors come back as a tool result with `isError`, not a JSON-RPC error: a
|
|
69
|
+
* refused path is a normal answer the agent should read and act on, not a
|
|
70
|
+
* protocol fault.
|
|
71
|
+
*/
|
|
72
|
+
export async function runLocalToolCall(msg, ctx) {
|
|
73
|
+
// A message with NO `id` is a notification: JSON-RPC says do the work and
|
|
74
|
+
// send nothing back. `msg.id ?? null` would have turned that into a normal
|
|
75
|
+
// reply carrying `id: null` — a spurious entry in a batch, and a body where a
|
|
76
|
+
// notification-only request expects none. An EXPLICIT null id is a different
|
|
77
|
+
// thing and still gets answered. (codex P2, gpt-5.6-sol.)
|
|
78
|
+
const isNotification = !('id' in msg);
|
|
79
|
+
const id = msg.id ?? null;
|
|
80
|
+
const name = msg.params?.name;
|
|
81
|
+
const args = msg.params?.arguments ?? {};
|
|
82
|
+
if (name !== SHARE_FILE_TOOL) {
|
|
83
|
+
return isNotification ? undefined : toolResult(id, `Unknown local tool: ${String(name)}`, true);
|
|
84
|
+
}
|
|
85
|
+
const rawPath = typeof args.path === 'string' ? args.path : '';
|
|
86
|
+
if (!rawPath)
|
|
87
|
+
return isNotification ? undefined : toolResult(id, 'A `path` is required.', true);
|
|
88
|
+
const check = (ctx.checkImpl ?? checkPathApproved)(rawPath, ctx.workspaceId, ctx.implicitRoots);
|
|
89
|
+
if (!check.approved || !check.resolvedPath) {
|
|
90
|
+
return isNotification ? undefined : toolResult(id, check.reason ?? `${rawPath} is not an approved path.`, true);
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
// Property 2: open what the check RESOLVED, not what the caller passed.
|
|
94
|
+
const result = await (ctx.shareImpl ?? runShare)(check.resolvedPath, {
|
|
95
|
+
commonApiBaseUrl: ctx.commonApiBaseUrl,
|
|
96
|
+
// The approval was checked against THIS workspace. The attachment on disk
|
|
97
|
+
// can have been replaced by a second `attach` since the proxy started, so
|
|
98
|
+
// bind the upload to the same workspace or refuse. (codex P2.)
|
|
99
|
+
expectedWorkspaceId: ctx.workspaceId,
|
|
100
|
+
write: () => { },
|
|
101
|
+
});
|
|
102
|
+
if (isNotification)
|
|
103
|
+
return undefined;
|
|
104
|
+
if (!result.ok)
|
|
105
|
+
return toolResult(id, result.message, true);
|
|
106
|
+
return toolResult(id, `Shared as asset ${result.assetId}. It is now visible in the cloud workspace.`);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
return isNotification ? undefined : toolResult(id, describeShareFailure(err), true);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Split an incoming body into "answered here" and "still needs the cloud".
|
|
114
|
+
*
|
|
115
|
+
* Handles the batch form because `injectToken` already has to, and a batch that
|
|
116
|
+
* mixes a local tool with cloud tools must not lose either half.
|
|
117
|
+
*
|
|
118
|
+
* Returns `undefined` when nothing is local — the overwhelmingly common case,
|
|
119
|
+
* so the normal path pays one `JSON.parse` and nothing else.
|
|
120
|
+
*/
|
|
121
|
+
export async function interceptLocalTools(rawBody, ctx) {
|
|
122
|
+
let parsed;
|
|
123
|
+
try {
|
|
124
|
+
parsed = JSON.parse(rawBody);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
if (!Array.isArray(parsed)) {
|
|
130
|
+
if (!isLocalToolCall(parsed))
|
|
131
|
+
return undefined;
|
|
132
|
+
const response = await runLocalToolCall(parsed, ctx);
|
|
133
|
+
// A notification produced no response; there is still nothing to forward.
|
|
134
|
+
return response
|
|
135
|
+
? { localResponse: JSON.stringify(response), localIds: [parsed.id ?? null] }
|
|
136
|
+
: { localIds: [] };
|
|
137
|
+
}
|
|
138
|
+
const localIdx = parsed.map((m, i) => (isLocalToolCall(m) ? i : -1)).filter((i) => i >= 0);
|
|
139
|
+
if (!localIdx.length)
|
|
140
|
+
return undefined;
|
|
141
|
+
const responses = [];
|
|
142
|
+
for (const i of localIdx) {
|
|
143
|
+
const r = await runLocalToolCall(parsed[i], ctx);
|
|
144
|
+
if (r)
|
|
145
|
+
responses.push(r);
|
|
146
|
+
}
|
|
147
|
+
const remainder = parsed.filter((_, i) => !localIdx.includes(i));
|
|
148
|
+
return {
|
|
149
|
+
localResponse: responses.length ? JSON.stringify(responses) : undefined,
|
|
150
|
+
forwardBody: remainder.length ? JSON.stringify(remainder) : undefined,
|
|
151
|
+
localIds: localIdx.map((i) => parsed[i]?.id ?? null),
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Does this request contain a `tools/list`, whose reply we must rewrite to
|
|
156
|
+
* advertise the locally-served tools?
|
|
157
|
+
*/
|
|
158
|
+
export function requestWantsToolsList(rawBody) {
|
|
159
|
+
try {
|
|
160
|
+
const parsed = JSON.parse(rawBody);
|
|
161
|
+
const messages = Array.isArray(parsed) ? parsed : [parsed];
|
|
162
|
+
return messages.some((m) => m?.method === 'tools/list');
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const SSE_DATA = /^data:\s?(.*)$/;
|
|
169
|
+
export function parseRpcEnvelope(text) {
|
|
170
|
+
const trimmed = text.trimStart();
|
|
171
|
+
// Plain JSON — the common case.
|
|
172
|
+
if (trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
173
|
+
try {
|
|
174
|
+
const parsed = JSON.parse(text);
|
|
175
|
+
const wasArray = Array.isArray(parsed);
|
|
176
|
+
return {
|
|
177
|
+
messages: wasArray ? parsed : [parsed],
|
|
178
|
+
rebuild: (m) => JSON.stringify(wasArray ? m : (m[0] ?? null)),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// SSE framing: one or more `data:` lines, blank-line separated.
|
|
186
|
+
if (!/(^|\n)data:/.test(text))
|
|
187
|
+
return undefined;
|
|
188
|
+
const lines = text.split(/\r?\n/);
|
|
189
|
+
const dataIdx = [];
|
|
190
|
+
const messages = [];
|
|
191
|
+
lines.forEach((line, i) => {
|
|
192
|
+
const m = SSE_DATA.exec(line);
|
|
193
|
+
if (!m)
|
|
194
|
+
return;
|
|
195
|
+
try {
|
|
196
|
+
messages.push(JSON.parse(m[1]));
|
|
197
|
+
dataIdx.push(i);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
/* a non-JSON data line (a keepalive, say) is left exactly as it is */
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (!messages.length)
|
|
204
|
+
return undefined;
|
|
205
|
+
return {
|
|
206
|
+
messages,
|
|
207
|
+
rebuild: (next) => {
|
|
208
|
+
const out = [...lines];
|
|
209
|
+
// Rewrite the frames we parsed, in order...
|
|
210
|
+
next.slice(0, dataIdx.length).forEach((msg, i) => {
|
|
211
|
+
out[dataIdx[i]] = `data: ${JSON.stringify(msg)}`;
|
|
212
|
+
});
|
|
213
|
+
// ...and append any EXTRA messages as their own frames, so a merged-in
|
|
214
|
+
// local result reaches a client that negotiated a stream.
|
|
215
|
+
const extra = next.slice(dataIdx.length);
|
|
216
|
+
const tail = extra.map((msg) => `event: message\ndata: ${JSON.stringify(msg)}\n`);
|
|
217
|
+
return tail.length ? `${out.join('\n').replace(/\n*$/, '\n\n')}${tail.join('\n')}\n` : out.join('\n');
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Add our tools to an upstream `tools/list` reply, so the agent can discover
|
|
223
|
+
* them alongside the cloud ones.
|
|
224
|
+
*
|
|
225
|
+
* Returns the text unchanged on anything unexpected — a malformed or
|
|
226
|
+
* error-shaped upstream reply must pass through untouched rather than be
|
|
227
|
+
* rewritten into something that only looks well-formed.
|
|
228
|
+
*/
|
|
229
|
+
export function augmentToolsList(requestBody, responseText) {
|
|
230
|
+
let request;
|
|
231
|
+
try {
|
|
232
|
+
request = JSON.parse(requestBody);
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return responseText;
|
|
236
|
+
}
|
|
237
|
+
const requests = Array.isArray(request) ? request : [request];
|
|
238
|
+
const listIds = new Set(requests
|
|
239
|
+
.filter((m) => m?.method === 'tools/list')
|
|
240
|
+
.map((m) => m?.id)
|
|
241
|
+
.filter((id) => id !== undefined));
|
|
242
|
+
if (!listIds.size)
|
|
243
|
+
return responseText;
|
|
244
|
+
const envelope = parseRpcEnvelope(responseText);
|
|
245
|
+
if (!envelope)
|
|
246
|
+
return responseText;
|
|
247
|
+
let changed = false;
|
|
248
|
+
for (const m of envelope.messages) {
|
|
249
|
+
if (!listIds.has(m?.id))
|
|
250
|
+
continue;
|
|
251
|
+
if (!Array.isArray(m?.result?.tools))
|
|
252
|
+
continue;
|
|
253
|
+
// Paginated discovery: a client that follows `nextCursor` aggregates every
|
|
254
|
+
// page, so appending on each one yields duplicate tool names and an
|
|
255
|
+
// ambiguous or rejected registration. Add them to the FINAL page only, so
|
|
256
|
+
// they appear exactly once across the sequence. (codex P2.)
|
|
257
|
+
if (m.result.nextCursor !== undefined && m.result.nextCursor !== null)
|
|
258
|
+
continue;
|
|
259
|
+
const present = new Set(m.result.tools.map((t) => t?.name));
|
|
260
|
+
for (const def of LOCAL_TOOL_DEFINITIONS) {
|
|
261
|
+
if (!present.has(def.name)) {
|
|
262
|
+
m.result.tools.push({ ...def });
|
|
263
|
+
changed = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return changed ? envelope.rebuild(envelope.messages) : responseText;
|
|
268
|
+
}
|
package/dist/mcp-proxy.js
CHANGED
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
* proxy serves have disjoint capabilities — see `providedSecrets` below.
|
|
70
70
|
*/
|
|
71
71
|
import * as http from 'node:http';
|
|
72
|
+
import { interceptLocalTools, augmentToolsList, parseRpcEnvelope } from './local-mcp-tools.js';
|
|
72
73
|
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
73
74
|
const DEFAULT_MCP_URL = 'https://services.yolo.studio';
|
|
74
75
|
export function mcpUrl() {
|
|
@@ -275,7 +276,7 @@ export async function startMcpProxy(opts) {
|
|
|
275
276
|
res.end(JSON.stringify({ error: 'missing or invalid proxy credential' }));
|
|
276
277
|
return;
|
|
277
278
|
}
|
|
278
|
-
handleRequest(req, res, upstream, tokenCache.getToken, tokenCache.forceRefresh, fetchImpl, tracker, log).catch((err) => {
|
|
279
|
+
handleRequest(req, res, upstream, tokenCache.getToken, tokenCache.forceRefresh, fetchImpl, tracker, log, opts.localTools).catch((err) => {
|
|
279
280
|
log(`yolo-bridge: local MCP proxy error: ${err instanceof Error ? err.message : String(err)}`);
|
|
280
281
|
if (!res.headersSent) {
|
|
281
282
|
res.writeHead(502, { 'Content-Type': 'application/json' });
|
|
@@ -495,6 +496,39 @@ function mergeRetryResponses(originalResponseText, retryResponseText, retriedIds
|
|
|
495
496
|
return originalResponseText;
|
|
496
497
|
}
|
|
497
498
|
}
|
|
499
|
+
/**
|
|
500
|
+
* Answer a mixed batch whose CLOUD half could not be sent, without discarding
|
|
501
|
+
* the local half that already ran.
|
|
502
|
+
*
|
|
503
|
+
* The local calls have side effects — a file is uploaded by the time we get
|
|
504
|
+
* here — so silently 503ing the whole batch would hide a completed upload and
|
|
505
|
+
* invite the client to repeat it. Each un-forwardable id gets an explicit
|
|
506
|
+
* JSON-RPC error instead. (codex P2, gpt-5.6-sol.)
|
|
507
|
+
*/
|
|
508
|
+
export function mergeLocalWithRemoteFailure(localResponseText, forwardBody, message) {
|
|
509
|
+
const local = (() => {
|
|
510
|
+
try {
|
|
511
|
+
const parsed = JSON.parse(localResponseText);
|
|
512
|
+
return Array.isArray(parsed) ? parsed : [parsed];
|
|
513
|
+
}
|
|
514
|
+
catch {
|
|
515
|
+
return [];
|
|
516
|
+
}
|
|
517
|
+
})();
|
|
518
|
+
const failures = [];
|
|
519
|
+
try {
|
|
520
|
+
const pending = JSON.parse(forwardBody ?? '[]');
|
|
521
|
+
for (const m of (Array.isArray(pending) ? pending : [pending])) {
|
|
522
|
+
if (m?.id === undefined)
|
|
523
|
+
continue;
|
|
524
|
+
failures.push({ jsonrpc: '2.0', id: m.id, error: { code: -32000, message } });
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
catch {
|
|
528
|
+
/* nothing forwardable to describe */
|
|
529
|
+
}
|
|
530
|
+
return JSON.stringify([...local, ...failures]);
|
|
531
|
+
}
|
|
498
532
|
async function forwardOnce(upstream, method, headers, body, fetchImpl, tracker) {
|
|
499
533
|
return tracker.run(async (signal) => {
|
|
500
534
|
const res = await fetchImpl(`${upstream}/mcp`, { method, headers, body, signal });
|
|
@@ -568,23 +602,72 @@ function hasValidSecret(req, secret) {
|
|
|
568
602
|
}
|
|
569
603
|
return valid;
|
|
570
604
|
}
|
|
571
|
-
async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchImpl, tracker, log) {
|
|
605
|
+
async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchImpl, tracker, log, localTools) {
|
|
572
606
|
const method = req.method ?? 'POST';
|
|
573
607
|
const body = method === 'POST' || method === 'DELETE' ? await readBody(req) : undefined;
|
|
608
|
+
// ── Locally-served tools ──────────────────────────────────────────────────
|
|
609
|
+
// BEFORE `getToken()` deliberately: a local call must never mint a delegated
|
|
610
|
+
// CLOUD credential, and must never leave this machine. A body that contains
|
|
611
|
+
// no local tool call falls straight through, paying one JSON.parse.
|
|
612
|
+
let forwardedAfterLocal = body;
|
|
613
|
+
let localResponseText;
|
|
614
|
+
if (localTools && method === 'POST' && body) {
|
|
615
|
+
const intercepted = await interceptLocalTools(body, localTools);
|
|
616
|
+
if (intercepted) {
|
|
617
|
+
if (!intercepted.forwardBody) {
|
|
618
|
+
if (!intercepted.localResponse) {
|
|
619
|
+
// Every local message was a NOTIFICATION, which must not be answered.
|
|
620
|
+
// 202 with an empty body is what the MCP Streamable HTTP transport
|
|
621
|
+
// returns for a notification-only POST. (codex P2.)
|
|
622
|
+
res.writeHead(202);
|
|
623
|
+
res.end();
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
// Nothing left for the cloud — answer entirely from here.
|
|
627
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
628
|
+
res.end(intercepted.localResponse);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
// A batch that mixed local and cloud tools: forward only the remainder,
|
|
632
|
+
// and merge our answers back in below so neither half is lost.
|
|
633
|
+
forwardedAfterLocal = intercepted.forwardBody;
|
|
634
|
+
localResponseText = intercepted.localResponse;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
574
637
|
const headers = { Accept: 'application/json, text/event-stream' };
|
|
575
638
|
const incomingContentType = req.headers['content-type'];
|
|
576
639
|
if (typeof incomingContentType === 'string')
|
|
577
640
|
headers['Content-Type'] = incomingContentType;
|
|
578
641
|
else if (body)
|
|
579
642
|
headers['Content-Type'] = 'application/json';
|
|
580
|
-
|
|
581
|
-
|
|
643
|
+
// ⚠️ DO NOT narrow the Accept header to make the rewrites below easier.
|
|
644
|
+
//
|
|
645
|
+
// An earlier attempt asked for `application/json` only on requests whose
|
|
646
|
+
// response gets rewritten, to dodge SSE framing. That would have broken tool
|
|
647
|
+
// discovery outright: the MCP SDK's
|
|
648
|
+
// `WebStandardStreamableHTTPServerTransport.handlePostRequest` returns 406
|
|
649
|
+
// unless the client accepts BOTH `application/json` and `text/event-stream`
|
|
650
|
+
// (verified in the installed SDK 1.29.0, not assumed — codex P1,
|
|
651
|
+
// gpt-5.6-sol). The rewrites handle both shapes instead; see
|
|
652
|
+
// `parseRpcEnvelope`.
|
|
653
|
+
let forwardedBody = forwardedAfterLocal;
|
|
654
|
+
if (method === 'POST' && forwardedAfterLocal) {
|
|
582
655
|
try {
|
|
583
656
|
const token = await getToken();
|
|
584
|
-
forwardedBody = injectToken(
|
|
657
|
+
forwardedBody = injectToken(forwardedAfterLocal, token);
|
|
585
658
|
}
|
|
586
659
|
catch (err) {
|
|
587
660
|
log(`yolo-bridge: MCP token unavailable: ${err instanceof Error ? err.message : String(err)}`);
|
|
661
|
+
// The local half of a mixed batch has ALREADY RUN by this point — the
|
|
662
|
+
// file is uploaded. Dropping its reply would leave the client never
|
|
663
|
+
// learning the assetId and retrying the batch, uploading the same file
|
|
664
|
+
// again. So answer with what actually happened: local results, plus an
|
|
665
|
+
// explicit error for each id we could not forward. (codex P2.)
|
|
666
|
+
if (localResponseText) {
|
|
667
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
668
|
+
res.end(mergeLocalWithRemoteFailure(localResponseText, forwardedAfterLocal, 'MCP token unavailable'));
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
588
671
|
res.writeHead(503, { 'Content-Type': 'application/json' });
|
|
589
672
|
res.end(JSON.stringify({ error: 'MCP token unavailable', code: 'MCP_TOKEN_UNAVAILABLE', retryable: true }));
|
|
590
673
|
return;
|
|
@@ -597,7 +680,12 @@ async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchIm
|
|
|
597
680
|
// isUnauthorizedToolResult's doc comment) — a real 401 from this upstream
|
|
598
681
|
// has never actually been observed; the in-band case is the one that
|
|
599
682
|
// matters in practice.
|
|
600
|
-
|
|
683
|
+
// ⚠️ Every path below uses `forwardedAfterLocal`, NEVER `body`. The original
|
|
684
|
+
// still contains any locally-intercepted call, and re-forwarding it on a
|
|
685
|
+
// retry would send the operator's local file path to the cloud — breaking the
|
|
686
|
+
// local-only invariant on the one path that skips the interception. (codex
|
|
687
|
+
// P1, gpt-5.6-sol.)
|
|
688
|
+
if (method === 'POST' && forwardedAfterLocal && (result.status === 401 || isUnauthorizedToolResult(result.text))) {
|
|
601
689
|
log(`yolo-bridge: MCP upstream reported an invalid/expired token (status ${result.status}), force-refreshing`);
|
|
602
690
|
try {
|
|
603
691
|
const refreshed = await forceRefresh();
|
|
@@ -607,7 +695,7 @@ async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchIm
|
|
|
607
695
|
// 200-with-mixed-results batch (Codex review, 2026-08-24, round 20)
|
|
608
696
|
// needs the narrower partial-batch retry below -- see
|
|
609
697
|
// `buildUnauthorizedRetryBatch`'s doc comment.
|
|
610
|
-
const retryBatch = result.status === 401 ? null : buildUnauthorizedRetryBatch(
|
|
698
|
+
const retryBatch = result.status === 401 ? null : buildUnauthorizedRetryBatch(forwardedAfterLocal, result.text);
|
|
611
699
|
if (retryBatch) {
|
|
612
700
|
const reinjected = injectToken(retryBatch.requestSubset, refreshed);
|
|
613
701
|
const retryResult = await forwardOnce(upstream, method, headers, reinjected, fetchImpl, tracker);
|
|
@@ -631,7 +719,7 @@ async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchIm
|
|
|
631
719
|
: { ...result, text: mergedText };
|
|
632
720
|
}
|
|
633
721
|
else {
|
|
634
|
-
const reinjected = injectToken(
|
|
722
|
+
const reinjected = injectToken(forwardedAfterLocal, refreshed);
|
|
635
723
|
result = await forwardOnce(upstream, method, headers, reinjected, fetchImpl, tracker);
|
|
636
724
|
}
|
|
637
725
|
}
|
|
@@ -639,10 +727,56 @@ async function handleRequest(req, res, upstream, getToken, forceRefresh, fetchIm
|
|
|
639
727
|
log(`yolo-bridge: MCP token refresh failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
640
728
|
}
|
|
641
729
|
}
|
|
730
|
+
let outText = result.text;
|
|
731
|
+
// A `tools/list` reply from the cloud does not know about the tools this
|
|
732
|
+
// daemon serves itself, so add them — otherwise the agent can only call them
|
|
733
|
+
// by guessing they exist.
|
|
734
|
+
if (localTools && method === 'POST' && forwardedAfterLocal) {
|
|
735
|
+
outText = augmentToolsList(forwardedAfterLocal, outText);
|
|
736
|
+
}
|
|
737
|
+
// Re-join the halves of a mixed batch. Order does not matter to a JSON-RPC
|
|
738
|
+
// client (ids correlate the replies), but LOSING one half would.
|
|
739
|
+
//
|
|
740
|
+
// ⚠️ STATUS MATTERS AS MUCH AS BODY. The local half already uploaded a file.
|
|
741
|
+
// If we propagate a non-2xx from the cloud half, a client may reject the whole
|
|
742
|
+
// HTTP response without reading it and retry the batch — uploading the same
|
|
743
|
+
// file twice. So once a local result exists, this answers 200 and reports the
|
|
744
|
+
// cloud failure per-id inside the body. (codex P1, gpt-5.6-sol.)
|
|
745
|
+
let outStatus = result.status;
|
|
746
|
+
if (localResponseText) {
|
|
747
|
+
let merged;
|
|
748
|
+
if (result.status >= 200 && result.status < 300) {
|
|
749
|
+
// The upstream half may be plain JSON or an SSE stream — it chooses. The
|
|
750
|
+
// envelope puts our local results back in whichever shape the client is
|
|
751
|
+
// already reading, instead of discarding the cloud half on a failed
|
|
752
|
+
// JSON.parse (codex P2).
|
|
753
|
+
const envelope = parseRpcEnvelope(outText);
|
|
754
|
+
if (envelope) {
|
|
755
|
+
try {
|
|
756
|
+
const local = JSON.parse(localResponseText);
|
|
757
|
+
merged = envelope.rebuild([
|
|
758
|
+
...envelope.messages,
|
|
759
|
+
...(Array.isArray(local) ? local : [local]),
|
|
760
|
+
]);
|
|
761
|
+
}
|
|
762
|
+
catch {
|
|
763
|
+
merged = undefined;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
outText = merged
|
|
768
|
+
?? mergeLocalWithRemoteFailure(localResponseText, forwardedAfterLocal, `upstream MCP request failed (status ${result.status})`);
|
|
769
|
+
outStatus = 200;
|
|
770
|
+
}
|
|
642
771
|
const outHeaders = {};
|
|
643
772
|
const contentType = result.headers.get('content-type');
|
|
644
773
|
if (contentType)
|
|
645
774
|
outHeaders['Content-Type'] = contentType;
|
|
646
|
-
|
|
647
|
-
|
|
775
|
+
// Only force JSON when we could NOT preserve the upstream's shape (the
|
|
776
|
+
// failure-merge path emits a plain array). A successful merge keeps whatever
|
|
777
|
+
// framing the upstream chose, so the header must keep matching it.
|
|
778
|
+
if (localResponseText && !outText.includes('data:'))
|
|
779
|
+
outHeaders['Content-Type'] = 'application/json';
|
|
780
|
+
res.writeHead(outStatus, outHeaders);
|
|
781
|
+
res.end(outText);
|
|
648
782
|
}
|
package/dist/share-cmd.js
CHANGED
|
@@ -185,6 +185,14 @@ export async function runShare(rawPath, deps) {
|
|
|
185
185
|
if (!attachment) {
|
|
186
186
|
return { ok: false, reason: 'not-attached', message: 'No active attachment — run `yolo-bridge attach` first.' };
|
|
187
187
|
}
|
|
188
|
+
if (deps.expectedWorkspaceId && attachment.workspaceId !== deps.expectedWorkspaceId) {
|
|
189
|
+
return {
|
|
190
|
+
ok: false,
|
|
191
|
+
reason: 'workspace-changed',
|
|
192
|
+
message: 'This machine is now attached to a different workspace than the one this request was '
|
|
193
|
+
+ 'authorised against. Nothing was sent. Re-run `yolo-bridge attach` or retry.',
|
|
194
|
+
};
|
|
195
|
+
}
|
|
188
196
|
const scopedToken = attachment.scopedToken;
|
|
189
197
|
if (!scopedToken) {
|
|
190
198
|
// Nothing on this machine can mint one for an existing attachment, so say
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yolo-labs/yolobridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "YoloBridge — local coding-agent daemon that attaches a user's own Claude Code/Codex session to a YOLO Studio workspace as a first-class tile (docs/YOLOBRIDGE_PLAN.md, build-order Phase 5).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|