Hermes Agent: MCP 协议

最后更新:2026-08-31

MCP 是 AI 工具互联的 USB 接口——通过 Model Context Protocol,Hermes Agent 可以发现和调用任何 MCP 兼容的工具服务器,无限扩展能力边界。

💡 提示:MCP(Model Context Protocol)是 Anthropic 提出的开放协议,让 AI 应用和工具服务器标准化互联。Hermes 原生支持 MCP,可以连接任何 MCP 服务器。

📋 前置知识:第8课 工具与工具集、第15课 插件

1. 你将学到

编号 内容
MCP 协议概念与原理
Hermes 的 MCP 集成
MCP 服务器配置
工具发现与调用
自定义 MCP 服务器开发

2. 故事

(1) 痛点:每个 AI 工具的接口都不一样

Bob 用了 5 个 AI 工具,每个都有自己的 API 格式、认证方式、工具定义。接入新工具就要重新开发适配层。

(2) 解法:MCP 统一协议,即插即用

Alice 通过 MCP 让 Hermes 连接各种工具服务器:

BASH
# 一行配置,连接 MCP 服务器
hermes mcp add filesystem --command "npx @anthropic/mcp-filesystem /home/alice"
hermes mcp add github --command "npx @anthropic/mcp-github"

# Hermes 自动发现所有可用工具
hermes mcp tools
# 发现 15 个新工具:read_file, write_file, search_files, create_issue...

3. MCP 协议概念

(1) 什么是 MCP?

MCP(Model Context Protocol)是一种开放协议,定义了 AI 应用(客户端)与工具服务器之间的通信标准:

100%
graph LR
    A[Hermes Agent<br/>MCP Client] -->|标准协议| B[MCP Server 1<br/>Filesystem]
    A -->|标准协议| C[MCP Server 2<br/>GitHub]
    A -->|标准协议| D[MCP Server 3<br/>Database]
    A -->|标准协议| E[MCP Server N<br/>Custom]

(2) MCP 核心概念

概念 说明 类比
MCP Client 发起请求的 AI 应用 USB 主机
MCP Server 提供工具的服务器 USB 设备
Tool Server 提供的具体操作 设备功能
Resource Server 提供的数据源 设备存储
Prompt Server 提供的提示模板 设备预设

(3) MCP vs 传统 API

维度 传统 API MCP
接口格式 各自定义 统一标准
工具发现 手动文档 自动发现
认证方式 各自实现 统一方案
连接方式 HTTP/SDK stdio/SSE
生态 孤岛 互联

4. Hermes 的 MCP 集成

(1) 配置 MCP

YAML
# ~/.hermes/config.yaml
mcp:
  enabled: true
  
  # MCP 服务器列表
  servers:
    filesystem:
      command: "npx"
      args: ["@anthropic/mcp-filesystem", "/home/alice/projects"]
      
    github:
      command: "npx"
      args: ["@anthropic/mcp-github"]
      env:
        GITHUB_TOKEN: "${GITHUB_TOKEN}"
        
    database:
      command: "python"
      args: ["-m", "mcp_server_postgres"]
      env:
        DATABASE_URL: "${DATABASE_URL}"
        
    brave-search:
      command: "npx"
      args: ["@anthropic/mcp-brave-search"]
      env:
        BRAVE_API_KEY: "${BRAVE_API_KEY}"

(2) 连接管理

BASH
# 添加 MCP 服务器
hermes mcp add filesystem --command "npx @anthropic/mcp-filesystem /home/alice"

# 列出已配置的服务器
hermes mcp list

# 测试连接
hermes mcp test filesystem

# 查看服务器提供的工具
hermes mcp tools filesystem

# 移除服务器
hermes mcp remove filesystem

5. 工具发现与调用

(1) 自动发现

Hermes 连接 MCP 服务器后,自动发现所有可用工具:

BASH
hermes mcp tools

# 输出示例:
# ┌─────────────────────┬──────────────┬────────────────────────┐
# │ Tool                │ Server       │ Description            │
# ├─────────────────────┼──────────────┼────────────────────────┤
# │ read_file           │ filesystem   │ Read file contents     │
# │ write_file          │ filesystem   │ Write to file          │
# │ search_files        │ filesystem   │ Search in files        │
# │ create_issue        │ github       │ Create GitHub issue    │
# │ list_prs            │ github       │ List pull requests     │
# │ query               │ database     │ Execute SQL query      │
# │ search              │ brave-search │ Web search             │
# └─────────────────────┴──────────────┴────────────────────────┘

(2) 在对话中使用

BASH
me: 读取 README.md 的内容
Agent: [MCP: filesystem/read_file]
       # My Project...

me: 在 GitHub 创建一个 Issue
Agent: [MCP: github/create_issue]
       标题?→ "Bug: Login fails on mobile"
       ✅ Issue #42 已创建

me: 搜索最新的 AI 新闻
Agent: [MCP: brave-search/search]
       找到5条结果...

(3) 工具路由

当多个 MCP 服务器提供类似工具时,Hermes 自动路由:

YAML
mcp:
  routing:
    # 优先使用 MCP 工具
    prefer_mcp: true
    
    # 同名工具优先级
    priority:
      filesystem.read_file: 100     # MCP 版本优先
      fs_read: 50                   # 内置版本次之
      
    # 冲突解决
    conflict_resolution: "priority"  # priority / server_order / ask_user

6. 自定义 MCP 服务器开发

(1) Python MCP 服务器

PYTHON
# mcp_server_custom.py
from mcp.server import Server, Tool

server = Server("custom-tools")

@server.tool("get_weather")
async def get_weather(city: str) -> dict:
    """Get current weather for a city"""
    # 自定义逻辑
    import httpx
    response = httpx.get(f"https://api.weather.com/{city}")
    return response.json()

@server.tool("send_notification")
async def send_notification(message: str, channel: str = "default") -> dict:
    """Send a notification to a channel"""
    # 自定义通知逻辑
    return {"status": "sent", "channel": channel}

if __name__ == "__main__":
    server.run()

(2) TypeScript MCP 服务器

TYPESCRIPT
// mcp-server-custom.ts
import { Server, Tool } from "@anthropic/mcp";

const server = new Server("custom-tools");

server.tool("query_database", {
  description: "Query the database",
  parameters: {
    sql: { type: "string", required: true },
    limit: { type: "number", default: 100 }
  },
  async execute({ sql, limit }) {
    // 自定义数据库查询
    const results = await db.query(sql, { limit });
    return { rows: results };
  }
});

server.run();

(3) 注册自定义服务器

BASH
# 注册自定义 MCP 服务器
hermes mcp add custom-tools \
  --command "python mcp_server_custom.py" \
  --working-dir /path/to/server

# 或配置到 config.yaml
YAML
mcp:
  servers:
    custom-tools:
      command: "python"
      args: ["mcp_server_custom.py"]
      working_dir: "/path/to/server"
      env:
        API_KEY: "${CUSTOM_API_KEY}"

7. MCP 生态

(1) 官方 MCP 服务器

服务器 功能 安装
filesystem 文件系统操作 npx @anthropic/mcp-filesystem
github GitHub 操作 npx @anthropic/mcp-github
gitlab GitLab 操作 npx @anthropic/mcp-gitlab
postgres PostgreSQL 查询 npx @anthropic/mcp-postgres
sqlite SQLite 操作 npx @anthropic/mcp-sqlite
brave-search Web 搜索 npx @anthropic/mcp-brave-search
google-drive Google Drive npx @anthropic/mcp-google-drive
slack Slack 操作 npx @anthropic/mcp-slack

(2) 社区 MCP 服务器

社区维护了 100+ MCP 服务器,覆盖:


❓ 常见问题

Q MCP 和插件有什么区别?
A 插件是 Hermes 专用扩展,深度集成。MCP 是开放协议,任何 AI 应用都能用。MCP 更通用,插件更深度。
Q MCP 服务器会影响性能吗?
A MCP 服务器是独立进程,空闲时零消耗。调用时通过 stdio/SSE 通信,延迟通常 <50ms。
Q 能同时连接多少 MCP 服务器?
A 无硬性限制。实际使用 5-10 个服务器完全正常。每个服务器独立进程。
Q MCP 通信安全吗?
A stdio 模式下通信完全本地。SSE 模式支持 TLS。认证通过环境变量传递,不暴露在协议中。
Q 如何调试 MCP 连接问题?
A hermes mcp test <server> 测试连接,--debug 查看通信详情。
Q MCP 支持流式响应吗?
A 支持。SSE 模式下支持流式返回,适合长时间运行的工具。

📖 小节


📝 作业

  1. 基础题(难度⭐):配置一个 MCP 服务器(如 filesystem),验证工具发现和调用。
  2. 进阶题(难度⭐⭐):同时配置 3 个 MCP 服务器,在对话中使用来自不同服务器的工具。
  3. 挑战题(难度⭐⭐⭐):开发一个自定义 MCP 服务器,封装你的常用 API,注册到 Hermes 中验证完整工作流。
Web-Tutorial.com

Web-Tutorial 技术团队

由多位开发者共同维护的编程教程平台。每篇教程由对应领域的开发者编写和审核,确保内容准确可靠。如发现任何问题,欢迎向我们反馈。

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏