Propagate body-level errors as synthetic JSON-RPC responses by Planview-JamesK · Pull Request #896 · modelcontextprotocol/java-sdk
Motivation and Context
Fixes #889. When body-level errors occur during response streaming in sendMessage() (e.g. DataBufferLimitException, JSON parse errors, ReadTimeoutException), the onErrorComplete operator silently drops them. Because sink.success() has already been called when the HTTP response headers arrive, the error has no path to reach the caller. The pending response in McpClientSession.pendingResponses hangs until requestTimeout.
Summary
- Change
onErrorCompletetoonErrorResumeinHttpClientStreamableHttpTransport.sendMessage() - When a body-level error occurs and the message has a request ID, emit a synthetic
JSONRPCResponsewithINTERNAL_ERRORto the handler soMcpClientSession.pendingResponsesgets resolved immediately - Notifications (no request ID) do not emit synthetic responses
How Has This Been Tested?
Three new tests in HttpClientStreamableHttpTransportBodyErrorTest:
- SSE parse error — server returns SSE with malformed JSON, error propagates
- JSON parse error — server returns malformed
application/json, synthetic error response emitted to handler with matching request ID - Notification — no synthetic response emitted for messages without a request ID
All existing error handling tests continue to pass.
Types of changes
- Bug fix (non-breaking change which fixes an issue)
Checklist
- I have read the MCP Documentation
- My code follows the repository's style guidelines
- New and existing tests pass locally
- I have added appropriate error handling