Skip to content

Commit 926c487

Browse files
committed
fix(sse-client): Fix StreamableHttpClientTransport to ignore empty data
Refactor StreamableHttpClientTransport to skip SSE with an empty data field.
1 parent fbbb430 commit 926c487

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

kotlin-sdk-client/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransport.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ import kotlin.concurrent.atomics.AtomicBoolean
4242
import kotlin.concurrent.atomics.ExperimentalAtomicApi
4343
import kotlin.time.Duration
4444

45-
private val logger = KotlinLogging.logger {}
46-
4745
private const val MCP_SESSION_ID_HEADER = "mcp-session-id"
4846
private const val MCP_PROTOCOL_VERSION_HEADER = "mcp-protocol-version"
4947
private const val MCP_RESUMPTION_TOKEN_HEADER = "Last-Event-ID"
@@ -67,6 +65,10 @@ public class StreamableHttpClientTransport(
6765
private val requestBuilder: HttpRequestBuilder.() -> Unit = {},
6866
) : AbstractTransport() {
6967

68+
private companion object {
69+
private val logger = KotlinLogging.logger {}
70+
}
71+
7072
public var sessionId: String? = null
7173
private set
7274
public var protocolVersion: String? = null
@@ -316,7 +318,10 @@ public class StreamableHttpClientTransport(
316318
var id: String? = null
317319
var eventName: String? = null
318320

319-
suspend fun dispatch(data: String) {
321+
suspend fun dispatch(id: String?, eventName: String?, data: String) {
322+
if (data.isBlank()) {
323+
return
324+
}
320325
id?.let {
321326
lastEventId = it
322327
onResumptionToken?.invoke(it)
@@ -335,16 +340,16 @@ public class StreamableHttpClientTransport(
335340
throw it
336341
}
337342
}
338-
// reset
339-
id = null
340-
eventName = null
341-
sb.clear()
342343
}
343344

344345
while (!channel.isClosedForRead) {
345346
val line = channel.readUTF8Line() ?: break
346347
if (line.isEmpty()) {
347-
dispatch(sb.toString())
348+
dispatch(id = id, eventName = eventName, data = sb.toString())
349+
// reset
350+
id = null
351+
eventName = null
352+
sb.clear()
348353
continue
349354
}
350355
when {

0 commit comments

Comments
 (0)