Skip to content

fix(mcp): decode complete multiline SSE events - #8

Merged
Balanov merged 1 commit into
Balanov:mainfrom
curforever:fix/mcp-multiline-sse
Nov 25, 2026
Merged

Balanov merged 1 commit into
Balanov:mainfrom
curforever:fix/mcp-multiline-sse

Conversation

@curforever

Copy link
Copy Markdown
Contributor

Fixes #7.

The MCP HTTP client currently decodes each SSE data: line as a whole JSON-RPC reply. Valid multi-line events therefore fail, including during initialization.

Collect data fields within one event, join them with newlines, and decode only at the terminating blank line. Decode into a fresh response per event so ignored messages cannot leave an error in the eventual reply. Preserve request-ID filtering, the existing transport limits and the JSON/stdio paths. No new dependencies.

Validation on Windows x64 / Go1.25.14:

  • The same7 offline httptest cases: original5 fail/2 pass; fixed7 pass. Covers LF/CRLF, preceding notifications/comments/metadata, ignored-error isolation, multi-line errors, existing single-line replies and separate events not being joined.
  • Actual CLI against the same local multi-line SSE fixture: original fails at initialize; fixed initializes, lists echo, and calls it successfully. No model/provider calls.
  • go fmt ./..., go test ./... -count=1 -v, go vet ./..., and go build -trimpath ./cmd/shim pass. The test command includes all packages; no exclusions or upstream configuration changes.

References: SSE event parsing, MCP2025-03-26 transport.

AI assistance: Codex generated the patch/tests and ran these checks under curforever's authorization. Linux/macOS and real remote MCP servers were not locally tested; no personal human review or full SSE conformance is claimed.

@Balanov Balanov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this, and for the framing in the issue: the SSE framing was wrong, not the transport.

Decoding each data: line as a whole JSON-RPC message was the bug. In SSE a data: field belongs to an event, and one event may carry several of them. Your fix collects the fields of a single event, joins them with newlines and decodes once at the terminating blank line, which is what the framing asks for.

Two details I checked:

  • the response is decoded into a fresh value per event, so an ignored notification or an error for another request id cannot leave an error behind in the reply that eventually matters. The test with an unrelated error covers exactly that.
  • request-id filtering, the transport limits and the JSON/stdio paths are untouched, and the single-line case still works, so this fixes the broken case rather than rewriting the client.

The seven cases in the table are the right set: multiline, CRLF, a notification before the response, an unrelated error, a multiline error, a single line, and two events that must not be joined.

Merging this. It ships in shim 0.1.2.

@Balanov
Balanov merged commit c9f076a into Balanov:main Nov 25, 2026
3 checks passed
@Balanov

Balanov commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Fixed by pull request #8, which is merged and released in shim 0.1.2.

The MCP HTTP client now collects the data: fields of one SSE event, joins them and decodes the reply once at the terminating blank line, instead of decoding every data: line as if it were a complete JSON-RPC message. A valid multi-line event, including one that carries the initialize reply, is now accepted; the request-id filtering and the transport limits are unchanged.

Thank you for the report and for the fix.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP HTTP client rejects valid multiline SSE replies

2 participants