Merge pull request #779 from Mohl/fix/streamable-http-mcp

fix(mcp): handle Streamable HTTP MCP responses with SSE framing
This commit is contained in:
Jaber Jaber
2026-03-27 05:36:28 +03:00
committed by GitHub
+15 -1
View File
@@ -365,7 +365,21 @@ impl McpConnection {
.await
.map_err(|e| format!("Failed to read SSE response: {e}"))?;
let rpc_response: JsonRpcResponse = serde_json::from_str(&body)
// Handle Streamable HTTP MCP responses that use SSE framing
// (e.g. "event: message\ndata: {...}\n\n"). Extract the JSON
// from the last `data:` line if the body looks like SSE.
let json_body = if body.trim_start().starts_with("event:") || body.trim_start().starts_with("data:") {
body.lines()
.filter_map(|line| line.strip_prefix("data: ").or_else(|| line.strip_prefix("data:")))
.filter(|s| !s.is_empty())
.last()
.unwrap_or(&body)
.to_string()
} else {
body
};
let rpc_response: JsonRpcResponse = serde_json::from_str(&json_body)
.map_err(|e| format!("Invalid MCP SSE JSON-RPC response: {e}"))?;
if let Some(err) = rpc_response.error {