Typescript mcp 客户端库。
概览
一种基于 LLM 代理的模型上下文协议(MCP)客户端的 TypeScript 实现。 - 完整实现MCP规范 - 支持stdio和HTTP+SSE传输 - 内置MCP服务器进程管理 - 与Claude原生工具调用集成 - 类型安全API - 基于事件的架构 - 基于承诺的异步/等待API - 支持所有MCP操作: - 资源 - 工具 - 提示符 - 采样 使用MCP客户端最常见的方式是通过npx与标准MCP服务器连接。创建一个配置文件(mcp-config.
概述(AI 翻译)
一种基于 LLM 代理的模型上下文协议(MCP)客户端的 TypeScript 实现。
安装
特色
- MCP规范的完整实现
- 支持 stdio 和 HTTP+SSE 传输
- 内置MCP服务器进程管理
- 与 Claude 原生工具调用集成
- 类型安全 API
- 基于事件的架构
- 基于承诺的异步/等待API
- 支持所有MCP操作:
- 资源
- 工具
- 提示
- 抽样
用途
使用 MCP 服务器
使用 MCP 客户端最常见的方式是通过 npx 与标准 MCP 服务器连接。创建一个配置文件(mcp-config.json):
然后用MCPConnectionManager连接到你的服务器:
与克劳德的融合
该客户端设计为无缝兼容 Claude 原生工具调用:
低层次使用
如果你需要更多控制,可以直接使用客户端:
可用MCP服务器
通过npx提供的常见MPC服务器:
@modelcontextprotocol/server-memory- 知识图谱操作@modelcontextprotocol/server-filesystem- 文件系统操作@modelcontextprotocol/server-brave-search- 网页搜索功能
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
MCP Client
A TypeScript implementation of a Model Context Protocol (MCP) client for LLM agents.
Installation
npm install mcp-client
Features
- Full implementation of the MCP specification
- Support for both stdio and HTTP+SSE transports
- Built-in MCP server process management
- Integration with Claude’s native tool calling
- Type-safe API
- Event-based architecture
- Promise-based async/await API
- Support for all MCP operations:
- Resources
- Tools
- Prompts
- Sampling
Usage
Using with MCP Servers
The most common way to use MCP Client is with standard MCP servers via npx. Create a configuration file (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"
}
}
}
}
Then use the MCPConnectionManager to connect to your servers:
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();
Integration with Claude
The client is designed to work seamlessly with Claude’s native tool calling:
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...
}
Low-level Usage
If you need more control, you can use the client directly:
安装
npx -y @modelcontextprotocol/server-memory配置
{
"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"
}
}
}
}