|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.client |
| 2 | + |
| 3 | +import io.kotest.matchers.collections.shouldContain |
| 4 | +import io.ktor.client.HttpClient |
| 5 | +import io.ktor.client.engine.apache5.Apache5 |
| 6 | +import io.ktor.client.plugins.sse.SSE |
| 7 | +import io.ktor.http.HttpStatusCode |
| 8 | +import io.ktor.sse.ServerSentEvent |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.ClientCapabilities |
| 10 | +import io.modelcontextprotocol.kotlin.sdk.Implementation |
| 11 | +import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest |
| 12 | +import io.modelcontextprotocol.kotlin.sdk.Tool |
| 13 | +import kotlinx.coroutines.delay |
| 14 | +import kotlinx.coroutines.flow.flow |
| 15 | +import kotlinx.coroutines.runBlocking |
| 16 | +import kotlinx.serialization.json.buildJsonObject |
| 17 | +import kotlinx.serialization.json.put |
| 18 | +import kotlinx.serialization.json.putJsonObject |
| 19 | +import me.kpavlov.mokksy.MokksyServer |
| 20 | +import me.kpavlov.mokksy.StubConfiguration |
| 21 | +import org.junit.jupiter.api.AfterAll |
| 22 | +import org.junit.jupiter.api.TestInstance |
| 23 | +import java.util.UUID |
| 24 | +import kotlin.test.AfterTest |
| 25 | +import kotlin.test.Test |
| 26 | +import kotlin.time.Duration.Companion.milliseconds |
| 27 | + |
| 28 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 29 | +class StreamableHttpClientTest { |
| 30 | + |
| 31 | + private val mokksy = MokksyServer(verbose = true) |
| 32 | + |
| 33 | + @AfterTest |
| 34 | + fun afterEach() { |
| 35 | + mokksy.checkForUnmatchedRequests() |
| 36 | + } |
| 37 | + |
| 38 | + @AfterAll |
| 39 | + fun afterAll() { |
| 40 | + mokksy.shutdown() |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `test streamableHttpClient`(): Unit = runBlocking { |
| 45 | + val client = Client( |
| 46 | + clientInfo = Implementation(name = "sample-client", version = "1.0.0"), |
| 47 | + options = ClientOptions( |
| 48 | + capabilities = ClientCapabilities(), |
| 49 | + ), |
| 50 | + ) |
| 51 | + |
| 52 | + val sessionId = UUID.randomUUID().toString() |
| 53 | + |
| 54 | + mockPostRequest( |
| 55 | + method = "initialize", |
| 56 | + sessionId = sessionId, |
| 57 | + ) { |
| 58 | + // language=json |
| 59 | + """ |
| 60 | + { |
| 61 | + "jsonrpc": "2.0", |
| 62 | + "id": 1, |
| 63 | + "result": { |
| 64 | + "capabilities": { |
| 65 | + "tools": { |
| 66 | + "listChanged": false |
| 67 | + } |
| 68 | + }, |
| 69 | + "protocolVersion": "2025-03-26", |
| 70 | + "serverInfo": { |
| 71 | + "name": "Mock MCP Server", |
| 72 | + "version": "1.0.0" |
| 73 | + }, |
| 74 | + "_meta": { |
| 75 | + "foo": "bar" |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + """.trimIndent() |
| 80 | + } |
| 81 | + |
| 82 | + mockPostRequest( |
| 83 | + method = "notifications/initialized", |
| 84 | + sessionId = sessionId, |
| 85 | + statusCode = HttpStatusCode.Accepted, |
| 86 | + ) { |
| 87 | + "" |
| 88 | + } |
| 89 | + |
| 90 | + mokksy.get(name = "MCP GETs", requestType = Any::class) { |
| 91 | + path("/mcp") |
| 92 | + containsHeader("Mcp-Session-Id", sessionId) |
| 93 | + containsHeader("Connection", "keep-alive") |
| 94 | + containsHeader("Cache-Control", "no-store") |
| 95 | + } respondsWithSseStream { |
| 96 | + headers += "Mcp-Session-Id" to sessionId |
| 97 | + flow = |
| 98 | + flow { |
| 99 | + delay(500.milliseconds) |
| 100 | + emit( |
| 101 | + ServerSentEvent( |
| 102 | + event = "message", |
| 103 | + id = "1", |
| 104 | + data = @Suppress("ktlint:standard:max-line-length") |
| 105 | + //language=json |
| 106 | + """{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"upload-123","progress":50,"total":100}}""", |
| 107 | + ), |
| 108 | + ) |
| 109 | + delay(200.milliseconds) |
| 110 | + emit( |
| 111 | + ServerSentEvent( |
| 112 | + data = @Suppress("ktlint:standard:max-line-length") |
| 113 | + //language=json |
| 114 | + """{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"upload-123","progress":50,"total":100}}""", |
| 115 | + ), |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + client.connect( |
| 121 | + StreamableHttpClientTransport( |
| 122 | + url = "http://localhost:${mokksy.port()}/mcp", |
| 123 | + client = HttpClient(Apache5) { |
| 124 | + install(SSE) |
| 125 | + }, |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + // TODO: get notifications |
| 130 | + |
| 131 | + mockPostRequest( |
| 132 | + method = "tools/list", |
| 133 | + sessionId = sessionId, |
| 134 | + ) { |
| 135 | + // language=json |
| 136 | + """ |
| 137 | + { |
| 138 | + "jsonrpc": "2.0", |
| 139 | + "id": 3, |
| 140 | + "result": { |
| 141 | + "tools": [ |
| 142 | + { |
| 143 | + "name": "get_weather", |
| 144 | + "title": "Weather Information Provider", |
| 145 | + "description": "Get current weather information for a location", |
| 146 | + "inputSchema": { |
| 147 | + "type": "object", |
| 148 | + "properties": { |
| 149 | + "location": { |
| 150 | + "type": "string", |
| 151 | + "description": "City name or zip code" |
| 152 | + } |
| 153 | + }, |
| 154 | + "required": ["location"] |
| 155 | + }, |
| 156 | + "outputSchema": { |
| 157 | + "type": "object", |
| 158 | + "properties": { |
| 159 | + "temperature": { |
| 160 | + "type": "number", |
| 161 | + "description": "Temperature, Celsius" |
| 162 | + } |
| 163 | + }, |
| 164 | + "required": ["temperature"] |
| 165 | + } |
| 166 | + } |
| 167 | + ] |
| 168 | + } |
| 169 | + } |
| 170 | + """.trimIndent() |
| 171 | + } |
| 172 | + |
| 173 | + val listToolsResult = client.listTools() |
| 174 | + |
| 175 | + listToolsResult.tools shouldContain Tool( |
| 176 | + name = "get_weather", |
| 177 | + title = "Weather Information Provider", |
| 178 | + description = "Get current weather information for a location", |
| 179 | + inputSchema = Tool.Input( |
| 180 | + properties = buildJsonObject { |
| 181 | + putJsonObject("location") { |
| 182 | + put("type", "string") |
| 183 | + put("description", "City name or zip code") |
| 184 | + } |
| 185 | + }, |
| 186 | + required = listOf("location"), |
| 187 | + ), |
| 188 | + outputSchema = Tool.Output( |
| 189 | + properties = buildJsonObject { |
| 190 | + putJsonObject("temperature") { |
| 191 | + put("type", "number") |
| 192 | + put("description", "Temperature, Celsius") |
| 193 | + } |
| 194 | + }, |
| 195 | + required = listOf("temperature"), |
| 196 | + ), |
| 197 | + annotations = null, |
| 198 | + ) |
| 199 | + } |
| 200 | + |
| 201 | + private fun mockPostRequest( |
| 202 | + method: String, |
| 203 | + sessionId: String, |
| 204 | + statusCode: HttpStatusCode = HttpStatusCode.OK, |
| 205 | + bodyBuilder: () -> String, |
| 206 | + ) { |
| 207 | + mokksy.post( |
| 208 | + configuration = StubConfiguration(removeAfterMatch = true), |
| 209 | + requestType = JSONRPCRequest::class, |
| 210 | + ) { |
| 211 | + path("/mcp") |
| 212 | + bodyMatchesPredicates( |
| 213 | + { |
| 214 | + it!!.method == method |
| 215 | + }, |
| 216 | + { |
| 217 | + it!!.jsonrpc == "2.0" |
| 218 | + }, |
| 219 | + ) |
| 220 | + } respondsWith { |
| 221 | + body = bodyBuilder.invoke() |
| 222 | + headers += "Content-Type" to "application/json; charset=utf-8" |
| 223 | + headers += "Mcp-Session-Id" to sessionId |
| 224 | + httpStatus = statusCode |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments