@@ -22,6 +22,9 @@ import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
22
22
import kotlinx.coroutines.CompletableDeferred
23
23
import kotlinx.coroutines.Job
24
24
import kotlinx.coroutines.runBlocking
25
+ import kotlinx.io.asSink
26
+ import kotlinx.io.asSource
27
+ import kotlinx.io.buffered
25
28
26
29
/* *
27
30
* Start sse-server mcp on port 3001.
@@ -35,9 +38,9 @@ fun main(args: Array<String>) {
35
38
val command = args.firstOrNull() ? : " --sse-server-ktor"
36
39
val port = args.getOrNull(1 )?.toIntOrNull() ? : 3001
37
40
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)
41
44
else -> {
42
45
System .err.println (" Unknown command: $command " )
43
46
}
@@ -114,11 +117,14 @@ fun configureServer(): Server {
114
117
return server
115
118
}
116
119
117
- fun `run mcp server using stdio` () {
120
+ fun runMcpServerUsingStdio () {
118
121
// Note: The server will handle listing prompts, tools, and resources automatically.
119
122
// The handleListResourceTemplates will return empty as defined in the Server code.
120
123
val server = configureServer()
121
- val transport = StdioServerTransport ()
124
+ val transport = StdioServerTransport (
125
+ inputStream = System .`in `.asSource().buffered(),
126
+ outputStream = System .out .asSink().buffered()
127
+ )
122
128
123
129
runBlocking {
124
130
server.connect(transport)
@@ -132,7 +138,7 @@ fun `run mcp server using stdio`() {
132
138
}
133
139
}
134
140
135
- fun `run sse mcp server with plain configuration` (port : Int ): Unit = runBlocking {
141
+ fun runSseMcpServerWithPlainConfiguration (port : Int ): Unit = runBlocking {
136
142
val servers = ConcurrentMap <String , Server >()
137
143
println (" Starting sse server on port $port . " )
138
144
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
179
185
* @param port The port number on which the SSE MCP server will listen for client connections.
180
186
* @return Unit This method does not return a value.
181
187
*/
182
- fun `run sse mcp server using Ktor plugin` (port : Int ): Unit = runBlocking {
188
+ fun runSseMcpServerUsingKtorPlugin (port : Int ): Unit = runBlocking {
183
189
println (" Starting sse server on port $port " )
184
190
println (" Use inspector to connect to the http://localhost:$port /sse" )
185
191
186
192
embeddedServer(CIO , host = " 0.0.0.0" , port = port) {
187
193
MCP {
188
194
return @MCP configureServer()
189
195
}
190
- }
196
+ }.start(wait = true )
191
197
}
0 commit comments