@@ -2,7 +2,11 @@ package io.modelcontextprotocol.sample.client
2
2
3
3
import com.anthropic.client.okhttp.AnthropicOkHttpClient
4
4
import com.anthropic.core.JsonValue
5
- import com.anthropic.models.messages.*
5
+ import com.anthropic.models.messages.MessageCreateParams
6
+ import com.anthropic.models.messages.MessageParam
7
+ import com.anthropic.models.messages.Model
8
+ import com.anthropic.models.messages.Tool
9
+ import com.anthropic.models.messages.ToolUnion
6
10
import com.fasterxml.jackson.core.type.TypeReference
7
11
import com.fasterxml.jackson.databind.ObjectMapper
8
12
import io.modelcontextprotocol.kotlin.sdk.Implementation
@@ -24,7 +28,7 @@ class MCPClient : AutoCloseable {
24
28
private val mcp: Client = Client (clientInfo = Implementation (name = " mcp-client-cli" , version = " 1.0.0" ))
25
29
26
30
private val messageParamsBuilder: MessageCreateParams .Builder = MessageCreateParams .builder()
27
- .model(Model .CLAUDE_3_5_SONNET_20241022 )
31
+ .model(Model .CLAUDE_4_SONNET_20250514 )
28
32
.maxTokens(1024 )
29
33
30
34
// List of tools offered by the server
@@ -56,15 +60,15 @@ class MCPClient : AutoCloseable {
56
60
// Setup I/O transport using the process streams
57
61
val transport = StdioClientTransport (
58
62
input = process.inputStream.asSource().buffered(),
59
- output = process.outputStream.asSink().buffered()
63
+ output = process.outputStream.asSink().buffered(),
60
64
)
61
65
62
66
// Connect the MCP client to the server using the transport
63
67
mcp.connect(transport)
64
68
65
69
// Request the list of available tools from the server
66
70
val toolsResult = mcp.listTools()
67
- tools = toolsResult? .tools? .map { tool ->
71
+ tools = toolsResult.tools.map { tool ->
68
72
ToolUnion .ofTool(
69
73
Tool .builder()
70
74
.name(tool.name)
@@ -74,11 +78,11 @@ class MCPClient : AutoCloseable {
74
78
.type(JsonValue .from(tool.inputSchema.type))
75
79
.properties(tool.inputSchema.properties.toJsonValue())
76
80
.putAdditionalProperty(" required" , JsonValue .from(tool.inputSchema.required))
77
- .build()
81
+ .build(),
78
82
)
79
- .build()
83
+ .build(),
80
84
)
81
- } ? : emptyList()
85
+ }
82
86
println (" Connected to server with tools: ${tools.joinToString(" , " ) { it.tool().get().name() }} " )
83
87
} catch (e: Exception ) {
84
88
println (" Failed to connect to MCP server: $e " )
@@ -93,15 +97,15 @@ class MCPClient : AutoCloseable {
93
97
MessageParam .builder()
94
98
.role(MessageParam .Role .USER )
95
99
.content(query)
96
- .build()
100
+ .build(),
97
101
)
98
102
99
103
// Send the query to the Anthropic model and get the response
100
104
val response = anthropic.messages().create(
101
105
messageParamsBuilder
102
106
.messages(messages)
103
107
.tools(tools)
104
- .build()
108
+ .build(),
105
109
)
106
110
107
111
val finalText = mutableListOf<String >()
@@ -119,7 +123,7 @@ class MCPClient : AutoCloseable {
119
123
// Call the tool with provided arguments
120
124
val result = mcp.callTool(
121
125
name = toolName,
122
- arguments = toolArgs ? : emptyMap()
126
+ arguments = toolArgs ? : emptyMap(),
123
127
)
124
128
finalText.add(" [Calling tool $toolName with args $toolArgs ]" )
125
129
@@ -131,17 +135,19 @@ class MCPClient : AutoCloseable {
131
135
"""
132
136
"type": "tool_result",
133
137
"tool_name": $toolName ,
134
- "result": ${result?.content?.joinToString(" \n " ) { (it as TextContent ).text ? : " " }}
135
- """ .trimIndent()
138
+ "result": ${result?.content?.joinToString(" \n " ) {
139
+ (it as TextContent ).text ? : " "
140
+ }}
141
+ """ .trimIndent(),
136
142
)
137
- .build()
143
+ .build(),
138
144
)
139
145
140
146
// Retrieve an updated response after tool execution
141
147
val aiResponse = anthropic.messages().create(
142
148
messageParamsBuilder
143
149
.messages(messages)
144
- .build()
150
+ .build(),
145
151
)
146
152
147
153
// Append the updated response to final text
@@ -160,7 +166,7 @@ class MCPClient : AutoCloseable {
160
166
161
167
while (true ) {
162
168
print (" \n Query: " )
163
- val message = readLine () ? : break
169
+ val message = readlnOrNull () ? : break
164
170
if (message.lowercase() == " quit" ) break
165
171
val response = processQuery(message)
166
172
println (" \n $response " )
@@ -173,4 +179,4 @@ class MCPClient : AutoCloseable {
173
179
anthropic.close()
174
180
}
175
181
}
176
- }
182
+ }
0 commit comments