fix(mcp): measure tool errors against their text-only delivery

Since #1277 an error result reaches the client without structuredContent,
but toErrorToolResult and the transport recovery still budgeted it as
structured JSON plus escaped text. An error whose text fit between about
half and all of the response budget was replaced by resource_constraint,
and without Evidence retention the original diagnostic was lost.

Count only the escaped text for errors. The boundary, process-global and
real Ghidra large-result probes now use diagnostics that exceed the
text-only budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 14:03:12 +00:00
co-authored by Claude Opus 5.5
parent 33bf585185
commit 7b51121f7c
8 changed files with 103 additions and 14 deletions
@@ -17,17 +17,22 @@ import { ToolResultDelivery } from "../../../src/server/toolResult.js";
const delivery = new ToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE);
const oversizedFailure = () =>
const failureWithName = (nameBytes: number) =>
delivery.toErrorToolResult(
new AnalysisInputError("procedure_address", undefined, [
{
path: ["procedure"],
reason: "invalid_value",
message: `Unknown Ghidra procedure name or address: REA_MISSING_${"x".repeat(Math.ceil(STDIO_DEFAULT_MAX_BUFFER_SIZE / 2) + 64 * 1024)}`,
message: `Unknown Ghidra procedure name or address: REA_MISSING_${"x".repeat(nameBytes)}`,
},
]),
);
// The delivered error carries its JSON once, as text, so only a diagnostic
// larger than the whole budget is oversized.
const oversizedFailure = () =>
failureWithName(STDIO_DEFAULT_MAX_BUFFER_SIZE + 64 * 1024);
const exerciseDeliveryFailure = async (
recordEvidence: EvidenceWriter["recordEvidence"] | undefined,
retention: "retained" | "unavailable" | "failed",
@@ -143,3 +148,42 @@ it("retains the complete oversized diagnostic before removing its private struct
);
expect(retained[0]?.raw_result).toEqual(oversizedFailure());
});
it("delivers an error whose text fits although structured and text copies would not", async () => {
const nameBytes = Math.ceil(STDIO_DEFAULT_MAX_BUFFER_SIZE / 2) + 64 * 1024;
const retained: Evidence[] = [];
const server = new EvidenceMcpServer(
{ name: "mid-sized-error-test", version: "1" },
{ capabilities: {} },
(evidence) => {
retained.push(evidence);
return ok("added");
},
delivery,
);
server.registerTool("mid_failure", { inputSchema: {} }, async () =>
failureWithName(nameBytes),
);
const client = new Client({ name: "mid-sized-error-client", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const failure = await client.callTool({
name: "mid_failure",
arguments: {},
});
expect(failure.isError).toBe(true);
expect(failure.structuredContent).toBeUndefined();
expect(parseMcpToolError(failure)).toEqual(
failureWithName(nameBytes).structuredContent,
);
expect(retained).toHaveLength(0);
expect(Buffer.byteLength(JSON.stringify(failure))).toBeLessThan(
STDIO_DEFAULT_MAX_BUFFER_SIZE,
);
} finally {
await Promise.all([client.close(), server.close()]);
}
});
@@ -62,8 +62,9 @@ it("keeps independent server budgets for successful results and oversized errors
largeEnvironment.REA_MCP_MAX_RESPONSE_BYTES = String(smallBudget);
vi.stubEnv("REA_MCP_MAX_RESPONSE_BYTES", "invalid-ambient-after-selection");
// Error text alone exceeds the small budget and fits the large one.
const diagnostic = "observed failure ".repeat(
Math.ceil((smallBudget / 2 + 65536) / 17),
Math.ceil((smallBudget + 65536) / 17),
);
for (const server of [small, large])
server.registerTool("selected_failure", { inputSchema: {} }, async () =>