Update sample project to use library version 0.3.0 (#47)

Co-authored-by: Leonid Stashevsky <e5l@users.noreply.github.com>
This commit is contained in:
Jose Flores 2025-02-25 02:20:17 -06:00 • committed by GitHub
commit 164d276336
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 11 deletions

View file

@ -13,10 +13,8 @@ group = "org.example"
version = "0.1.0" version = "0.1.0"
dependencies { 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") implementation("org.slf4j:slf4j-nop:2.0.9")
testImplementation(kotlin("test"))
} }
tasks.test { tasks.test {

View file

@ -22,6 +22,9 @@ import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.io.asSink
import kotlinx.io.asSource
import kotlinx.io.buffered
/** /**
* Start sse-server mcp on port 3001. * Start sse-server mcp on port 3001.
@ -35,9 +38,9 @@ fun main(args: Array<String>) {
val command = args.firstOrNull() ?: "--sse-server-ktor" val command = args.firstOrNull() ?: "--sse-server-ktor"
val port = args.getOrNull(1)?.toIntOrNull() ?: 3001 val port = args.getOrNull(1)?.toIntOrNull() ?: 3001
when (command) { when (command) {
"--stdio" -> `run mcp server using stdio`() "--stdio" -> runMcpServerUsingStdio()
"--sse-server-ktor" -> `run sse mcp server using Ktor plugin`(port) "--sse-server-ktor" -> runSseMcpServerUsingKtorPlugin(port)
"--sse-server" -> `run sse mcp server with plain configuration`(port) "--sse-server" -> runSseMcpServerWithPlainConfiguration(port)
else -> { else -> {
System.err.println("Unknown command: $command") System.err.println("Unknown command: $command")
} }
@ -114,11 +117,14 @@ fun configureServer(): Server {
return server return server
} }
fun `run mcp server using stdio`() { fun runMcpServerUsingStdio() {
// Note: The server will handle listing prompts, tools, and resources automatically. // Note: The server will handle listing prompts, tools, and resources automatically.
// The handleListResourceTemplates will return empty as defined in the Server code. // The handleListResourceTemplates will return empty as defined in the Server code.
val server = configureServer() val server = configureServer()
val transport = StdioServerTransport() val transport = StdioServerTransport(
inputStream = System.`in`.asSource().buffered(),
outputStream = System.out.asSink().buffered()
)
runBlocking { runBlocking {
server.connect(transport) 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<String, Server>() val servers = ConcurrentMap<String, Server>()
println("Starting sse server on port $port. ") println("Starting sse server on port $port. ")
println("Use inspector to connect to the http://localhost:$port/sse") 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. * @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. * @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("Starting sse server on port $port")
println("Use inspector to connect to the http://localhost:$port/sse") 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 { MCP {
return@MCP configureServer() return@MCP configureServer()
} }
} }.start(wait = true)
} }