diff --git a/kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/SseIntegrationTest.kt b/kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/SseIntegrationTest.kt index 19d84589..72052518 100644 --- a/kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/SseIntegrationTest.kt +++ b/kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/SseIntegrationTest.kt @@ -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 @@ -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 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 { @@ -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" - } } \ No newline at end of file