
模型上下文协议 (MCP) 快速入门
最近爆火的MCP【模型上下文协议】快速入门
文章目录
前言
当大型语言模型首次出现时,用户必须将代码复制并粘贴到文本界面才能与其交互。这很快就被证明是不够的,因此需要自定义集成以更好地加载上下文。然而,这些集成是分散的,需要单独开发。模型上下文协议(MCP, Model Context Protocol)
通过提供通用协议来解决此问题,以便与本地和远程资源进行高效的 AI 交互。
原文:Model Context Protocol (MCP) Quickstart
开源的MCP服务器:awesome-mcp-servers
1. 协议
该协议的高层概述:
- 主机
(host)
是发起连接的 LLM 应用程序(如 Claude Desktop 或 IDE); - 客户端
(client)
维护与主机应用程序内的服务器的连接; - 服务器
(server)
向客户端提供上下文、工具和提示。
1.1 Server
如果您已经熟悉工具的使用,那么 MCP 也会让你感到熟悉。我们来看一个服务器实现示例:Brave Search 服务器。
在内部,服务器实现定义了它可以访问的工具,并将其可用性传达给客户端。
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [WEB_SEARCH_TOOL, LOCAL_SEARCH_TOOL],
}));
这里WEB_SEARCH_TOOL
是描述可用工具的对象,例如:
{
name: "brave_web_search",
description:
"Performs a web search using the Brave Search API, ideal for general queries, news, articles, and online content. " +
"Use this for broad information gathering, recent events, or when you need diverse web sources. " +
"Supports pagination, content filtering, and freshness controls. " +
"Maximum 20 results per request, with offset for pagination. ",
inputSchema: {
type: "object",
properties: {
query: {
type: "string",
description: "Search query (max 400 chars, 50 words)"
},
count: {
type: "number",
description: "Number of results (1-20, default 10)",
default: 10
},
offset: {
type: "number",
description: "Pagination offset (max 9, default 0)",
default: 0
},
},
required: ["query"],
},
}
这与Anthropic Claude Tool Use API
和 OpenAI Function Calling
的定义基本相同,然后它会公开一个请求处理程序:
server.setRequestHandler(CallToolRequestSchema, async (request) => {
// ...
});
请求处理程序将传入的请求与工具进行匹配,然后调用与该工具相关联的任何函数。
case "brave_web_search": {
if (!isBraveWebSearchArgs(args)) {
throw new Error("Invalid arguments for brave_web_search");
}
const { query, count = 10 } = args;
const results = await performWebSearch(query, count);
return {
content: [{ type: "text", text: results }],
isError: false,
};
}
每个MPC
服务器都有一个URI
(例如tool://brave_web_search/brave_web_search
),作为单独的进程运行,并通过JSON-RPC
与客户端通信。例如,你可以通过以下命令启动Brave Search
服务器:
export BRAVE_API_KEY=your-api-key
npx -y @modelcontextprotocol/server-brave-search
你可以通过注册 Brave 账户获得免费的
Brave Search API
密钥。
1.2 Client
客户端需要知道它可以连接到哪个服务器。
const transport = new StdioClientTransport({
command: "tool://brave_web_search/brave_web_search"
});
然后,它需要连接到服务器:
await client.connect(transport);
连接的目的是通过协议协商功能。连接期间:
- 客户端发送带有协议版本和功能的初始化请求;
- 服务器以其协议版本和功能进行响应;
- 客户端发送初始化通知作为确认;
- 开始正常信息交换。
最后,客户端可以向服务器发送消息:
const resources = await client.request(
{ method: "brave_web_search" },
BraveWebSearchResultSchema
);
协议定义了4种类型的消息:
- 请求
(Request)
:需要响应的消息; - 通知
(Notification)
:不需要回复的消息; - 结果
(Result)
:成功响应请求; - 错误
(Error)
:请求响应失败。
1.3 Transport
MCP
支持多种传输机制:
- 标准传输:
- 使用标准输入/输出进行通信;
- 非常适合本地流程。
- 带有
SSE
传输的HTTP
:- 使用服务器发送事件来发送服务器到客户端的消息;
- 客户端到服务器消息的
HTTP POST
。
所有传输都使用JSON-RPC 2.0
来交换消息。
1.4 Host
主机是启动与服务器连接的应用程序(例如Claude Desktop
),负责发现服务器的功能,并规划如何利用它们来解决最终用户的问题。这部分协议相当模糊。不过,据我所知,期望是主机实现自定义的路由逻辑。这种实现的示例可以看这篇文章:Implementing Tool Functionality in Conversational AI
2. 开源组件
2.1 框架
这些是高级框架,可以更轻松地构建MCP
服务器:
FastMCP (Python)
:https://github.com/jlowin/fastmcpFastMCP (TypeScript)
:https://github.com/punkpeye/fastmcp
2.2 SDKs
TypeScript
:https://github.com/modelcontextprotocol/typescript-sdkPython
:https://github.com/modelcontextprotocol/python-sdk
2.3 生态系统
Inspector
:用于测试和调试MCP
服务器的开发工具 https://github.com/modelcontextprotocol/inspectorSpecification
:MCP
协议的规范 https://github.com/modelcontextprotocol/specification
2.4 服务器
Filesystem
:通过可配置的访问控制来保护文件操作 https://github.com/modelcontextprotocol/servers/tree/main/src/filesystemGitHub
:存储库管理、文件操作和 GitHub API 集成 https://github.com/modelcontextprotocol/servers/tree/main/src/githubGoogle Drive
:Google Drive 的文件访问和搜索功能 https://github.com/modelcontextprotocol/servers/tree/main/src/gdrivePostgreSQL
:带有架构检查的只读数据库访问 https://github.com/modelcontextprotocol/servers/tree/main/src/postgresSlack
:频道管理和消息传递功能 https://github.com/modelcontextprotocol/servers/tree/main/src/slackMemory
:基于知识图谱的持久记忆系统 https://github.com/modelcontextprotocol/servers/tree/main/src/memoryPuppeteer
:浏览器自动化和网页抓取 https://github.com/modelcontextprotocol/servers/tree/main/src/puppeteerBrave Search
:使用 Brave 的搜索 API 进行网络和本地搜索 https://github.com/modelcontextprotocol/servers/tree/main/src/brave-searchGoogle Maps
:定位服务、路线和地点详情 https://github.com/modelcontextprotocol/servers/tree/main/src/google-mapsFetch
:获取和转换 Web 内容,实现 LLM 的有效使用 https://github.com/modelcontextprotocol/servers/tree/main/src/fetch
2.5 社区服务器
MCP YouTube
:用于yt-dlp
从 YouTube 下载字幕 https://github.com/anaisbetts/mcp-youtubemcp-security-scanner
:使用 MCP 插件构建的安全漏洞扫描器,用于分析 JavaScript 代码 https://github.com/DMontgomery40/mcp-security-scannerCloudflare MCP Server
:这是一个用于与 Cloudflare 服务交互的模型上下文协议 (MCP) 服务器。它提供了用于管理 Cloudflare KV Store、R2 Storage、D1 Database、Workers 和 Analytics 的统一界面 https://github.com/cloudflare/mcp-server-cloudflaremcp-get
:用于安装和管理模型上下文协议 (MCP) 服务器的命令行工具 https://github.com/michaellatman/mcp-get
我们现在有基于 Web 的 MCP 服务器目录,网址为https://glama.ai/mcp/servers
该列表的未来更新将发布至https://github.com/punkpeye/awesome-mcp-servers
3. 早期采用和用例
一些组织和工具已经采用了MCP
:
Sourcegraph Cody
:增强编码任务的上下文检索 https://sourcegraph.com/blog/cody-supports-anthropic-model-context-protocolZed Editor
:通过 MCP 扩展开发人员的功能 https://zed.dev/blog/mcp
3.1 Sourcegraph Cody
摘自Cody 博客文章:
以下是 Cody 查看数据库模式后连接到 Postgres 数据库以编写 Prisma 查询的示例:
3.2 Zed Editor
摘自Zed Editor 博客文章:
下面是使用 PostgreSQL 的 Zed 扩展的示例,它可以立即向模型告知我们的整个数据库模式,因此当我们要求它为我们编写查询时,它会确切地知道存在哪些表和列,以及它们的类型是什么。
4. 使用 Claude Desktop 测试 MCP
Claude Desktop app是测试 MCP 最简单的方法。在文本编辑器中打开 Claude Desktop app
的配置文件~/Library/Application Support/Claude/claude_desktop_config.json
,如果不存在需要手动创建。特别注意,不是config.json
文件!添加配置内容:
{
"mcpServers": {
"brave_search": {
"command": "npx",
"args": ["@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": "your-api-key"
}
}
}
}
这告诉 Claude Desktop:
- 有一个名为
brave_search
MCP 服务 - 使用
npx @modelcontextprotocol/server-brave-search
启动它
保存文件并重新启动 Claude Desktop。现在,你可以让 Claude Desktop在网上搜索某些内容。例如,尝试让 Claude Desktop在网络上搜索 glama.ai,"search the web for glama.ai"
。首先会提示你允许 Claude Desktop 使用 MCP:
单击"Deny"
以允许 Claude Desktop 使用 MCP。然后,你将看到 Claude Desktop 使用 Brave Search MCP 服务器在网络上搜索"glama.ai"
并获取结果。
更多推荐
所有评论(0)