3.2 KiB
Executable File
3.2 KiB
Executable File
MCP(Model Context Protocol)
定义
MCP(Model 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
最佳实践
- 最小权限:只开放必要的MCP Server
- 环境隔离:不同环境使用不同的MCP配置
- 监控日志:记录AI的MCP调用
- 定期审查:定期审查MCP配置
相关概念
- Harness工程:MCP是Harness的组成部分
- Agent_as_Code:MCP是Agent as Code的组成部分
- Skills:MCP和Skills都是AI的能力扩展方式