Skip to content

Update sample project to use library version 0.3.0 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions samples/kotlin-mcp-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions samples/kotlin-mcp-server/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -35,9 +38,9 @@ fun main(args: Array<String>) {
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")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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>()
println("Starting sse server on port $port. ")
println("Use inspector to connect to the http://localhost:$port/sse")
Expand Down Expand Up @@ -179,13 +185,13 @@ 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")

embeddedServer(CIO, host = "0.0.0.0", port = port) {
MCP {
return@MCP configureServer()
}
}
}.start(wait = true)
}