Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ awaitility = "4.3.0"

# Samples
mcp-kotlin = "0.7.0"
anthropic = "0.8.0"
anthropic = "2.7.0"
shadow = "8.1.1"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package io.modelcontextprotocol.sample.client

import com.anthropic.client.okhttp.AnthropicOkHttpClient
import com.anthropic.core.JsonValue
import com.anthropic.models.messages.*
import com.anthropic.models.messages.MessageCreateParams
import com.anthropic.models.messages.MessageParam
import com.anthropic.models.messages.Model
import com.anthropic.models.messages.Tool
import com.anthropic.models.messages.ToolUnion
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import io.modelcontextprotocol.kotlin.sdk.Implementation
Expand All @@ -24,7 +28,7 @@ class MCPClient : AutoCloseable {
private val mcp: Client = Client(clientInfo = Implementation(name = "mcp-client-cli", version = "1.0.0"))

private val messageParamsBuilder: MessageCreateParams.Builder = MessageCreateParams.builder()
.model(Model.CLAUDE_3_5_SONNET_20241022)
.model(Model.CLAUDE_4_SONNET_20250514)
.maxTokens(1024)

// List of tools offered by the server
Expand Down Expand Up @@ -56,15 +60,15 @@ class MCPClient : AutoCloseable {
// Setup I/O transport using the process streams
val transport = StdioClientTransport(
input = process.inputStream.asSource().buffered(),
output = process.outputStream.asSink().buffered()
output = process.outputStream.asSink().buffered(),
)

// Connect the MCP client to the server using the transport
mcp.connect(transport)

// Request the list of available tools from the server
val toolsResult = mcp.listTools()
tools = toolsResult?.tools?.map { tool ->
tools = toolsResult.tools.map { tool ->
ToolUnion.ofTool(
Tool.builder()
.name(tool.name)
Expand All @@ -74,11 +78,11 @@ class MCPClient : AutoCloseable {
.type(JsonValue.from(tool.inputSchema.type))
.properties(tool.inputSchema.properties.toJsonValue())
.putAdditionalProperty("required", JsonValue.from(tool.inputSchema.required))
.build()
.build(),
)
.build()
.build(),
)
} ?: emptyList()
}
println("Connected to server with tools: ${tools.joinToString(", ") { it.tool().get().name() }}")
} catch (e: Exception) {
println("Failed to connect to MCP server: $e")
Expand All @@ -93,15 +97,15 @@ class MCPClient : AutoCloseable {
MessageParam.builder()
.role(MessageParam.Role.USER)
.content(query)
.build()
.build(),
)

// Send the query to the Anthropic model and get the response
val response = anthropic.messages().create(
messageParamsBuilder
.messages(messages)
.tools(tools)
.build()
.build(),
)

val finalText = mutableListOf<String>()
Expand All @@ -119,7 +123,7 @@ class MCPClient : AutoCloseable {
// Call the tool with provided arguments
val result = mcp.callTool(
name = toolName,
arguments = toolArgs ?: emptyMap()
arguments = toolArgs ?: emptyMap(),
)
finalText.add("[Calling tool $toolName with args $toolArgs]")

Expand All @@ -131,17 +135,19 @@ class MCPClient : AutoCloseable {
"""
"type": "tool_result",
"tool_name": $toolName,
"result": ${result?.content?.joinToString("\n") { (it as TextContent).text ?: "" }}
""".trimIndent()
"result": ${result?.content?.joinToString("\n") {
(it as TextContent).text ?: ""
}}
""".trimIndent(),
)
.build()
.build(),
)

// Retrieve an updated response after tool execution
val aiResponse = anthropic.messages().create(
messageParamsBuilder
.messages(messages)
.build()
.build(),
)

// Append the updated response to final text
Expand All @@ -160,7 +166,7 @@ class MCPClient : AutoCloseable {

while (true) {
print("\nQuery: ")
val message = readLine() ?: break
val message = readlnOrNull() ?: break
if (message.lowercase() == "quit") break
val response = processQuery(message)
println("\n$response")
Expand All @@ -173,4 +179,4 @@ class MCPClient : AutoCloseable {
anthropic.close()
}
}
}
}
Loading