Files
chill_notes/AI工程/概念/MCP.md
2026-06-22 11:30:51 +08:00

158 lines
3.2 KiB
Markdown
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# MCPModel Context Protocol
> 相关:[[Harness工程]]、[[Agent_as_Code]]、[[Skills]]
## 定义
**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配置
```json
{
"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. 数据库访问
```json
{
"mcpServers": {
"database": {
"command": "node",
"args": ["mcp-server-postgres"],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost:5432/db"
}
}
}
}
```
### 2. 文件系统访问
```json
{
"mcpServers": {
"filesystem": {
"command": "node",
"args": ["mcp-server-filesystem", "/path/to/project"]
}
}
}
```
### 3. Web搜索
```json
{
"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配置
## 相关概念
- [[Harness工程]]MCP是Harness的组成部分
- [[Agent_as_Code]]MCP是Agent as Code的组成部分
- [[Skills]]MCP和Skills都是AI的能力扩展方式