关于 MCP Client
MCP Client 是 MCP 协议的 TypeScript 客户端库,支持 stdio 和 HTTP+SSE 传输。为 LLM Agent 提供完整的 MCP 规范实现,与 Claude 的原生工具调用无缝集成。
主要功能
- 01完整的 MCP 规范实现
- 02Stdio 和 HTTP+SSE 传输支持
- 03MCP 服务器进程管理
- 04与 Claude 工具调用集成
- 05基于事件的异步 API
- 06支持资源、工具、提示词和采样操作
适用场景
- 01使用标准 MCP 服务器增强 LLM Agent 能力
- 02在应用中集成知识图谱、文件系统等服务
- 03构建支持网络搜索和自动化的 AI 应用
MCP 相关
作为 LLM Agent 的 MCP 客户端,通过 MCPConnectionManager 管理连接至多个 MCP 服务器,支持 Claude 的原生工具调用。
详细介绍
MCP 客户端
Model Context Protocol (MCP) 客户端的 TypeScript 实现,用于大模型 Agent。
安装
npm install mcp-client
功能特性
- MCP 规范的完整实现
- 支持 stdio 和 HTTP+SSE 传输方式
- 内置 MCP 服务进程管理
- 与 Claude 原生工具调用的集成
- 类型安全的 API
- 基于事件的架构
- 基于 Promise 的 async/await API
- 支持所有 MCP 操作:
- 资源
- 工具
- 提示词
- 采样
使用方法
与 MCP 服务配合使用
使用 MCP 客户端最常见的方法是通过 npx 与标准 MCP 服务配合。创建一个配置文件(mcp-config.json):
{
"mcpServers": {
"memory": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-memory"
]
},
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/path/to/allowed/directory"
]
},
"brave-search": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-brave-search"
],
"env": {
"BRAVE_API_KEY": "your-api-key"
}
}
}
}
然后使用 MCPConnectionManager 连接到你的服务:
import { MCPConnectionManager } from 'mcp-client';
const manager = new MCPConnectionManager();
await manager.initialize('./mcp-config.json');
// Get clients for specific servers
const memoryClient = manager.getClient('memory');
const fsClient = manager.getClient('filesystem');
// Use tools from the servers
const memoryTools = await memoryClient?.listTools();
const fsTools = await fsClient?.listTools();
// Clean up when done
await manager.cleanup();
与 Claude 集成
该客户端设计用于与 Claude 原生工具调用无缝配合:
import { Anthropic } from '@anthropic-ai/sdk';
import { MCPConnectionManager } from 'mcp-client';
// Initialize Claude and MCP
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const manager = new MCPConnectionManager();
await manager.initialize('./mcp-config.json');
// Get a client
const memoryClient = manager.getClient('memory');
const tools = await memoryClient?.listTools();
// Use with Claude
const response = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1024,
messages: [{ role: 'user', content: 'your prompt' }],
tools: tools?.map(tool => ({
name: tool.name,
description: tool.description || '',
parameters: tool.inputSchema
}))
});
// Handle tool calls
if (!response.content[0].text && response.content[0].type === 'tool_call') {
const toolCall = response.content[0].tool_calls[0];
const result = await memoryClient?.callTool(
toolCall.name,
toolCall.parameters
);
// Send result back to Claude...
}
低级别使用
如果你需要更多控制,可以直接使用该客户端:
import { MCPClient, StdioTransport } from 'mcp-client';
// Create a transport
const transport = new StdioTransport();
// Create and connect client
const client = new MCPClient({ transport });
await client.connect();
// List available tools
const tools = await client.listTools();
// Call a tool
const result = await client.callTool('tool-name', {
// tool parameters
});
可用的 MCP 服务
通过 npx 可用的常见 MCP 服务:
@modelcontextprotocol/server-memory- 知识图谱操作@modelcontextprotocol/server-filesystem- 文件系统操作@modelcontextprotocol/server-brave-search- 网页搜索功能@modelcontextprotocol/server-puppeteer- 网页自动化@modelcontextprotocol/server-fetch- HTTP 请求- 以及更多...
示例
查看 /examples 目录获取完整示例:
memory-app- 使用内存服务进行知识图谱操作llm-app- 带有 MCP 工具的基础大模型应用llm-app-tools- 带有 Claude 原生工具调用的高级大模型应用
API 参考
MCPConnectionManager
class MCPConnectionManager {
initialize(configPath: string): Promise<void>;
getClient(serverName: string): MCPClient | undefined;
cleanup(): Promise<void>;
}
MCPClient
class MCPClient {
constructor(options: MCPClientOptions);
// Connection
connect(): Promise<void>;
close(): Promise<void>;
// Tools
listTools(): Promise<Tool[]>;
callTool(name: string, params: Record<string, any>): Promise<any>;
// Resources
listResources(): Promise<Resource[]>;
readResource(uri: string): Promise<string | Buffer>;
// Prompts
listPrompts(): Promise<Prompt[]>;
getPrompt(name: string, args?: Record<string, any>): Promise<string>;
}
贡献
欢迎贡献!请随时提交 Pull Request。
许可证
MIT
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。