-
Notifications
You must be signed in to change notification settings - Fork 184
Fix kotlin-mcp-server sample #459
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
+43
−18
Merged
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions
17
samples/kotlin-mcp-server/src/test/kotlin/McpServerType.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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import io.ktor.server.engine.EmbeddedServer | ||
| import io.modelcontextprotocol.sample.server.runSseMcpServerUsingKtorPlugin | ||
| import io.modelcontextprotocol.sample.server.runSseMcpServerWithPlainConfiguration | ||
|
|
||
| enum class McpServerType( | ||
| val sseEndpoint: String, | ||
| val serverFactory: (port: Int) -> EmbeddedServer<*, *> | ||
| ) { | ||
| KTOR_PLUGIN( | ||
| sseEndpoint = "", | ||
| serverFactory = { port -> runSseMcpServerUsingKtorPlugin(port, wait = false) } | ||
| ), | ||
| PLAIN_CONFIGURATION( | ||
| sseEndpoint = "/sse", | ||
| serverFactory = { port -> runSseMcpServerWithPlainConfiguration(port, wait = false) } | ||
| ) | ||
| } | ||
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,34 +1,34 @@ | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.cio.CIO | ||
| import io.ktor.client.plugins.sse.SSE | ||
| import io.modelcontextprotocol.kotlin.sdk.Implementation | ||
| import io.ktor.server.engine.EmbeddedServer | ||
| import io.modelcontextprotocol.kotlin.sdk.client.Client | ||
| import io.modelcontextprotocol.kotlin.sdk.client.mcpSseTransport | ||
| import io.modelcontextprotocol.sample.server.runSseMcpServerUsingKtorPlugin | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Implementation | ||
| import kotlinx.coroutines.runBlocking | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| object TestEnvironment { | ||
| class TestEnvironment(private val serverConfig: McpServerType) { | ||
|
|
||
| val server = runSseMcpServerUsingKtorPlugin(0, wait = false) | ||
| val server: EmbeddedServer<*, *> = serverConfig.serverFactory(0) | ||
| val client: Client | ||
|
|
||
| init { | ||
| client = runBlocking { | ||
| val port = server.engine.resolvedConnectors().single().port | ||
| initClient(port) | ||
| initClient(port, serverConfig) | ||
| } | ||
|
|
||
| Runtime.getRuntime().addShutdownHook( | ||
| Thread { | ||
| println("🏁 Shutting down server") | ||
| println("🏁 Shutting down server (${serverConfig.name})") | ||
| server.stop(500, 700, TimeUnit.MILLISECONDS) | ||
| println("☑️ Shutdown complete") | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| private suspend fun initClient(port: Int): Client { | ||
| private suspend fun initClient(port: Int, config: McpServerType): Client { | ||
| val client = Client( | ||
| Implementation(name = "test-client", version = "0.1.0"), | ||
| ) | ||
|
|
@@ -37,13 +37,7 @@ object TestEnvironment { | |
| install(SSE) | ||
| } | ||
|
|
||
| // Create a transport wrapper that captures the session ID and received messages | ||
| val transport = httpClient.mcpSseTransport { | ||
| url { | ||
| this.host = "127.0.0.1" | ||
| this.port = port | ||
| } | ||
| } | ||
| val transport = httpClient.mcpSseTransport("http://127.0.0.1:$port/${config.sseEndpoint}") | ||
|
||
| client.connect(transport) | ||
| return client | ||
| } | ||
|
|
||
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.
The
sseEndpointshould be"sse"instead of"/sse"to avoid creating a double-slash URL (//sse) when concatenated inTestEnvironment.ktline 40. The leading slash is already provided in the URL construction.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.
make sense, but can be addressed separately