MCP HTTP客户端

一个自包含的Node.js客户端,用于通过HTTP访问MCP(模型上下文协议)服务器。

  • by KarpelesLab
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 KarpelesLab/mcprun

使用说明

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)

根据对象映射(将名称映射到端点)创建多个客户端。