第25篇-MCP集成
·
【Hermes Agent 从入门到精通】第 25 篇:MCP 集成 — 连接外部工具生态
本系列定位:零基础入门,从安装配置到高级架构全覆盖。无论你是开发者、运维工程师、还是技术爱好者,本系列带你彻底掌握 Hermes Agent。
本篇你将学到
- MCP(Model Context Protocol)的概念
- 添加和管理 MCP 服务器
- 内置 MCP 预设
- MCP 工具的自动注册
- 实战:连接 GitHub MCP 和数据库 MCP
学完本篇,你将能通过 MCP 协议无限扩展 Hermes 的工具能力。
一、MCP 是什么
1.1 概念
MCP(Model Context Protocol)是 Anthropic 提出的开放标准——一种让 AI Agent 连接外部工具和数据源的统一协议。
没有 MCP:
Hermes 内置工具:web / terminal / file / vision / ...
→ 工具是固定的,不能随意扩展
有 MCP:
Hermes 内置工具 + MCP 服务器提供的工具
→ GitHub MCP → 管理 Issues/PR
→ 数据库 MCP → 直接查询 SQL
→ Slack MCP → 发送消息
→ 文件系统 MCP → 访问特定目录
→ 任何 MCP 服务器...
1.2 MCP 的价值
- 无限扩展:社区有数百个 MCP 服务器可用
- 统一协议:一个标准连接所有工具
- 即插即用:配置一个 MCP 服务器,Hermes 自动发现并注册其工具
下面是 MCP 架构与传统内置工具架构的对比:
二、MCP 服务器类型
2.1 两种类型
| 类型 | 通信方式 | 适用场景 |
|---|---|---|
| stdio | 标准输入输出 | 本地 MCP 服务器(子进程) |
| HTTP (SSE) | HTTP + Server-Sent Events | 远程 MCP 服务器 |
# stdio 类型:本地子进程
hermes mcp add github --command "npx -y @modelcontextprotocol/server-github"
# HTTP 类型:远程服务器
hermes mcp add remote-db --url "https://mcp.example.com/sse"
选择 MCP 服务器类型的决策流程如下:
三、管理 MCP 服务器
3.1 添加
# 交互式添加
hermes mcp add github
# 直接指定命令
hermes mcp add github --command "npx -y @modelcontextprotocol/server-github"
# 直接指定 URL
hermes mcp add jira --url "https://mcp.atlassian.com/sse"
3.2 查看
hermes mcp list
已配置的 MCP 服务器:
Name Type Status Tools
github stdio ✅ 运行中 12 tools
filesystem stdio ✅ 运行中 5 tools
remote-db http ❌ 连接失败 -
3.3 测试连接
hermes mcp test github
✅ MCP 服务器 'github' 连接成功
工具列表:
- search_issues 搜索 GitHub Issues
- create_issue 创建 Issue
- get_pr_details 获取 PR 详情
- merge_pr 合并 PR
- ...
3.4 配置工具选择
# 交互式选择启用哪些 MCP 工具
hermes mcp configure github
3.5 删除
hermes mcp remove github
四、内置 MCP 预设
Hermes 提供了一些常用 MCP 的预设配置:
| 预设名 | 功能 | 配置方式 |
|---|---|---|
| GitHub | 管理 Issues / PR / 仓库 | 需要 GITHUB_TOKEN |
| Filesystem | 文件系统访问 | 指定允许的目录 |
| PostgreSQL | 数据库查询 | 需要连接字符串 |
| SQLite | SQLite 数据库 | 指定数据库文件 |
| Brave Search | 网页搜索 | 需要 API Key |
| Memory | 持久化知识图谱 | 本地运行 |
# 用预设快速添加
hermes mcp add github # 自动使用预设配置
五、配置示例
5.1 GitHub MCP
# 设置 GitHub Token
echo 'GITHUB_TOKEN=ghp_xxxxxxxxxxxx' >> ~/.hermes/.env
# 添加 GitHub MCP
hermes mcp add github --command "npx -y @modelcontextprotocol/server-github"
使用:
你:"帮我看看 nousresearch/hermes-agent 仓库最近的 5 个 Issue"
Hermes:
🔧 mcp: github.search_issues(repo="NousResearch/hermes-agent", state="open", limit=5)
📋 最近 5 个开放 Issue:
#42 - Feature: Support for custom model routing
#41 - Bug: Gateway crashes on Discord reconnect
...
5.2 PostgreSQL MCP
hermes mcp add postgres \
--command "npx -y @modelcontextprotocol/server-postgres" \
--env "DATABASE_URL=postgresql://user:pass@localhost:5432/mydb"
使用:
你:"查询用户表中有多少条记录"
Hermes:
🔧 mcp: postgres.query("SELECT COUNT(*) FROM users")
📊 用户表共有 1,234 条记录。
5.3 Filesystem MCP
hermes mcp add filesystem \
--command "npx -y @modelcontextprotocol/server-filesystem" \
--env "ALLOWED_DIRECTORIES=/home/user/documents,/home/user/projects"
六、MCP 工具的自动注册
6.1 工具发现
当 MCP 服务器启动后,Hermes 自动:
- 连接 MCP 服务器
- 获取其提供的工具列表
- 把工具注册到 Hermes 的工具系统中
- Agent 可以像使用内置工具一样使用 MCP 工具
6.2 工具命名
MCP 工具的名称格式:mcp:服务器名.工具名
mcp:github.search_issues
mcp:github.create_issue
mcp:postgres.query
mcp:filesystem.read_file
6.3 会话中重新加载
如果修改了 MCP 配置:
/reload-mcp
MCP 工具从发现到使用的完整流程:
–
七、MCP 高级配置
7.1 mTLS / 客户端证书
对于需要证书认证的 MCP 服务器:
hermes mcp add secure-service \
--url "https://mcp.internal.com/sse" \
--cert "/path/to/client.crt" \
--key "/path/to/client.key" \
--ca "/path/to/ca.crt"
7.2 Hermes 作为 MCP 服务器
Hermes 也可以作为 MCP 服务器供其他 Agent 使用:
hermes mcp serve
其他支持 MCP 的客户端(如 Claude Desktop)可以连接 Hermes 作为工具提供方。
本篇小结
| 知识点 | 核心内容 |
|---|---|
| MCP | Model Context Protocol,连接外部工具的统一协议 |
| stdio 类型 | 本地子进程 MCP |
| HTTP 类型 | 远程 MCP 服务器 |
hermes mcp add | 添加 MCP 服务器 |
hermes mcp list | 查看已配置 |
hermes mcp test | 测试连接 |
| 内置预设 | GitHub / PostgreSQL / Filesystem 等 |
| 自动注册 | MCP 工具自动发现并注册 |
hermes mcp serve | Hermes 作为 MCP 服务器 |
下篇预告
怎么让 Hermes 有自己的性格?下一篇学习 SOUL.md、人格设置和项目上下文文件。
如果本篇内容对你有帮助,欢迎点赞收藏!有任何疑问,欢迎在评论区交流。
更多推荐



所有评论(0)