@@ -2,7 +2,11 @@ package io.modelcontextprotocol.sample.client
22
33import com.anthropic.client.okhttp.AnthropicOkHttpClient
44import 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
610import com.fasterxml.jackson.core.type.TypeReference
711import com.fasterxml.jackson.databind.ObjectMapper
812import io.modelcontextprotocol.kotlin.sdk.Implementation
@@ -24,7 +28,7 @@ class MCPClient : AutoCloseable {
2428 private val mcp: Client = Client (clientInfo = Implementation (name = " mcp-client-cli" , version = " 1.0.0" ))
2529
2630 private val messageParamsBuilder: MessageCreateParams .Builder = MessageCreateParams .builder()
27- .model(Model .CLAUDE_3_5_SONNET_20241022 )
31+ .model(Model .CLAUDE_4_SONNET_20250514 )
2832 .maxTokens(1024 )
2933
3034 // List of tools offered by the server
@@ -56,15 +60,15 @@ class MCPClient : AutoCloseable {
5660 // Setup I/O transport using the process streams
5761 val transport = StdioClientTransport (
5862 input = process.inputStream.asSource().buffered(),
59- output = process.outputStream.asSink().buffered()
63+ output = process.outputStream.asSink().buffered(),
6064 )
6165
6266 // Connect the MCP client to the server using the transport
6367 mcp.connect(transport)
6468
6569 // Request the list of available tools from the server
6670 val toolsResult = mcp.listTools()
67- tools = toolsResult? .tools? .map { tool ->
71+ tools = toolsResult.tools.map { tool ->
6872 ToolUnion .ofTool(
6973 Tool .builder()
7074 .name(tool.name)
@@ -74,11 +78,11 @@ class MCPClient : AutoCloseable {
7478 .type(JsonValue .from(tool.inputSchema.type))
7579 .properties(tool.inputSchema.properties.toJsonValue())
7680 .putAdditionalProperty(" required" , JsonValue .from(tool.inputSchema.required))
77- .build()
81+ .build(),
7882 )
79- .build()
83+ .build(),
8084 )
81- } ? : emptyList()
85+ }
8286 println (" Connected to server with tools: ${tools.joinToString(" , " ) { it.tool().get().name() }} " )
8387 } catch (e: Exception ) {
8488 println (" Failed to connect to MCP server: $e " )
@@ -93,15 +97,15 @@ class MCPClient : AutoCloseable {
9397 MessageParam .builder()
9498 .role(MessageParam .Role .USER )
9599 .content(query)
96- .build()
100+ .build(),
97101 )
98102
99103 // Send the query to the Anthropic model and get the response
100104 val response = anthropic.messages().create(
101105 messageParamsBuilder
102106 .messages(messages)
103107 .tools(tools)
104- .build()
108+ .build(),
105109 )
106110
107111 val finalText = mutableListOf<String >()
@@ -119,7 +123,7 @@ class MCPClient : AutoCloseable {
119123 // Call the tool with provided arguments
120124 val result = mcp.callTool(
121125 name = toolName,
122- arguments = toolArgs ? : emptyMap()
126+ arguments = toolArgs ? : emptyMap(),
123127 )
124128 finalText.add(" [Calling tool $toolName with args $toolArgs ]" )
125129
@@ -131,17 +135,19 @@ class MCPClient : AutoCloseable {
131135 """
132136 "type": "tool_result",
133137 "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(),
136142 )
137- .build()
143+ .build(),
138144 )
139145
140146 // Retrieve an updated response after tool execution
141147 val aiResponse = anthropic.messages().create(
142148 messageParamsBuilder
143149 .messages(messages)
144- .build()
150+ .build(),
145151 )
146152
147153 // Append the updated response to final text
@@ -160,7 +166,7 @@ class MCPClient : AutoCloseable {
160166
161167 while (true ) {
162168 print (" \n Query: " )
163- val message = readLine () ? : break
169+ val message = readlnOrNull () ? : break
164170 if (message.lowercase() == " quit" ) break
165171 val response = processQuery(message)
166172 println (" \n $response " )
@@ -173,4 +179,4 @@ class MCPClient : AutoCloseable {
173179 anthropic.close()
174180 }
175181 }
176- }
182+ }
0 commit comments