|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.integration |
| 2 | + |
| 3 | +import io.ktor.client.HttpClient |
| 4 | +import io.ktor.client.plugins.sse.SSE |
| 5 | +import io.ktor.server.cio.CIOApplicationEngine |
| 6 | +import io.ktor.server.engine.EmbeddedServer |
| 7 | +import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest |
| 8 | +import io.modelcontextprotocol.kotlin.sdk.GetPromptResult |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.Implementation |
| 10 | +import io.modelcontextprotocol.kotlin.sdk.PromptArgument |
| 11 | +import io.modelcontextprotocol.kotlin.sdk.PromptMessage |
| 12 | +import io.modelcontextprotocol.kotlin.sdk.Role |
| 13 | +import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities |
| 14 | +import io.modelcontextprotocol.kotlin.sdk.TextContent |
| 15 | +import io.modelcontextprotocol.kotlin.sdk.client.Client |
| 16 | +import io.modelcontextprotocol.kotlin.sdk.client.mcpSseTransport |
| 17 | +import io.modelcontextprotocol.kotlin.sdk.server.Server |
| 18 | +import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions |
| 19 | +import kotlinx.coroutines.Dispatchers |
| 20 | +import kotlinx.coroutines.test.runTest |
| 21 | +import kotlinx.coroutines.withContext |
| 22 | +import kotlin.test.Test |
| 23 | +import kotlin.test.assertTrue |
| 24 | +import kotlin.time.Duration.Companion.seconds |
| 25 | +import io.ktor.client.engine.cio.CIO as ClientCIO |
| 26 | + |
| 27 | +typealias CIOEmbeddedServer = EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration> |
| 28 | + |
| 29 | +abstract class AbstractSseIntegrationTest { |
| 30 | + @Test |
| 31 | + fun `client should be able to connect to sse server`() = runTest(timeout = 5.seconds) { |
| 32 | + var server: CIOEmbeddedServer? = null |
| 33 | + var client: Client? = null |
| 34 | + |
| 35 | + try { |
| 36 | + withContext(Dispatchers.Default) { |
| 37 | + val (s, path) = initServer() |
| 38 | + server = s |
| 39 | + |
| 40 | + val port = server.engine.resolvedConnectors().first().port |
| 41 | + client = initClient(serverPort = port, path = path) |
| 42 | + } |
| 43 | + } finally { |
| 44 | + client?.close() |
| 45 | + server?.stopSuspend(1000, 2000) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test Case #1: One opened connection, a client gets a prompt |
| 51 | + * |
| 52 | + * 1. Open SSE from Client A. |
| 53 | + * 2. Send a POST request from Client A to POST /prompts/get. |
| 54 | + * 3. Observe that Client A receives a response related to it. |
| 55 | + */ |
| 56 | + @Test |
| 57 | + fun `single sse connection`() = runTest(timeout = 5.seconds) { |
| 58 | + var server: CIOEmbeddedServer? = null |
| 59 | + var client: Client? = null |
| 60 | + try { |
| 61 | + withContext(Dispatchers.Default) { |
| 62 | + val (s, path) = initServer() |
| 63 | + server = s |
| 64 | + |
| 65 | + val port = server.engine.resolvedConnectors().first().port |
| 66 | + client = initClient("Client A", port, path) |
| 67 | + |
| 68 | + val promptA = getPrompt(client, "Client A") |
| 69 | + assertTrue { "Client A" in promptA } |
| 70 | + } |
| 71 | + } finally { |
| 72 | + client?.close() |
| 73 | + server?.stopSuspend(1000, 2000) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test Case #1: Two open connections, each client gets a client-specific prompt |
| 79 | + * |
| 80 | + * 1. Open SSE connection #1 from Client A and note the sessionId=<sessionId#1> value. |
| 81 | + * 2. Open SSE connection #2 from Client B and note the sessionId=<sessionId#2> value. |
| 82 | + * 3. Send a POST request to POST /message with the corresponding sessionId#1. |
| 83 | + * 4. Observe that Client B (connection #2) receives a response related to sessionId#1. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + fun `multiple sse connections`() = runTest(timeout = 5.seconds) { |
| 87 | + var server: CIOEmbeddedServer? = null |
| 88 | + var clientA: Client? = null |
| 89 | + var clientB: Client? = null |
| 90 | + |
| 91 | + try { |
| 92 | + withContext(Dispatchers.Default) { |
| 93 | + val (s, path) = initServer() |
| 94 | + server = s |
| 95 | + val port = server.engine.resolvedConnectors().first().port |
| 96 | + |
| 97 | + clientA = initClient("Client A", port, path) |
| 98 | + clientB = initClient("Client B", port, path) |
| 99 | + |
| 100 | + // Step 3: Send a prompt request from Client A |
| 101 | + val promptA = getPrompt(clientA, "Client A") |
| 102 | + // Step 4: Send a prompt request from Client B |
| 103 | + val promptB = getPrompt(clientB, "Client B") |
| 104 | + |
| 105 | + assertTrue { "Client A" in promptA } |
| 106 | + assertTrue { "Client B" in promptB } |
| 107 | + } |
| 108 | + } finally { |
| 109 | + clientA?.close() |
| 110 | + clientB?.close() |
| 111 | + server?.stopSuspend(1000, 2000) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private suspend fun initClient(name: String = "", serverPort: Int, path: List<String>): Client { |
| 116 | + val client = Client( |
| 117 | + Implementation(name = name, version = "1.0.0"), |
| 118 | + ) |
| 119 | + |
| 120 | + val httpClient = HttpClient(ClientCIO) { |
| 121 | + install(SSE) |
| 122 | + } |
| 123 | + |
| 124 | + // Create a transport wrapper that captures the session ID and received messages |
| 125 | + val transport = httpClient.mcpSseTransport { |
| 126 | + url { |
| 127 | + host = URL |
| 128 | + port = serverPort |
| 129 | + pathSegments = path |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + client.connect(transport) |
| 134 | + |
| 135 | + return client |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Create initialise the webserver for testing. |
| 140 | + * Concrete test classes implement this. |
| 141 | + */ |
| 142 | + protected abstract suspend fun initServer(): Pair<CIOEmbeddedServer, List<String>> |
| 143 | + |
| 144 | + /** |
| 145 | + * Construct a new instance of the mcp server under test |
| 146 | + */ |
| 147 | + protected fun newMcpServer(): Server { |
| 148 | + val server = Server( |
| 149 | + Implementation(name = "sse-server", version = "1.0.0"), |
| 150 | + ServerOptions( |
| 151 | + capabilities = ServerCapabilities(prompts = ServerCapabilities.Prompts(listChanged = true)), |
| 152 | + ), |
| 153 | + ) |
| 154 | + |
| 155 | + server.addPrompt( |
| 156 | + name = "prompt", |
| 157 | + description = "Prompt description", |
| 158 | + arguments = listOf( |
| 159 | + PromptArgument( |
| 160 | + name = "client", |
| 161 | + description = "Client name who requested a prompt", |
| 162 | + required = true, |
| 163 | + ), |
| 164 | + ), |
| 165 | + ) { request -> |
| 166 | + GetPromptResult( |
| 167 | + "Prompt for ${request.name}", |
| 168 | + messages = listOf( |
| 169 | + PromptMessage( |
| 170 | + role = Role.user, |
| 171 | + content = TextContent("Prompt for client ${request.arguments?.get("client")}"), |
| 172 | + ), |
| 173 | + ), |
| 174 | + ) |
| 175 | + } |
| 176 | + return server |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Retrieves a prompt result using the provided client and client name. |
| 181 | + */ |
| 182 | + private suspend fun getPrompt(client: Client, clientName: String): String { |
| 183 | + val response = client.getPrompt( |
| 184 | + GetPromptRequest( |
| 185 | + "prompt", |
| 186 | + arguments = mapOf("client" to clientName), |
| 187 | + ), |
| 188 | + ) |
| 189 | + |
| 190 | + return (response.messages.first().content as? TextContent)?.text |
| 191 | + ?: error("Failed to receive prompt for Client $clientName") |
| 192 | + } |
| 193 | + |
| 194 | + companion object { |
| 195 | + protected const val URL = "127.0.0.1" |
| 196 | + protected const val PORT = 0 |
| 197 | + } |
| 198 | +} |
0 commit comments