1
1
package io.modelcontextprotocol.kotlin.sdk.integration
2
2
3
3
import io.ktor.client.HttpClient
4
- import io.ktor.client.plugins.sse.SSE
5
4
import io.ktor.server.application.install
6
5
import io.ktor.server.cio.CIOApplicationEngine
7
6
import io.ktor.server.engine.EmbeddedServer
@@ -20,36 +19,32 @@ import kotlinx.coroutines.withContext
20
19
import kotlin.test.Test
21
20
import kotlin.test.fail
22
21
import io.ktor.client.engine.cio.CIO as ClientCIO
22
+ import io.ktor.client.plugins.sse.SSE as ClientSSE
23
23
import io.ktor.server.cio.CIO as ServerCIO
24
+ import io.ktor.server.sse.SSE as ServerSSE
25
+
26
+ private const val URL = " 127.0.0.1"
24
27
25
28
class SseIntegrationTest {
26
29
@Test
27
30
fun `client should be able to connect to sse server` () = runTest {
28
31
val serverEngine = initServer()
29
- var client: Client ? = null
30
32
try {
31
33
withContext(Dispatchers .Default ) {
32
- assertDoesNotThrow { client = initClient() }
34
+ val port = serverEngine.engine.resolvedConnectors().first().port
35
+ val client = initClient(port)
36
+ client.close()
33
37
}
34
38
} catch (e: Exception ) {
35
39
fail(" Failed to connect client: $e " )
36
40
} finally {
37
- client?.close()
38
41
// Make sure to stop the server
39
42
serverEngine.stopSuspend(1000 , 2000 )
40
43
}
41
44
}
42
45
43
- private inline fun <T > assertDoesNotThrow (block : () -> T ): T {
44
- return try {
45
- block()
46
- } catch (e: Throwable ) {
47
- fail(" Expected no exception, but got: $e " )
48
- }
49
- }
50
-
51
- private suspend fun initClient (): Client {
52
- return HttpClient (ClientCIO ) { install(SSE ) }.mcpSse(" http://$URL :$PORT " )
46
+ private suspend fun initClient (port : Int ): Client {
47
+ return HttpClient (ClientCIO ) { install(ClientSSE ) }.mcpSse(" http://$URL :$port " )
53
48
}
54
49
55
50
private suspend fun initServer (): EmbeddedServer <CIOApplicationEngine , CIOApplicationEngine .Configuration > {
@@ -58,16 +53,11 @@ class SseIntegrationTest {
58
53
ServerOptions (capabilities = ServerCapabilities ()),
59
54
)
60
55
61
- return embeddedServer(ServerCIO , host = URL , port = PORT ) {
62
- install(io.ktor.server.sse. SSE )
56
+ return embeddedServer(ServerCIO , host = URL , port = 0 ) {
57
+ install(ServerSSE )
63
58
routing {
64
59
mcp { server }
65
60
}
66
61
}.startSuspend(wait = false )
67
62
}
68
-
69
- companion object {
70
- private const val PORT = 3001
71
- private const val URL = " localhost"
72
- }
73
63
}
0 commit comments