Refactor integration tests (#256)

## Motivation and Context

Flaky integration tests

## How Has This Been Tested?

`./gradlew check`

## Changes

- Use random port for Ktor in tests
- Remove unused `TestUtils` utility class.
- Replace `runTest` with `runBlocking` in test cases.
- Adjust imports and formatting for consistency.
- Add `Awaitility` dependency for dynamic server setup.
- Update `Duration` usage from milliseconds to seconds.
- Minor configuration and code formatting updates.

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [x] Test update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [ ] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [ ] My code follows the repository's style guidelines
- [ ] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
<!-- Add any other context, implementation notes, or design decisions
-->
This commit is contained in:
Konstantin Pavlov 2025-09-11 19:38:09 +03:00 • committed by GitHub
commit 7bdd6b93f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,21 +25,20 @@ import io.modelcontextprotocol.kotlin.sdk.server.Server
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
import io.modelcontextprotocol.kotlin.sdk.server.SseServerTransport
import io.modelcontextprotocol.kotlin.sdk.server.mcp
import kotlin.collections.set
fun configureServer(): Server {
val server = Server(
Implementation(
name = "mcp-kotlin test server",
version = "0.1.0"
version = "0.1.0",
),
ServerOptions(
capabilities = ServerCapabilities(
prompts = ServerCapabilities.Prompts(listChanged = true),
resources = ServerCapabilities.Resources(subscribe = true, listChanged = true),
tools = ServerCapabilities.Tools(listChanged = true),
)
)
),
),
)
server.addPrompt(
@ -49,18 +48,20 @@ fun configureServer(): Server {
PromptArgument(
name = "Project Name",
description = "Project name for the new project",
required = true
)
)
required = true,
),
),
) { request ->
GetPromptResult(
"Description for ${request.name}",
messages = listOf(
PromptMessage(
role = Role.user,
content = TextContent("Develop a kotlin project named <name>${request.arguments?.get("Project Name")}</name>")
)
)
content = TextContent(
"Develop a kotlin project named <name>${request.arguments?.get("Project Name")}</name>",
),
),
),
)
}
@ -68,10 +69,10 @@ fun configureServer(): Server {
server.addTool(
name = "kotlin-sdk-tool",
description = "A test tool",
inputSchema = Tool.Input()
inputSchema = Tool.Input(),
) { request ->
CallToolResult(
content = listOf(TextContent("Hello, world!"))
content = listOf(TextContent("Hello, world!")),
)
}
@ -80,19 +81,19 @@ fun configureServer(): Server {
uri = "https://search.com/",
name = "Web Search",
description = "Web search engine",
mimeType = "text/html"
mimeType = "text/html",
) { request ->
ReadResourceResult(
contents = listOf(
TextResourceContents("Placeholder content for ${request.uri}", request.uri, "text/html")
)
TextResourceContents("Placeholder content for ${request.uri}", request.uri, "text/html"),
),
)
}
return server
}
suspend fun runSseMcpServerWithPlainConfiguration(port: Int): Unit {
suspend fun runSseMcpServerWithPlainConfiguration(port: Int) {
val servers = ConcurrentMap<String, Server>()
println("Starting sse server on port $port. ")
println("Use inspector to connect to the http://localhost:$port/sse")
@ -139,7 +140,7 @@ suspend fun runSseMcpServerWithPlainConfiguration(port: Int): Unit {
* @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): Unit {
suspend fun runSseMcpServerUsingKtorPlugin(port: Int) {
println("Starting sse server on port $port")
println("Use inspector to connect to the http://localhost:$port/sse")
@ -148,4 +149,4 @@ suspend fun runSseMcpServerUsingKtorPlugin(port: Int): Unit {
return@mcp configureServer()
}
}.startSuspend(wait = true)
}
}