fix(mcp): substitute static error envelope on encode failure instead of writing empty body#1146
Merged
Merged
Conversation
…of writing empty body
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MCPExchangeResponder.respondno longer falls back toData()when the primary encode fails AND the secondary internal-error envelope encode also fails. The two-tier path now ends in a hardcoded static envelope{"jsonrpc":"2.0","id":null,"error":{"code":-32603,"message":"internal_error"}}.MCPExchangeResponder.respondErrorandMCPExchangeResponder.rejectuse the same static fallback whenJSONEncoder().encode(envelope)fails.MCPHttpRequestRouter.handleIntegrationsExchange(success path) writesinternalServerErrorwithinternal_errorbody viawritePlainJsonError(...)if encodingExchangeResponsefails, instead of writing a 200 with an empty body.MCPHttpRequestRouter.respondTopLeveluses the same static JSON-RPC envelope fallback when encoding the top-level error envelope fails.errorlevel with the encode error description.Why this matters
This is bug B8 from the full-app audit. Five sites in the MCP HTTP transport previously did
(try? JSONEncoder().encode(envelope)) ?? Data(). An MCP client receiving a zero-byte body on a200 OKor a500response sees a protocol violation, then either disconnects or hangs waiting for a frame that will never arrive. The fix gives the client a valid JSON-RPC error envelope (or a small JSONinternal_errorfor the non-RPC pairing endpoint), so the failure surface is observable from both sides.The static envelope is a literal
Datavalue, not a re-encode, so it cannot itself fail.Test plan
JsonRpcMessage(synthetic NaN if injected), confirm Console shows the encode-failed log and the client receives the static internal_error envelope rather than a zero-byte body.