Skip to content

fix(mcp): count the error flag in error result budgets - #2848

Merged
morluto merged 1 commit into
morluto:mainfrom
shubhamahuja9999:fix/mcp-error-result-budget-flag
Oct 13, 2026
Merged

morluto merged 1 commit into
morluto:mainfrom
shubhamahuja9999:fix/mcp-error-result-budget-flag

Conversation

@shubhamahuja9999

@shubhamahuja9999 shubhamahuja9999 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #2557

Error results reach the MCP wire as text plus isError: true (and the target-content _meta marker when applicable). Their budget accounting omitted the 15-byte ,"isError":true member, so an error up to 15 bytes above the result budget passed both delivery checks and was sent as the original diagnostic instead of taking the existing oversized-error recovery path.

Problem and expected behavior

Smallest trigger: a projected tool error whose delivered result (content + isError, without the private structuredContent carrier) is exactly resultBudgetBytes + 1 bytes. On main the client receives the original invalid_request error. Expected: the resource_constraint recovery diagnostic, as for any other oversized error. An error that exactly fits the budget must still arrive unchanged.

Violated invariant: encodeToolResult accounts for the delivered representation before allocation (toolResultEncoding.ts), but its text-only envelope did not match the delivered error shape. ToolResultDelivery.projectedErrorResult and ErrorEvidenceTransport.recoverError both inherited the omission.

Change and scope

  • src/server/toolResultEncoding.ts: the text-only representation is used only for errors, so it is renamed from "text" to "error-text" and its envelope is sized from { content: [...], isError: true }. This fixes the owning representation once instead of adding 15 bytes at each caller.
  • src/server/toolResult.ts and src/server/EvidenceMcpServer.ts: both error budget checks use "error-text", so they keep sharing one accounting.
  • docs/mcp-contracts.md: the result-budget paragraph now says the error text and isError flag count against the budget.
  • Successful "structured-and-text" accounting is unchanged.
  • Not included: the 1024-byte JSON-RPC envelope reserve and the early-stopping response_bytes_at_least lower bound are unchanged.

Contribution scope: ordinary fix within existing boundaries; maintainer decision not applicable.

Contract and boundary impact

  • Semantic owner and earliest changed stage: encodeToolResult error envelope accounting (src/server/toolResultEncoding.ts).
  • Contribution scope fit and linked maintainer decision: not applicable.
  • CLI and MCP/tool-catalog contract: no schema or catalog change. Errors within 15 bytes of the budget now take the documented resource_constraint recovery path; all other errors and all successful results are unchanged.
  • Provider, bridge, target-format, or platform compatibility: none.
  • Evidence, artifact, provenance, or reconstruction contract: unchanged; oversized errors are retained and replaced exactly as before.
  • Target selection, authority, upstream behavior, and cleanup details: not applicable.
  • Generated metadata, package, or installation impact: none (docs:check passes).

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

None of these apply: this changes MCP response size accounting only.

Evidence and regression coverage

  • Tests added: tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts, "applies the result budget to the complete delivered error", for both targetContent: false and true. It runs the production ToolResultDelivery and EvidenceMcpServer with the SDK client over linked in-memory transports. An error whose delivered wire is exactly resultBudgetBytes arrives as the original diagnostic; one byte larger yields resource_constraint with original_error_code: "invalid_request".
  • Tests updated: the two src/server/toolResultEncoding.test.ts cases for text-only errors measured the expected bytes without isError, which encoded the bug. They now measure the delivered error shape, with and without _meta.
  • Base reproduction: on 48a683ac8 with the new test and without the fix, both new cases fail (the client receives invalid_request; 2 failed, 4 passed). With the fix, all 6 pass.
  • User-visible output: unchanged shapes. Errors within 15 bytes over the budget now return the existing resource_constraint diagnostic.
  • Remaining proof gaps: actual stdio framing and a real provider were not exercised; the boundary test uses the SDK's in-memory transport.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

Windows 11, Node.js 24.18.0, npm 11.16.0, rebased on main at 6e25cc3c7:

  • npm run test:focused -- tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts: the new cases failed without the fix (the client received invalid_request; 2 failed, 4 passed, first reproduced on 48a683ac8). With the fix, oversizedErrorDeliveryMcp, toolResultEncoding and largeToolResult pass 20/20.
  • npm run test:focused -- the 38 test files that reference ToolResultDelivery, toolResultEncoding, EvidenceMcpServer, resultBudgetBytes or resource_constraint: the same 42 tests fail with and without this change. They are Windows-environment cases such as native-command capture, HAR/qlog heap limits, and Flutter APK extraction. The failure sets are identical, so there are no new failures, and every new or updated test passes.
  • npm run check:fast (typecheck and lint): passed, including as the pre-push hook.
  • npm run docs:check: passed (6/6 tasks, README consistency 4/4).
  • npm run verify:module-boundaries and oxfmt --check on changed files: passed.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. ToolResultRepresentations is internal to src/server/; its two callers are updated.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; Windows source and in-memory MCP tests.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: tightens response-size admission by 15 bytes for error results; no new data is delivered or retained.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

🤖 Generated with Claude Code

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

Error results reach the wire as text plus `isError: true`, but the
text-only envelope omitted the flag. An error up to 15 bytes above the
result budget passed both the delivery and transport checks and was sent
as the original diagnostic instead of taking the oversized-error recovery
path.

Rename the error representation to "error-text" and size its envelope
with the flag, so ToolResultDelivery and the error transport share the
complete delivered shape. Successful structured results are unchanged.

Fixes morluto#2557

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no blocking issue was found.

Summary

Counts isError: true in error-result budgets, fixing a 15-byte undercount.

  • MCP error budgets now count the isError flag.

Reviews (2) · Last reviewed commit: "fix(mcp): count the error flag in error ..." · Reviewed by Greptile

@shubhamahuja9999
shubhamahuja9999 force-pushed the fix/mcp-error-result-budget-flag branch from 4a09bd4 to 01a1ad0 Compare October 13, 2026 14:39
@morluto
morluto merged commit 3e7c630 into morluto:main Oct 13, 2026
1 check passed
@morluto morluto added bug Something isn't working area: mcp MCP server, transport, and tool registration labels Oct 14, 2026
@morluto

morluto commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks, @shubhamahuja9999, for counting isError in the MCP response budget and adding exact-fit and over-budget checks. This catches errors that previously slipped past the size guard. Welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: mcp MCP server, transport, and tool registration bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(mcp): error results pass the budget check with 15 bytes uncounted

2 participants