[PR #2848] [MERGED] fix(mcp): count the error flag in error result budgets #5435

Closed
opened 2026-10-31 01:06:36 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/morluto/rea/pull/2848
Author: @shubhamahuja9999
Created: 10/13/2026
Status: ✅ Merged
Merged: 10/13/2026
Merged by: @morluto

Base: main ← Head: fix/mcp-error-result-budget-flag


📝 Commits (1)

  • 01a1ad0 fix(mcp): count the error flag in error result budgets

📊 Changes

6 files changed (+108 additions, -26 deletions)

View changed files

📝 docs/mcp-contracts.md (+1 -1)
📝 src/server/EvidenceMcpServer.ts (+7 -2)
📝 src/server/toolResult.ts (+1 -1)
📝 src/server/toolResultEncoding.test.ts (+24 -17)
📝 src/server/toolResultEncoding.ts (+6 -5)
📝 tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts (+69 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/morluto/rea/pull/2848 **Author:** [@shubhamahuja9999](https://github.com/shubhamahuja9999) **Created:** 10/13/2026 **Status:** ✅ Merged **Merged:** 10/13/2026 **Merged by:** [@morluto](https://github.com/morluto) **Base:** `main` ← **Head:** `fix/mcp-error-result-budget-flag` --- ### 📝 Commits (1) - [`01a1ad0`](https://github.com/morluto/rea/commit/01a1ad09345996b312ba2ed1f9d037284ac2f399) fix(mcp): count the error flag in error result budgets ### 📊 Changes **6 files changed** (+108 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `docs/mcp-contracts.md` (+1 -1) 📝 `src/server/EvidenceMcpServer.ts` (+7 -2) 📝 `src/server/toolResult.ts` (+1 -1) 📝 `src/server/toolResultEncoding.test.ts` (+24 -17) 📝 `src/server/toolResultEncoding.ts` (+6 -5) 📝 `tests/boundary/mcp/oversizedErrorDeliveryMcp.test.ts` (+69 -0) </details> ### 📄 Description ## 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: - [x] Observed, derived, and inferred claims remain distinguishable. - [x] Artifact identity, source provenance, and failed attempts remain preserved. - [x] 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 - [x] This PR addresses a concrete problem or an agreed enhancement. - [x] Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff. - [x] The PR has one focused outcome and the title follows `type(scope): outcome`. - [x] Related issue is linked, or the reason for not linking one is stated above. - [x] Tests cover changed observable behavior and meaningful failure paths. - [x] Owning docs, contracts, and generated metadata are updated where needed. - [x] User-visible CLI/MCP changes include representative output. - [x] I checked the final diff for secrets, unrelated cleanup, and unsupported claims. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 01:06:36 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#5435
No description provided.