Merge pull request #1288 from morluto/fix/mcp-error-text-budget
fix(mcp): measure tool errors against their text-only delivery
This commit is contained in:
@@ -153,7 +153,8 @@ class ErrorEvidenceTransport implements Transport {
|
||||
return message;
|
||||
const structured = jsonObjectSchema.parse(message.result.structuredContent);
|
||||
const budget = this.delivery.resultBudgetBytes;
|
||||
const encoded = encodeToolResult(structured, budget);
|
||||
// The delivered error omits structuredContent, so only its text counts.
|
||||
const encoded = encodeToolResult(structured, budget, "text");
|
||||
if (encoded.ok) return message;
|
||||
const operation = tool ?? "mcp_tool_error";
|
||||
const originalError = analysisErrorProjectionSchema.safeParse(
|
||||
|
||||
@@ -65,7 +65,13 @@ export class ToolResultDelivery {
|
||||
/** Project an error without allocating oversized repeated MCP text. */
|
||||
toErrorToolResult(error: AnalysisError): CallToolResult {
|
||||
const structuredContent = { error: projectAnalysisError(error) };
|
||||
const encoded = encodeToolResult(structuredContent, this.resultBudgetBytes);
|
||||
// Errors reach the wire as text only; the structured copy is a private
|
||||
// carrier the transport removes after its own budget check.
|
||||
const encoded = encodeToolResult(
|
||||
structuredContent,
|
||||
this.resultBudgetBytes,
|
||||
"text",
|
||||
);
|
||||
// The transport retains an oversized projected error before replacing it
|
||||
// with a recoverable delivery constraint, using this same budget.
|
||||
return {
|
||||
|
||||
@@ -41,6 +41,25 @@ describe("MCP result encoding budget", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ error: { code: "invalid_request", message: '中文"\n'.repeat(30000) } },
|
||||
{ error: { message: "😀\ud800" } },
|
||||
])("counts only escaped text for text-only error delivery", (candidate) => {
|
||||
const textBytes = Buffer.byteLength(
|
||||
JSON.stringify({
|
||||
content: [{ type: "text", text: JSON.stringify(candidate) }],
|
||||
}),
|
||||
);
|
||||
const encoded = encodeToolResult(candidate, textBytes, "text");
|
||||
if (!encoded.ok) throw new Error("Expected complete encoding");
|
||||
expect(encoded.text).toBe(JSON.stringify(candidate));
|
||||
expect(encoded.bytes).toBe(textBytes);
|
||||
expect(encodeToolResult(candidate, textBytes - 1, "text")).toMatchObject({
|
||||
ok: false,
|
||||
bytesAtLeast: textBytes,
|
||||
});
|
||||
});
|
||||
|
||||
it("stops before reading the remainder of an oversized result", () => {
|
||||
const candidate: JsonValue = { text: "x".repeat(1000000) };
|
||||
Object.defineProperty(candidate, "remainder", {
|
||||
|
||||
@@ -13,6 +13,16 @@ const RESULT_ENVELOPE_BYTES =
|
||||
}),
|
||||
) - 4;
|
||||
|
||||
const TEXT_RESULT_ENVELOPE_BYTES = Buffer.byteLength(
|
||||
JSON.stringify({ content: [{ type: "text", text: "" }] }),
|
||||
);
|
||||
|
||||
/**
|
||||
* MCP representations a result carries on the wire. Successful results repeat
|
||||
* the JSON as structured content and escaped text; error results carry text only.
|
||||
*/
|
||||
export type ToolResultRepresentations = "structured-and-text" | "text";
|
||||
|
||||
/** Result of encoding the complete repeated MCP representations within a wire budget. */
|
||||
export type ToolResultEncoding =
|
||||
| { readonly ok: true; readonly text: string; readonly bytes: number }
|
||||
@@ -23,18 +33,23 @@ export type ToolResultEncoding =
|
||||
readonly constraint: "receive-buffer" | "string-length";
|
||||
};
|
||||
|
||||
/** Account for structured JSON and escaped text before allocating the complete text. */
|
||||
/** Account for the delivered JSON representations before allocating the complete text. */
|
||||
export const encodeToolResult = (
|
||||
value: JsonValue,
|
||||
budgetBytes: number,
|
||||
representations: ToolResultRepresentations = "structured-and-text",
|
||||
): ToolResultEncoding => {
|
||||
const structured = representations === "structured-and-text";
|
||||
const parts: string[] = [];
|
||||
let bytes = RESULT_ENVELOPE_BYTES;
|
||||
let codeUnits = RESULT_ENVELOPE_BYTES;
|
||||
let bytes = structured ? RESULT_ENVELOPE_BYTES : TEXT_RESULT_ENVELOPE_BYTES;
|
||||
let codeUnits = bytes;
|
||||
for (const part of bufferedJsonParts(jsonParts(value))) {
|
||||
const escapedPart = JSON.stringify(part);
|
||||
bytes += Buffer.byteLength(part) + Buffer.byteLength(escapedPart) - 2;
|
||||
codeUnits += part.length + escapedPart.length - 2;
|
||||
bytes +=
|
||||
(structured ? Buffer.byteLength(part) : 0) +
|
||||
Buffer.byteLength(escapedPart) -
|
||||
2;
|
||||
codeUnits += (structured ? part.length : 0) + escapedPart.length - 2;
|
||||
if (bytes > budgetBytes || codeUnits > MCP_RESULT_STRING_LIMIT)
|
||||
return {
|
||||
ok: false,
|
||||
|
||||
Reference in New Issue
Block a user