From 5f749c2096af2617d4b6b387ff19c13001af8e54 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Fri, 9 Oct 2026 22:44:24 +0800 Subject: [PATCH] fix(mcp): only recommend exporting retained evidence --- src/server/toolResult.ts | 56 ++++++++++++---------- tests/boundary/mcp/largeToolResult.test.ts | 14 ++++-- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/server/toolResult.ts b/src/server/toolResult.ts index 90f54dda1..1f24f0a0e 100644 --- a/src/server/toolResult.ts +++ b/src/server/toolResult.ts @@ -1,7 +1,7 @@ import type { CallToolResult } from "@modelcontextprotocol/server"; import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server"; -import type { ToolContract } from "../contracts/toolContracts.js"; +import type { ToolContract } from "../contracts/toolContractTypes.js"; import type { Evidence } from "../domain/evidence.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { type AnalysisError } from "../domain/analysisErrorBase.js"; @@ -87,29 +87,35 @@ export class ToolResultDelivery { recovery: Readonly> = {}, ): CallToolResult { const encoded = encodeToolResult(value, this.resultBudgetBytes); - if (!encoded.ok) - return this.toErrorToolResult( - new AnalysisResourceConstraintError( - contract.name, - "transport", - encoded.constraint === "string-length" - ? "The operation completed, but its complete MCP response exceeds Node's single-string representation limit. Export retained evidence to consume the complete analysis, or use complete CLI JSON output." - : "The operation completed, but its complete MCP response exceeds the stdio response budget. The analysis result was not truncated; export retained evidence to consume it, or use complete CLI JSON output.", - { - boundary: "mcp-response", - default_receive_buffer_bytes: STDIO_DEFAULT_MAX_BUFFER_SIZE, - result_budget_bytes: this.resultBudgetBytes, - max_string_code_units: MCP_RESULT_STRING_LIMIT, - response_bytes_at_least: encoded.bytesAtLeast, - response_code_units_at_least: encoded.codeUnitsAtLeast, - constraint: encoded.constraint, - ...recovery, - }, - ), - ); - return { - content: [{ type: "text", text: encoded.text }], - structuredContent: value, - }; + if (encoded.ok) + return { + content: [{ type: "text", text: encoded.text }], + structuredContent: value, + }; + const retainedEvidence = recovery.evidence_reference !== undefined; + const recoveryAdvice = retainedEvidence + ? "Export retained evidence to consume the complete analysis, or use complete CLI JSON output." + : encoded.constraint === "string-length" + ? "Use complete CLI JSON output to consume the analysis." + : "Use a larger MCP response budget with a matching client receive buffer, or use complete CLI JSON output to consume the analysis."; + return this.toErrorToolResult( + new AnalysisResourceConstraintError( + contract.name, + "transport", + encoded.constraint === "string-length" + ? `The operation completed, but its complete MCP response exceeds Node's single-string representation limit. ${recoveryAdvice}` + : `The operation completed, but its complete MCP response exceeds the stdio response budget. The analysis result was not truncated. ${recoveryAdvice}`, + { + boundary: "mcp-response", + default_receive_buffer_bytes: STDIO_DEFAULT_MAX_BUFFER_SIZE, + result_budget_bytes: this.resultBudgetBytes, + max_string_code_units: MCP_RESULT_STRING_LIMIT, + response_bytes_at_least: encoded.bytesAtLeast, + response_code_units_at_least: encoded.codeUnitsAtLeast, + constraint: encoded.constraint, + ...recovery, + }, + ), + ); } } diff --git a/tests/boundary/mcp/largeToolResult.test.ts b/tests/boundary/mcp/largeToolResult.test.ts index 00afa2d7a..8b61f9be6 100644 --- a/tests/boundary/mcp/largeToolResult.test.ts +++ b/tests/boundary/mcp/largeToolResult.test.ts @@ -218,13 +218,21 @@ describe("large complete Evidence MCP delivery", () => { const limits = z .object({ error: z.object({ + message: z.string(), details: z.object({ reported_limits: z.record(z.string(), z.unknown()), }), }), }) - .parse(detached.structuredContent).error.details.reported_limits; - expect(limits.evidence_id).toBe(evidence.evidence_id); - expect(limits).not.toHaveProperty("evidence_reference"); + .parse(detached.structuredContent).error; + expect(limits.message).toContain("larger MCP response budget"); + expect(limits.message).not.toContain("Export retained evidence"); + expect(limits.details.reported_limits).not.toHaveProperty( + "evidence_reference", + ); + expect(limits.details.reported_limits).toHaveProperty( + "evidence_id", + evidence.evidence_id, + ); }); });