Skip to content

Commit 8042777

Browse files
committed
Fix tools convert and message with tool result
1 parent dfee1b6 commit 8042777

File tree

1 file changed

+21
-9
lines changed
  • samples/kotlin-mcp-client/src/main/kotlin/io/modelcontextprotocol/sample/client

1 file changed

+21
-9
lines changed

samples/kotlin-mcp-client/src/main/kotlin/io/modelcontextprotocol/sample/client/MCPClient.kt

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import com.anthropic.models.messages.*
66
import com.fasterxml.jackson.core.type.TypeReference
77
import com.fasterxml.jackson.databind.ObjectMapper
88
import io.modelcontextprotocol.kotlin.sdk.Implementation
9+
import io.modelcontextprotocol.kotlin.sdk.TextContent
910
import io.modelcontextprotocol.kotlin.sdk.client.Client
1011
import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport
1112
import kotlinx.coroutines.runBlocking
1213
import kotlinx.io.asSink
1314
import kotlinx.io.asSource
1415
import kotlinx.io.buffered
16+
import kotlinx.serialization.json.JsonObject
1517
import kotlin.jvm.optionals.getOrNull
1618

1719
class MCPClient : AutoCloseable {
@@ -28,6 +30,12 @@ class MCPClient : AutoCloseable {
2830
// List of tools offered by the server
2931
private lateinit var tools: List<ToolUnion>
3032

33+
private fun JsonObject.toJsonValue(): JsonValue {
34+
val mapper = ObjectMapper()
35+
val node = mapper.readTree(this.toString())
36+
return JsonValue.fromJsonNode(node)
37+
}
38+
3139
// Connect to the server using the path to the server
3240
suspend fun connectToServer(serverScriptPath: String) {
3341
try {
@@ -63,16 +71,13 @@ class MCPClient : AutoCloseable {
6371
.description(tool.description ?: "")
6472
.inputSchema(
6573
Tool.InputSchema.builder()
66-
.properties(JsonValue.from(tool.inputSchema.properties))
67-
.putAdditionalProperty(
68-
"required",
69-
JsonValue.from(tool.inputSchema.required ?: emptyList<String>())
70-
)
74+
.type(JsonValue.from(tool.inputSchema.type))
75+
.properties(tool.inputSchema.properties.toJsonValue())
76+
.putAdditionalProperty("required", JsonValue.from(tool.inputSchema.required))
7177
.build()
7278
)
7379
.build()
7480
)
75-
7681
} ?: emptyList()
7782
println("Connected to server with tools: ${tools.joinToString(", ") { it.tool().get().name() }}")
7883
} catch (e: Exception) {
@@ -108,20 +113,27 @@ class MCPClient : AutoCloseable {
108113
// If the response indicates a tool use, process it further
109114
content.isToolUse() -> {
110115
val toolName = content.toolUse().get().name()
111-
val toolArgs = content.toolUse().get()._additionalProperties()
116+
val toolArgs =
117+
content.toolUse().get()._input().convert(object : TypeReference<Map<String, JsonValue>>() {})
112118

113119
// Call the tool with provided arguments
114120
val result = mcp.callTool(
115121
name = toolName,
116-
arguments = toolArgs
122+
arguments = toolArgs ?: emptyMap()
117123
)
118124
finalText.add("[Calling tool $toolName with args $toolArgs]")
119125

120126
// Add the tool result message to the conversation
121127
messages.add(
122128
MessageParam.builder()
123129
.role(MessageParam.Role.USER)
124-
.content(result?.content.toString())
130+
.content(
131+
"""
132+
"type": "tool_result",
133+
"tool_name": $toolName,
134+
"result": ${result?.content?.joinToString("\n") { (it as TextContent).text ?: "" }}
135+
""".trimIndent()
136+
)
125137
.build()
126138
)
127139

0 commit comments

Comments
 (0)