MCP HTTP 客户端
一个通过HTTP访问MCP(模型上下文协议)服务器的自包含Node.js客户端。
建筑
npm install
npm run build这产生了 dist/mcp-client.js,一个独立的自包含文件。
使用方法
在你的 Node.js 会话中包含构建后的文件:
const { MCPClient, createClients } = require('./dist/mcp-client.js');
// Single server
const client = new MCPClient('http://localhost:3000/mcp');
await client.initialize({ name: 'my-client', version: '1.0.0' });
// List and call tools
const tools = await client.listTools();
// Method 1: Direct call
const result = await client.callTool('tool-name', { arg1: 'value' });
// Method 2: Syntax sugar
const result = await client.tools.tool_name({ arg1: 'value' });
// Multiple servers
const clients = createClients({
server1: 'http://localhost:3000/mcp',
server2: 'http://localhost:3001/mcp'
});
await clients.server1.initialize();
await clients.server2.initialize();API
MCPClient(中文可译为“MCP客户端”)
constructor(endpoint, options)- 创建一个新客户端initialize(clientInfo)- 初始化MCP会话listTools()- 列出可用工具callTool(name, args)- 调用一个工具listResources()- 列出可用资源readResource(uri)- 读取资源listPrompts()- 列出可用的提示getPrompt(name, args)- 获取提示request(method, params)- 发送一个自定义的JSON-RPC请求notify(method, params)- 发送通知onMessage(handler)- 注册服务器消息的处理程序close()- 关闭连接
创建客户端(endpoints)
根据对象映射(将名称映射到端点)创建多个客户端。
