@@ -22,6 +22,9 @@ import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
2222import kotlinx.coroutines.CompletableDeferred
2323import kotlinx.coroutines.Job
2424import kotlinx.coroutines.runBlocking
25+ import kotlinx.io.asSink
26+ import kotlinx.io.asSource
27+ import kotlinx.io.buffered
2528
2629/* *
2730 * Start sse-server mcp on port 3001.
@@ -35,9 +38,9 @@ fun main(args: Array<String>) {
3538 val command = args.firstOrNull() ? : " --sse-server-ktor"
3639 val port = args.getOrNull(1 )?.toIntOrNull() ? : 3001
3740 when (command) {
38- " --stdio" -> `run mcp server using stdio` ()
39- " --sse-server-ktor" -> `run sse mcp server using Ktor plugin` (port)
40- " --sse-server" -> `run sse mcp server with plain configuration` (port)
41+ " --stdio" -> runMcpServerUsingStdio ()
42+ " --sse-server-ktor" -> runSseMcpServerUsingKtorPlugin (port)
43+ " --sse-server" -> runSseMcpServerWithPlainConfiguration (port)
4144 else -> {
4245 System .err.println (" Unknown command: $command " )
4346 }
@@ -114,11 +117,14 @@ fun configureServer(): Server {
114117 return server
115118}
116119
117- fun `run mcp server using stdio` () {
120+ fun runMcpServerUsingStdio () {
118121 // Note: The server will handle listing prompts, tools, and resources automatically.
119122 // The handleListResourceTemplates will return empty as defined in the Server code.
120123 val server = configureServer()
121- val transport = StdioServerTransport ()
124+ val transport = StdioServerTransport (
125+ inputStream = System .`in `.asSource().buffered(),
126+ outputStream = System .out .asSink().buffered()
127+ )
122128
123129 runBlocking {
124130 server.connect(transport)
@@ -132,7 +138,7 @@ fun `run mcp server using stdio`() {
132138 }
133139}
134140
135- fun `run sse mcp server with plain configuration` (port : Int ): Unit = runBlocking {
141+ fun runSseMcpServerWithPlainConfiguration (port : Int ): Unit = runBlocking {
136142 val servers = ConcurrentMap <String , Server >()
137143 println (" Starting sse server on port $port . " )
138144 println (" Use inspector to connect to the http://localhost:$port /sse" )
@@ -179,13 +185,13 @@ fun `run sse mcp server with plain configuration`(port: Int): Unit = runBlocking
179185 * @param port The port number on which the SSE MCP server will listen for client connections.
180186 * @return Unit This method does not return a value.
181187 */
182- fun `run sse mcp server using Ktor plugin` (port : Int ): Unit = runBlocking {
188+ fun runSseMcpServerUsingKtorPlugin (port : Int ): Unit = runBlocking {
183189 println (" Starting sse server on port $port " )
184190 println (" Use inspector to connect to the http://localhost:$port /sse" )
185191
186192 embeddedServer(CIO , host = " 0.0.0.0" , port = port) {
187193 MCP {
188194 return @MCP configureServer()
189195 }
190- }
196+ }.start(wait = true )
191197}
0 commit comments