From f6e401aa971aa9e43900d3299b160d20934879c3 Mon Sep 17 00:00:00 2001 From: Ayal Spitz Date: Sun, 27 Apr 2025 09:50:08 -0400 Subject: [PATCH] Fix event stream line parsing to handle CRLF and empty lines correctly (#89) * Optimized code in `connectToEventStream` to use `.lines` computed value of `AsyncSequence` to produce a `AsyncLineSequence` instead of building a lines manually. * - Reverter use of `lines` (it swallows newlines) - Fiexed original issue I experienced with line 229 `while let newlineIndex = buffer.firstIndex(of: "\n") {` not handling "\r\n" properly - Added unit test to test for "\r\n" scenario - Simplified `end of event` test - `line` creation trims newline, added code trims carriage return. Only thing that should be left is an empty line --- .../Base/Transports/HTTPClientTransport.swift | 14 ++++++-- Tests/MCPTests/HTTPClientTransportTests.swift | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/Sources/MCP/Base/Transports/HTTPClientTransport.swift b/Sources/MCP/Base/Transports/HTTPClientTransport.swift index 5a68b61..33d033a 100644 --- a/Sources/MCP/Base/Transports/HTTPClientTransport.swift +++ b/Sources/MCP/Base/Transports/HTTPClientTransport.swift @@ -226,12 +226,16 @@ public actor HTTPClientTransport: Actor, Transport { buffer.append(char) // Process complete lines - while let newlineIndex = buffer.firstIndex(of: "\n") { - let line = buffer[..