@mcp-z/客户端
文件:https://mcp-z.github.io/client 用于生成、连接和调用MCP服务器的可编程MCP客户端。
常见用途
- 从脚本运行MCP工具
- 在一个进程中连接到多个服务器
- MCP服务器的集成测试
安装
npm install --save-dev @mcp-z/client需要Node.js>=22。
快速启动
import { createServerRegistry } from '@mcp-z/client';
const registry = createServerRegistry({
todoist: { type: 'http', url: 'https://ai.todoist.net/mcp' }
});
const client = await registry.connect('todoist');
await client.callTool('add-tasks', {
tasks: [{ content: 'Learn MCP', priority: 4 }]
});
await registry.close();配置
MCP支持stdio和HTTP。
工作室
{
echo: {
command: 'node',
args: ['server.js'],
env: { LOG_LEVEL: 'info' }
}
}超文本传输协议
{
todoist: {
type: 'http',
url: 'https://ai.todoist.net/mcp',
headers: { Authorization: 'Bearer token' }
}
}带有起始块(扩展名)的HTTP
使用 dialects: ['start'] 生成HTTP服务器 start 阻碍。
const registry = createServerRegistry(
{
api: {
type: 'http',
url: 'http://localhost:3000/mcp',
start: { command: 'node', args: ['server.js'] }
}
},
{ dialects: ['start'] }
);API概述
注册表
createServerRegistry(config, options?)registry.connect(name, options?)registry.searchCapabilities(query, options?)registry.close()
托管客户端
client.callTool(name, args)client.getPrompt(name, args)client.readResource(uri)client.listTools()/client.listResources()/client.listPrompts()client.callToolRaw()/client.getPromptRaw()/client.readResourceRaw()(原始SDK响应)
响应助手
工具、提示和资源调用返回包装器,其中包含:
json()-解析结构化内容text()-第一个文本结果raw()-原始MCP响应
例子
调用工具
const response = await client.callTool('drive-search', { query: 'Q4 Reports' });
const data = response.json();获得提示
const prompt = await client.getPrompt('query-syntax', { service: 'gmail' });
console.log(prompt.text());阅读资源
const resource = await client.readResource('mcp-pdf://abc123');
console.log(resource.text());搜索功能
const results = await registry.searchCapabilities('message send', {
types: ['tool'],
servers: ['gmail', 'outlook']
});createServerRegistry选项
cwd-生成进程的工作目录(默认:process.cwd())env-所有服务器的基本环境(如果设置,process.env未合并)dialects-生成哪些服务器:['servers'](stdio),['start'](HTTP起始块),或两者兼而有之
OAuth(DCR)
如果HTTP服务器支持DCR,请通过以下方式传递令牌存储 dcrAuthenticator:
import Keyv from 'keyv';
import { createServerRegistry } from '@mcp-z/client';
const registry = createServerRegistry({
todoist: { type: 'http', url: 'https://ai.todoist.net/mcp' }
});
const client = await registry.connect('todoist', {
dcrAuthenticator: { tokenStore: new Keyv() }
});需求
- Node.js>=22
