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