@ agent-infra/mcp-客户端
QQ 在 TypeScript 中执行一个统一的 MCP 客户端, 支持四大运出框 : 记忆Stdio,SSE(服务生事件),以及 可流取的 HTTP。 。 。 .
* 特性
- 🟦 在类型脚本中写入:类型安全,现代,并易于整合.
- 🔌 多运输支助: 外出支持四大运输:
- 🧠 记忆:为快速,本地工具集成.
- 🖥️ 斯特迪奥:通过标准输入/输出与工具进行交流,适合基于进程的工具.
- 🔄 SSE( 仆人事件): 实时,事件驱动的通信超過HTTP.
- 🌐 可流取的 HTTP:高效,以流为基础的HTTP通信,用于可伸缩远程工具.
- 🛠️ 统一 API: 与所有运输机使用单一,一致的接口进行交互.
- 🧩 范围很广:根据需要方便地添加自定义运输或工具.
- 🔍 过滤支持: 使用带有允许/块列表的 glob 模式过滤工具和提示.
QQ 快速启动
import { MCPClient } from '@agent-infra/mcp-client';
// type: module project usage
import { createServer as createFileSystemServer } from '@agent-infra/mcp-server-filesystem';
// commonjs project usage
// const { createServer as createFileSystemServer } = await import('@agent-infra/mcp-server-filesystem')
const mcpClient = new MCPClient([
// In-memory
{
type: 'builtin',
name: 'FileSystem',
description: 'filesystem tool',
mcpServer: createFileSystemServer({
allowedDirectories: [omegaDir],
}),
},
// stdio
{
type: 'stdio',
name: 'FileSystem-Stdio',
description: 'filesystem tool',
command: 'npx',
args: [
'-y',
'@agent-infra/mcp-server-filesystem'
]
},
// sse
{
type: 'sse',
name: 'FileSystem-sse',
description: 'filesystem tool',
url: 'http://localhost:8889/sse'
},
// streamable-http
{
type: 'sse',
name: 'FileSystem-http',
description: 'filesystem tool',
url: 'http://localhost:8889/mcp'
}
]);
await mcpClient.listTools();
await mcpClient.listPrompts();
const result = await mcpClient.callTool({
client: 'FileSystem-sse',
name: 'list_directory',
arguments: {
path: '~/your_computer'
},
});
过滤工具和提示
您可以使用 glob 模式过滤工具和提示, 包含允许和块列表 :
const mcpClient = new MCPClient([
{
type: 'builtin',
name: 'FileSystem',
description: 'filesystem tool',
mcpServer: createFileSystemServer({
allowedDirectories: [omegaDir],
}),
// Filter configuration
filters: {
tools: {
allow: ['list_*', 'read_*'], // Only allow tools starting with 'list_' or 'read_'
block: ['delete_*'] // Block any tools starting with 'delete_'
},
prompts: {
allow: ['safe_*'], // Only allow prompts starting with 'safe_'
block: ['admin_*'] // Block prompts starting with 'admin_'
}
}
}
]);
// List all tools (filtered)
const tools = await mcpClient.listTools();
// List all prompts (filtered)
const prompts = await mcpClient.listPrompts();
// List tools from specific server
const serverTools = await mcpClient.listTools('FileSystem');
过滤规则 :
- 允许模式: 如果指定, 只包含符合这些模式的项目
- 块图案: 不包括符合这些模式的项目
- 模式语法: 用途 小型匹配 平面图案( u)
*, (中文(简体) ),**, (中文(简体) ),?, (中文(简体) ),[...]页:1 - 处理顺序: 允许先应用过滤器, 然后用块过滤器
• 信贷
感谢:
- 康芬马奥 用于创建伟大的 AI 聊天器产品 樱桃工作室 我们从中汲取了很多浏览器检测功能的灵感.
- 这个 @ modelcontextprotocol/ sdk (英语) 帮助我们更好地开发和使用代理工具的项目.