|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.client |
| 2 | + |
| 3 | +import dev.mokksy.mokksy.Mokksy |
| 4 | +import dev.mokksy.mokksy.StubConfiguration |
| 5 | +import io.ktor.http.ContentType |
| 6 | +import io.ktor.http.HttpMethod |
| 7 | +import io.ktor.http.HttpStatusCode |
| 8 | +import io.ktor.sse.ServerSentEvent |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest |
| 10 | +import kotlinx.coroutines.flow.Flow |
| 11 | + |
| 12 | +/** |
| 13 | + * High-level helper for simulating an MCP server over Streaming HTTP transport with Server-Sent Events (SSE), |
| 14 | + * built on top of an HTTP server using the [Mokksy](https://mokksy.dev) library. |
| 15 | + * |
| 16 | + * Provides test utilities to mock server behavior based on specific request conditions. |
| 17 | + * |
| 18 | + * @param verbose Whether to print detailed logs. Defaults to `false`. |
| 19 | + * @author Konstantin Pavlov |
| 20 | + */ |
| 21 | +internal class MockMcp(verbose: Boolean = false) { |
| 22 | + |
| 23 | + private val mokksy: Mokksy = Mokksy(verbose = verbose) |
| 24 | + |
| 25 | + fun checkForUnmatchedRequests() { |
| 26 | + mokksy.checkForUnmatchedRequests() |
| 27 | + } |
| 28 | + |
| 29 | + val url = mokksy.baseUrl() + "/mcp" |
| 30 | + |
| 31 | + @Suppress("LongParameterList") |
| 32 | + fun onJSONRPCRequest( |
| 33 | + httpMethod: HttpMethod = HttpMethod.Post, |
| 34 | + jsonRpcMethod: String, |
| 35 | + expectedSessionId: String? = null, |
| 36 | + sessionId: String, |
| 37 | + contentType: ContentType = ContentType.Application.Json, |
| 38 | + statusCode: HttpStatusCode = HttpStatusCode.OK, |
| 39 | + bodyBuilder: () -> String, |
| 40 | + ) { |
| 41 | + mokksy.method( |
| 42 | + configuration = StubConfiguration(removeAfterMatch = true), |
| 43 | + httpMethod = httpMethod, |
| 44 | + requestType = JSONRPCRequest::class, |
| 45 | + ) { |
| 46 | + path("/mcp") |
| 47 | + expectedSessionId?.let { |
| 48 | + containsHeader("Mcp-Session-Id", it) |
| 49 | + } |
| 50 | + bodyMatchesPredicates( |
| 51 | + { |
| 52 | + it!!.method == jsonRpcMethod |
| 53 | + }, |
| 54 | + { |
| 55 | + it!!.jsonrpc == "2.0" |
| 56 | + }, |
| 57 | + ) |
| 58 | + } respondsWith { |
| 59 | + body = bodyBuilder.invoke() |
| 60 | + this.contentType = contentType |
| 61 | + headers += "Mcp-Session-Id" to sessionId |
| 62 | + httpStatus = statusCode |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fun onSubscribeWithGet(sessionId: String, block: () -> Flow<ServerSentEvent>) { |
| 67 | + mokksy.get(name = "MCP GETs", requestType = Any::class) { |
| 68 | + path("/mcp") |
| 69 | + containsHeader("Mcp-Session-Id", sessionId) |
| 70 | + containsHeader("Accept", "application/json,text/event-stream") |
| 71 | + containsHeader("Cache-Control", "no-store") |
| 72 | + } respondsWithSseStream { |
| 73 | + headers += "Mcp-Session-Id" to sessionId |
| 74 | + this.flow = block.invoke() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fun mockUnsubscribeRequest(sessionId: String) { |
| 79 | + mokksy.delete( |
| 80 | + configuration = StubConfiguration(removeAfterMatch = true), |
| 81 | + requestType = JSONRPCRequest::class, |
| 82 | + ) { |
| 83 | + path("/mcp") |
| 84 | + containsHeader("Mcp-Session-Id", sessionId) |
| 85 | + } respondsWith { |
| 86 | + body = null |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments