Hermes Agent 进阶实战

MCP 协议集成、多模型路由策略、Agent Teams 协作、Cron 高级用法、上下文压缩与优化。适合已掌握基础的用户深入提升。

Hermes MCP Multi-Model Agent Teams 性能优化

本教程是《Hermes Agent 快速上手》的进阶篇,涵盖生产级高级用法。阅读前请先掌握基础配置(见快速上手教程)。

MCP 协议深度集成

MCP (Model Context Protocol) 让 Hermes 可以连接任意数据源和工具。官方内置 20+ 服务器,涵盖 GitHub、Filesystem、Memory、Slack 等。

什么是 MCP

MCP 是 Anthropic 提出的标准协议,类似于 USB-C——一个接口连接任何设备。通过 MCP,Agent 可以访问文件系统、数据库、API、浏览器等外部世界。

安装 MCP 服务器

$
# 使用 npx 运行官方服务器 npx -y @modelcontextprotocol/server-filesystem /path/to/workspace # GitHub MCP 服务器 npx -y @modelcontextprotocol/server-github # Puppeteer 浏览器自动化 npx -y @modelcontextprotocol/server-puppeteer # Slack 消息 npx -y @modelcontextprotocol/server-slack

配置 MCP 服务器

~/.hermes/config.yaml 中添加:

mcp:
  servers:
    - name: filesystem
      command: npx
      args: ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/workspace"]
      env: {}

    - name: github
      command: npx
      args: ["-y", "@modelcontextprotocol/server-github"]
      env:
        GITHUB_PERSONAL_ACCESS_TOKEN: "{GITHUB_TOKEN}"

    - name: puppeteer
      command: npx
      args: ["-y", "@modelcontextprotocol/server-puppeteer"]
      env: {}

    - name: memory
      command: npx
      args: ["-y", "@modelcontextprotocol/server-memory"]
      env: {}

  # 工具调用设置
  tool_timeout: 30  # 秒
  max_retries: 2

在对话中使用 MCP 工具

$
hermes chat # 自然语言调用 MCP 工具: # "Read the README.md from my GitHub repo and summarize the setup steps" # Agent 会自动调用 github->read_file # "Search my memory for anything about the Python project we worked on last month" # Agent 会自动调用 memory->search

自定义 MCP 服务器

创建一个自己的 MCP 服务器:

# my_mcp_server.py
from mcp.server import MCPServer
from mcp.types import Tool, TextContent

server = MCPServer("my-tools")

@server.tool()
def get_weather(city: str) -> TextContent:
    """Get current weather for a city."""
    # 接入真实天气 API
    return TextContent(text=f"Weather in {city}: 22°C, sunny")

@server.tool()
def search_jobs(query: str) -> list[dict]:
    """Search for job listings."""
    # 接入招聘 API
    return [{"title": "Senior Dev", "company": "Acme", "url": "..."}]

if __name__ == "__main__":
    server.run()
$
# 在 config.yaml 中注册 mcp: servers: - name: my-tools command: python args: ["/path/to/my_mcp_server.py"] env: API_KEY: "xxx"

多模型路由策略

不同任务用不同模型,节省成本的同时保证质量。

配置模型路由

models:
  default: anthropic/claude-sonnet-4-5
  fallback: openai/gpt-4o

  routing:
    # 复杂推理用 Opus
    "complex-reasoning": anthropic/claude-opus-4-6
    # 快速响应用 Haiku
    "fast-response": anthropic/claude-haiku-3
    # 代码任务用 Sonnet(性价比最高)
    "code-generation": anthropic/claude-sonnet-4-5
    # 长文本分析用 Opus
    "deep-analysis": anthropic/claude-opus-4-6
    # 简单问答用 Haiku
    "simple-qa": anthropic/claude-haiku-3

  # 根据 token 数量自动选择
  auto_route_by_length: true
  auto_route_threshold: 32000  # 超过这个长度自动切 Opus

手动指定模型

$
# 在对话中指定模型 /hermes:model anthropic/claude-opus-4-6 # 开始复杂分析任务 # 快速切换回 Sonnet /hermes:model anthropic/claude-haiku-3 # 简单问答

成本控制策略

模型 输入 $/1M 输出 $/1M 适用场景
Claude Opus 4 $15 $75 复杂推理、长文档分析
Claude Sonnet 4.5 $3 $15 日常开发、代码生成(推荐)
Claude Haiku 3 $0.25 $1.25 快速问答、简单任务
GPT-4o $5 $15 Fallback、多模型冗余
💡
路由实战
通过智能路由,平均成本可降低 60%。简单任务用 Haiku,复杂任务才上 Opus,90% 的任务用 Sonnet 即可完成得很好。

Agent Teams 协作

Hermes 支持多 Agent 协作,一个任务拆分给多个 Agent 并行处理。

创建 Agent Team

# ~/.hermes/teams/research.yaml
name: research-team
description: "Multi-agent research workflow"

agents:
  - name: researcher
    model: anthropic/claude-sonnet-4-5
    role: "Researcher - searches and gathers information"
    tools: [web_search, web_extract, memory]

  - name: analyst
    model: anthropic/claude-opus-4-6
    role: "Analyst - analyzes data and draws conclusions"
    tools: [execute_code, memory]

  - name: writer
    model: anthropic/claude-haiku-3
    role: "Writer - formats and writes the final report"
    tools: [file_write, memory]

workflow:
  # researcher 先搜集
  step1:
    agent: researcher
    task: "Research {query} and save key findings to memory"
  # analyst 分析
  step2:
    agent: analyst
    depends_on: [step1]
    task: "Analyze research findings from memory, identify patterns"
  # writer 输出
  step3:
    agent: writer
    depends_on: [step2]
    task: "Format analysis into a markdown report"

触发 Team 任务

$
# 启动团队协作 /hermes:team research "AI coding assistants in 2026" # researcher 并行搜索 -> analyst 分析 -> writer 输出报告

上下文压缩与优化

当对话变长时,Hermes 会自动压缩上下文,但也可以手动优化。

配置上下文压缩

context:
  # 自动压缩阈值(token 数)
  compression_threshold: 80000

  # 压缩保留策略
  compression_strategy: "hybrid"  # hybrid | recent | summary

  # hybrid 模式:保留最近 + 重要摘要
  hybrid:
    recent_ratio: 0.6      # 60% 保留最近
    summary_ratio: 0.3    # 30% 保留摘要
    discard_ratio: 0.1    # 10% 丢弃低价值

  # 强制压缩(立即压缩当前上下文)
  /hermes:compress

  # 查看当前上下文大小
  /hermes:context-size

上下文优化技巧

Cron 高级用法

依赖外部脚本的 Cron

创建 ~/.hermes/scripts/check_revenue.py

#!/usr/bin/env python3
"""Check yesterday's revenue metrics from Stripe"""
import requests
import json
from datetime import datetime, timedelta

yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
api_key = "sk_live_xxx"

response = requests.get(
    "https://api.stripe.com/v1/charges",
    params={"created[gte]": yesterday, "limit": 100},
    auth=(api_key, "")
)

charges = response.json().get("data", [])
total = sum(c["amount"] for c in charges) / 100

print(f"Date: {yesterday}")
print(f"Total Revenue: ${total:.2f}")
print(f"Transactions: {len(charges)}")
print(f"Avg Order: ${total/len(charges):.2f}" if charges else "No transactions")
$
# 每天 9 点运行,带脚本输出 hermes cron create \ --name "Daily Revenue" \ --schedule "0 9 * * *" \ --prompt "Review the revenue data: {script_output}. Send a summary to Telegram." \ --script "python ~/.hermes/scripts/check_revenue.py" \ --deliver "telegram" \ --model "anthropic/claude-sonnet-4-5"

失败重试与告警

cron:
  default_retry: 3
  retry_delay: 300  # 5分钟

  on_failure:
    action: telegram_notify
    message: "❌ Cron job '{job_name}' failed: {error}. Retry {retry_count}/3 in 5min."

  on_success:
    action: telegram_notify
    message: "✅ Cron job '{job_name}' completed successfully"

Cron 链式任务

$
# 创建依赖链:A -> B -> C hermes cron create --name "Data Pipeline" \ --schedule "0 */6 * * *" \ --trigger-file "/tmp/pipeline_trigger.json" \ --prompt "Run ETL step 1: extract data from source" # 下游 Cron 监听 trigger file hermes cron create --name "ETL Step 2" \ --schedule "*/15 * * * *" \ --watch-file "/tmp/pipeline_trigger.json" \ --prompt "Run ETL step 2: transform data"

流式响应与工具并行

启用流式输出

# ~/.hermes/config.yaml
display:
  streaming: true        # 流式显示 agent 思考过程
  streaming_speed: 5      # 字符/帧
  show_tool_calls: true  # 显示工具调用

# Telegram 中流式响应
telegram:
  streaming: true
  # Agent 思考过程实时显示在消息中

工具并行执行

Hermes 默认并行执行独立的工具调用(不等一个完成再调用下一个):

$
# "Search for Claude API docs, check the weather in Tokyo, and find my recent files" # Hermes 会同时调用: # -> web_search (Claude docs) # -> get_weather (Tokyo) # -> search_files (recent files) # 三者并行,汇总结果后回复
性能对比
串行:3 个工具各 2 秒 = 6 秒总耗时
并行:3 个工具同时执行 ≈ 2 秒总耗时
工具并行通常能让响应速度提升 2-5 倍。

多 Profile 与场景切换

Hermes 支持多 Profile,不同场景使用不同配置,互不干扰。

创建 Profile

$
# 创建工作 Profile hermes profile create work \ --model anthropic/claude-sonnet-4-5 \ --tools terminal,web,file,github \ --telegram-bot-token "work-bot-token" # 创建个人 Project Profile hermes profile create my-project \ --model anthropic/claude-opus-4-6 \ --tools terminal,file,github \ --github-token "project-token" # 切换 Profile hermes profile use work # 查看所有 Profile hermes profile list

Profile 特定配置

# ~/.hermes/profiles/work/config.yaml
models:
  default: anthropic/claude-sonnet-4-5

tools:
  enabled: [terminal, web, file, github, pagerduty]
  disabled: []

telegram:
  bot_token: "work-bot-token"
  admin_ids: [123456]

cron:
  default_deliver: telegram

# ~/.hermes/profiles/my-project/config.yaml
models:
  default: anthropic/claude-opus-4-6

tools:
  enabled: [terminal, file, github]
  disabled: []

github:
  token: "project-token"

生产级监控

日志与审计

$
# 查看最近日志 hermes logs --lines 100 # 实时监控 hermes logs --follow # 导出审计日志 hermes logs --format json --since 24h > audit.json # 查看工具调用统计 hermes stats tools --period 7d # 查看模型使用量 hermes stats models --period 30d

健康检查

$
# 完整健康检查 hermes doctor # 检查项: # ✅ Gateway 连接 # ✅ Telegram Bot 状态 # ✅ 模型可用性 # ✅ MCP 服务器可达性 # ✅ Cron 调度器运行中 # ✅ 磁盘空间 # ✅ API Key 有效性

Alerting 配置

alerts:
  # Cron 连续失败告警
  cron_failure:
    threshold: 3        # 连续失败 3 次
    action: telegram_notify
    message: "⚠️ Cron job '{job_name}' failed {count} times consecutively"

  # 模型响应超时
  model_timeout:
    threshold: 60        # 超过 60 秒无响应
    action: telegram_notify
    message: "⚠️ Model response timeout ({duration}s)"

  # API Key 余额不足
  api_balance:
    threshold: 10        # 余额低于 $10
    action: telegram_notify
    message: "⚠️ API balance low: ${balance}"
Advertisement