Skip to content
Merged
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,7 +1,6 @@
package io.modelcontextprotocol.kotlin.sdk.integration

import io.ktor.client.HttpClient
import io.ktor.client.plugins.sse.SSE
import io.ktor.server.application.install
import io.ktor.server.cio.CIOApplicationEngine
import io.ktor.server.engine.EmbeddedServer
Expand All @@ -20,36 +19,32 @@ import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.test.fail
import io.ktor.client.engine.cio.CIO as ClientCIO
import io.ktor.client.plugins.sse.SSE as ClientSSE
import io.ktor.server.cio.CIO as ServerCIO
import io.ktor.server.sse.SSE as ServerSSE

private const val URL = "127.0.0.1"

class SseIntegrationTest {
@Test
fun `client should be able to connect to sse server`() = runTest {
val serverEngine = initServer()
var client: Client? = null
try {
withContext(Dispatchers.Default) {
assertDoesNotThrow { client = initClient() }
val port = serverEngine.engine.resolvedConnectors().first().port
val client = initClient(port)
client.close()
}
} catch (e: Exception) {
fail("Failed to connect client: $e")
} finally {
client?.close()
// Make sure to stop the server
serverEngine.stopSuspend(1000, 2000)
}
}

private inline fun <T> assertDoesNotThrow(block: () -> T): T {
return try {
block()
} catch (e: Throwable) {
fail("Expected no exception, but got: $e")
}
}

private suspend fun initClient(): Client {
return HttpClient(ClientCIO) { install(SSE) }.mcpSse("http://$URL:$PORT")
private suspend fun initClient(port: Int): Client {
return HttpClient(ClientCIO) { install(ClientSSE) }.mcpSse("http://$URL:$port")
}

private suspend fun initServer(): EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration> {
Expand All @@ -58,16 +53,11 @@ class SseIntegrationTest {
ServerOptions(capabilities = ServerCapabilities()),
)

return embeddedServer(ServerCIO, host = URL, port = PORT) {
install(io.ktor.server.sse.SSE)
return embeddedServer(ServerCIO, host = URL, port = 0) {
install(ServerSSE)
routing {
mcp { server }
}
}.startSuspend(wait = false)
}

companion object {
private const val PORT = 3001
private const val URL = "localhost"
}
}
Loading