什么是 MCP
MCP(Model Context Protocol)是 Anthropic 于 2024 年 11 月 25 日推出的 AI 领域开放通信标准,可以理解为"AI 世界的 USB-C 接口"。它为大型语言模型(LLM)连接外部数据源和工具提供了统一、标准化的方式。在 MCP 出现之前,每个 AI 应用要连接数据库、文件系统或 API,都得自己写适配代码;有了 MCP,只需写一个 MCP Server,任何兼容 MCP 的客户端(Claude、Cursor、Trae 等)都能直接使用。
一句话概括:MCP 之于 AI 工具,就像 USB-C 之于电子设备——一个标准接口连接一切。
为什么 MCP 重要
MCP 解决的核心问题是"工具集成的 N×M 问题":N 个 AI 应用 × M 个工具,需要写 N×M 套适配代码。MCP 将其变为 N+M——应用实现一次 MCP 客户端,工具实现一个 MCP Server,即可互通。2025 年 OpenAI 宣布在 ChatGPT 桌面版中集成 MCP,标志其成为事实上的行业标准。如今,Claude Code、Cursor、Trae、Windsurf、Continue 等主流工具均支持 MCP。
# Without MCP: every app × every tool needs its own adapter
Claude ──adapter──> database
Cursor ──adapter──> database # reinventing the wheel
Trae ──adapter──> database
# With MCP: apps and tools each implement once
Claude ─┐
Cursor ─┤── MCP protocol ──< database MCP Server
Trae ─┘ filesystem MCP Server
GitHub MCP Server架构
MCP 采用客户端-服务器架构:MCP Client 嵌入在 AI 应用中(如 Claude Code),发起请求;MCP Server 是独立进程,封装具体的工具能力(如读文件、查数据库、调 API)。两者通过 JSON-RPC 通信,Server 向 Client 暴露三类能力:Tools(可执行操作)、Resources(可读数据)、Prompts(预设提示词模板)。一个 Client 可同时连接多个 Server。
# Three core MCP capabilities
Tools: # Executable operations (with side effects)
- query_database(sql)
- send_email(to, subject)
- create_issue(title)
Resources: # Readable data (read-only)
- file:///project/README.md
- database://users/schema
Prompts: # Preset prompt templates
- "Review the security of this code: {code}"
# Configure an MCP Server in Claude Code
# ~/.claude/claude_desktop_config.json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"]
}
}
}如何使用 MCP
对终端用户来说,使用 MCP 是零代码的:只需在支持 MCP 的工具(如 Claude Code、Cursor)的配置文件中添加现成的 MCP Server 即可。对开发者来说,官方 SDK(TypeScript/Python)可快速编写自定义 MCP Server,将内部系统暴露给 AI。社区已有数百个开源 MCP Server,覆盖文件系统、数据库、GitHub、Slack、浏览器等场景。
# 1. Use an off-the-shelf MCP Server (filesystem example)
# Add to the Claude Code config file:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem",
"/Users/me/projects"]
}
}
}
# 2. Configure in Cursor: Settings → MCP → Add Server
# 3. Develop your own MCP Server (Python SDK)
pip install mcp
# from mcp.server import Server
# Define your tools and expose them to all MCP clients
# 4. Common community MCP Servers
# @modelcontextprotocol/server-github # GitHub operations
# @modelcontextprotocol/server-postgres # PostgreSQL queries
# @modelcontextprotocol/server-puppeteer # Browser automation提示:MCP Server 在本地运行,数据不经过第三方,适合企业内网和隐私敏感场景。
Ready to try Model Context Protocol?
Visit the official site for the latest version and full documentation.
Visit Model Context Protocol