1
1
package io.modelcontextprotocol.kotlin.sdk
2
2
3
+ import io.kotest.assertions.json.shouldEqualJson
3
4
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
4
5
import kotlinx.serialization.json.Json
5
- import kotlinx.serialization.json.JsonArray
6
6
import kotlinx.serialization.json.JsonPrimitive
7
7
import kotlinx.serialization.json.buildJsonObject
8
- import kotlinx.serialization.json.decodeFromJsonElement
9
- import kotlinx.serialization.json.encodeToJsonElement
10
8
import kotlin.test.Test
11
9
import kotlin.test.assertEquals
12
10
13
11
class ToolSerializationTest {
14
12
15
13
// see https://docs.anthropic.com/en/docs/build-with-claude/tool-use
16
- private val getWeatherToolJson = buildJsonObject {
17
- put( " name " , JsonPrimitive ( " get_weather " ))
18
- put( " description " , JsonPrimitive ( " Get the current weather in a given location " ))
19
- put( " inputSchema " , buildJsonObject {
20
- put( " properties " , buildJsonObject {
21
- put( " location " , buildJsonObject {
22
- put( " type" , JsonPrimitive ( " string " ))
23
- put( " description " , JsonPrimitive ( " The city and state, e.g. San Francisco, CA " ))
24
- })
25
- })
26
- put( " required " , JsonArray ( listOf ( JsonPrimitive ( " location " ))))
27
- put( " type " , JsonPrimitive ( " object " ))
28
- })
29
- put( " outputSchema " , buildJsonObject {
30
- put( " type " , JsonPrimitive ( " object " ))
31
- put( " properties " , buildJsonObject {
32
- put( " temperature " , buildJsonObject {
33
- put( " type " , JsonPrimitive ( " number " ))
34
- put( " description " , JsonPrimitive ( " Temperature in celsius " ))
35
- })
36
- put( " conditions " , buildJsonObject {
37
- put( " type " , JsonPrimitive ( " string " ))
38
- put( " description " , JsonPrimitive ( " Weather conditions description " ))
39
- })
40
- put( " humidity " , buildJsonObject {
41
- put( " type " , JsonPrimitive ( " number " ))
42
- put( " description " , JsonPrimitive ( " Humidity percentage " ))
43
- })
44
- })
45
- put(
46
- " required " ,
47
- JsonArray ( listOf ( JsonPrimitive ( " temperature" ), JsonPrimitive ( " conditions" ), JsonPrimitive ( " humidity" )))
48
- )
49
- })
50
- }
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
+ "outputSchema": {
30
+ "type": "object",
31
+ "properties": {
32
+ "temperature": {
33
+ "type": "number",
34
+ "description": "Temperature in celsius"
35
+ },
36
+ " conditions": {
37
+ "type": "string",
38
+ "description": "Weather conditions description"
39
+ },
40
+ "humidity": {
41
+ "type": "number",
42
+ "description": "Humidity percentage"
43
+ }
44
+ } ,
45
+ "required": [ "temperature", "conditions", "humidity"]
46
+ }
47
+ }
48
+ """ .trimIndent()
51
49
52
50
val getWeatherTool = Tool (
53
51
name = " get_weather" ,
@@ -83,18 +81,12 @@ class ToolSerializationTest {
83
81
84
82
@Test
85
83
fun `should serialize get_weather tool` () {
86
- val actual = McpJson .encodeToJsonElement(getWeatherTool)
87
-
88
- assertEquals(
89
- getWeatherToolJson,
90
- actual,
91
- " Expected $actual to be equal to $getWeatherToolJson "
92
- )
84
+ McpJson .encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
93
85
}
94
86
95
87
@Test
96
88
fun `should deserialize get_weather tool` () {
97
- val tool = McpJson .decodeFromJsonElement <Tool >(getWeatherToolJson)
89
+ val tool = McpJson .decodeFromString <Tool >(getWeatherToolJson)
98
90
assertEquals(expected = getWeatherTool, actual = tool)
99
91
}
100
92
@@ -103,11 +95,6 @@ class ToolSerializationTest {
103
95
val json = Json (from = McpJson ) {
104
96
encodeDefaults = false
105
97
}
106
- val actual = json.encodeToJsonElement(getWeatherTool)
107
- assertEquals(
108
- getWeatherToolJson,
109
- actual,
110
- " Expected $actual to be equal to $getWeatherToolJson "
111
- )
98
+ json.encodeToString(getWeatherTool) shouldEqualJson getWeatherToolJson
112
99
}
113
100
}
0 commit comments