fix(client): improve performance of large SSE tool responses#1052
Open
arnabnandy7 wants to merge 1 commit into
Open
fix(client): improve performance of large SSE tool responses#1052arnabnandy7 wants to merge 1 commit into
arnabnandy7 wants to merge 1 commit into
Conversation
Replaced the JDK's slow fromLineSubscriber line-assembly mechanism with a custom byte-level streaming SSE parser SseByteSubscriber. This resolves an O(n^2) bottleneck when parsing large compact JSON payloads that are returned on a single line, improving throughput by ~45x. Closes modelcontextprotocol#1042
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.
Replaced the JDK's slow
fromLineSubscriberline-assembly mechanism with a custom byte-level streaming SSE parser (SseByteSubscriber) inResponseSubscribers.java. This resolves an O(n²) performance bottleneck when receiving and parsing large compact JSON payload events (which are sent on a single line), without breaking streaming event delivery for notifications/progress.Motivation and Context
When a tool returns a large payload (~4 MB compact JSON on a single line) over the Streamable HTTP client transport, the Java MCP SDK takes ~5 seconds to receive and parse it. This is roughly 10–13× slower than a raw
HttpClient.ofString()read (~0.4s).The bottleneck is the JDK HTTP Client's
HttpResponse.BodySubscribers.fromLineSubscriber()used inResponseSubscribers.sseToBodySubscriber(). It scans the incoming bytes to find newline boundaries, assembling the entire 4 MB JSON payload into a single string. The internalCharSequence/String assembly infromLineSubscriberis extremely slow when processing one very long line from many smallByteBufferchunks.This PR replaces the line subscriber with a streaming byte-level SSE parser (
SseByteSubscriber), which processes raw byte chunks (List<ByteBuffer>) usingBodySubscribers.fromSubscriber(...). This bypasses the JDK's line-assembly entirely while preserving the streaming delivery of events.How Has This Been Tested?
Tested locally via new unit tests in SseByteSubscriberTests.java and running the entire
mcp-coretest suite::), split boundaries across chunks, empty data, and standard line endings (\n,\r\n,\r).mcp-corepass successfully.Breaking Changes
None. This change is fully internal to the package-private subscriber in the transport layer and does not modify the public APIs or the
ResponseEventcontract.Types of changes
Checklist
Additional context
SseByteBuffer(a fast rolling byte buffer) insideResponseSubscribers.javato handle raw chunk aggregation and shifting at the primitive byte array level.Long.MAX_VALUEon subscription to receive the raw byte chunks as they arrive from the network, maintaining standard push-model streaming semantics.Resolves #1042