diff --git a/samples/kotlin-mcp-server/build.gradle.kts b/samples/kotlin-mcp-server/build.gradle.kts index f833f9e..af21141 100644 --- a/samples/kotlin-mcp-server/build.gradle.kts +++ b/samples/kotlin-mcp-server/build.gradle.kts @@ -13,10 +13,8 @@ group = "org.example" version = "0.1.0" dependencies { - implementation("io.modelcontextprotocol:kotlin-sdk:0.2.0") + implementation("io.modelcontextprotocol:kotlin-sdk:0.3.0") implementation("org.slf4j:slf4j-nop:2.0.9") - - testImplementation(kotlin("test")) } tasks.test { diff --git a/samples/kotlin-mcp-server/src/main/kotlin/Main.kt b/samples/kotlin-mcp-server/src/main/kotlin/Main.kt index b31b758..a2d23e0 100644 --- a/samples/kotlin-mcp-server/src/main/kotlin/Main.kt +++ b/samples/kotlin-mcp-server/src/main/kotlin/Main.kt @@ -22,6 +22,9 @@ import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Job import kotlinx.coroutines.runBlocking +import kotlinx.io.asSink +import kotlinx.io.asSource +import kotlinx.io.buffered /** * Start sse-server mcp on port 3001. @@ -35,9 +38,9 @@ fun main(args: Array) { val command = args.firstOrNull() ?: "--sse-server-ktor" val port = args.getOrNull(1)?.toIntOrNull() ?: 3001 when (command) { - "--stdio" -> `run mcp server using stdio`() - "--sse-server-ktor" -> `run sse mcp server using Ktor plugin`(port) - "--sse-server" -> `run sse mcp server with plain configuration`(port) + "--stdio" -> runMcpServerUsingStdio() + "--sse-server-ktor" -> runSseMcpServerUsingKtorPlugin(port) + "--sse-server" -> runSseMcpServerWithPlainConfiguration(port) else -> { System.err.println("Unknown command: $command") } @@ -114,11 +117,14 @@ fun configureServer(): Server { return server } -fun `run mcp server using stdio`() { +fun runMcpServerUsingStdio() { // Note: The server will handle listing prompts, tools, and resources automatically. // The handleListResourceTemplates will return empty as defined in the Server code. val server = configureServer() - val transport = StdioServerTransport() + val transport = StdioServerTransport( + inputStream = System.`in`.asSource().buffered(), + outputStream = System.out.asSink().buffered() + ) runBlocking { server.connect(transport) @@ -132,7 +138,7 @@ fun `run mcp server using stdio`() { } } -fun `run sse mcp server with plain configuration`(port: Int): Unit = runBlocking { +fun runSseMcpServerWithPlainConfiguration(port: Int): Unit = runBlocking { val servers = ConcurrentMap() println("Starting sse server on port $port. ") println("Use inspector to connect to the http://localhost:$port/sse") @@ -179,7 +185,7 @@ fun `run sse mcp server with plain configuration`(port: Int): Unit = runBlocking * @param port The port number on which the SSE MCP server will listen for client connections. * @return Unit This method does not return a value. */ -fun `run sse mcp server using Ktor plugin`(port: Int): Unit = runBlocking { +fun runSseMcpServerUsingKtorPlugin(port: Int): Unit = runBlocking { println("Starting sse server on port $port") println("Use inspector to connect to the http://localhost:$port/sse") @@ -187,5 +193,5 @@ fun `run sse mcp server using Ktor plugin`(port: Int): Unit = runBlocking { MCP { return@MCP configureServer() } - } + }.start(wait = true) } \ No newline at end of file