Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.modelcontextprotocol.kotlin.sdk.shared

import io.github.oshai.kotlinlogging.KotlinLogging
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
import kotlinx.io.Buffer
import kotlinx.io.indexOf
Expand All @@ -9,6 +10,9 @@ import kotlinx.io.readString
* Buffers a continuous stdio stream into discrete JSON-RPC messages.
*/
public class ReadBuffer {

private val logger = KotlinLogging.logger { }

private val buffer: Buffer = Buffer()

public fun append(chunk: ByteArray) {
Expand Down Expand Up @@ -37,7 +41,13 @@ public class ReadBuffer {
string
}
}
return deserializeMessage(line)
try {
return deserializeMessage(line)
} catch (e: Exception) {
logger.error(e) { "Failed to deserialize message from line: $line\nSkipping..." }
}

return null
}

public fun clear() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ class StdioClientTransportTest : BaseTransportTest() {
process.waitFor()
process.destroy()
}

@Test
fun `should ignore first output messages`() = runTest {
val processBuilder = ProcessBuilder("/usr/bin/tee")
val process = processBuilder.start()
process.outputStream.write("Stdio server started".toByteArray())

val input = process.inputStream.asSource().buffered()
val output = process.outputStream.asSink().buffered()

val client = StdioClientTransport(
input = input,
output = output,
)

testClientRead(client)

process.waitFor()
process.destroy()
}
}
Loading