##什么是MCP?
模型上下文协议(MCP) 是一个开放标准,使OpenCode等AI助手能够与外部工具、数据库和服务进行交互。MCP由Anthropic开发,于2025年12月捐赠给Linux基金会,目前生态系统中已有超过1200个服务器可用。
###为什么使用MCP服务器?
- 扩展能力:查询数据库、管理文件、自动化浏览器等
- 标准化集成:一个协议适用于所有工具,无需自定义集成
- 安全优先:沙盒执行,明确的权限控制
- 生态繁荣:1200+服务器可用,来自官方和社区
##安装MCP服务器
###配置文件
OpenCode使用配置文件管理MCP服务器。创建或编辑 ~/.config/opencode/config.toml:
[mcp]
enabled = true
[mcp.servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/directory"]
[mcp.servers.github]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_PERSONAL_ACCESS_TOKEN = "ghp_your_token_here" }
###项目级配置
对于项目特定的服务器,在项目根目录创建 .opencode.json:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost/mydb"]
},
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"]
}
}
}
##官方参考服务器
这些由MCP团队维护,是最可靠的选择:
###文件系统服务器
访问和管理指定目录中的文件。
[mcp.servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/Users/me/projects"]
功能:
- 读取、写入和编辑文件
- 创建和删除目录
- 使用glob模式搜索文件
- 获取文件元数据和状态
###GitHub服务器
完整的GitHub API集成,用于仓库管理。
[mcp.servers.github]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_PERSONAL_ACCESS_TOKEN = "ghp_xxx" }
功能:
- 创建和管理仓库
- 处理Pull Request和Issue
- 管理分支和提交
- 访问GitHub搜索API
###Git服务器
本地Git操作,用于版本控制。
[mcp.servers.git]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-git"]
功能:
- Clone、pull、push操作
- 分支管理
- 提交历史和差异
- 状态和暂存
###PostgreSQL服务器
查询和管理PostgreSQL数据库。
[mcp.servers.postgres]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost:5432/mydb"]
功能:
- 执行SQL查询
- Schema内省
- 数据操作(INSERT、UPDATE、DELETE)
- 事务支持
###SQLite服务器
SQLite轻量级数据库操作。
[mcp.servers.sqlite]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-sqlite", "--db-path", "./data.db"]
功能:
- SQL查询执行
- Schema管理
- 数据库文件操作
- 只读或读写模式
###Memory服务器
持久化知识图谱,用于上下文保留。
[mcp.servers.memory]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-memory"]
功能:
- 存储和检索知识实体
- 创建概念之间的关系
- 跨会话持久化记忆
- 基于图的查询
###Sequential Thinking服务器
动态思维链增强推理能力。
[mcp.servers.sequential-thinking]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-sequential-thinking"]
功能:
- 多步推理链
- 假设测试
- 复杂问题分解
- 思维修正和完善
###Fetch服务器
Web API的HTTP请求能力。
[mcp.servers.fetch]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-fetch"]
功能:
- GET、POST、PUT、DELETE请求
- 自定义头部和认证
- JSON和文本响应处理
- URL内容提取
##热门社区服务器
###Playwright MCP
使用Microsoft Playwright进行浏览器自动化和测试。
[mcp.servers.playwright]
command = "npx"
args = ["-y", "@anthropic/mcp-server-playwright"]
使用场景:
- 端到端测试
- JavaScript渲染的网页抓取
- 截图捕获
- 表单自动化
- 无障碍测试
###Brave Search服务器
使用Brave Search API进行网络搜索。
[mcp.servers.brave-search]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-brave-search"]
env = { BRAVE_API_KEY = "your_api_key" }
功能:
- 网络搜索查询
- 新闻搜索
- 图片搜索
- 本地搜索结果
###Puppeteer服务器
Chrome浏览器自动化(Playwright的替代方案)。
[mcp.servers.puppeteer]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-puppeteer"]
功能:
- 无头Chrome控制
- 页面导航和交互
- PDF生成
- 性能分析
##数据库服务器
###Supabase MCP
完整的Supabase平台集成。
[mcp.servers.supabase]
command = "npx"
args = ["-y", "supabase-mcp-server"]
env = { SUPABASE_URL = "https://xxx.supabase.co", SUPABASE_KEY = "your_key" }
特性:
- 数据库查询
- 认证管理
- 存储操作
- 实时订阅
###Neo4j MCP
Neo4j图数据库操作。
[mcp.servers.neo4j]
command = "npx"
args = ["-y", "@neo4j/mcp-neo4j"]
env = { NEO4J_URI = "bolt://localhost:7687", NEO4J_USER = "neo4j", NEO4J_PASSWORD = "password" }
功能:
- Cypher查询执行
- 节点和关系管理
- 图遍历
- Schema检查
###ClickHouse MCP
ClickHouse分析数据库。
[mcp.servers.clickhouse]
command = "npx"
args = ["-y", "@clickhouse/mcp-server"]
env = { CLICKHOUSE_URL = "http://localhost:8123" }
使用场景:
- 分析查询
- 时序数据
- 日志分析
- OLAP操作
##生产力服务器
###Notion MCP
Notion工作空间集成。
[mcp.servers.notion]
command = "npx"
args = ["-y", "notion-mcp-server"]
env = { NOTION_API_KEY = "secret_xxx" }
功能:
- 读取和更新页面
- 数据库查询
- 创建新内容
- 搜索工作空间
###Linear MCP
Linear问题跟踪。
[mcp.servers.linear]
command = "npx"
args = ["-y", "@linear/mcp-server"]
env = { LINEAR_API_KEY = "lin_api_xxx" }
特性:
- 创建和更新Issue
- 管理项目和周期
- 搜索Issue
- 工作流自动化
###Slack MCP
Slack工作空间集成。
[mcp.servers.slack]
command = "npx"
args = ["-y", "slack-mcp-server"]
env = { SLACK_BOT_TOKEN = "xoxb-xxx" }
功能:
- 发送消息
- 读取频道历史
- 管理频道
- 用户查询
##云与DevOps服务器
###AWS MCP
Amazon Web Services集成。
[mcp.servers.aws]
command = "npx"
args = ["-y", "aws-mcp-server"]
env = { AWS_ACCESS_KEY_ID = "xxx", AWS_SECRET_ACCESS_KEY = "xxx", AWS_REGION = "us-east-1" }
支持的服务:
- S3(存储)
- Lambda(函数)
- EC2(计算)
- DynamoDB(数据库)
- CloudWatch(监控)
###Docker MCP
Docker容器管理。
[mcp.servers.docker]
command = "npx"
args = ["-y", "docker-mcp-server"]
功能:
- 列出和管理容器
- 构建和推送镜像
- 网络管理
- 卷操作
###Kubernetes MCP
Kubernetes集群操作。
[mcp.servers.kubernetes]
command = "npx"
args = ["-y", "kubernetes-mcp-server"]
特性:
- Pod管理
- Deployment操作
- Service配置
- 日志检索
##使用Python服务器
一些MCP服务器使用Python编写,使用 uvx 而不是 npx:
[mcp.servers.python-example]
command = "uvx"
args = ["mcp-server-example"]
确保已安装 uv:
# 安装uv(Python包管理器)
curl -LsSf https://astral.sh/uv/install.sh | sh
##示例:完整开发环境配置
以下是全栈开发的完整配置:
[mcp]
enabled = true
# 项目文件系统访问
[mcp.servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/Users/me/projects"]
# GitHub集成
[mcp.servers.github]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_PERSONAL_ACCESS_TOKEN = "${GITHUB_TOKEN}" }
# PostgreSQL数据库
[mcp.servers.postgres]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-postgres", "${DATABASE_URL}"]
# 浏览器自动化测试
[mcp.servers.playwright]
command = "npx"
args = ["-y", "@anthropic/mcp-server-playwright"]
# 网络搜索能力
[mcp.servers.brave-search]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-brave-search"]
env = { BRAVE_API_KEY = "${BRAVE_API_KEY}" }
# 知识记忆
[mcp.servers.memory]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-memory"]
##安全最佳实践
###1. 使用环境变量
永远不要在配置文件中硬编码凭证:
# 添加到 ~/.bashrc 或 ~/.zshrc
export GITHUB_TOKEN="ghp_..."
export DATABASE_URL="postgresql://..."
export BRAVE_API_KEY="..."
然后在配置中引用:
env = { GITHUB_PERSONAL_ACCESS_TOKEN = "${GITHUB_TOKEN}" }
###2. 限制文件系统访问
只授予必要目录的访问权限:
# 好 - 特定项目目录
args = ["-y", "@modelcontextprotocol/server-filesystem", "/Users/me/myproject"]
# 避免 - 整个主目录
args = ["-y", "@modelcontextprotocol/server-filesystem", "/Users/me"]
###3. 审计服务器权限
安装前检查每个服务器可以访问什么:
# 检查npm包内容
npm info @modelcontextprotocol/server-github
# 在GitHub上查看源代码
# https://github.com/modelcontextprotocol/servers
###4. 尽可能使用只读
对于数据库,不需要写入时使用只读连接:
[mcp.servers.postgres-readonly]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-postgres", "postgresql://readonly_user:pass@host/db"]
##故障排除
###服务器无法启动
Error: MCP server 'github' failed to start
解决方案:
- 检查npm/npx已安装并在PATH中
- 验证包名是否正确
- 检查环境变量是否设置
- 尝试手动运行命令查看错误
###认证错误
Error: GitHub API authentication failed
解决方案:
- 验证令牌具有所需的scope(repo、read:org)
- 检查令牌是否过期
- 确保环境变量已导出
- 尝试重新生成令牌
###连接超时
Error: Database connection timeout
解决方案:
- 验证数据库服务器正在运行
- 检查防火墙规则
- 确认连接字符串正确
- 使用
psql或其他客户端测试连接
##资源
- 官方MCP服务器:github.com/modelcontextprotocol/servers
- Awesome MCP Servers:github.com/wong2/awesome-mcp-servers
- MCP规范:spec.modelcontextprotocol.io
- MCP服务器目录:mcp-awesome.com(1200+服务器)