Skip to content
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
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jreleaser = "1.19.0"
binaryCompatibilityValidatorPlugin = "0.18.1"
slf4j = "2.0.17"
kotest = "5.9.1"
awaitility = "4.3.0"

# Samples
mcp-kotlin = "0.7.0"
Expand Down Expand Up @@ -45,11 +46,12 @@ ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", v
ktor-server-core = { group = "io.ktor", name = "ktor-server-core", version.ref = "ktor" }

# Testing
awaitility = { group = "org.awaitility", name = "awaitility-kotlin", version.ref = "awaitility" }
kotest-assertions-json = { group = "io.kotest", name = "kotest-assertions-json", version.ref = "kotest" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
ktor-client-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" }
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }
kotest-assertions-json = { group = "io.kotest", name = "kotest-assertions-json", version.ref = "kotest" }

# Samples
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.serializer
import kotlin.collections.get
import kotlin.reflect.KType
import kotlin.reflect.typeOf
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

private val LOGGER = KotlinLogging.logger { }

Expand Down Expand Up @@ -85,7 +84,7 @@ public open class ProtocolOptions(
/**
* The default request timeout.
*/
public val DEFAULT_REQUEST_TIMEOUT: Duration = 60000.milliseconds
public val DEFAULT_REQUEST_TIMEOUT: Duration = 60.seconds

/**
* Options that can be given per request.
Expand Down
1 change: 1 addition & 0 deletions kotlin-sdk-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ kotlin {
jvmTest {
dependencies {
implementation(kotlin("test-junit5"))
implementation(libs.awaitility)
runtimeOnly(libs.slf4j.simple)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
import io.modelcontextprotocol.kotlin.sdk.server.mcp
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.awaitility.kotlin.await
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import kotlin.time.Duration.Companion.seconds
Expand All @@ -27,7 +28,7 @@ import io.ktor.server.sse.SSE as ServerSSE
abstract class KotlinTestBase {

protected val host = "localhost"
protected abstract val port: Int
protected var port: Int = 0

protected lateinit var server: Server
protected lateinit var client: Client
Expand All @@ -39,6 +40,12 @@ abstract class KotlinTestBase {
@BeforeEach
fun setUp() {
setupServer()
await
.ignoreExceptions()
.until {
port = runBlocking { serverEngine.engine.resolvedConnectors().first().port }
port != 0
}
runBlocking {
setupClient()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import io.modelcontextprotocol.kotlin.sdk.PromptMessage
import io.modelcontextprotocol.kotlin.sdk.Role
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.TextContent
import io.modelcontextprotocol.kotlin.sdk.integration.utils.TestUtils.runTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
Expand All @@ -18,8 +19,6 @@ import kotlin.test.assertTrue

class PromptEdgeCasesTest : KotlinTestBase() {

override val port = 3008

private val basicPromptName = "basic-prompt"
private val basicPromptDescription = "A basic prompt for testing"

Expand Down Expand Up @@ -183,7 +182,7 @@ class PromptEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testBasicPrompt() = runTest {
fun testBasicPrompt() = runBlocking(Dispatchers.IO) {
val testName = "Alice"
val result = client.getPrompt(
GetPromptRequest(
Expand Down Expand Up @@ -215,7 +214,7 @@ class PromptEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testComplexPromptWithManyArguments() = runTest {
fun testComplexPromptWithManyArguments() = runBlocking(Dispatchers.IO) {
val arguments = (1..10).associate { i -> "arg$i" to "value$i" }

val result = client.getPrompt(
Expand Down Expand Up @@ -253,7 +252,7 @@ class PromptEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testLargePrompt() = runTest {
fun testLargePrompt() = runBlocking(Dispatchers.IO) {
val result = client.getPrompt(
GetPromptRequest(
name = largePromptName,
Expand All @@ -275,7 +274,7 @@ class PromptEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testSpecialCharacters() = runTest {
fun testSpecialCharacters() = runBlocking(Dispatchers.IO) {
val result = client.getPrompt(
GetPromptRequest(
name = specialCharsPromptName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.modelcontextprotocol.kotlin.sdk.PromptMessageContent
import io.modelcontextprotocol.kotlin.sdk.Role
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.TextContent
import io.modelcontextprotocol.kotlin.sdk.integration.utils.TestUtils.runTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand All @@ -19,7 +19,6 @@ import kotlin.test.assertTrue

class PromptIntegrationTest : KotlinTestBase() {

override val port = 3004
private val testPromptName = "greeting"
private val testPromptDescription = "A simple greeting prompt"
private val complexPromptName = "multimodal-prompt"
Expand Down Expand Up @@ -219,7 +218,7 @@ class PromptIntegrationTest : KotlinTestBase() {
}

@Test
fun testListPrompts() = runTest {
fun testListPrompts() = runBlocking(Dispatchers.IO) {
val result = client.listPrompts()

assertNotNull(result, "List prompts result should not be null")
Expand Down Expand Up @@ -247,7 +246,7 @@ class PromptIntegrationTest : KotlinTestBase() {
}

@Test
fun testGetPrompt() = runTest {
fun testGetPrompt() = runBlocking(Dispatchers.IO) {
val testName = "Alice"
val result = client.getPrompt(
GetPromptRequest(
Expand Down Expand Up @@ -290,7 +289,7 @@ class PromptIntegrationTest : KotlinTestBase() {
}

@Test
fun testMissingRequiredArguments() = runTest {
fun testMissingRequiredArguments() = runBlocking(Dispatchers.IO) {
val promptsList = client.listPrompts()
assertNotNull(promptsList, "Prompts list should not be null")
val strictPrompt = promptsList.prompts.find { it.name == strictPromptName }
Expand Down Expand Up @@ -364,7 +363,7 @@ class PromptIntegrationTest : KotlinTestBase() {
}

@Test
fun testComplexContentTypes() = runTest {
fun testComplexContentTypes() = runBlocking(Dispatchers.IO) {
val topic = "artificial intelligence"
val result = client.getPrompt(
GetPromptRequest(
Expand Down Expand Up @@ -418,7 +417,7 @@ class PromptIntegrationTest : KotlinTestBase() {
}

@Test
fun testMultipleMessagesAndRoles() = runTest {
fun testMultipleMessagesAndRoles() = runBlocking(Dispatchers.IO) {
val topic = "climate change"
val result = client.getPrompt(
GetPromptRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.SubscribeRequest
import io.modelcontextprotocol.kotlin.sdk.TextResourceContents
import io.modelcontextprotocol.kotlin.sdk.UnsubscribeRequest
import io.modelcontextprotocol.kotlin.sdk.integration.utils.TestUtils.runTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.util.concurrent.atomic.AtomicBoolean
Expand All @@ -21,8 +22,6 @@ import kotlin.test.assertTrue

class ResourceEdgeCasesTest : KotlinTestBase() {

override val port = 3007

private val testResourceUri = "test://example.txt"
private val testResourceName = "Test Resource"
private val testResourceDescription = "A test resource for integration testing"
Expand Down Expand Up @@ -129,7 +128,7 @@ class ResourceEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testBinaryResource() = runTest {
fun testBinaryResource() = runBlocking(Dispatchers.IO) {
val result = client.readResource(ReadResourceRequest(uri = binaryResourceUri))

assertNotNull(result, "Read resource result should not be null")
Expand All @@ -142,7 +141,7 @@ class ResourceEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testLargeResource() = runTest {
fun testLargeResource() = runBlocking(Dispatchers.IO) {
val result = client.readResource(ReadResourceRequest(uri = largeResourceUri))

assertNotNull(result, "Read resource result should not be null")
Expand Down Expand Up @@ -172,7 +171,7 @@ class ResourceEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testDynamicResource() = runTest {
fun testDynamicResource() = runBlocking(Dispatchers.IO) {
val initialResult = client.readResource(ReadResourceRequest(uri = dynamicResourceUri))
assertNotNull(initialResult, "Initial read result should not be null")
val initialContent = (initialResult.contents.firstOrNull() as? TextResourceContents)?.text
Expand All @@ -188,7 +187,7 @@ class ResourceEdgeCasesTest : KotlinTestBase() {
}

@Test
fun testResourceAddAndRemove() = runTest {
fun testResourceAddAndRemove() = runBlocking(Dispatchers.IO) {
val initialList = client.listResources()
assertNotNull(initialList, "Initial list result should not be null")
val initialCount = initialList.resources.size
Expand Down Expand Up @@ -261,7 +260,7 @@ class ResourceEdgeCasesTest : KotlinTestBase() {

@Test
fun testSubscribeAndUnsubscribe() {
runTest {
runBlocking(Dispatchers.IO) {
val subscribeResult = client.subscribeResource(SubscribeRequest(uri = testResourceUri))
assertNotNull(subscribeResult, "Subscribe result should not be null")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.SubscribeRequest
import io.modelcontextprotocol.kotlin.sdk.TextResourceContents
import io.modelcontextprotocol.kotlin.sdk.UnsubscribeRequest
import io.modelcontextprotocol.kotlin.sdk.integration.utils.TestUtils.runTest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class ResourceIntegrationTest : KotlinTestBase() {

override val port = 3005
private val testResourceUri = "test://example.txt"
private val testResourceName = "Test Resource"
private val testResourceDescription = "A test resource for integration testing"
Expand Down Expand Up @@ -57,7 +57,7 @@ class ResourceIntegrationTest : KotlinTestBase() {
}

@Test
fun testListResources() = runTest {
fun testListResources() = runBlocking(Dispatchers.IO) {
val result = client.listResources()

assertNotNull(result, "List resources result should not be null")
Expand All @@ -70,7 +70,7 @@ class ResourceIntegrationTest : KotlinTestBase() {
}

@Test
fun testReadResource() = runTest {
fun testReadResource() = runBlocking(Dispatchers.IO) {
val result = client.readResource(ReadResourceRequest(uri = testResourceUri))

assertNotNull(result, "Read resource result should not be null")
Expand All @@ -83,7 +83,7 @@ class ResourceIntegrationTest : KotlinTestBase() {

@Test
fun testSubscribeAndUnsubscribe() {
runTest {
runBlocking(Dispatchers.IO) {
val subscribeResult = client.subscribeResource(SubscribeRequest(uri = testResourceUri))
assertNotNull(subscribeResult, "Subscribe result should not be null")

Expand Down
Loading
Loading