MCP 客户端
x/mcpClient 组件:v0.36.0+ MCP(Model Context Protocol)客户端节点,连接远程 MCP 服务器并调用指定工具,将结果写入消息体传递给下游节点。同时可作为 MCPToolProvider 注册到 RuleConfig UDF,供 ai/agent 智能体的 self 模式调用远程工具。
# 双重角色
MCP 客户端在规则链中有两种使用方式:
- 直接调用模式:作为规则链节点,在
OnMsg时调用指定的远程 MCP 工具 - 工具提供者模式:启动时自动发现远程 MCP 服务器的全部工具,注册为
MCPToolProvider,供智能体的 MCP 工具(server: "self")调用
# 配置
| 字段 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| server | string | MCP 服务器地址。支持 HTTP URL(http:///https://)和 stdio 命令 | 必填 |
| toolName | string | 调用的远程工具名称。支持 ${metadata.xxx} ${msg.xxx} 表达式。为空时从 metadata.mcpToolName 获取 | |
| args | string | 工具参数 JSON 模板。支持 ${msg.xxx} ${metadata.xxx} 表达式。为空时使用消息体 JSON | |
| toolFilter | []string | 工具过滤器(仅影响 MCPToolProvider 注册),空或 ["*"] 表示注册全部工具 |
# 执行结果
- 工具调用结果写入
msg.Data,通过Success连接类型传递给下游节点 - 调用失败时通过
Failure连接类型传递错误信息
# 配置示例
# 直接调用远程工具
{
"id": "s1",
"type": "x/mcpClient",
"name": "获取天气",
"configuration": {
"server": "http://localhost:8080/mcp",
"toolName": "get_weather",
"args": "{\"city\": \"${msg.city}\", \"unit\": \"celsius\"}"
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 动态工具名(从消息元数据获取)
{
"id": "s1",
"type": "x/mcpClient",
"name": "MCP工具调用",
"configuration": {
"server": "http://localhost:8080/mcp",
"toolName": "${metadata.mcpToolName}",
"args": ""
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
toolName 为空时,组件从 msg.Metadata["mcpToolName"] 获取工具名。args 为空时,使用 msg.Data 的 JSON 作为工具参数。
# Stdio 模式(本地进程)
{
"id": "s1",
"type": "x/mcpClient",
"name": "本地MCP工具",
"configuration": {
"server": "mcp-server --port 8080",
"toolName": "search",
"args": "{\"query\": \"${msg.query}\"}"
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
当 server 不是 HTTP URL 时,组件将其解析为命令行,通过 stdio 传输与 MCP 服务通信。
# 作为智能体的工具提供者
MCP 客户端启动时(Start())自动连接远程服务器、发现工具并注册为 MCPToolProvider。在规则链中配置后,智能体可以通过 self 模式使用这些远程工具:
{
"tools": [
{
"type": "mcp",
"config": {
"server": "self",
"tools": ["get_weather", "search"]
}
}
]
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
toolFilter 可控制注册哪些工具到 MCPToolProvider:
{
"toolFilter": ["get_weather", "search"]
}
1
2
3
2
3
# 应用示例
{
"ruleChain": {
"id": "mcp-demo",
"name": "MCP调用示例",
"root": true
},
"metadata": {
"firstNodeIndex": 0,
"nodes": [
{
"id": "node_mcp",
"type": "x/mcpClient",
"name": "调用远程工具",
"configuration": {
"server": "http://localhost:8080/mcp",
"toolName": "get_weather",
"args": "{\"city\": \"${msg.city}\"}"
}
},
{
"id": "node_log",
"type": "log",
"name": "记录结果",
"configuration": {
"jsScript": "return 'Weather result: ' + msg.data;"
}
}
],
"connections": [
{"fromId": "node_mcp", "toId": "node_log", "type": "Success"}
]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
在 GitHub 上编辑此页 (opens new window)
上次更新: 2026/05/28, 01:50:04