MCP (Model Context Protocol) 是一种标准协议,允许 AI 模型与外部工具和数据源交互。
MCP 定义了 AI 模型与外部系统交互的标准方式:
| 字段 | 说明 |
|---|---|
| 名称 | 服务器标识名称 |
| 命令 | 启动命令 |
| 参数 | 命令参数 |
| 环境变量 | 环境变量配置 |
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
"env": {}
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "your-token"
}
}
}
}
| 选项 | 说明 |
|---|---|
| 自动启动 | 应用启动时自动连接 |
| 重连间隔 | 断开后重连的等待时间 |
| 超时时间 | 连接超时设置 |
连接 MCP 服务器后,可以查看提供的工具:
每个工具显示:
通过 API 调用 MCP 工具:
curl http://127.0.0.1:9090/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [
{"role": "user", "content": "读取 /tmp/test.txt 文件内容"}
],
"tools": [
{
"type": "function",
"function": {
"name": "read_file",
"description": "读取文件内容",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"}
}
}
}
}
]
}'
{
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/allowed/path"]
}
}
{
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "ghp_xxx"
}
}
}
{
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"DATABASE_URL": "postgresql://..."
}
}
}
| 状态 | 说明 |
|---|---|
| 🟢 已连接 | 服务器正常运行 |
| 🔴 已断开 | 服务器未连接 |
| 🟡 连接中 | 正在建立连接 |
查看 MCP 服务器的运行日志: