CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

Can't connect to MCP with SSE. Error connecting to open-brain.

Open djairjr opened this issue 6 months ago • 2 comments

MCP_RS_ISSUE.md — SSE parser misses CRLF event separators from uvicorn/FastMCP

File

crates/tui/src/mcp.rs — SseTransport::run_sse_loop method (lines ~480–500)

Status

  • Bug #1 (race condition on endpoint discovery): fixed upstream in PR #1225 / commit f29d1a3.
  • Bug #2 (CRLF event separator): still present as of 2026-05-09 on origin/main.

Symptom

MCP SSE servers served by uvicorn (e.g. FastMCP, open-brain) fail to connect. The HTTP GET to /sse succeeds (200 OK), but the client never parses the endpoint event. wait_for_endpoint times out with:

SSE endpoint not received within 30000ms

Even with connect_timeout set to 3 seconds on a LAN, the endpoint is emitted by the server in under 1 ms but never seen by the client.

Servers using pure-LF SSE (e.g. mcp Python SDK's test helpers, some Node.js implementations) work fine.


Root Cause

HTTP/1.1 specifies \r\n (CRLF) as the line terminator. uvicorn faithfully uses CRLF line endings throughout, including its SSE event stream. The resulting wire format is:

event: endpoint\r\n
data: /messages/?session_id=abc\r\n
\r\n
event: message\r\n
data: {"jsonrpc":"2.0","id":1,...}\r\n
\r\n

The SSE event separator is \r\n\r\n — a blank CRLF line.

The DeepSeek-TUI parser only searches for \n\n (LF-only), so the separator is never matched and events are never extracted. The endpoint goes unseen and the connection times out.

Additionally, the lines parsed from each event block carry a trailing \r, which prevents prefix matching ("endpoint\r" != "endpoint").

Server-side proof

A raw-socket test against a FastMCP server confirmed CRLF framing:

$ python3 mcp_test.py
ENDPOINT: /messages/?session_id=183cc59e...
INIT: Accepted
NOTIF: Accepted
TOOLS: Accepted

=== SSE RAW ===
event: message\r\n
data: {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05",...}}\r\n
\r\n

The tools/list response arrives correctly on the wire — the bug is purely in the Rust client parser.


Fix — three small changes in run_sse_loop

1. Accept both \n\n and \r\n\r\n as event separators

// Before:
while let Some(pos) = buffer.find("\n\n") {

// After:
while let Some(pos) = buffer.find("\n\n")
    .or_else(|| buffer.find("\r\n\r\n"))
{

2. Advance the buffer by the correct number of bytes

// Before:
buffer = buffer[pos + 2..].to_string();

// After:
let has_crlf = buffer[pos..].starts_with("\r\n\r\n");
buffer = if has_crlf {
    buffer[pos + 4..].to_string()
} else {
    buffer[pos + 2..].to_string()
};

3. Strip trailing \r from lines before prefix matching

// Before:
for line in event_block.lines() {
    if let Some(stripped) = line.strip_prefix("event: ") {

// After:
for line in event_block.lines() {
    let line = line.trim_end_matches('\r');
    if let Some(stripped) = line.strip_prefix("event: ") {

Full diff

-            while let Some(pos) = buffer.find("\n\n") {
+            while let Some(pos) = buffer.find("\n\n")
+                .or_else(|| buffer.find("\r\n\r\n"))
+            {
                 let event_block = buffer[..pos].to_string();
-                buffer = buffer[pos + 2..].to_string();
+                let has_crlf = buffer[pos..].starts_with("\r\n\r\n");
+                buffer = if has_crlf {
+                    buffer[pos + 4..].to_string()
+                } else {
+                    buffer[pos + 2..].to_string()
+                };

                 let mut event_type = "message";
                 let mut data = String::new();

                 for line in event_block.lines() {
+                    let line = line.trim_end_matches('\r');
                     if let Some(stripped) = line.strip_prefix("event: ") {

Verification

  1. Set up any FastMCP server (or the open-brain SSE MCP server)
  2. Configure mcp.json with the SSE URL
  3. Before the fix: mcp connect times out / tools never listed
  4. After the fix: mcp connect succeeds immediately, mcp tools <name> lists all tools
$ deepseek-tui mcp connect open-brain
Connected to MCP server: open-brain

$ deepseek-tui mcp tools open-brain
Tools for open-brain:
  - memory_search: Search memories by query, tags, source, or date range.
  - memory_store: Store a new memory with auto-tagging and entity extraction.
  - memory_get_related: Get memories related to a specific memory by ID.
  - memory_get_entity: Get all memories containing a specific entity.
  - memory_today: Get memories created today.
  - memory_stats: Get memory statistics.
  - memory_weekly_report: Generate a weekly report of memory activity.

Impact

This affects every MCP SSE server running behind uvicorn, which includes most FastMCP servers. The fix is backward-compatible — it accepts both \n\n (LF) and \r\n\r\n (CRLF), so existing stdio/LF servers are unaffected.

djairjr avatar May 09 '26 14:05 djairjr

Code review 确认了这个问题。SseTransport::run_sse_loop 中的 SSE parser 只匹配 \n\n 作为事件分隔符,不处理 \r\n\r\n(CRLF)。

issue 里已经贴了完整的 diff,3 处改动:

  1. 同时匹配 \n\n 和 \r\n\r\n
  2. 按实际分隔符长度推进 buffer
  3. 行尾 trim \r

这个修复是向后兼容的 — 接受 LF 和 CRLF 两种格式,现有的 stdio/LF server 不受影响。

punkcanyang avatar May 10 '26 15:05 punkcanyang

Thank you @reidliu41 for the fix and @djairjr for the report — CRLF SSE event parsing for MCP shipped in v0.8.29 (0164ee869).

So sorry I'm only now getting to this — and for the late thank-you. Your contribution is now recorded in our credits (CONTRIBUTORS.md / CHANGELOG). Thank you for helping make CodeWhale what it is today, and I look forward to hearing how you think it can still improve in the future! Reopen if this resurfaces.

Hmbown avatar Jun 15 '26 06:06 Hmbown