Skip to content

Add support for testing JSON serialization of the mcp API #45

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
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ kotlin {
implementation(libs.ktor.server.test.host)
implementation(libs.kotlinx.coroutines.test)
implementation(libs.kotlinx.coroutines.debug)
implementation(libs.kotest.assertions.json)
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ logging = "7.0.0"
jreleaser = "1.15.0"
binaryCompatibilityValidatorPlugin = "0.17.0"
slf4j = "2.0.16"
kotest = "5.9.1"

[libraries]
# Kotlinx libraries
Expand All @@ -32,8 +33,7 @@ kotlinx-coroutines-debug = { group = "org.jetbrains.kotlinx", name = "kotlinx-co
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }


kotest-assertions-json = { group = "io.kotest", name = "kotest-assertions-json", version.ref = "kotest" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Expand Down
57 changes: 57 additions & 0 deletions src/commonTest/kotlin/ToolSerializationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.modelcontextprotocol.kotlin.sdk

import io.kotest.assertions.json.shouldEqualJson
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlin.test.Test
import kotlin.test.assertEquals

class ToolSerializationTest {

// see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
/* language=json */
private val getWeatherToolJson = """
Copy link
Contributor

@e5l e5l Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
"name": "get_weather",
"description": "Get the current weather in a given location",
"inputSchema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
""".trimIndent()

val getWeatherTool = Tool(
name = "get_weather",
description = "Get the current weather in a given location",
inputSchema = Tool.Input(
properties = buildJsonObject {
put("location", buildJsonObject {
put("type", JsonPrimitive("string"))
put("description", JsonPrimitive("The city and state, e.g. San Francisco, CA"))
})
},
required = listOf("location")
)
)

@Test
fun `should serialize get_weather tool`() {
McpJson.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
}

@Test
fun `should deserialize get_weather tool`() {
val tool = McpJson.decodeFromString<Tool>(getWeatherToolJson)
assertEquals(expected = getWeatherTool, actual = tool)
}

}