|
| 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