Skip to content

Commit acc50d3

Browse files
committed
Introduce Kotlin<->TypeScript integration tests
Signed-off-by: Sergey Karpov <sergey.karpov@jetbrains.com>
1 parent 91cf120 commit acc50d3

18 files changed

+4050
-41
lines changed

kotlin-sdk-test/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ plugins {
33
}
44

55
kotlin {
6+
jvm {
7+
testRuns["test"].executionTask.configure {
8+
useJUnitPlatform()
9+
}
10+
}
611
sourceSets {
712
commonTest {
813
dependencies {
@@ -12,5 +17,13 @@ kotlin {
1217
implementation(libs.kotlinx.coroutines.test)
1318
}
1419
}
20+
jvmTest {
21+
dependencies {
22+
implementation(kotlin("test-junit5"))
23+
implementation(libs.kotlin.logging)
24+
implementation(libs.ktor.server.cio)
25+
implementation(libs.ktor.client.cio)
26+
}
27+
}
1528
}
1629
}

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/ClientIntegrationTest.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.modelcontextprotocol.kotlin.sdk.integration.kotlin
2+
3+
import io.ktor.client.HttpClient
4+
import io.ktor.client.engine.cio.CIO
5+
import io.ktor.client.plugins.sse.SSE
6+
import io.ktor.server.application.install
7+
import io.ktor.server.engine.EmbeddedServer
8+
import io.ktor.server.engine.embeddedServer
9+
import io.ktor.server.routing.routing
10+
import io.modelcontextprotocol.kotlin.sdk.Implementation
11+
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
12+
import io.modelcontextprotocol.kotlin.sdk.client.Client
13+
import io.modelcontextprotocol.kotlin.sdk.client.SseClientTransport
14+
import io.modelcontextprotocol.kotlin.sdk.integration.utils.Retry
15+
import io.modelcontextprotocol.kotlin.sdk.server.Server
16+
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
17+
import io.modelcontextprotocol.kotlin.sdk.server.mcp
18+
import kotlinx.coroutines.runBlocking
19+
import kotlinx.coroutines.withTimeout
20+
import org.junit.jupiter.api.AfterEach
21+
import org.junit.jupiter.api.BeforeEach
22+
import kotlin.time.Duration.Companion.seconds
23+
import io.ktor.server.cio.CIO as ServerCIO
24+
import io.ktor.server.sse.SSE as ServerSSE
25+
26+
@Retry(times = 3)
27+
abstract class KotlinTestBase {
28+
29+
protected val host = "localhost"
30+
protected abstract val port: Int
31+
32+
protected lateinit var server: Server
33+
protected lateinit var client: Client
34+
protected lateinit var serverEngine: EmbeddedServer<*, *>
35+
36+
protected abstract fun configureServerCapabilities(): ServerCapabilities
37+
protected abstract fun configureServer()
38+
39+
@BeforeEach
40+
fun setUp() {
41+
setupServer()
42+
runBlocking {
43+
setupClient()
44+
}
45+
}
46+
47+
protected suspend fun setupClient() {
48+
val transport = SseClientTransport(
49+
HttpClient(CIO) {
50+
install(SSE)
51+
},
52+
"http://$host:$port",
53+
)
54+
client = Client(
55+
Implementation("test", "1.0"),
56+
)
57+
client.connect(transport)
58+
}
59+
60+
protected fun setupServer() {
61+
val capabilities = configureServerCapabilities()
62+
63+
server = Server(
64+
Implementation(name = "test-server", version = "1.0"),
65+
ServerOptions(capabilities = capabilities),
66+
)
67+
68+
configureServer()
69+
70+
serverEngine = embeddedServer(ServerCIO, host = host, port = port) {
71+
install(ServerSSE)
72+
routing {
73+
mcp { server }
74+
}
75+
}.start(wait = false)
76+
}
77+
78+
@AfterEach
79+
fun tearDown() {
80+
// close client
81+
if (::client.isInitialized) {
82+
try {
83+
runBlocking {
84+
withTimeout(3.seconds) {
85+
client.close()
86+
}
87+
}
88+
} catch (e: Exception) {
89+
println("Warning: Error during client close: ${e.message}")
90+
}
91+
}
92+
93+
// stop server
94+
if (::serverEngine.isInitialized) {
95+
try {
96+
serverEngine.stop(500, 1000)
97+
} catch (e: Exception) {
98+
println("Warning: Error during server stop: ${e.message}")
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)