Skip to content

Commit be5d3be

Browse files
authored
Add support for testing JSON serialization of the mcp API (modelcontextprotocol#45)
* Add kotest-assertions-json dependency + ToolSerializationTest * ToolSerializationTest update
1 parent 0bbc9b0 commit be5d3be

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ kotlin {
219219
implementation(libs.ktor.server.test.host)
220220
implementation(libs.kotlinx.coroutines.test)
221221
implementation(libs.kotlinx.coroutines.debug)
222+
implementation(libs.kotest.assertions.json)
222223
}
223224
}
224225

gradle/libs.versions.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ logging = "7.0.0"
1313
jreleaser = "1.15.0"
1414
binaryCompatibilityValidatorPlugin = "0.17.0"
1515
slf4j = "2.0.16"
16+
kotest = "5.9.1"
1617

1718
[libraries]
1819
# Kotlinx libraries
@@ -32,8 +33,7 @@ kotlinx-coroutines-debug = { group = "org.jetbrains.kotlinx", name = "kotlinx-co
3233
ktor-server-test-host = { group = "io.ktor", name = "ktor-server-test-host", version.ref = "ktor" }
3334
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
3435
slf4j-simple = { group = "org.slf4j", name = "slf4j-simple", version.ref = "slf4j" }
35-
36-
36+
kotest-assertions-json = { group = "io.kotest", name = "kotest-assertions-json", version.ref = "kotest" }
3737

3838
[plugins]
3939
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.modelcontextprotocol.kotlin.sdk
2+
3+
import io.kotest.assertions.json.shouldEqualJson
4+
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
5+
import kotlinx.serialization.encodeToString
6+
import kotlinx.serialization.json.JsonPrimitive
7+
import kotlinx.serialization.json.buildJsonObject
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class ToolSerializationTest {
12+
13+
// see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
14+
/* language=json */
15+
private val getWeatherToolJson = """
16+
{
17+
"name": "get_weather",
18+
"description": "Get the current weather in a given location",
19+
"inputSchema": {
20+
"type": "object",
21+
"properties": {
22+
"location": {
23+
"type": "string",
24+
"description": "The city and state, e.g. San Francisco, CA"
25+
}
26+
},
27+
"required": ["location"]
28+
}
29+
}
30+
""".trimIndent()
31+
32+
val getWeatherTool = Tool(
33+
name = "get_weather",
34+
description = "Get the current weather in a given location",
35+
inputSchema = Tool.Input(
36+
properties = buildJsonObject {
37+
put("location", buildJsonObject {
38+
put("type", JsonPrimitive("string"))
39+
put("description", JsonPrimitive("The city and state, e.g. San Francisco, CA"))
40+
})
41+
},
42+
required = listOf("location")
43+
)
44+
)
45+
46+
@Test
47+
fun `should serialize get_weather tool`() {
48+
McpJson.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
49+
}
50+
51+
@Test
52+
fun `should deserialize get_weather tool`() {
53+
val tool = McpJson.decodeFromString<Tool>(getWeatherToolJson)
54+
assertEquals(expected = getWeatherTool, actual = tool)
55+
}
56+
57+
}

0 commit comments

Comments
 (0)