MCP 模型上下文协议
MCP(Model Context Protocol) 是 Anthropic 推出的开放协议,让 AI 助手能够连接外部数据源和工具。
这页适合作为“MCP 概念与接入入口”。很多人第一次接触 MCP 时最容易混淆的是:它不是某个具体工具,而是一套把模型、工具、数据和客户端连接起来的标准方式。
核心概念
MCP 采用客户端-服务器架构:
AI 助手(客户端) ←→ MCP 服务器 ←→ 外部服务/数据
MCP 服务器可以提供三种能力:
- Resources:向 AI 暴露数据和内容(文件、数据库记录等)
- Tools:让 AI 调用外部功能(API 调用、计算等)
- Prompts:预定义的提示词模板
Skills 和 MCP 的区别
很多人在刚接触 AI Agent 时,会把 skills 和 MCP 混在一起。最简单的理解方式是:
| 概念 | 更像什么 | 解决的问题 |
|---|---|---|
| Skills | 可复用的方法论 / 操作手册 | 告诉 Agent“该怎么做” |
| MCP | 标准化工具接口 | 让 Agent“能够做什么” |
也就是说:
- Skills 适合封装固定工作流、代码规范、发布检查单、排障步骤
- MCP 适合连接文件系统、GitHub、数据库、浏览器、Docker、SaaS API 等外部能力
- 两者通常一起使用:Skills 管流程,MCP 管执行能力
如果你想把重复任务沉淀成 SKILL.md、脚本和参考资料,可以继续看 AI Agent Skills 指南。
配置
Kiro
// .kiro/settings/mcp.json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"],
"disabled": false,
"autoApprove": ["read_file", "list_directory"],
},
},
}
Cursor
// .cursor/mcp.json
{
"mcpServers": {
"sqlite": {
"command": "uvx",
"args": ["mcp-server-sqlite", "--db-path", "./data.db"],
},
},
}
Claude Desktop
// %APPDATA%/Claude/claude_desktop_config.json (Windows)
// ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)
{
"mcpServers": {
"brave-search": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-brave-search"],
"env": {
"BRAVE_API_KEY": "your-api-key",
},
},
},
}
常用 MCP 服务器
开发工具
文件系统
{
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "./"],
},
}
Git
{
"git": {
"command": "uvx",
"args": ["mcp-server-git", "--repository", "."],
},
}
GitHub
{
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx",
},
},
}
Playwright(浏览器自动化)
{
"playwright": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-playwright"],
},
}
Context7(代码文档)
为 AI 提供最新的库文档和代码示例:
{
"context7": {
"command": "npx",
"args": ["-y", "@upstash/context7-mcp@latest"],
},
}
Sequential Thinking(思维链)
让 AI 进行结构化的多步推理:
{
"sequential-thinking": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-sequential-thinking"],
},
}
容器化 MCP(Docker)
当某个 MCP Server 依赖复杂、团队成员系统不一致,或你希望把依赖隔离在容器里时,可以直接让客户端通过 docker run 启动 MCP Server。
{
"my-dockerized-server": {
"command": "docker",
"args": ["run", "--rm", "-i", "ghcr.io/acme/my-mcp-server:latest"]
}
}
这样做的优点:
- 本机不需要额外安装一堆运行时依赖
- 团队可以共享统一镜像,降低“我这里能跑”的问题
- 更容易把访问权限、环境变量和版本固定到同一份配置中
如果你使用较新的 Docker Desktop,还可以关注官方提供的 Docker MCP Toolkit。相关内容可参考 Docker Desktop 安装与使用。
数据库
PostgreSQL
{
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://user:pass@localhost:5432/mydb",
},
},
}
Supabase
{
"supabase": {
"command": "npx",
"args": [
"-y",
"@supabase/mcp-server-supabase@latest",
"--access-token",
"your-token",
],
},
}
SQLite
{
"sqlite": {
"command": "uvx",
"args": ["mcp-server-sqlite", "--db-path", "./data.db"],
},
}
搜索与网络
Brave Search
{
"brave-search": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-brave-search"],
"env": {
"BRAVE_API_KEY": "your-key",
},
},
}
Fetch(网页抓取)
{
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"],
},
}
Firecrawl(结构化网页数据)
{
"firecrawl": {
"command": "npx",
"args": ["-y", "firecrawl-mcp"],
"env": {
"FIRECRAWL_API_KEY": "your-key",
},
},
}
生产力工具
Notion
{
"notion": {
"command": "npx",
"args": ["-y", "@notionhq/notion-mcp-server"],
"env": {
"OPENAPI_MCP_HEADERS": "{\"Authorization\":\"Bearer ntn_xxx\",\"Notion-Version\":\"2022-06-28\"}",
},
},
}
Slack
{
"slack": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-slack"],
"env": {
"SLACK_BOT_TOKEN": "xoxb-xxx",
},
},
}
知识与记忆
Memory(知识图谱)
{
"memory": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-memory"],
},
}
热门服务器排行
根据社区使用量和 GitHub Stars 排名:
| 服务器 | 用途 | 推荐度 |
|---|---|---|
| Context7 | 代码库文档查询 | ⭐⭐⭐⭐⭐ |
| Playwright | 浏览器自动化 | ⭐⭐⭐⭐⭐ |
| Sequential Thinking | 结构化推理 | ⭐⭐⭐⭐⭐ |
| GitHub | 代码仓库管理 | ⭐⭐⭐⭐ |
| Filesystem | 文件读写 | ⭐⭐⭐⭐ |
| Brave Search | 网络搜索 | ⭐⭐⭐⭐ |
| Supabase | 数据库操作 | ⭐⭐⭐⭐ |
| Notion | 知识库管理 | ⭐⭐⭐⭐ |
| Fetch | 网页内容获取 | ⭐⭐⭐ |
| Memory | 持久化记忆 | ⭐⭐⭐ |
自定义 MCP 服务器
TypeScript 实现
pnpm add @modelcontextprotocol/sdk
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "my-mcp-server",
version: "1.0.0",
});
// 定义工具
server.tool(
"get_weather",
"获取天气信息",
{
city: z.string().describe("城市名称"),
},
async ({ city }) => {
const weather = await fetchWeather(city);
return {
content: [{ type: "text", text: JSON.stringify(weather) }],
};
},
);
// 定义资源
server.resource("config", "config://app", async (uri) => ({
contents: [
{
uri: uri.href,
mimeType: "application/json",
text: JSON.stringify(config),
},
],
}));
// 启动
const transport = new StdioServerTransport();
await server.connect(transport);
Python 实现
pip install mcp
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-server")
@mcp.tool()
def get_weather(city: str) -> str:
"""获取天气信息"""
# 实现逻辑
return f"{city} 的天气:晴,25°C"
@mcp.resource("config://app")
def get_config() -> str:
"""应用配置"""
return json.dumps(config)
if __name__ == "__main__":
mcp.run()
调试
MCP Inspector
npx @modelcontextprotocol/inspector npx -y my-mcp-server
浏览器打开 Inspector 界面,可以测试工具调用和资源访问。
日志
{
"my-server": {
"command": "node",
"args": ["server.js"],
"env": {
"DEBUG": "mcp:*",
"FASTMCP_LOG_LEVEL": "DEBUG",
},
},
}
安全注意事项
- MCP 服务器以本地进程运行,拥有系统权限
- 谨慎配置
autoApprove,敏感操作应手动确认 - 不要在 MCP 配置中硬编码密钥,使用环境变量
- 审查第三方 MCP 服务器的源码
推荐上手顺序
建议按这个顺序理解和落地:
- 先理解 MCP 的客户端 / 服务器关系
- 再接一个最简单的文件系统或搜索服务器
- 再接 GitHub、数据库、浏览器等更强工具
- 再开始写自己的服务器
- 最后再做团队级配置和权限治理
常见问题
MCP 和插件 / SDK 是什么关系
可以把 MCP 理解成更偏“标准化接线方式”,而插件和 SDK 更像“具体实现和接入载体”。
为什么接了很多 MCP,代理还是不好用
通常不是工具太少,而是:
- 工具太多但没有明确用途
- 权限太大或太小
- 规则和工作流没跟上
- 客户端没有把工具调用策略管好
一开始该接哪些 MCP
大多数开发者的起步组合通常是:
- 文件系统
- GitHub
- 搜索 / Fetch
- 文档类服务
延伸阅读
参考链接
- MCP 官网 — 规范与文档
- MCP Servers — 官方服务器集合
- Docker MCP Toolkit — Docker 对 MCP 的支持
- MCP TypeScript SDK — TS SDK
- MCP Python SDK — Python SDK
- Awesome MCP Servers — 社区精选