Files
2026-06-22 11:30:51 +08:00

3.2 KiB
Executable File
Raw Permalink Blame History

MCPModel Context Protocol

相关:Harness工程Agent_as_CodeSkills

定义

MCPModel Context Protocol是AI与外部系统通信的标准协议由Anthropic提出。

核心思想标准化接口、双向通信、即插即用让AI可以访问外部工具。

核心特征

1. 标准化接口

  • 统一的协议规范
  • 跨工具兼容
  • 易于扩展

2. 双向通信

  • AI可以调用外部工具
  • 外部工具可以返回结果
  • 支持流式通信

3. 即插即用

  • 配置即可使用
  • 无需修改AI代码
  • 支持热插拔

架构

Client-Server架构

AI工具Client
    ↓
MCP协议
    ↓
MCP Server外部工具
    ↓
数据库/文件系统/API等

常用MCP Server

类型 Server 用途
数据库 mcp-server-sqlite SQLite数据库访问
数据库 mcp-server-postgres PostgreSQL数据库访问
文件系统 mcp-server-filesystem 文件系统访问
Web搜索 mcp-server-brave-search Brave搜索
GitHub mcp-server-github GitHub API访问
浏览器 mcp-server-playwright 浏览器自动化

配置示例

Claude Code配置

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["mcp-server-sqlite"]
    },
    "filesystem": {
      "command": "node",
      "args": ["mcp-server-filesystem", "/path/to/dir"]
    },
    "brave-search": {
      "command": "node",
      "args": ["mcp-server-brave-search"],
      "env": {
        "BRAVE_API_KEY": "your-api-key"
      }
    }
  }
}

配置文件位置

  • Claude Code.claude/mcp.json
  • Cursor.cursor/mcp.json
  • OpenCode.opencode/mcp.json

使用场景

1. 数据库访问

{
  "mcpServers": {
    "database": {
      "command": "node",
      "args": ["mcp-server-postgres"],
      "env": {
        "DATABASE_URL": "postgresql://user:pass@localhost:5432/db"
      }
    }
  }
}

2. 文件系统访问

{
  "mcpServers": {
    "filesystem": {
      "command": "node",
      "args": ["mcp-server-filesystem", "/path/to/project"]
    }
  }
}

3. Web搜索

{
  "mcpServers": {
    "brave-search": {
      "command": "node",
      "args": ["mcp-server-brave-search"],
      "env": {
        "BRAVE_API_KEY": "your-api-key"
      }
    }
  }
}

适用场景

  • 数据库访问让AI直接查询数据库
  • 文件系统访问让AI读写文件
  • API调用让AI调用外部API
  • 浏览器自动化让AI操作浏览器

优势

  • 标准化:统一的协议规范
  • 易用性:配置即可使用
  • 可扩展支持自定义Server
  • 跨工具多个AI工具可以共用

挑战

  • 安全性需要控制AI的访问权限
  • 性能可能影响AI响应速度
  • 维护成本需要维护MCP Server

最佳实践

  1. 最小权限只开放必要的MCP Server
  2. 环境隔离不同环境使用不同的MCP配置
  3. 监控日志记录AI的MCP调用
  4. 定期审查定期审查MCP配置

相关概念