|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.testing |
| 2 | + |
| 3 | +import io.kotest.matchers.collections.shouldHaveSize |
| 4 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 5 | +import io.modelcontextprotocol.kotlin.sdk.ExperimentalMcpApi |
| 6 | +import io.modelcontextprotocol.kotlin.sdk.client.Client |
| 7 | +import io.modelcontextprotocol.kotlin.sdk.server.Server |
| 8 | +import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.server.ServerSession |
| 10 | +import io.modelcontextprotocol.kotlin.sdk.types.Implementation |
| 11 | +import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequest |
| 12 | +import io.modelcontextprotocol.kotlin.sdk.types.InitializeResult |
| 13 | +import io.modelcontextprotocol.kotlin.sdk.types.LATEST_PROTOCOL_VERSION |
| 14 | +import io.modelcontextprotocol.kotlin.sdk.types.ListResourcesRequest |
| 15 | +import io.modelcontextprotocol.kotlin.sdk.types.ListResourcesResult |
| 16 | +import io.modelcontextprotocol.kotlin.sdk.types.Method |
| 17 | +import io.modelcontextprotocol.kotlin.sdk.types.Resource |
| 18 | +import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities |
| 19 | +import kotlinx.coroutines.CompletableDeferred |
| 20 | +import kotlinx.coroutines.joinAll |
| 21 | +import kotlinx.coroutines.launch |
| 22 | +import kotlinx.coroutines.runBlocking |
| 23 | +import kotlin.test.Test |
| 24 | + |
| 25 | +@OptIn(ExperimentalMcpApi::class) |
| 26 | +class ChannelTransportTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + fun `should connect and list resources`(): Unit = runBlocking { |
| 30 | + val serverOptions = ServerOptions( |
| 31 | + capabilities = ServerCapabilities( |
| 32 | + resources = ServerCapabilities.Resources(), |
| 33 | + ), |
| 34 | + ) |
| 35 | + val server = Server( |
| 36 | + Implementation(name = "test server", version = "1.0"), |
| 37 | + serverOptions, |
| 38 | + ) |
| 39 | + |
| 40 | + val (clientTransport, serverTransport) = ChannelTransport.createLinkedPair() |
| 41 | + |
| 42 | + val client = Client( |
| 43 | + clientInfo = Implementation(name = "test client", version = "1.0"), |
| 44 | + ) |
| 45 | + |
| 46 | + val serverSessionResult = CompletableDeferred<ServerSession>() |
| 47 | + |
| 48 | + listOf( |
| 49 | + launch { |
| 50 | + client.connect(clientTransport) |
| 51 | + }, |
| 52 | + launch { |
| 53 | + serverSessionResult.complete(server.createSession(serverTransport)) |
| 54 | + }, |
| 55 | + ).joinAll() |
| 56 | + |
| 57 | + val serverSession = serverSessionResult.await() |
| 58 | + serverSession.setRequestHandler<InitializeRequest>(Method.Defined.Initialize) { _, _ -> |
| 59 | + InitializeResult( |
| 60 | + protocolVersion = LATEST_PROTOCOL_VERSION, |
| 61 | + capabilities = ServerCapabilities( |
| 62 | + resources = ServerCapabilities.Resources(null, null), |
| 63 | + tools = ServerCapabilities.Tools(null), |
| 64 | + ), |
| 65 | + serverInfo = Implementation(name = "test", version = "1.0"), |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + serverSession.setRequestHandler<ListResourcesRequest>(Method.Defined.ResourcesList) { _, _ -> |
| 70 | + ListResourcesResult( |
| 71 | + resources = listOf( |
| 72 | + Resource( |
| 73 | + uri = "/foo/bar", |
| 74 | + name = "foo-bar-resource", |
| 75 | + ), |
| 76 | + ), |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + // These should not throw |
| 81 | + client.listResources() shouldNotBeNull { |
| 82 | + this.resources shouldHaveSize 1 |
| 83 | + } |
| 84 | + |
| 85 | + client.close() |
| 86 | + server.close() |
| 87 | + } |
| 88 | +} |
0 commit comments