Skip to content

Commit 7d50d95

Browse files
JsFloe5l
andauthored
Update sample project to use library version 0.3.0 (modelcontextprotocol#47)
Co-authored-by: Leonid Stashevsky <[email protected]>
1 parent c343568 commit 7d50d95

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

samples/kotlin-mcp-server/build.gradle.kts

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ group = "org.example"
1313
version = "0.1.0"
1414

1515
dependencies {
16-
implementation("io.modelcontextprotocol:kotlin-sdk:0.2.0")
16+
implementation("io.modelcontextprotocol:kotlin-sdk:0.3.0")
1717
implementation("org.slf4j:slf4j-nop:2.0.9")
18-
19-
testImplementation(kotlin("test"))
2018
}
2119

2220
tasks.test {

samples/kotlin-mcp-server/src/main/kotlin/Main.kt

+14-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
2222
import kotlinx.coroutines.CompletableDeferred
2323
import kotlinx.coroutines.Job
2424
import 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

Comments
 (0)