- Python
- TypeScript
- Java
- Kotlin
- C#
- Ruby
这里可以找到本教程的完整代码。将 完整的 client 将会:
系统要求
开始前,请确保系统满足以下要求:- Mac 或 Windows 电脑。
- 已安装最新 Python 版本。
- 已安装最新版本的
uv。
设置环境
首先使用uv 创建新的 Python 项目:# Create project directory
uv init mcp-client
cd mcp-client
# Create virtual environment
uv venv
# Activate virtual environment
source .venv/bin/activate
# Install required packages
uv add mcp anthropic python-dotenv
# Remove boilerplate files
rm main.py
# Create our main file
touch client.py
设置 API Key
你需要从 Anthropic Console 获取 Anthropic API key。创建.env 文件来保存它:echo "ANTHROPIC_API_KEY=your-api-key-goes-here" > .env
.env 添加到 .gitignore:echo ".env" >> .gitignore
请确保妥善保护你的
ANTHROPIC_API_KEY!创建客户端
基础客户端结构
首先设置 imports,并创建基础客户端类:import asyncio
from typing import Optional
from contextlib import AsyncExitStack
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from anthropic import Anthropic
from dotenv import load_dotenv
load_dotenv() # load environment variables from .env
class MCPClient:
def __init__(self):
# Initialize session and client objects
self.session: Optional[ClientSession] = None
self.exit_stack = AsyncExitStack()
self.anthropic = Anthropic()
# methods will go here
服务器连接管理
接下来实现连接 MCP server 的方法:async def connect_to_server(self, server_script_path: str):
"""Connect to an MCP server
Args:
server_script_path: Path to the server script (.py or .js)
"""
is_python = server_script_path.endswith('.py')
is_js = server_script_path.endswith('.js')
if not (is_python or is_js):
raise ValueError("Server script must be a .py or .js file")
command = "python" if is_python else "node"
server_params = StdioServerParameters(
command=command,
args=[server_script_path],
env=None
)
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
self.stdio, self.write = stdio_transport
self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
await self.session.initialize()
# List available tools
response = await self.session.list_tools()
tools = response.tools
print("\nConnected to server with tools:", [tool.name for tool in tools])
查询处理逻辑
现在添加处理查询和 tool calls 的核心功能:async def process_query(self, query: str) -> str:
"""Process a query using Claude and available tools"""
messages = [
{
"role": "user",
"content": query
}
]
response = await self.session.list_tools()
available_tools = [{
"name": tool.name,
"description": tool.description,
"input_schema": tool.inputSchema
} for tool in response.tools]
# Initial Claude API call
response = self.anthropic.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=messages,
tools=available_tools
)
# Process response and handle tool calls
final_text = []
assistant_message_content = []
for content in response.content:
if content.type == 'text':
final_text.append(content.text)
assistant_message_content.append(content)
elif content.type == 'tool_use':
tool_name = content.name
tool_args = content.input
# Execute tool call
result = await self.session.call_tool(tool_name, tool_args)
final_text.append(f"[Calling tool {tool_name} with args {tool_args}]")
assistant_message_content.append(content)
messages.append({
"role": "assistant",
"content": assistant_message_content
})
messages.append({
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": content.id,
"content": result.content
}
]
})
# Get next response from Claude
response = self.anthropic.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1000,
messages=messages,
tools=available_tools
)
final_text.append(response.content[0].text)
return "\n".join(final_text)
交互式聊天界面
现在添加聊天循环和清理逻辑:async def chat_loop(self):
"""Run an interactive chat loop"""
print("\nMCP Client Started!")
print("Type your queries or 'quit' to exit.")
while True:
try:
query = input("\nQuery: ").strip()
if query.lower() == 'quit':
break
response = await self.process_query(query)
print("\n" + response)
except Exception as e:
print(f"\nError: {str(e)}")
async def cleanup(self):
"""Clean up resources"""
await self.exit_stack.aclose()
主入口
最后,添加主执行逻辑:async def main():
if len(sys.argv) < 2:
print("Usage: python client.py <path_to_server_script>")
sys.exit(1)
client = MCPClient()
try:
await client.connect_to_server(sys.argv[1])
await client.chat_loop()
finally:
await client.cleanup()
if __name__ == "__main__":
import sys
asyncio.run(main())
client.py 文件见这里。关键组件说明
1. Client 初始化
MCPClient类会初始化 session 管理和 API clients- 使用
AsyncExitStack正确管理资源 - 配置用于 Claude 交互的 Anthropic client
2. Server 连接
- 同时支持 Python 和 Node.js servers
- 校验 server script 类型
- 设置正确的通信通道
- 初始化 session 并列出可用 tools
3. 查询处理
- 维护对话上下文
- 处理 Claude 响应和 tool calls
- 管理 Claude 与 tools 之间的消息流
- 将结果组合成连贯响应
4. 交互界面
- 提供简单命令行界面
- 处理用户输入并显示响应
- 包含基础错误处理
- 支持优雅退出
5. 资源管理
- 正确清理资源
- 处理连接问题
- 执行优雅关闭流程
常见自定义点
-
Tool 处理
- 修改
process_query()以处理特定 tool 类型 - 为 tool calls 添加自定义错误处理
- 实现特定 tool 的响应格式化
- 修改
-
响应处理
- 自定义 tool results 的格式化方式
- 添加响应过滤或转换
- 实现自定义日志
-
用户界面
- 添加 GUI 或 Web 界面
- 实现富控制台输出
- 添加命令历史或自动补全
运行 Client
要让 client 连接任意 MCP server:uv run client.py path/to/server.py # python server
uv run client.py path/to/build/index.js # node server
如果你继续使用 server quickstart 中的天气教程,命令可能类似这样:
python client.py .../quickstart-resources/weather-server-python/weather.py- 连接到指定 server
- 列出可用 tools
- 启动交互式聊天 session,你可以:
- 输入查询
- 查看 tool 执行
- 获取 Claude 的响应

工作原理
提交查询时:- client 从 server 获取可用 tools 列表
- 你的查询会连同 tool 描述一起发送给 Claude
- Claude 决定使用哪些 tools(如果需要)
- client 通过 server 执行任何被请求的 tool calls
- 结果发回 Claude
- Claude 提供自然语言响应
- 响应显示给你
最佳实践
-
错误处理
- 始终用 try-catch blocks 包裹 tool calls
- 提供有意义的错误消息
- 优雅处理连接问题
-
资源管理
- 使用
AsyncExitStack正确清理 - 完成后关闭连接
- 处理 server 断开连接
- 使用
-
安全
- 将 API keys 安全存储在
.env中 - 校验 server 响应
- 谨慎处理 tool 权限
- 将 API keys 安全存储在
-
Tool 名称
- Tool 名称可以根据这里指定的格式进行校验
- 如果 tool 名称符合指定格式,MCP client 不应使其校验失败
排障
Server 路径问题
- 仔细检查 server script 路径是否正确
- 如果相对路径不工作,请使用绝对路径
- Windows 用户请确保路径中使用正斜杠 (/) 或转义反斜杠 (\)
- 确认 server 文件扩展名正确(Python 用 .py,Node.js 用 .js)
# Relative path
uv run client.py ./server/weather.py
# Absolute path
uv run client.py /Users/username/projects/mcp-server/weather.py
# Windows path (either format works)
uv run client.py C:/projects/mcp-server/weather.py
uv run client.py C:\\projects\\mcp-server\\weather.py
响应时间
- 第一次响应可能最多需要 30 秒才返回
- 这是正常现象,期间会发生:
- server 初始化
- Claude 处理查询
- tools 正在执行
- 后续响应通常更快
- 在初始等待期间不要中断进程
常见错误消息
如果看到:FileNotFoundError:检查 server 路径Connection refused:确保 server 正在运行且路径正确Tool execution failed:确认 tool 所需环境变量已设置Timeout error:考虑在 client 配置中增加 timeout
本教程完整代码见这里。更新 在项目根目录创建 将 client 将会:
系统要求
开始前,请确保系统满足以下要求:- Mac 或 Windows 电脑
- 已安装 Node.js 17 或更高版本
- 已安装最新版
npm - Anthropic API key (Claude)
设置环境
首先,创建并设置项目:# Create project directory
mkdir mcp-client-typescript
cd mcp-client-typescript
# Initialize npm project
npm init -y
# Install dependencies
npm install @anthropic-ai/sdk @modelcontextprotocol/sdk dotenv
# Install dev dependencies
npm install -D @types/node typescript
# Create source file
touch index.ts
package.json,设置 type: "module" 并添加 build script:package.json
{
"type": "module",
"scripts": {
"build": "tsc && chmod 755 build/index.js"
}
}
tsconfig.json:tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["index.ts"],
"exclude": ["node_modules"]
}
设置 API Key
你需要从 Anthropic Console 获取 Anthropic API key。创建.env 文件来存储它:echo "ANTHROPIC_API_KEY=<your key here>" > .env
.env 添加到 .gitignore:echo ".env" >> .gitignore
请确保妥善保护
ANTHROPIC_API_KEY!创建 Client
基础 Client 结构
首先,在index.ts 中设置 imports 并创建基础 client 类:import { Anthropic } from "@anthropic-ai/sdk";
import {
MessageParam,
Tool,
} from "@anthropic-ai/sdk/resources/messages/messages.mjs";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import readline from "readline/promises";
import dotenv from "dotenv";
dotenv.config();
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
if (!ANTHROPIC_API_KEY) {
throw new Error("ANTHROPIC_API_KEY is not set");
}
class MCPClient {
private mcp: Client;
private anthropic: Anthropic;
private transport: StdioClientTransport | null = null;
private tools: Tool[] = [];
constructor() {
this.anthropic = new Anthropic({
apiKey: ANTHROPIC_API_KEY,
});
this.mcp = new Client({ name: "mcp-client-cli", version: "1.0.0" });
}
// methods will go here
}
Server 连接管理
接下来,实现连接 MCP server 的方法:async connectToServer(serverScriptPath: string) {
try {
const isJs = serverScriptPath.endsWith(".js");
const isPy = serverScriptPath.endsWith(".py");
if (!isJs && !isPy) {
throw new Error("Server script must be a .js or .py file");
}
const command = isPy
? process.platform === "win32"
? "python"
: "python3"
: process.execPath;
this.transport = new StdioClientTransport({
command,
args: [serverScriptPath],
});
await this.mcp.connect(this.transport);
const toolsResult = await this.mcp.listTools();
this.tools = toolsResult.tools.map((tool) => {
return {
name: tool.name,
description: tool.description,
input_schema: tool.inputSchema,
};
});
console.log(
"Connected to server with tools:",
this.tools.map(({ name }) => name)
);
} catch (e) {
console.log("Failed to connect to MCP server: ", e);
throw e;
}
}
查询处理逻辑
现在添加用于处理查询和 tool calls 的核心功能:async processQuery(query: string) {
const messages: MessageParam[] = [
{
role: "user",
content: query,
},
];
const response = await this.anthropic.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1000,
messages,
tools: this.tools,
});
const finalText = [];
for (const content of response.content) {
if (content.type === "text") {
finalText.push(content.text);
} else if (content.type === "tool_use") {
const toolName = content.name;
const toolArgs = content.input as { [x: string]: unknown } | undefined;
const result = await this.mcp.callTool({
name: toolName,
arguments: toolArgs,
});
finalText.push(
`[Calling tool ${toolName} with args ${JSON.stringify(toolArgs)}]`
);
messages.push({
role: "user",
content: result.content as string,
});
const response = await this.anthropic.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1000,
messages,
});
finalText.push(
response.content[0].type === "text" ? response.content[0].text : ""
);
}
}
return finalText.join("\n");
}
交互式聊天界面
现在添加聊天循环和清理功能:async chatLoop() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
console.log("\nMCP Client Started!");
console.log("Type your queries or 'quit' to exit.");
while (true) {
const message = await rl.question("\nQuery: ");
if (message.toLowerCase() === "quit") {
break;
}
const response = await this.processQuery(message);
console.log("\n" + response);
}
} finally {
rl.close();
}
}
async cleanup() {
await this.mcp.close();
}
主入口
最后,添加主执行逻辑:async function main() {
if (process.argv.length < 3) {
console.log("Usage: node index.ts <path_to_server_script>");
return;
}
const mcpClient = new MCPClient();
try {
await mcpClient.connectToServer(process.argv[2]);
await mcpClient.chatLoop();
} catch (e) {
console.error("Error:", e);
await mcpClient.cleanup();
process.exit(1);
} finally {
await mcpClient.cleanup();
process.exit(0);
}
}
main();
运行 Client
要让 client 连接任意 MCP server:# Build TypeScript
npm run build
# Run the client
node build/index.js path/to/server.py # python server
node build/index.js path/to/build/index.js # node server
如果你继续使用 server quickstart 中的天气教程,命令可能类似这样:
node build/index.js .../quickstart-resources/weather-server-typescript/build/index.js- 连接到指定 server
- 列出可用 tools
- 启动交互式聊天 session,你可以:
- 输入查询
- 查看 tool 执行
- 获取 Claude 的响应
工作原理
提交查询时:- client 从 server 获取可用 tools 列表
- 你的查询会连同 tool 描述一起发送给 Claude
- Claude 决定使用哪些 tools(如果需要)
- client 通过 server 执行任何被请求的 tool calls
- 结果发回 Claude
- Claude 提供自然语言响应
- 响应显示给你
最佳实践
-
错误处理
- 使用 TypeScript 类型系统更好地检测错误
- 用 try-catch blocks 包裹 tool calls
- 提供有意义的错误消息
- 优雅处理连接问题
-
安全
- 将 API keys 安全存储在
.env中 - 校验 server 响应
- 谨慎处理 tool 权限
- 将 API keys 安全存储在
排障
Server 路径问题
- 仔细检查 server script 路径是否正确
- 如果相对路径不工作,请使用绝对路径
- Windows 用户请确保路径中使用正斜杠 (/) 或转义反斜杠 (\)
- 确认 server 文件扩展名正确(Node.js 用 .js,Python 用 .py)
# Relative path
node build/index.js ./server/build/index.js
# Absolute path
node build/index.js /Users/username/projects/mcp-server/build/index.js
# Windows path (either format works)
node build/index.js C:/projects/mcp-server/build/index.js
node build/index.js C:\\projects\\mcp-server\\build\\index.js
响应时间
- 第一次响应可能最多需要 30 秒才返回
- 这是正常现象,期间会发生:
- server 初始化
- Claude 处理查询
- tools 正在执行
- 后续响应通常更快
- 在初始等待期间不要中断进程
常见错误消息
如果看到:Error: Cannot find module:检查 build 文件夹,并确保 TypeScript 编译成功Connection refused:确保 server 正在运行且路径正确Tool execution failed:确认 tool 所需环境变量已设置ANTHROPIC_API_KEY is not set:检查.env文件和环境变量TypeError:确保 tool 参数使用正确类型BadRequestError:确保你有足够额度访问 Anthropic API
这是一个基于 Spring AI MCP auto-configuration 和 boot starters 的 quickstart demo。
如需了解如何手动创建同步和异步 MCP Clients,请参考 Java SDK Client 文档。
系统要求
开始前,请确保系统满足以下要求:- Java 17 or higher
- Maven 3.6+
- npx package manager
- Anthropic API key (Claude)
- Brave Search API key
设置环境
-
安装 npx (Node Package eXecute):
首先确保已安装 npm,然后运行:
npm install -g npx -
克隆仓库:
git clone https://github.com/spring-projects/spring-ai-examples.git cd model-context-protocol/web-search/brave-chatbot -
设置 API keys:
export ANTHROPIC_API_KEY='your-anthropic-api-key-here' export BRAVE_API_KEY='your-brave-api-key-here' -
构建应用:
./mvnw clean install -
使用 Maven 运行应用:
./mvnw spring-boot:run
请确保妥善保护
ANTHROPIC_API_KEY 和 BRAVE_API_KEY!工作原理
该应用通过几个组件将 Spring AI 与 Brave Search MCP server 集成:MCP Client 配置
- pom.xml 中所需依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-anthropic</artifactId>
</dependency>
- Application properties (application.yml):
spring:
ai:
mcp:
client:
enabled: true
name: brave-search-client
version: 1.0.0
type: SYNC
request-timeout: 20s
stdio:
root-change-notification: true
servers-configuration: classpath:/mcp-servers-config.json
toolcallback:
enabled: true
anthropic:
api-key: ${ANTHROPIC_API_KEY}
spring-ai-starter-mcp-client,并根据提供的 server 配置创建一个或多个 McpClient。
spring.ai.mcp.client.toolcallback.enabled=true 属性会启用 tool callback 机制,自动将所有 MCP tools 注册为 Spring AI tools。
默认情况下它是禁用的。- MCP Server 配置 (
mcp-servers-config.json):
{
"mcpServers": {
"brave-search": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": "<PUT YOUR BRAVE API KEY>"
}
}
}
}
Chat 实现
chatbot 使用集成了 MCP tool 的 Spring AI ChatClient 实现:var chatClient = chatClientBuilder
.defaultSystem("You are useful assistant, expert in AI and Java.")
.defaultToolCallbacks((Object[]) mcpToolAdapter.toolCallbacks())
.defaultAdvisors(new MessageChatMemoryAdvisor(new InMemoryChatMemory()))
.build();
- 使用 Claude AI 模型进行自然语言理解
- 通过 MCP 集成 Brave Search,获得实时 Web 搜索能力
- 使用 InMemoryChatMemory 维护对话记忆
- 作为交互式命令行应用运行
构建并运行
./mvnw clean install
java -jar ./target/ai-mcp-brave-chatbot-0.0.1-SNAPSHOT.jar
./mvnw spring-boot:run
- 使用内置知识回答问题
- 在需要时使用 Brave Search 执行 Web 搜索
- 记住对话中前面消息的上下文
- 组合多个来源的信息,提供更全面的回答
高级配置
MCP client 支持额外配置选项:- 通过
McpSyncClientCustomizer或McpAsyncClientCustomizer自定义 client - 支持多个 clients 和多种 transport 类型:
STDIO与SSE(Server-Sent Events) - 集成 Spring AI 的 tool 执行框架
- 自动 client 初始化和生命周期管理
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
</dependency>
本教程完整代码见这里。现在创建并设置项目:运行 验证所有内容已正确设置:
也可以直接使用 Gradle 运行:client 将会:
系统要求
开始前,请确保系统满足以下要求:- JDK 11 or higher
- Anthropic API key (Claude)
设置环境
首先,如果尚未安装,请安装java 和 gradle。
你可以从 official Oracle JDK website 下载 java。
验证 java 安装:java --version
# Create a new directory for our project
mkdir kotlin-mcp-client
cd kotlin-mcp-client
# Initialize a new kotlin project
gradle init
gradle init 后,选择 Application 作为项目类型,选择 Kotlin 作为编程语言。或者,也可以使用 IntelliJ IDEA project wizard 创建 Kotlin 应用。创建项目后,用以下内容替换 build.gradle.kts:build.gradle.kts
// Check latest versions at https://github.com/modelcontextprotocol/kotlin-sdk/releases
val mcpVersion = "0.9.0"
val ktorVersion = "3.2.3"
val anthropicVersion = "2.15.0"
val slf4jVersion = "2.0.17"
plugins {
kotlin("jvm") version "2.3.20"
id("com.gradleup.shadow") version "8.3.9"
application
}
application {
mainClass.set("MainKt")
}
dependencies {
implementation("io.modelcontextprotocol:kotlin-sdk:$mcpVersion")
implementation("io.ktor:ktor-client-cio:$ktorVersion")
implementation("com.anthropic:anthropic-java:$anthropicVersion")
implementation("org.slf4j:slf4j-simple:$slf4jVersion")
}
./gradlew build
设置 API key
你需要从 Anthropic Console 获取 Anthropic API key。设置 API key:export ANTHROPIC_API_KEY='your-anthropic-api-key-here'
请确保妥善保护
ANTHROPIC_API_KEY!创建 Client
基础 Client 结构
首先,创建基础 client 类:class MCPClient(apiKey: String) : AutoCloseable {
private val anthropic = AnthropicOkHttpClient.builder()
.apiKey(apiKey)
.build()
private val mcp: Client = Client(
clientInfo = Implementation(name = "mcp-client-cli", version = "1.0.0")
)
private var serverProcess: Process? = null
private lateinit var tools: List<ToolUnion>
// methods will go here
override fun close() {
runBlocking {
mcp.close()
}
serverProcess?.destroy()
anthropic.close()
}
}
Server 连接管理
接下来,实现连接 MCP server 的方法:suspend fun connectToServer(serverScriptPath: String) {
val command = buildList {
when (serverScriptPath.substringAfterLast(".")) {
"js" -> add("node")
"py" -> add(if (System.getProperty("os.name").lowercase().contains("win")) "python" else "python3")
"jar" -> addAll(listOf("java", "-jar"))
else -> throw IllegalArgumentException("Server script must be a .js, .py or .jar file")
}
add(serverScriptPath)
}
val process = ProcessBuilder(command).start()
serverProcess = process
val transport = StdioClientTransport(
input = process.inputStream.asSource().buffered(),
output = process.outputStream.asSink().buffered(),
)
mcp.connect(transport)
val toolsResult = mcp.listTools()
tools = toolsResult.tools.map { tool ->
ToolUnion.ofTool(
Tool.builder()
.name(tool.name)
.description(tool.description ?: "")
.inputSchema(
Tool.InputSchema.builder()
.type(JsonValue.from(tool.inputSchema.type))
.properties(tool.inputSchema.properties?.toJsonValue() ?: EmptyJsonObject.toJsonValue())
.putAdditionalProperty("required", JsonValue.from(tool.inputSchema.required))
.build(),
)
.build(),
)
}
println("Connected to server with tools: ${tools.joinToString(", ") { it.tool().get().name() }}")
}
JsonObject.toJsonValue() helper
JsonObject.toJsonValue() helper
该 helper 使用 Jackson 将 kotlinx.serialization
JsonObject 转换为 Anthropic SDK JsonValue:private fun JsonObject.toJsonValue(): JsonValue {
val mapper = ObjectMapper()
val node = mapper.readTree(this.toString())
return JsonValue.fromJsonNode(node)
}
查询处理逻辑
现在添加用于处理查询和 tool calls 的核心功能:suspend fun processQuery(query: String): String {
val messages = mutableListOf(
MessageParam.builder()
.role(MessageParam.Role.USER)
.content(query)
.build(),
)
val response = anthropic.messages().create(
MessageCreateParams.builder()
.model("claude-sonnet-4-20250514")
.maxTokens(1024)
.messages(messages)
.tools(tools)
.build(),
)
val finalText = mutableListOf<String>()
response.content().forEach { content ->
when {
content.isText() -> finalText.add(content.text().get().text())
content.isToolUse() -> {
val toolName = content.toolUse().get().name()
val toolArgs =
content.toolUse().get()._input().convert(object : TypeReference<Map<String, JsonValue>>() {})
val result = mcp.callTool(
name = toolName,
arguments = toolArgs ?: emptyMap(),
)
finalText.add("[Calling tool $toolName with args $toolArgs]")
messages.add(
MessageParam.builder()
.role(MessageParam.Role.USER)
.content(
result.content
.filterIsInstance<TextContent>()
.joinToString("\n") { it.text }
)
.build(),
)
val aiResponse = anthropic.messages().create(
MessageCreateParams.builder()
.model("claude-sonnet-4-20250514")
.maxTokens(1024)
.messages(messages)
.build(),
)
finalText.add(aiResponse.content().first().text().get().text())
}
}
}
return finalText.joinToString("\n")
}
交互式聊天
添加聊天循环:suspend fun chatLoop() {
println("\nMCP Client Started!")
println("Type your queries or 'quit' to exit.")
while (true) {
print("\nQuery: ")
val message = readlnOrNull() ?: break
if (message.trim().lowercase() == "quit") break
try {
val response = processQuery(message)
println("\n$response")
} catch (e: Exception) {
println("\nError: ${e.message}")
}
}
}
主入口
最后,添加主执行函数:fun main(args: Array<String>) = runBlocking {
require(args.isNotEmpty()) { "Usage: java -jar <path> <path_to_server_script>" }
val apiKey = System.getenv("ANTHROPIC_API_KEY")
require(!apiKey.isNullOrBlank()) { "ANTHROPIC_API_KEY environment variable is not set" }
val client = MCPClient(apiKey)
client.use {
client.connectToServer(args.first())
client.chatLoop()
}
}
运行 client
要让 client 连接任意 MCP server:./gradlew build
# Run the client
java -jar build/libs/kotlin-mcp-client-0.1.0-all.jar path/to/server.jar # JVM server
java -jar build/libs/kotlin-mcp-client-0.1.0-all.jar path/to/server.py # Python server
java -jar build/libs/kotlin-mcp-client-0.1.0-all.jar path/to/build/index.js # Node server
./gradlew run --args="path/to/server.jar"
如果你继续使用 server quickstart 中的天气教程,命令可能类似这样:
java -jar build/libs/kotlin-mcp-client-0.1.0-all.jar .../samples/weather-stdio-server/build/libs/weather-stdio-server-0.1.0-all.jar- 连接到指定 server
- 列出可用 tools
- 启动交互式聊天 session,你可以:
- 输入查询
- 查看 tool 执行
- 获取 Claude 的响应
工作原理
下面是高层 workflow schema:提交查询时:- client 从 server 获取可用 tools 列表
- 你的查询会连同 tool 描述一起发送给 Claude
- Claude 决定使用哪些 tools(如果需要)
- client 通过 server 执行任何被请求的 tool calls
- 结果发回 Claude
- Claude 提供自然语言响应
- 响应显示给你
最佳实践
-
错误处理
- 利用 Kotlin 类型系统显式建模错误
- 可能出现异常时,用
try-catchblocks 包裹外部 tool 和 API calls - 提供清晰且有意义的错误消息
- 优雅处理网络超时和连接问题
-
安全
- 将 API keys 和 secrets 安全存储在
local.properties、环境变量或 secret managers 中 - 校验所有外部响应,避免意外或不安全的数据使用
- 使用 tools 时谨慎处理权限和信任边界
- 将 API keys 和 secrets 安全存储在
-
环境
- 通过环境变量设置
ANTHROPIC_API_KEY,而不是硬编码 - 本地开发时使用
.env文件,并配合适当.gitignore规则
- 通过环境变量设置
排障
Server 路径问题
- 仔细检查 server script 路径是否正确
- 如果相对路径不工作,请使用绝对路径
- Windows 用户请确保路径中使用正斜杠 (/) 或转义反斜杠 (\)
- 确保已安装所需运行时(Java 用 java,Node.js 用 npm,Python 用 uv)
- 确认 server 文件扩展名正确(Java 用 .jar,Node.js 用 .js,Python 用 .py)
# Relative path
java -jar build/libs/client.jar ./server/build/libs/server.jar
# Absolute path
java -jar build/libs/client.jar /Users/username/projects/mcp-server/build/libs/server.jar
# Windows path (either format works)
java -jar build/libs/client.jar C:/projects/mcp-server/build/libs/server.jar
java -jar build/libs/client.jar C:\\projects\\mcp-server\\build\\libs\\server.jar
构建问题
- 使用
./gradlew build或./gradlew shadowJar(不要用./gradlew jar)创建包含所有依赖的 shadow JAR - 如果遇到 JDK 版本错误,请确保已安装 JDK 版本匹配或高于
build.gradle.kts中的jvmToolchain设置
响应时间
- 第一次响应可能最多需要 30 秒才返回
- 这是正常现象,期间会发生:
- server 初始化
- Claude 处理查询
- tools 正在执行
- 后续响应通常更快
- 在初始等待期间不要中断进程
常见错误消息
如果看到:Connection refused:确保 server 正在运行且路径正确Tool execution failed:确认 tool 所需环境变量已设置ANTHROPIC_API_KEY is not set:检查环境变量
You can find the complete code for this tutorial here.然后,将所需依赖添加到项目:这会创建一个 .NET console application 的基础结构,能够从 user secrets 读取 API key。接下来,设置 MCP Client:将此函数添加到 这会创建一个 MCP client,连接到通过命令行参数提供的 server。随后,它会列出已连接 server 中的可用 tools。client 将会:
系统要求
开始前,请确保系统满足以下要求:- .NET 8.0 or higher
- Anthropic API key (Claude)
- Windows, Linux, or macOS
设置环境
首先,创建新的 .NET 项目:dotnet new console -n QuickstartClient
cd QuickstartClient
dotnet add package ModelContextProtocol --prerelease
dotnet add package Anthropic.SDK
dotnet add package Microsoft.Extensions.Hosting
dotnet add package Microsoft.Extensions.AI
设置 API key
你需要从 Anthropic Console 获取 Anthropic API key。dotnet user-secrets init
dotnet user-secrets set "ANTHROPIC_API_KEY" "<your key here>"
创建 Client
基础 Client 结构
首先,在Program.cs 文件中设置基础 client 类:using Anthropic.SDK;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
var builder = Host.CreateApplicationBuilder(args);
builder.Configuration
.AddEnvironmentVariables()
.AddUserSecrets<Program>();
var (command, arguments) = GetCommandAndArguments(args);
var clientTransport = new StdioClientTransport(new()
{
Name = "Demo Server",
Command = command,
Arguments = arguments,
});
await using var mcpClient = await McpClient.CreateAsync(clientTransport);
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
Console.WriteLine($"Connected to server with tools: {tool.Name}");
}
Program.cs 文件末尾:static (string command, string[] arguments) GetCommandAndArguments(string[] args)
{
return args switch
{
[var script] when script.EndsWith(".py") => ("python", args),
[var script] when script.EndsWith(".js") => ("node", args),
[var script] when Directory.Exists(script) || (File.Exists(script) && script.EndsWith(".csproj")) => ("dotnet", ["run", "--project", script, "--no-build"]),
_ => throw new NotSupportedException("An unsupported server script was provided. Supported scripts are .py, .js, or .csproj")
};
}
查询处理逻辑
现在添加用于处理查询和 tool calls 的核心功能:using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
.Messages
.AsBuilder()
.UseFunctionInvocation()
.Build();
var options = new ChatOptions
{
MaxOutputTokens = 1000,
ModelId = "claude-sonnet-4-20250514",
Tools = [.. tools]
};
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("MCP Client Started!");
Console.ResetColor();
PromptForInput();
while(Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrWhiteSpace(query))
{
PromptForInput();
continue;
}
await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options))
{
Console.Write(message);
}
Console.WriteLine();
PromptForInput();
}
static void PromptForInput()
{
Console.WriteLine("Enter a command (or 'exit' to quit):");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("> ");
Console.ResetColor();
}
关键组件说明
1. Client 初始化
- client 使用
McpClient.CreateAsync()初始化,该方法会设置 transport 类型以及用于运行 server 的命令。
2. Server 连接
- 支持 Python、Node.js 和 .NET servers。
- 使用参数中指定的命令启动 server。
- 配置使用 stdio 与 server 通信。
- 初始化 session 和可用 tools。
3. 查询处理
- 使用 Microsoft.Extensions.AI 作为 chat client。
- 配置
IChatClient使用自动 tool (function) invocation。 - client 读取用户输入并发送给 server。
- server 处理查询并返回响应。
- 响应展示给用户。
运行 Client
要让 client 连接任意 MCP server:dotnet run -- path/to/server.csproj # dotnet server
dotnet run -- path/to/server.py # python server
dotnet run -- path/to/server.js # node server
如果你继续使用 server quickstart 中的天气教程,命令可能类似这样:
dotnet run -- path/to/QuickstartWeatherServer。- 连接到指定 server
- 列出可用 tools
- 启动交互式聊天 session,你可以:
- 输入查询
- 查看 tool 执行
- 获取 Claude 的响应
- 完成后退出 session

本教程完整代码见这里。将 完整的 client 将会:
系统要求
开始前,请确保系统满足以下要求:- Mac 或 Windows 电脑
- 已安装 Ruby 3.2.0 或更高版本(Anthropic SDK 要求)
- Anthropic API key (Claude)
设置环境
首先,创建新的 Ruby 项目:# Create project directory
mkdir mcp-client
cd mcp-client
# Create a Gemfile
bundle init
# Add required dependencies
bundle add anthropic base64 dotenv mcp
# Create our main file
touch client.rb
设置 API Key
你需要从 Anthropic Console 获取 Anthropic API key。创建.env 文件来存储它:echo "ANTHROPIC_API_KEY=your-api-key-goes-here" > .env
.env 添加到 .gitignore:echo ".env" >> .gitignore
请确保妥善保护
ANTHROPIC_API_KEY!创建 Client
基础 Client 结构
首先,设置 requires 并创建基础 client 类:require "anthropic"
require "dotenv/load"
require "json"
require "mcp"
class MCPClient
ANTHROPIC_MODEL = "claude-sonnet-4-20250514"
def initialize
@mcp_client = nil
@transport = nil
@anthropic_client = nil
end
# methods will go here
end
Server 连接管理
接下来,实现连接 MCP server 的方法:def connect_to_server(server_script_path)
command = case File.extname(server_script_path)
when ".rb"
"ruby"
when ".py"
"python3"
when ".js"
"node"
else
raise ArgumentError, "Server script must be a .rb, .py, or .js file."
end
@transport = MCP::Client::Stdio.new(command: command, args: [server_script_path])
@mcp_client = MCP::Client.new(transport: @transport)
@mcp_client.connect
tool_names = @mcp_client.tools.map(&:name)
puts "\nConnected to server with tools: #{tool_names}"
end
查询处理逻辑
现在添加用于处理查询和 tool calls 的核心功能:private
def process_query(query)
messages = [{ role: "user", content: query }]
available_tools = @mcp_client.tools.map do |tool|
{ name: tool.name, description: tool.description, input_schema: tool.input_schema }
end
# Initial Claude API call.
response = chat(messages, tools: available_tools)
# Process response and handle tool calls.
if response.content.any?(Anthropic::Models::ToolUseBlock)
assistant_content = response.content.filter_map do |content_block|
case content_block
when Anthropic::Models::TextBlock
{ type: "text", text: content_block.text }
when Anthropic::Models::ToolUseBlock
{ type: "tool_use", id: content_block.id, name: content_block.name, input: content_block.input }
end
end
messages << { role: "assistant", content: assistant_content }
end
response.content.each_with_object([]) do |content, response_parts|
case content
when Anthropic::Models::TextBlock
response_parts << content.text
when Anthropic::Models::ToolUseBlock
# Execute tool call via MCP.
result = @mcp_client.call_tool(name: content.name, arguments: content.input)
response_parts << "[Calling tool #{content.name} with args #{content.input.to_json}]"
tool_result_content = result.dig("result", "content")
result_text = if tool_result_content.is_a?(Array)
tool_result_content.filter_map { |content_item| content_item["text"] }.join("\n")
else
tool_result_content.to_s
end
messages << {
role: "user",
content: [{
type: "tool_result",
tool_use_id: content.id,
content: result_text
}]
}
# Get next response from Claude.
response = chat(messages)
response.content.each do |content_block|
response_parts << content_block.text if content_block.is_a?(Anthropic::Models::TextBlock)
end
end
end.join("\n")
end
def chat(messages, tools: nil)
params = { model: ANTHROPIC_MODEL, max_tokens: 1000, messages: messages }
params[:tools] = tools if tools
anthropic_client.messages.create(**params)
end
def anthropic_client
@anthropic_client ||= Anthropic::Client.new(api_key: ENV["ANTHROPIC_API_KEY"])
end
交互式聊天界面
现在添加聊天循环和清理功能:def chat_loop
puts <<~MESSAGE
MCP Client Started!
Type your queries or 'quit' to exit.
MESSAGE
loop do
print "\nQuery: "
line = $stdin.gets
break if line.nil?
query = line.chomp.strip
break if query.downcase == "quit"
next if query.empty?
begin
response = process_query(query)
puts "\n#{response}"
rescue => e
puts "\nError: #{e.message}"
end
end
end
def cleanup
@transport&.close
end
主入口
最后,添加主执行逻辑:if ARGV.empty?
puts "Usage: ruby client.rb <path_to_server_script>"
exit 1
end
client = MCPClient.new
begin
client.connect_to_server(ARGV[0])
api_key = ENV["ANTHROPIC_API_KEY"]
if api_key.nil? || api_key.empty?
puts <<~MESSAGE
No ANTHROPIC_API_KEY found. To query these tools with Claude, set your API key:
export ANTHROPIC_API_KEY=your-api-key-here
MESSAGE
exit
end
client.chat_loop
rescue => e
puts "Error: #{e.message}"
exit 1
ensure
client.cleanup
end
client.rb 文件见这里。关键组件说明
1. Client 初始化
MCPClient类使用 nil 引用进行延迟设置- Anthropic client 通过
anthropic_client方法延迟初始化 - 使用
dotenv从.env加载环境变量
2. Server 连接
- 支持 Ruby、Python 和 Node.js servers
- 使用
File.extname判断 server script 类型 - 使用
MCP::Client::Stdio作为 stdio transport - 初始化 MCP client 并列出可用 tools
3. 查询处理
- 将 MCP tools 映射为 Anthropic tool 格式(
name、description、input_schema) - 使用
Anthropic::Models::TextBlock和Anthropic::Models::ToolUseBlock做模式匹配 - 在遍历 tool calls 前先构建一次 assistant content
- 通过
@mcp_client.call_tool执行 tool calls - 使用
chathelper 方法封装 Anthropic API calls - 使用
result.dig("result", "content")提取 tool result content - 将 tool results 传回 Claude 以获得最终响应
4. 交互界面
- 提供简单命令行界面
- 处理用户输入并显示响应
- 跳过空查询
- 包含基础错误处理
5. 资源管理
- 通过
begin…ensure正确清理 transport - 使用顶层
rescue处理错误 - server 连接后校验 API key
运行 Client
要让 client 连接任意 MCP server:bundle exec ruby client.rb path/to/server.rb # ruby server
bundle exec ruby client.rb path/to/server.py # python server
bundle exec ruby client.rb path/to/build/index.js # node server
如果你继续使用 server quickstart 中的天气教程,命令可能类似这样:
bundle exec ruby client.rb /path/to/weather-server-ruby/weather.rb- 连接到指定 server
- 列出可用 tools
- 启动交互式聊天 session,你可以:
- 输入查询
- 查看 tool 执行
- 获取 Claude 的响应
工作原理
提交查询时:- client 从 server 获取可用 tools 列表
- 你的查询会连同 tool 描述一起发送给 Claude
- Claude 决定使用哪些 tools(如果需要)
- client 通过 server 执行任何被请求的 tool calls
- 结果发回 Claude
- Claude 提供自然语言响应
- 响应显示给你
最佳实践
-
错误处理
- 用
begin…rescueblocks 包裹 tool calls - 提供有意义的错误消息
- 优雅处理连接问题
- 用
-
资源管理
- 完成后始终关闭 transport
- 使用
begin…ensure正确清理 - 处理 server 断开连接
-
安全
- 将 API keys 安全存储在
.env中 - 校验 server 响应
- 谨慎处理 tool 权限
- 将 API keys 安全存储在
-
Tool 名称
- Tool 名称可以根据这里指定的格式进行校验
- 如果 tool 名称符合指定格式,MCP client 不应使其校验失败
排障
Server 路径问题
- 仔细检查 server script 路径是否正确
- 如果相对路径不工作,请使用绝对路径
- Windows 用户请确保路径中使用正斜杠 (/) 或转义反斜杠 (\)
- 确认 server 文件扩展名正确(Python 用 .py,Node.js 用 .js,Ruby 用 .rb)
# Relative path
bundle exec ruby client.rb ./server/weather.rb
# Absolute path
bundle exec ruby client.rb /Users/username/projects/mcp-server/weather.rb
# Windows path (either format works)
bundle exec ruby client.rb C:/projects/mcp-server/weather.rb
bundle exec ruby client.rb C:\\projects\\mcp-server\\weather.rb
响应时间
- 第一次响应可能最多需要 30 秒才返回
- 这是正常现象,期间会发生:
- server 初始化
- Claude 处理查询
- tools 正在执行
- 后续响应通常更快
- 在初始等待期间不要中断进程
常见错误消息
如果看到:Errno::ENOENT:检查 server 路径,并确保命令(ruby、python3、node)可用Connection refused:确保 server 正在运行且路径正确Tool execution failed:确认 tool 所需环境变量已设置Anthropic::Errors::AuthenticationError:检查.env文件中是否有有效的ANTHROPIC_API_KEY
后续步骤
示例服务器
查看官方 MCP servers 和 implementations 展示列表