-
Notifications
You must be signed in to change notification settings - Fork 174
Fix kotlin-mcp-server example and update samples #345
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
+115
−144
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d95024d
Update Kotlin, Ktor, and MCP-Kotlin dependencies across samples
devcrocod e4d6a88
Refactor server structure and configuration to align with JVM-first d…
devcrocod 420b9ff
Simplify server functions by removing redundant coroutines support an…
devcrocod a13636a
Replace `System.err.println` with `error` for cleaner error handling
devcrocod 065e8e2
Merge branch 'main' into devcrocod/update-samples
devcrocod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,26 @@ | ||
| @file:OptIn(ExperimentalWasmDsl::class, ExperimentalKotlinGradlePluginApi::class) | ||
|
|
||
| import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi | ||
| import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.kotlin.multiplatform) | ||
| alias(libs.plugins.kotlin.jvm) | ||
| alias(libs.plugins.kotlin.serialization) | ||
| application | ||
| } | ||
|
|
||
| group = "org.example" | ||
| version = "0.1.0" | ||
|
|
||
| val jvmMainClass = "Main_jvmKt" | ||
| application { | ||
| mainClass.set("io.modelcontextprotocol.sample.server.MainKt") | ||
| } | ||
|
|
||
| kotlin { | ||
| jvmToolchain(17) | ||
| jvm { | ||
| binaries { | ||
| executable { | ||
| mainClass.set(jvmMainClass) | ||
| } | ||
| } | ||
| val jvmJar by tasks.getting(org.gradle.jvm.tasks.Jar::class) { | ||
| duplicatesStrategy = DuplicatesStrategy.EXCLUDE | ||
| doFirst { | ||
| manifest { | ||
| attributes["Main-Class"] = jvmMainClass | ||
| } | ||
| dependencies { | ||
| implementation(libs.mcp.kotlin.server) | ||
| implementation(libs.ktor.server.cio) | ||
| implementation(libs.slf4j.simple) | ||
| } | ||
|
|
||
| from(configurations["jvmRuntimeClasspath"].map { if (it.isDirectory) it else zipTree(it) }) | ||
| } | ||
| } | ||
| } | ||
| wasmJs { | ||
| nodejs() | ||
| binaries.executable() | ||
| } | ||
| tasks.test { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| sourceSets { | ||
| commonMain.dependencies { | ||
| implementation(libs.mcp.kotlin.server) | ||
| implementation(libs.ktor.server.cio) | ||
| } | ||
| jvmMain.dependencies { | ||
| implementation(libs.slf4j.simple) | ||
| } | ||
| wasmJsMain.dependencies {} | ||
| } | ||
| kotlin { | ||
| jvmToolchain(17) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
11 changes: 7 additions & 4 deletions
11
...rver/src/wasmJsMain/kotlin/main.wasmJs.kt → ...odelcontextprotocol/sample/server/main.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| import shared.runSseMcpServerUsingKtorPlugin | ||
| import shared.runSseMcpServerWithPlainConfiguration | ||
| package io.modelcontextprotocol.sample.server | ||
|
|
||
| import kotlinx.coroutines.runBlocking | ||
|
|
||
| /** | ||
| * Start sse-server mcp on port 3001. | ||
| * | ||
| * @param args | ||
| * - "--stdio": Runs an MCP server using standard input/output. | ||
| * - "--sse-server-ktor <port>": Runs an SSE MCP server using Ktor plugin (default if no argument is provided). | ||
| * - "--sse-server <port>": Runs an SSE MCP server with a plain configuration. | ||
| */ | ||
| suspend fun main(args: Array<String>) { | ||
| fun main(args: Array<String>): Unit = runBlocking { | ||
| val command = args.firstOrNull() ?: "--sse-server-ktor" | ||
| val port = args.getOrNull(1)?.toIntOrNull() ?: 3001 | ||
| when (command) { | ||
| "--stdio" -> runMcpServerUsingStdio() | ||
| "--sse-server-ktor" -> runSseMcpServerUsingKtorPlugin(port) | ||
| "--sse-server" -> runSseMcpServerWithPlainConfiguration(port) | ||
| else -> { | ||
| error("Unknown command: $command") | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package shared | ||
| package io.modelcontextprotocol.sample.server | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
|
|
||
| import io.ktor.http.HttpStatusCode | ||
| import io.ktor.server.application.install | ||
|
|
@@ -25,7 +25,13 @@ import io.modelcontextprotocol.kotlin.sdk.server.Server | |
| import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.server.ServerSession | ||
| import io.modelcontextprotocol.kotlin.sdk.server.SseServerTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.server.mcp | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.io.asSink | ||
| import kotlinx.io.asSource | ||
| import kotlinx.io.buffered | ||
|
|
||
| fun configureServer(): Server { | ||
| val server = Server( | ||
|
|
@@ -71,7 +77,7 @@ fun configureServer(): Server { | |
| name = "kotlin-sdk-tool", | ||
| description = "A test tool", | ||
| inputSchema = Tool.Input(), | ||
| ) { request -> | ||
| ) { _ -> | ||
| CallToolResult( | ||
| content = listOf(TextContent("Hello, world!")), | ||
| ) | ||
|
|
@@ -94,10 +100,10 @@ fun configureServer(): Server { | |
| return server | ||
| } | ||
|
|
||
| suspend fun runSseMcpServerWithPlainConfiguration(port: Int) { | ||
| fun runSseMcpServerWithPlainConfiguration(port: Int) { | ||
| val serverSessions = ConcurrentMap<String, ServerSession>() | ||
| println("Starting sse server on port $port. ") | ||
| println("Use inspector to connect to the http://localhost:$port/sse") | ||
| println("Starting SSE server on port $port") | ||
| println("Use inspector to connect to http://localhost:$port/sse") | ||
|
|
||
| val server = configureServer() | ||
|
|
||
|
|
@@ -106,21 +112,21 @@ suspend fun runSseMcpServerWithPlainConfiguration(port: Int) { | |
| routing { | ||
| sse("/sse") { | ||
| val transport = SseServerTransport("/message", this) | ||
|
|
||
| // For SSE, you can also add prompts/tools/resources if needed: | ||
| // server.addTool(...), server.addPrompt(...), server.addResource(...) | ||
|
|
||
| val serverSession = server.connect(transport) | ||
| serverSessions[transport.sessionId] = server.connect(transport) | ||
| serverSessions[transport.sessionId] = serverSession | ||
|
|
||
| serverSession.onClose { | ||
| println("Server closed") | ||
| println("Server session closed for: ${transport.sessionId}") | ||
| serverSessions.remove(transport.sessionId) | ||
| } | ||
| } | ||
| post("/message") { | ||
| println("Received Message") | ||
| val sessionId: String = call.request.queryParameters["sessionId"]!! | ||
| val sessionId: String? = call.request.queryParameters["sessionId"] | ||
| if (sessionId == null) { | ||
| call.respond(HttpStatusCode.BadRequest, "Missing sessionId parameter") | ||
| return@post | ||
| } | ||
|
|
||
| val transport = serverSessions[sessionId]?.transport as? SseServerTransport | ||
| if (transport == null) { | ||
| call.respond(HttpStatusCode.NotFound, "Session not found") | ||
|
|
@@ -130,24 +136,47 @@ suspend fun runSseMcpServerWithPlainConfiguration(port: Int) { | |
| transport.handlePostMessage(call) | ||
| } | ||
| } | ||
| }.startSuspend(wait = true) | ||
| }.start(wait = true) | ||
| } | ||
|
|
||
| /** | ||
| * Starts an SSE (Server Sent Events) MCP server using the Ktor framework and the specified port. | ||
| * Starts an SSE (Server-Sent Events) MCP server using the Ktor plugin. | ||
| * | ||
| * The url can be accessed in the MCP inspector at [http://localhost:$port] | ||
| * This is the recommended approach for SSE servers as it simplifies configuration. | ||
| * The URL can be accessed in the MCP inspector at http://localhost:[port]/sse | ||
| * | ||
| * @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. | ||
| */ | ||
| suspend fun runSseMcpServerUsingKtorPlugin(port: Int) { | ||
| println("Starting sse server on port $port") | ||
| println("Use inspector to connect to the http://localhost:$port/sse") | ||
| fun runSseMcpServerUsingKtorPlugin(port: Int) { | ||
| println("Starting SSE server on port $port") | ||
| println("Use inspector to connect to http://localhost:$port/sse") | ||
|
|
||
| embeddedServer(CIO, host = "127.0.0.1", port = port) { | ||
| mcp { | ||
| return@mcp configureServer() | ||
| } | ||
| }.startSuspend(wait = true) | ||
| }.start(wait = true) | ||
| } | ||
|
|
||
| /** | ||
| * Starts an MCP server using Standard I/O transport. | ||
| * | ||
| * This mode is useful for process-based communication where the server | ||
| * communicates via stdin/stdout with a parent process or client. | ||
| */ | ||
| fun runMcpServerUsingStdio() { | ||
| val server = configureServer() | ||
| val transport = StdioServerTransport( | ||
| inputStream = System.`in`.asSource().buffered(), | ||
| outputStream = System.out.asSink().buffered() | ||
| ) | ||
|
|
||
| runBlocking { | ||
| server.connect(transport) | ||
| val done = Job() | ||
| server.onClose { | ||
| done.complete() | ||
| } | ||
| done.join() | ||
| } | ||
| } | ||
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use Netty as server engine, because CIO does not support Http2 and Netty is de-facto a standard for high-performance servers for decades.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example is essentially crossplatform
Therefore, I think it’s better to keep cio as the default ktor engine, since ktor itself uses it in its own examples