Model Context Protocol ?AI 领域?"USB-C"
MCP(Model Context Protocol?/strong> ?Anthropic ?2024 ?11 月发布的开源协议,旨在?AI 模型提供连接外部数据源和工具的标准接口。就?USB-C 统一了设备充电接口一样,MCP 正在统一 AI 与外部世界的连接方式?
统一的协议规范,一次实现到处可?/p>
用户授权机制,AI 不能越权访问资源
100+ 开?MCP Server,持续增长中
MIT 协议开源,任何人都可以使用和贡?/p>
MCP 通信架构
发起请求的一方,?Claude Desktop、Cursor、Claude Code ?/p>
提供服务的一方,连接具体资源如文件系统、数据库?/p>
AI 可以调用的函数,?读取文件"?执行搜索"
AI 可以访问的数据,如文件内容、数据库记录
预定义的提示词模板,简化常见任?/p>
Server ?Client 推送的实时更新
读写本地文件系统,支持文件搜索、创建、编辑、删除等操作?/p>
?GitHub 交互,管理仓库、Issue、PR,进行代码搜索等?/p>
控制浏览器,执行网页自动化、截图、爬取等任务?/p>
查询和操?SQLite 数据库,执行 SQL 语句?/p>
使用 Brave 搜索引擎进行网络搜索?/p>
读取和发?Gmail 邮件,管理邮箱?/p>
Claude Desktop ?MCP 配置文件(macOS: ~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/yourname/Documents"
]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "your_token_here"
}
},
"sqlite": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-sqlite",
"--db-path",
"/path/to/your/database.db"
]
}
}
}
Anthropic 官方桌面应用,原生支?MCP
AI 代码编辑器,支持 MCP 连接外部工具
命令?AI 助手,深度集?MCP
VS Code 插件、Zed 等持续增加中
使用 TypeScript 开发简单的 MCP Server?/p>
import { Server } from "@modelcontextprotocol/sdk/server";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";
const server = new Server({
name: "my-mcp-server",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
// 注册工具
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "hello",
description: "Say hello to someone",
inputSchema: {
type: "object",
properties: {
name: { type: "string", description: "Name to greet" }
},
required: ["name"]
}
}]
}));
// 处理工具调用
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "hello") {
const name = request.params.arguments.name;
return { content: [{ type: "text", text: `Hello, ${name}!` }] };
}
});
// 启动服务?
const transport = new StdioServerTransport();
server.connect(transport);