MCP 集成
Hermes Agent 完整支持 Model Context Protocol (MCP),可连接外部工具服务器,也可作为 MCP 服务器向其他客户端暴露工具。
什么是 MCP?
Model Context Protocol (MCP) 是一个开放协议,用于连接 AI 模型与外部工具和数据源。通过 MCP,Hermes 可以:
- 作为 MCP 客户端:连接外部 MCP 服务器,使用其提供的工具
- 作为 MCP 服务器:向其他 MCP 客户端暴露 Hermes 的工具能力
- 动态工具发现:自动发现和加载 MCP 服务器提供的工具
支持的传输协议
| 协议 | 说明 |
|---|---|
| stdio | 标准输入/输出,适用于本地进程间通信 |
| HTTP (SSE) | 基于 Server-Sent Events 的 HTTP 传输,适用于远程服务 |
配置 MCP 服务器
在 config.yaml 中添加 MCP 服务器:
yaml
mcp:
servers:
# stdio 类型服务器
filesystem:
transport: stdio
command: npx
args: ["-y", "@anthropic/mcp-server-filesystem", "/path/to/dir"]
env:
NODE_ENV: production
# HTTP 类型服务器
remote-tools:
transport: http
url: https://mcp-server.example.com/sse
headers:
Authorization: Bearer ${MCP_SERVER_TOKEN}
工具过滤
可以按服务器维度过滤工具,控制哪些工具可用:
yaml
mcp:
servers:
filesystem:
transport: stdio
command: npx
args: ["-y", "@anthropic/mcp-server-filesystem", "/data"]
# 工具过滤选项
filter:
mode: include # include | exclude | enabled
tools:
- read_file
- list_directory
# 或者排除特定工具
# mode: exclude
# tools:
# - write_file
# - delete_file
过滤模式说明
| 模式 | 说明 |
|---|---|
include | 白名单模式,仅启用列出的工具 |
exclude | 黑名单模式,禁用列出的工具 |
enabled | 显式启用模式,使用布尔值控制每个工具 |
MCP Sampling 支持
Hermes 支持 MCP Sampling 协议,允许 MCP 服务器向 Hermes 请求 LLM 推理:
- MCP 服务器可以请求 Hermes 调用 LLM 进行推理
- 支持上下文传递和结果返回
- 可用于构建更智能的 MCP 工具
Hermes 作为 MCP 服务器
Hermes 可以作为 MCP 服务器运行,向其他 MCP 客户端暴露 10 种工具:
bash
# 启动 Hermes MCP 服务器
hermes mcp-server
# 或指定传输协议
hermes mcp-server --transport stdio
hermes mcp-server --transport http --port 8080
凭证安全:MCP 服务器的环境变量会经过过滤处理,敏感凭证不会被传递到不信任的服务器。详见 安全 页面的 MCP 凭证处理部分。
动态工具发现
当 MCP 服务器连接后,Hermes 会自动:
- 发现服务器提供的所有工具
- 注册为以
mcp-为前缀的工具集 - 将工具定义添加到 LLM 的可用工具列表中
- 在工具调用时路由到正确的 MCP 服务器
MCP 工具集会自动出现在 /tools 命令的输出中。使用 /tools list mcp-server-name 可以查看特定 MCP 服务器提供的工具。