diff --git a/CHANGELOG.md b/CHANGELOG.md index 37ca28fc5b..333bb0bc13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Changes +- **MCP results carry the full report inside `structuredContent` (#3472, #2752 follow-up).** Claude Code 2.1.300 replaces the whole `content` array of any SUCCESSFUL MCP tool result that carries a `structuredContent` record with `JSON.stringify` of that record — error results keep their text — so every bili tool call from such a host reached the model as the bare envelope (`{"outcome":"success"}`, 21 bytes; `{"outcome":"applied","blocksCreated":1}`, 39 bytes) while the report itself went nowhere. The proxy was healthy throughout: `bili.log` shows `tool acp_cache executed via plugin (routed by body, #1685) (2455 chars, outcome=success)` for the same calls whose transcripts hold the 21-byte result, and driving `dist/mcp.js` by hand over stdio returns the full 2455-char text on `content` next to the structured record. `acp_status`/`acp_cache` return their entire payload as prose — exactly the report the model was told to paste verbatim and had nothing to paste — and `search_context`/`decompress` lost their results the same way. Result assembly now goes through `mcpToolResultEnvelope` (`src/mcp.ts`), which carries the text as `structuredContent.text`, byte-identical to `content[0].text`, alongside the machine fields. `content` stays byte-identical and a result with no `outcome`/`blocksCreated` still carries no `structuredContent` at all, so the pi, dsh and codex lanes are untouched; the structured contract only gains a field. Cost, disclosed: the report now travels twice, so the stdio result roughly doubles for the table-shaped reports (`acp_status` ranges, `acp_cache` ledger) on hosts that do read `content` — availability of the receipt outranks the payload, per #3128. - **dsh PTC sessions get compression back: a `run_code` omnibus on the wire reads as reachable, not unreachable (#3600).** dsh PTC presets collapse EVERY tool into a single `run_code` omnibus (the SDK bindings nest inside it), so the top-level tools array carries no `compress` entry. The #3427 reachability gate read exactly that shape as "tool not host-scoped → suppress the compress surface", and released 0.1.198 suppressed every dsh PTC session: 89× `ARMED-SUPPRESSED` nudges while the window ran to 100%, then the output got clamped and the session was effectively dead — silently, while standard-mode sessions on the same install kept compressing (v0.1.197 had no tools factor in its nudge gate, so this is a release regression, not "never worked"). The gate now treats a `run_code` omnibus declaration as reachable: top-level absence is not evidence of unreachability when the omnibus swallows everything. The #3177 true negative keeps its suppression (a bare single tool that is neither `compress` nor the omnibus still drops the surface), and the #3401 positive case is unchanged. - **dsh desktop settings pane: the embed probe is admitted for the Electron scheme origin (#3513, desktop variant).** The dsh DESKTOP app renders its settings pane from the custom scheme origin `dsh-app://app` (not an http origin like dsh web). The #3514 fix echoed the requesting Origin — but the request never reached the echo: `isTrustedAdminOrigin` only accepts http/https origins, so the probe's `GET /__bili/?embed=1` died in the trust gate with a bare 403 and no `access-control-allow-origin` — the pane stayed on "正在连接 bili 代理…" forever (real-world console: "blocked by CORS policy … from origin 'dsh-app://app'"). The gate now admits exactly that one origin (scheme `dsh-app`, hostname `app`): browsers set the Origin header themselves and the `dsh-app` scheme only exists inside the dsh Electron app, so a website can never produce that Origin — while rebinding attacks still arrive with http/https attacker hostnames and stay rejected. Foreign hostnames under the same scheme (`dsh-app://evil`) and every other custom scheme stay 403; the existing #3514 echo then covers the response. diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 7dbd65d17f..3b6f8289ec 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -21,6 +21,7 @@ export const LIVE_FILES = [ "tests/index-entry-guard.test.ts", "tests/issue1958-routed-warn.test.ts", "tests/issue2752-forward-partial-outcome.test.ts", + "tests/issue3472-mcp-structured-text.test.ts", "tests/issue760-percall-conversation.test.ts", "tests/issue927-opencode-install.test.ts", "tests/kimi-native.test.ts", diff --git a/src/mcp.ts b/src/mcp.ts index dfba273ed8..a8e296d2c8 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -151,6 +151,32 @@ function ensureManifest(): Promise { // partial note for clients that only render content text. export type McpForwardedToolResult = { text: string; failed: boolean; outcome?: ToolOutcomeKind; blocksCreated?: number }; +// #3472: Claude Code 2.1.300 replaces the WHOLE content array of any SUCCESSFUL +// MCP tool result that carries a structuredContent record with JSON.stringify of +// that record (reproduced over stdio: text + structuredContent → the model sees +// `{"outcome":"success"}`; text only → text; empty structuredContent → `{}`). +// Error results keep their text. Since #2752 attaches that record, every +// successful call from such a host reached the model as the bare envelope — 21 +// bytes — while acp_status/acp_cache/search_context/decompress return their +// entire payload as prose, so the model was told to paste a report it never got. +// Mirror the text into the structured object (`text`, byte-identical to +// content[0].text) so a structured-only reader still receives the full receipt. +// The mirror is unconditional, not success-gated: failure receipts are +// one-liners (no payload to save) and the error-results exemption above is +// observed host-version behavior, so the receipts that matter most must not +// depend on it holding. Hosts that read content keep byte-identical envelopes, +// and a result with no outcome/blocksCreated still carries no structuredContent +// at all. +export function mcpToolResultEnvelope(out: McpForwardedToolResult): Record { + const result: Record = { content: [{ type: "text", text: out.text }], isError: out.failed }; + if (out.outcome === undefined && out.blocksCreated === undefined) return result; + const structured: Record = { text: out.text }; + if (out.outcome !== undefined) structured.outcome = out.outcome; + if (out.blocksCreated !== undefined) structured.blocksCreated = out.blocksCreated; + result.structuredContent = structured; + return result; +} + export async function forwardTool(tool: string, args: unknown, timeoutMs: number = TOOL_TIMEOUT_MS, conversationIdOverride: string | undefined = undefined, nativeCaller: boolean = false): Promise { const effectiveConversationId = conversationIdOverride ?? conversationId; for (let attempt = 0; ; attempt++) { @@ -351,14 +377,9 @@ async function handleMessage(msg: { // #2752: the business state rides MCP structuredContent (spec // field alongside content; clients predating it ignore it). // isError semantics are unchanged — partial stays non-error. - const result: Record = { content: [{ type: "text", text: out.text }], isError: out.failed }; - if (out.outcome !== undefined || out.blocksCreated !== undefined) { - const structured: Record = {}; - if (out.outcome !== undefined) structured.outcome = out.outcome; - if (out.blocksCreated !== undefined) structured.blocksCreated = out.blocksCreated; - result.structuredContent = structured; - } - sendResult(id, result); + // #3472: the report text rides inside that record too; see + // mcpToolResultEnvelope. + sendResult(id, mcpToolResultEnvelope(out)); } catch (err) { // Protocol failures are results (isError), not JSON-RPC // errors, so the host surfaces them to the model. diff --git a/tests/issue2752-forward-partial-outcome.test.ts b/tests/issue2752-forward-partial-outcome.test.ts index 1581013a71..405be4e057 100644 --- a/tests/issue2752-forward-partial-outcome.test.ts +++ b/tests/issue2752-forward-partial-outcome.test.ts @@ -457,7 +457,9 @@ describe("#2752: the shim's tools/call response carries structuredContent on the assert.ok(r.result, "a result object comes back"); assert.notEqual(r.result!.isError, true, "partial is NOT an error on the wire"); assert.equal(r.result!.content?.[0]?.text, `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`); - assert.deepEqual(r.result!.structuredContent, { outcome: "partial", blocksCreated: 1 }); + // #3472: structuredContent also carries the full report text so + // structured-preferring hosts do not lose the receipt body. + assert.deepEqual(r.result!.structuredContent, { text: `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`, outcome: "partial", blocksCreated: 1 }); }); it("failure: isError true with its outcome still on structuredContent", async () => { @@ -465,7 +467,8 @@ describe("#2752: the shim's tools/call response carries structuredContent on the const r = await client.request(2, "tools/call", { name: "decompress", arguments: { blockId: "b999" } }); assert.ok(!r.error, `no JSON-RPC error: ${JSON.stringify(r.error)}`); assert.equal(r.result?.isError, true); - assert.deepEqual(r.result?.structuredContent, { outcome: "failure" }); + // #3472: the failure receipt text rides structuredContent too. + assert.deepEqual(r.result?.structuredContent, { text: FAILURE_TEXT, outcome: "failure" }); }); it("legacy envelope: no structuredContent field at all", async () => { diff --git a/tests/issue3472-mcp-structured-text.test.ts b/tests/issue3472-mcp-structured-text.test.ts new file mode 100644 index 0000000000..3199aa011c --- /dev/null +++ b/tests/issue3472-mcp-structured-text.test.ts @@ -0,0 +1,216 @@ +// #3472: since #2752 the MCP shim attaches structuredContent {outcome, +// blocksCreated} to every result carrying machine state. Some hosts (Claude +// Code via its MCP bridge) prefer structuredContent when present and DROP the +// content text — every success call reached the model as bare +// {"outcome":"success"} and the whole report (compressible ranges, GRAND +// LEDGER, search hits, restored content) was lost. The fix makes +// structuredContent self-contained: it carries the full report itself +// (structured.text = out.text, byte-identical to content[0].text) alongside +// the machine fields. Pinned here through the REAL shim over stdio JSON-RPC: +// 1. success envelopes (acp_status / acp_cache / search_context / decompress): +// structuredContent.text === content[0].text byte-for-byte, outcome kept; +// 2. compress applied/partial: the #2752 machine fields are unchanged, the +// text is present and byte-stable (partial incl. the marker line); +// 3. legacy envelopes (no outcome/blocksCreated): NO structuredContent at +// all — pre-#2752 clients see exactly the old shape; +// 4. isError semantics unchanged (failure/refused stay errors, partial stays +// non-error) and content[0].text stays byte-stable everywhere, so +// content-reading hosts are untouched. +import { afterEach, beforeEach, describe, it } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { spawn } from "node:child_process"; + +import { partialOutcomeNote } from "../src/proxy-tool-result.ts"; + +const STATUS_REPORT = [ + "ACTIVE SURFACE:", + "Breakdown: 10.0K system (30%) | 23.3K tool (70%)", + "Compressible ranges (1, oldest first):", + "- m00001–m00010 1.2K effective tool", +].join("\n"); +const CACHE_REPORT = "GRAND LEDGER:\ntotal input 1,000,000 · cached 960,000 (96.0%)\nFOLD ECONOMICS:\nb1 net saved 12,000 — paid back"; +const SEARCH_REPORT = 'Found 2 block(s) for "auth":\n\n[b2] "Auth System Exploration" — m00010–m00050'; +const DECOMPRESSED_BODY = "[decompress b1 m00001–m00010: restored 10 item(s) — full content follows]\n"; +const APPLIED_TEXT = "[Compressed m00001–m00100 → 2 block(s), ~5000 tokens saved.]"; +const PARTIAL_TEXT = "[Compressed m00001–m00200 → 1 block(s), ~4000 tokens saved. Errors: m00101–m00200: unknown ref]"; +const REFUSED_TEXT = "[Compression FAILED: requested range(s) resolved to no messages]"; + +type Envelope = Record; + +async function startCannedProxy(): Promise<{ origin: string; canned: Map; close(): Promise }> { + const canned = new Map(); + const server = http.createServer((req, res) => { + if (req.url !== "/__bili/plugin/tool") { res.writeHead(404).end("nope"); return; } + let raw = ""; + req.on("data", (d) => { raw += d; }); + req.on("end", () => { + const parsed = JSON.parse(raw) as { tool?: string }; + const payload = canned.get(parsed.tool ?? "") ?? { ok: false, error: "unexpected tool" }; + res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(payload)); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const addr = server.address(); + const port = typeof addr === "object" && addr ? addr.port : 0; + let closed = false; + return { + origin: `http://127.0.0.1:${port}`, + canned, + close: () => closed ? Promise.resolve() : (closed = true, new Promise((r) => server.close(() => r()))), + }; +} + +type WireResult = { content?: Array<{ type?: string; text?: string }>; isError?: boolean; structuredContent?: Record }; + +class WireShimClient { + private child: ReturnType; + private stderr = ""; + private pending = new Map void>(); + private buf = ""; + + constructor(env: NodeJS.ProcessEnv) { + this.child = spawn(process.execPath, ["--import", "tsx", "src/mcp.ts"], { + cwd: process.cwd(), + env, + stdio: ["pipe", "pipe", "pipe"], + }); + this.child.stdout?.setEncoding("utf8"); + this.child.stderr?.setEncoding("utf8"); + this.child.stdout?.on("data", (d: string) => { + this.buf += d; + let nl: number; + while ((nl = this.buf.indexOf("\n")) >= 0) { + const line = this.buf.slice(0, nl).trim(); + this.buf = this.buf.slice(nl + 1); + if (!line) continue; + try { + const msg = JSON.parse(line) as { id?: unknown }; + if (typeof msg.id === "number") { + const waiter = this.pending.get(msg.id); + if (waiter) { + this.pending.delete(msg.id); + waiter(msg); + } + } + } catch { /* non-JSON line — ignore */ } + } + }); + this.child.stderr?.on("data", (d: string) => { this.stderr += d; }); + } + + request(id: number, method: string, params?: Record): Promise<{ result?: WireResult; error?: unknown }> { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + this.pending.delete(id); + reject(new Error(`shim ${method} (id ${id}) timed out; stderr: ${this.stderr.slice(0, 400)}`)); + }, 20_000); + this.pending.set(id, (msg) => { + clearTimeout(timer); + resolve(msg as { result?: WireResult; error?: unknown }); + }); + this.child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, ...(params ? { params } : {}) })}\n`); + }); + } + + async init(): Promise { + const r = await this.request(0, "initialize", { + protocolVersion: "2025-06-18", + capabilities: {}, + clientInfo: { name: "bili-wire-test", version: "0.1.0" }, + }); + assert.ok(r.result && !r.error, `initialize must succeed; stderr: ${this.stderr.slice(0, 400)}`); + this.child.stdin?.write(`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" })}\n`); + } + + close(): void { this.child.kill(); } +} + +describe("#3472: structuredContent carries the full report for structured-preferring hosts", () => { + let proxy: Awaited> | null = null; + let client: WireShimClient | null = null; + + beforeEach(async () => { + proxy = await startCannedProxy(); + proxy.canned.set("acp_status", { ok: true, result: STATUS_REPORT, outcome: "success" }); + proxy.canned.set("acp_cache", { ok: true, result: CACHE_REPORT, outcome: "success" }); + proxy.canned.set("search_context", { ok: true, result: SEARCH_REPORT, outcome: "success" }); + proxy.canned.set("decompress", { ok: true, result: DECOMPRESSED_BODY, outcome: "success" }); + proxy.canned.set("compress", { ok: true, result: APPLIED_TEXT, outcome: "applied", blocksCreated: 2 }); + client = new WireShimClient({ ...process.env, BILI_MCP_PROXY: proxy.origin, BILI_CONVERSATION_ID: "conv-wire-3472" }); + await client.init(); + }); + afterEach(() => { + client?.close(); + client = null; + if (proxy) void proxy.close(); + proxy = null; + }); + + const call = async (name: string, args: Record = {}): Promise => { + assert.ok(client); + const r = await client.request(name.length + 1, "tools/call", { name, arguments: args }); + assert.ok(!r.error, `no JSON-RPC error: ${JSON.stringify(r.error)}`); + assert.ok(r.result, "a result object comes back"); + return r.result!; + }; + + it("acp_status success: the complete report is recoverable from structuredContent alone", async () => { + const r = await call("acp_status"); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, STATUS_REPORT); + assert.deepEqual(r.structuredContent, { text: STATUS_REPORT, outcome: "success" }, + "a host that renders only structuredContent must still see the full status report"); + }); + + it("acp_cache / search_context / decompress success: full body inside structuredContent, outcome kept", async () => { + const cases: Array<[string, Record, string]> = [ + ["acp_cache", {}, CACHE_REPORT], + ["search_context", { query: "auth" }, SEARCH_REPORT], + ["decompress", { blockId: "b1" }, DECOMPRESSED_BODY], + ]; + for (const [name, args, body] of cases) { + const r = await call(name, args); + assert.notEqual(r.isError, true, name); + assert.equal(r.content?.[0]?.text, body, `${name}: content text byte-stable`); + assert.deepEqual(r.structuredContent, { text: body, outcome: "success" }, `${name}: structured carries the full body`); + } + }); + + it("compress applied: the #2752 machine fields stay intact, text rides alongside", async () => { + const r = await call("compress", { content: [] }); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, APPLIED_TEXT); + assert.deepEqual(r.structuredContent, { text: APPLIED_TEXT, outcome: "applied", blocksCreated: 2 }); + }); + + it("compress partial: marker line present in BOTH carriers, machine state unchanged", async () => { + assert.ok(proxy); + proxy.canned.set("compress", { ok: true, result: PARTIAL_TEXT, outcome: "partial", blocksCreated: 1 }); + const r = await call("compress", { content: [] }); + assert.notEqual(r.isError, true, "partial stays non-error (work landed)"); + const expected = `${PARTIAL_TEXT}\n${partialOutcomeNote(1)}`; + assert.equal(r.content?.[0]?.text, expected); + assert.deepEqual(r.structuredContent, { text: expected, outcome: "partial", blocksCreated: 1 }); + }); + + it("refused: isError true, receipt text in both carriers", async () => { + assert.ok(proxy); + proxy.canned.set("compress", { ok: true, result: REFUSED_TEXT, outcome: "refused", blocksCreated: 0 }); + const r = await call("compress", { content: [] }); + assert.equal(r.isError, true); + assert.equal(r.content?.[0]?.text, REFUSED_TEXT); + assert.deepEqual(r.structuredContent, { text: REFUSED_TEXT, outcome: "refused", blocksCreated: 0 }); + }); + + it("legacy envelope (no outcome/blocksCreated): NO structuredContent — old clients see the old shape", async () => { + assert.ok(proxy); + proxy.canned.set("acp_status", { ok: true, result: "CONTEXT STATUS" }); + const r = await call("acp_status"); + assert.notEqual(r.isError, true); + assert.equal(r.content?.[0]?.text, "CONTEXT STATUS"); + assert.equal(r.structuredContent, undefined, "pre-#2752 envelopes keep their exact wire shape"); + }); +}); diff --git a/tests/mcp-tool-result-envelope.test.ts b/tests/mcp-tool-result-envelope.test.ts new file mode 100644 index 0000000000..c7971465c0 --- /dev/null +++ b/tests/mcp-tool-result-envelope.test.ts @@ -0,0 +1,76 @@ +// #3472: Claude Code 2.1.300 replaces a SUCCESSFUL MCP result's content array +// with JSON.stringify of its structuredContent record (error results keep their +// text), so the text channel alone never reached the model for the prose-only +// panel tools (acp_status/acp_cache) — it saw `{"outcome":"success"}`. +// This suite pins the envelope itself, host-free: content stays byte-identical, +// structuredContent keeps its outcome/blocksCreated contract, and the report +// text rides inside it as `text`. The wire-level suite through the real stdio +// shim (issue3472-mcp-structured-text.test.ts) covers the per-tool paths. +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; + +import { mcpToolResultEnvelope, type McpForwardedToolResult } from "../src/mcp.ts"; + +const PANEL = "ACP CACHE REPORT (12:29:18) — 45 requests\n[summary] cache hit 81.0% · per-fold breakeven 6 turns"; + +function textOf(env: Record): string { + const content = env.content as Array<{ text: string }>; + return content[0]!.text; +} + +describe("mcpToolResultEnvelope", () => { + it("keeps a legacy result byte-identical: no structuredContent at all", () => { + const env = mcpToolResultEnvelope({ text: PANEL, failed: false }); + assert.deepEqual(env, { content: [{ type: "text", text: PANEL }], isError: false }); + assert.equal("structuredContent" in env, false); + }); + + it("carries the report inside structuredContent for a successful panel tool", () => { + const env = mcpToolResultEnvelope({ text: PANEL, failed: false, outcome: "success" }); + assert.deepEqual(env.content, [{ type: "text", text: PANEL }]); + assert.deepEqual(env.structuredContent, { text: PANEL, outcome: "success" }); + // The host substitutes JSON.stringify(structuredContent) for content — + // this is the exact string the model then sees. + assert.match(JSON.stringify(env.structuredContent), /ACP CACHE REPORT/); + }); + + it("carries text alongside outcome/blocksCreated on a compress result", () => { + const env = mcpToolResultEnvelope({ text: PANEL, failed: false, outcome: "applied", blocksCreated: 2 }); + assert.deepEqual(env.structuredContent, { text: PANEL, outcome: "applied", blocksCreated: 2 }); + }); + + it("keeps structuredContent.text === content[0].text for every shape", () => { + const shapes: McpForwardedToolResult[] = [ + { text: PANEL, failed: false, outcome: "success" }, + { text: PANEL, failed: false, outcome: "applied", blocksCreated: 1 }, + { text: PANEL, failed: false, outcome: "partial", blocksCreated: 1 }, + { text: PANEL, failed: true, outcome: "refused" }, + { text: PANEL, failed: true, outcome: "failure" }, + { text: "", failed: false, outcome: "success" }, + ]; + for (const out of shapes) { + const env = mcpToolResultEnvelope(out); + assert.equal(textOf(env), out.text, "content text is the report"); + assert.equal((env.structuredContent as { text: string }).text, out.text, `mirror holds for ${out.outcome}`); + } + }); + + it("still carries the text field when there is no report body", () => { + // Empty, not absent: one shape, no reader has to ask why text vanished. + const env = mcpToolResultEnvelope({ text: "", failed: false, outcome: "success" }); + assert.deepEqual(env.structuredContent, { text: "", outcome: "success" }); + }); + + it("keeps isError semantics and still mirrors the report for a failure receipt", () => { + const failed = mcpToolResultEnvelope({ text: PANEL, failed: true, outcome: "refused" }); + assert.equal(failed.isError, true); + assert.deepEqual(failed.structuredContent, { text: PANEL, outcome: "refused" }); + }); + + it("mirrors the partial note verbatim — it is part of the text channel", () => { + const text = `${PANEL}\n[acp-result: PARTIAL …]`; + const env = mcpToolResultEnvelope({ text, failed: false, outcome: "partial", blocksCreated: 1 }); + assert.deepEqual(env.structuredContent, { text, outcome: "partial", blocksCreated: 1 }); + assert.deepEqual(env.content, [{ type: "text", text }]); + }); +});