Skip to main content

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"# 使用 MCP 与 Hermes

MCP(Model Control Protocol)是一种用于控制和协调多个 AI 模型的协议。Hermes 是一个由 Nous Research 开发的开源大语言模型,以其高性能和可扩展性著称。将 MCP 与 Hermes 结合使用,可以实现更智能、更高效的多模型协作。

快速开始

1. 安装依赖

pip install hermes-agent mcp-client

2. 启动 Hermes 模型

# 使用 Docker 启动 Hermes
docker run -d --gpus all -p 8080:8080 \
--name hermes \
nousresearch/hermes:latest

3. 配置 MCP 客户端

创建 mcp_config.json

{
"models": [
{
"name": "hermes",
"type": "llm",
"endpoint": "http://localhost:8080/v1",
"api_key": "your-api-key"
}
],
"default_model": "hermes"
}

4. 运行 MCP 代理

from hermes_agent import HermesAgent
from mcp_client import MCPClient

# 初始化 MCP 客户端
client = MCPClient(config_path="mcp_config.json")

# 创建 Hermes 代理
agent = HermesAgent(client=client)

# 执行任务
response = agent.execute("请总结这篇论文的主要贡献")
print(response)

高级功能

多模型协同

# 配置多个模型
multi_model_config = {
"models": [
{"name": "hermes", "endpoint": "http://localhost:8080/v1"},
{"name": "openai", "endpoint": "https://api.openai.com/v1", "api_key": "sk-..."}
],
"routing": {
"strategy": "load_balancing",
"fallback": "hermes"
}
}

# 动态选择模型
result = client.route_task("分析市场趋势", model_preference=["hermes", "openai"])

任务链编排

from mcp_client import TaskChain

chain = TaskChain()

# 添加任务步骤
chain.add_step(
name="summarize",
model="hermes",
prompt="总结以下内容:{input}"
)

chain.add_step(
name="analyze",
model="hermes",
prompt="分析上述摘要中的关键点:{summary}"
)

# 执行链式任务
output = chain.run(input="这是一篇关于AI发展的最新研究文章...")

部署建议

Kubernetes 部署

# helm-chart/values.yaml
hermes:
replicas: 3
resources:
requests:
memory: "8Gi"
cpu: "4"
limits:
memory: "16Gi"
cpu: "8"

mcp-server:
replicas: 2
service:
type: LoadBalancer

监控与日志

# prometheus监控配置
metrics:
enabled: true
endpoint: "/metrics"
labels:
app: "hermes-mcp"
version: "1.0"

常见问题

Q: 如何处理模型响应超时?

A: 在 MCP 配置中设置超时参数:

{
"timeout": 30,
"retry_attempts": 3,
"backoff_factor": 2
}

Q: 如何验证模型输出质量?

A: 使用评估模块进行自动评分:

from mcp_client.evaluation import evaluate_response

score = evaluate_response(
response="Hermes 的性能非常出色。",
reference="Hermes 在多项基准测试中表现优异。",
metrics=["accuracy", "relevance"]
)

参考资源

提示:在生产环境中,请确保为 API 密钥和敏感数据启用适当的加密和访问控制。" description: "一个实用指南:将 MCP 服务器连接至 Hermes Agent,筛选其工具,并在实际工作流中安全使用"

使用 MCP 与 Hermes

本指南将展示如何在日常工作中实际使用 MCP 与 Hermes Agent,快速且安全地从中获取价值。

如果说功能页面解释了 MCP 是什么,那么本指南则聚焦于如何迅速、安全地从 MCP 中获得实际收益。

何时应使用 MCP?

当满足以下任一条件时,请使用 MCP:

  • 已存在现成的 MCP 形式工具,你不想自行开发原生 Hermes 工具
  • 希望 Hermes 通过清晰的 RPC 层操作本地或远程系统
  • 需要对每个服务器的暴露范围进行细粒度控制
  • 想连接 Hermes 到内部 API、数据库或公司系统,而无需修改 Hermes 核心代码

不要使用 MCP 的情况包括:

  • 内置 Hermes 工具已能很好地完成任务
  • 服务器暴露了大量危险功能,而你尚未准备好进行过滤
  • 仅需一个非常狭窄的集成,原生工具会更简单、更安全

心理模型

将 MCP 理解为一个适配层:

  • Hermes 仍是智能体(agent)
  • MCP 服务器提供工具
  • Hermes 在启动或重新加载时发现这些工具
  • 模型可像使用普通工具一样调用它们
  • 你完全控制每个服务器可见的部分

最后一点至关重要。优秀的 MCP 使用方式并非“连接一切”,而是“连接正确的内容,且只暴露最小必要的功能面”。

第一步:安装 MCP 支持

如果你通过标准安装脚本安装了 Hermes,MCP 支持已默认包含(安装程序会自动运行 uv pip install -e ".[all]")。

若你未安装附加组件,需要单独添加 MCP 支持:

cd ~/.hermes/hermes-agent
uv pip install -e ".[mcp]"

对于基于 npm 的服务器,请确保 Node.js 和 npx 可用。

对于多数 Python MCP 服务器,uvx 是一个不错的默认选择。

第二步:先添加一个服务器

从单一、安全的服务器开始。

示例:仅访问某个项目目录的文件系统。

mcp_servers:
project_fs:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/my-project"]

然后启动 Hermes:

hermes chat

接着提出一个具体问题:

Inspect this project and summarize the repo layout.

第三步:验证 MCP 是否已加载

可通过以下几种方式验证 MCP 是否正常工作:

  • 配置正确时,Hermes 启动界面/状态栏应显示 MCP 集成信息
  • 询问 Hermes 当前可用的工具列表
  • 在配置更改后使用 /reload-mcp 进行检查
  • 若服务器连接失败,检查日志

一个实用的测试提示:

Tell me which MCP-backed tools are available right now.

第四步:立即开始过滤

如果服务器暴露的功能过多,请不要拖延,立刻进行限制。

示例:仅允许所需功能(白名单)

mcp_servers:
github:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-github"]
env:
GITHUB_PERSONAL_ACCESS_TOKEN: "***"
tools:
include: [list_issues, create_issue, search_code]

这通常是敏感系统的最佳默认策略。

示例:屏蔽危险操作(黑名单)

mcp_servers:
stripe:
url: "https://mcp.stripe.com"
headers:
Authorization: "Bearer ***"
tools:
exclude: [delete_customer, refund_payment]

示例:同时禁用辅助封装器

mcp_servers:
docs:
url: "https://mcp.docs.example.com"
tools:
prompts: false
resources: false

过滤实际影响哪些内容?

Hermes 中通过 MCP 暴露的功能分为两类:

  1. 服务器原生的 MCP 工具

    • 通过以下方式过滤:
      • tools.include
      • tools.exclude
  2. Hermes 添加的辅助封装工具

    • 通过以下方式过滤:
      • tools.resources
      • tools.prompts

你可能看到的辅助封装功能

资源类:

  • list_resources
  • read_resource

提示类:

  • list_prompts
  • get_prompt

这些封装功能仅在以下条件下出现:

  • 你的配置允许启用它们
  • MCP 服务器会话确实支持相关能力

因此,Hermes 不会假装某个服务器具备资源或提示功能,除非它真的支持。

常见使用模式

模式 1:本地项目助手

当希望 Hermes 在有限的工作空间内推理时,使用 MCP 连接仓库级的文件系统或 Git 服务。

mcp_servers:
fs:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/project"]

git:
command: "uvx"
args: ["mcp-server-git", "--repository", "/home/user/project"]

推荐提示:

Review the project structure and identify where configuration lives.
Check the local git state and summarize what changed recently.

模式 2:GitHub 问题分类助手

mcp_servers:
github:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-github"]
env:
GITHUB_PERSONAL_ACCESS_TOKEN: "***"
tools:
include: [list_issues, create_issue, update_issue, search_code]
prompts: false
resources: false

推荐提示:

List open issues about MCP, cluster them by theme, and draft a high-quality issue for the most common bug.
Search the repo for uses of _discover_and_register_server and explain how MCP tools are registered.

模式 3:内部 API 助手

mcp_servers:
internal_api:
url: "https://mcp.internal.example.com"
headers:
Authorization: "Bearer ***"
tools:
include: [list_customers, get_customer, list_invoices]
resources: false
prompts: false

推荐提示:

Look up customer ACME Corp and summarize recent invoice activity.

在这种场景中,严格的白名单远优于排除列表。

模式 4:文档 / 知识库服务器

某些 MCP 服务器暴露的提示或资源更像是共享知识资产,而非直接操作。

mcp_servers:
docs:
url: "https://mcp.docs.example.com"
tools:
prompts: true
resources: true

推荐提示:

List available MCP resources from the docs server, then read the onboarding guide and summarize it.
List prompts exposed by the docs server and tell me which ones would help with incident response.

教程:带过滤的端到端设置

以下是逐步实践流程。

阶段 1:添加 GitHub MCP 并设置严格白名单

mcp_servers:
github:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-github"]
env:
GITHUB_PERSONAL_ACCESS_TOKEN: "***"
tools:
include: [list_issues, create_issue, search_code]
prompts: false
resources: false

启动 Hermes 并提问:

Search the codebase for references to MCP and summarize the main integration points.

阶段 2:按需扩展

若后续需要处理问题更新:

tools:
include: [list_issues, create_issue, update_issue, search_code]

然后重新加载:

/reload-mcp

阶段 3:添加第二个具有不同策略的服务器

mcp_servers:
github:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-github"]
env:
GITHUB_PERSONAL_ACCESS_TOKEN: "***"
tools:
include: [list_issues, create_issue, update_issue, search_code]
prompts: false
resources: false

filesystem:
command: "npx"
args: ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/project"]

现在 Hermes 可以组合多个系统:

Inspect the local project files, then create a GitHub issue summarizing the bug you find.

这正是 MCP 的强大之处:实现跨系统工作流,而无需修改 Hermes 核心。

安全使用建议

对危险系统优先使用白名单

对于金融、客户交互或破坏性操作相关的系统:

  • 使用 tools.include
  • 从最小可用集合开始

禁用不必要的辅助功能

若不希望模型浏览服务器提供的资源或提示,请关闭它们:

tools:
resources: false
prompts: false

将服务器作用域限制得尽可能窄

示例:

  • 文件系统服务器仅指向一个项目目录,而非整个主目录
  • Git 服务器仅指向一个仓库
  • 内部 API 服务器默认仅开放读取类工具

配置变更后务必重新加载

/reload-mcp

在以下情况后执行此操作:

  • 修改包含/排除列表
  • 更改启用标志
  • 切换资源/提示开关
  • 更新认证环境变量或请求头

按症状排查问题

“服务器已连接,但预期的工具缺失”

可能原因:

  • tools.include 过滤
  • tools.exclude 排除
  • 辅助封装器因 resources: falseprompts: false 被禁用
  • 服务器本身不支持资源/提示功能

“服务器已配置,但无任何工具加载”

请检查:

  • enabled: false 是否误留在配置中
  • 执行命令或运行时是否存在(如 npxuvx 等)
  • HTTP 端点是否可达
  • 认证环境变量或请求头是否正确

“为何看到的工具比 MCP 服务器宣称的少?”

因为 Hermes 现在尊重你为每个服务器设定的策略和能力感知注册机制。这是预期行为,通常也是理想状态。

“如何移除 MCP 服务器而不删除配置?”

使用:

enabled: false

这将保留配置,但阻止连接与注册。

推荐的首个 MCP 设置

对大多数用户而言,良好的初始 MCP 服务器选择包括:

  • 文件系统
  • Git
  • GitHub
  • fetch / 文档类 MCP 服务器
  • 一个特定的内部 API

不太适合初学者的服务器:

  • 包含大量破坏性操作且无法过滤的大型业务系统
  • 你无法充分理解并加以约束的系统

相关文档