FEAT: Support all Tier 1, 2 and 3 targets by Kotlin & remove dependency on Kotest Json due to not supporting all targets (#151)

Co-authored-by: Leonid Stashevsky <e5l@users.noreply.github.com>
This commit is contained in:
Simon Vergauwen 2025-07-17 09:50:50 +02:00 • committed by GitHub
commit 21aec46cd0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 93 additions and 56 deletions

View file

@ -220,10 +220,6 @@ kotlin {
} }
} }
iosArm64()
iosX64()
iosSimulatorArm64()
js(IR) { js(IR) {
nodejs { nodejs {
testTask { testTask {
@ -238,6 +234,31 @@ kotlin {
nodejs() nodejs()
} }
// Tier 1
macosX64()
macosArm64()
iosArm64()
iosX64()
iosSimulatorArm64()
// Tier 2
linuxX64()
linuxArm64()
watchosSimulatorArm64()
watchosX64()
// watchosArm32() not supported by libs.kotlin.logging
watchosArm64()
tvosSimulatorArm64()
tvosX64()
tvosArm64()
// Tier 3
androidNativeArm32()
androidNativeArm64()
androidNativeX86()
androidNativeX64()
mingwX64()
explicitApi = ExplicitApiMode.Strict explicitApi = ExplicitApiMode.Strict
jvmToolchain(21) jvmToolchain(21)
@ -262,7 +283,6 @@ kotlin {
implementation(libs.kotlin.test) implementation(libs.kotlin.test)
implementation(libs.ktor.server.test.host) implementation(libs.ktor.server.test.host)
implementation(libs.kotlinx.coroutines.test) implementation(libs.kotlinx.coroutines.test)
implementation(libs.kotest.assertions.json)
} }
} }

View file

@ -1,20 +1,20 @@
package io.modelcontextprotocol.kotlin.sdk package io.modelcontextprotocol.kotlin.sdk
import io.kotest.assertions.json.shouldEqualJson
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.serialization.encodeToString import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.put
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
class AudioContentSerializationTest { class AudioContentSerializationTest {
private val audioContentJson = """ private val audioContentJson = buildJsonObject {
{ put("data", "base64-encoded-audio-data")
"data": "base64-encoded-audio-data", put("mimeType", "audio/wav")
"mimeType": "audio/wav", put("type", "audio")
"type": "audio" }
}
""".trimIndent()
private val audioContent = AudioContent( private val audioContent = AudioContent(
data = "base64-encoded-audio-data", data = "base64-encoded-audio-data",
@ -23,12 +23,17 @@ class AudioContentSerializationTest {
@Test @Test
fun `should serialize audio content`() { fun `should serialize audio content`() {
McpJson.encodeToString(audioContent) shouldEqualJson audioContentJson val actual = McpJson.encodeToJsonElement(audioContent)
assertEquals(
audioContentJson,
actual,
"Expected $actual to be equal to $audioContentJson"
)
} }
@Test @Test
fun `should deserialize audio content`() { fun `should deserialize audio content`() {
val content = McpJson.decodeFromString<AudioContent>(audioContentJson) val content = McpJson.decodeFromJsonElement<AudioContent>(audioContentJson)
assertEquals(expected = audioContent, actual = content) assertEquals(expected = audioContent, actual = content)
} }
} }

View file

@ -1,51 +1,53 @@
package io.modelcontextprotocol.kotlin.sdk package io.modelcontextprotocol.kotlin.sdk
import io.kotest.assertions.json.shouldEqualJson
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
class ToolSerializationTest { class ToolSerializationTest {
// see https://docs.anthropic.com/en/docs/build-with-claude/tool-use // see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
/* language=json */ private val getWeatherToolJson = buildJsonObject {
private val getWeatherToolJson = """ put("name", JsonPrimitive("get_weather"))
{ put("description", JsonPrimitive("Get the current weather in a given location"))
"name": "get_weather", put("inputSchema", buildJsonObject {
"description": "Get the current weather in a given location", put("properties", buildJsonObject {
"inputSchema": { put("location", buildJsonObject {
"type": "object", put("type", JsonPrimitive("string"))
"properties": { put("description", JsonPrimitive("The city and state, e.g. San Francisco, CA"))
"location": { })
"type": "string", })
"description": "The city and state, e.g. San Francisco, CA" put("required", JsonArray(listOf(JsonPrimitive("location"))))
} put("type", JsonPrimitive("object"))
}, })
"required": ["location"] put("outputSchema", buildJsonObject {
}, put("type", JsonPrimitive("object"))
"outputSchema": { put("properties", buildJsonObject {
"type": "object", put("temperature", buildJsonObject {
"properties": { put("type", JsonPrimitive("number"))
"temperature": { put("description", JsonPrimitive("Temperature in celsius"))
"type": "number", })
"description": "Temperature in celsius" put("conditions", buildJsonObject {
}, put("type", JsonPrimitive("string"))
"conditions": { put("description", JsonPrimitive("Weather conditions description"))
"type": "string", })
"description": "Weather conditions description" put("humidity", buildJsonObject {
}, put("type", JsonPrimitive("number"))
"humidity": { put("description", JsonPrimitive("Humidity percentage"))
"type": "number", })
"description": "Humidity percentage" })
} put(
}, "required",
"required": ["temperature", "conditions", "humidity"] JsonArray(listOf(JsonPrimitive("temperature"), JsonPrimitive("conditions"), JsonPrimitive("humidity")))
} )
} })
""".trimIndent() }
val getWeatherTool = Tool( val getWeatherTool = Tool(
name = "get_weather", name = "get_weather",
@ -81,12 +83,18 @@ class ToolSerializationTest {
@Test @Test
fun `should serialize get_weather tool`() { fun `should serialize get_weather tool`() {
McpJson.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson val actual = McpJson.encodeToJsonElement(getWeatherTool)
assertEquals(
getWeatherToolJson,
actual,
"Expected $actual to be equal to $getWeatherToolJson"
)
} }
@Test @Test
fun `should deserialize get_weather tool`() { fun `should deserialize get_weather tool`() {
val tool = McpJson.decodeFromString<Tool>(getWeatherToolJson) val tool = McpJson.decodeFromJsonElement<Tool>(getWeatherToolJson)
assertEquals(expected = getWeatherTool, actual = tool) assertEquals(expected = getWeatherTool, actual = tool)
} }
@ -95,6 +103,11 @@ class ToolSerializationTest {
val json = Json(from = McpJson) { val json = Json(from = McpJson) {
encodeDefaults = false encodeDefaults = false
} }
json.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson val actual = json.encodeToJsonElement(getWeatherTool)
assertEquals(
getWeatherToolJson,
actual,
"Expected $actual to be equal to $getWeatherToolJson"
)
} }
} }

View file

@ -4,7 +4,6 @@ import io.ktor.utils.io.charsets.Charsets
import io.ktor.utils.io.core.toByteArray import io.ktor.utils.io.core.toByteArray
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals

View file

@ -0,0 +1,8 @@
package io.modelcontextprotocol.kotlin.sdk.internal
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
internal actual val IODispatcher: CoroutineDispatcher
get() = Dispatchers.IO