套用一句关于幺半群的名言:"MCP 是一种开放协议,用于标准化应用程序向 LLM 提供上下文的方式,问题何在?"但即使花数小时阅读 MCP 的定义并实操示例,仍难以清晰把握其具体运作流程:LLM 负责什么?MCP 服务器做什么?MCP 客户端的角色是什么?数据流如何流动?决策点位于何处?本文将深入解析 MCP 的本质、工作原理,并通过完整的分步示例展示各组件如何协同工作,以及每一步的具体操作。
具体而言,我们部署了一个故意包含漏洞的 Cloudflare Worker。该错误在 Sentry 中触发警报后,Visual Studio Code(VS Code)内运行的 AI 助手(Cline)通过托管的 Sentry MCP 服务器提取堆栈跟踪信息,通过本地 GitHub MCP 服务器创建对应的 GitHub 问题,修补代码,提交修复并重新部署 —— 所有操作均在人工审批下完成。MCP 将集成工作从 M×N 定制桥接模式简化为 M+N 适配器模式,但也带来了延迟、安全审查和学习曲线等成本。
当 AI 助手需要协调现实系统(如用于监控的 Sentry、代码管理的 GitHub、工单系统 Jira、数据库 Postgres 等)时,每增加一对工具集成,就需要一个定制适配器、一组令牌垫片和一处潜在故障点。最终形成的 "胶水代码" 不仅难以维护,还增加了安全风险。MCP 的诞生旨在通过统一的交互协议取代这种混乱局面,使任何兼容的主机都能直接与合规工具通信。
若没有 MCP,每个大型语言模型(LLM)主机或代理(如 ChatGPT、Claude、Cline 或 VS Code Copilot)与每个工具(如 Sentry、GitHub、Jira、MySQL 或 Stripe)之间都需要独立的连接器 —— 即 M 个主机 ×N 个工具的组合,产生大量胶水代码。每个连接器都需重新实现:
身份验证与令牌刷新
数据格式映射
错误处理与重试机制
速率限制规则
安全强化这导致成本呈二次方增长。可以预见,团队最终只能优先实现少数集成,其余则被归入 "待办事项"。
MCP 试图为 AI 工具带来类似 USB-C 的统一体验:每个主机只需实现一次 MCP 协议,每个工具只需暴露一个 MCP 服务器,任意主机与工具组合即可通信。复杂度从 M×N 骤降至 M+N。为验证这一设想,我们进行了实际测试。
在进入分步演示前,先快速了解核心概念:
若您熟悉 LSP(语言服务器协议)或 JSON-RPC,会对 MCP 感到亲切。若不熟悉,以下是 30 秒快速指南:
MCP 采用持久连接设计,远程服务器通常使用 HTTP + 服务器发送事件(SSE),本地进程则使用纯 stdio。服务器可记忆每个客户端的上下文(如身份令牌、工作目录、进行中的作业 ID)。这种设计虽比无状态 REST 协议更复杂,但支持流式差异更新、长运行作业和服务器发起的回调。
客户端调用tools/list询问服务器:"你能做什么?"
服务器返回 JSON 数据,描述每个工具的名称、摘要及参数和结果的 JSON Schema。
主机将该 JSON 注入模型上下文。
当用户提示需要执行操作时,模型生成结构化调用:
ounter(line{"name":"create_issue","arguments":{"title":"...","body":"..."}}为便于理解,设想如下场景:周五下午,你是办公室最后一人,正当收拾行李时,Slack 突然警报称worker.ts:12出现新的回归错误。我们希望找到从第一条 Slack 消息到部署修复的最短路径。
漏洞百出的 Cloudflare Worker 向 Sentry 报告异常,通过托管的 Sentry MCP(SSE)传递至 VS Code 内的 Cline。同一会话随后连接至本地运行的 GitHub MCP(stdio),允许代理在人工监督下创建问题、添加评论并推送拉取请求至 GitHub 仓库。
在终端执行:
ounter(linenpx-ywranglerinitbug-demo
按提示选择:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line╭Createan applicationwithCloudflare Step1of3│├Inwhich directory do you wanttocreateyour application?│ dir ./bug-demo│├ What would youliketostartwith?│ category Hello World example│├ Which template would youliketouse?│ type Workeronly│├ Whichlanguagedo you wanttouse?│ lang TypeScript│├ Copying template files│ files copiedtoproject directory│├ Updating namein`package.json`│ updated `package.json`│├ Installing dependencies│ installed via `npm install`│╰ Application created╭ Configuring your applicationforCloudflare Step2of3│├ Installing wrangler A command line toolforbuilding Cloudflare Workers│ installed via `npm install wrangler--save-dev`│├ Installing@cloudflare/workers-types│ installed via npm│├ Adding latest typesto`tsconfig.json`│ added@cloudflare/workers-types/2023-07-01│├ Retrievingcurrentworkerd compatibilitydate│ compatibilitydate2025-04-26│├ Do you wanttouse gitforversion control?│ yes git│├ Initializing git repo│ initialized git│├ Committingnewfiles│ gitcommit│╰ Application configured╭ DeploywithCloudflare Step3of3│├ Do you wanttodeploy your application?│nodeploy via `npm run deploy`│╰ Done
进入项目目录:
ounter(linecdbug-demo
安装 Sentry SDK:
ounter(linenpminstall@sentry/cloudflare--save
在 VS Code 中打开项目:
ounter(linecode.
编辑wrangler.jsonc,添加compatibility_flags数组:
ounter(line"compatibility_flags":["nodejs_compat"]
访问Cloudflare Worker 的 Sentry 设置指南,复制示例代码至src/index.ts,并在fetch()方法中添加故意漏洞:
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line// !mark(10:11)import*asSentryfrom"@sentry/cloudflare";exportdefaultSentry.withSentry((env) =>({dsn:"https://[SENTRY_KEY]@[SENTRY_HOSTNAME].ingest.us.sentry.io/[SENTRY_PROJECT_ID]",tracesSampleRate:1.0,}),{asyncfetch(request, env, ctx) {// ❌ intentional bugundefined.call();returnnewResponse("Hello World!");},} satisfiesExportedHandler<Env>,);
部署并触发漏洞:
ounter(linenpmrundeploy
在浏览器访问 Cloudflare Worker 地址
ounter(linehttps://bug-demo.<your-cf-hostname>.workers.dev
应看到错误:
ounter(line错误1101-Worker抛出异常Error
在 VS Code 中安装 Cline 后,执行以下步骤:
点击侧边栏的 Cline 图标。
点击 Cline 面板顶部的 "MCP Servers" 按钮。
选择 "Installed" 标签页。
点击 "Configure MCP Servers"。
粘贴以下 Sentry MCP 服务器配置 JSON 并保存(Cmd + S):
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line{"mcpServers":{"sentry":{"command":"npx","args":["-y","mcp-remote","https://mcp.sentry.dev/sse"]}}}生成 GitHub 个人访问令牌:
在 GitHub 中点击头像,进入设置。
左侧栏选择 "Developer settings" → "
ersonal access tokens" → "Fine-grained tokens"。
点击 "Generate new token",输入名称,选择 "All repositories",权限勾选:
Administration: Read and Write
Contents: Read and Write
Issues: Read and Write
Pull requests: Read and Write
在 Cline 中添加 GitHub MCP 服务器:
重复配置 Sentry MCP 的步骤 1-4。
粘贴以下配置 JSON(替换<YOUR_TOKEN>为实际令牌):
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line{"mcpServers":{"sentry":{...},"github":{"command":"docker","args":["run","-i","--rm","-e","GITHUB_PERSONAL_ACCESS_TOKEN","ghcr.io/github/github-mcp-server"],"env":{"GITHUB_PERSONAL_ACCESS_TOKEN":"<YOUR_TOKEN>"}}}}向 Cline 提问:"创建一个名为 bug-demo 的私有 GitHub 仓库"。
系统提示与工具调用Cline 向 LLM 发送包含提示、工具列表及模式的请求(提示构建逻辑见Cline 仓库 src/core/prompts/system.ts)。
LLM 生成工具调用LLM 生成结构化工具调用(XML 格式):
ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line<use_mcp_tool><server_name>github</server_name><tool_name>create_repository</tool_name><arguments>{"name":"bug-demo","private":true}</arguments></use_mcp_tool>ounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(lineounter(line{"jsonrpc":"2.0","id":"42","method":"create_repository","params":{"name":"bug-demo","private":true}}ounter(line{"jsonrpc":"2.0","id":"42","result":{"id":123456789,"name":"bug-demo","visibility":"private","default_branch":"main","git_url":"git://github.com/speakeasy/bug-demo.git","etc":"..."}}ounter(linegitremoteaddorigingit@github.com:speakeasy/bug-demo.git&&gitpush-uoriginmain
向 Cline 下达任务:
从 Sentry 获取最新问题。
创建 GitHub 问题并关联 Sentry 链接。
根据 Sentry 问题修复漏洞。
在新分支提交变更,引用两个问题。
推送分支至 GitHub。
打开拉取请求(PR)。
ounter(line{"jsonrpc":"2.0","id":"42","method":"list_issues","params":{"sortBy":"last_seen"}}响应显示漏洞位于src/index.ts第 12 行的undefined.call()。
ounter(line{"jsonrpc":"2.0","id":"44","method":"create_issue","params":{"owner":"speakeasy","repo":"bug-demo","title":"Fixbuginindex.ts","body":"关联Sentry问题:https://speakeasy.sentry.io/issues/NODE-CLOUDFLARE-WORKERS-1"}}LLM 通过read_file工具读取代码,生成修复方案(删除undefined.call())。
Cline 创建分支、提交变更并推送:
ounter(linegitcheckout-bfix-bug-NODE-CLOUDFLARE-WORKERS-1gitaddsrc/index.ts&&gitcommit-m"FixesNODE-CLOUDFLARE-WORKERS-1andcloses#1"gitpushoriginfix-bug-NODE-CLOUDFLARE-WORKERS-1
ounter(line{"jsonrpc":"2.0","id":"47","method":"create_pull_request","params":{"owner":"speakeasy","repo":"bug-demo","title":"Fixbuginindex.ts","head":"fix-bug-NODE-CLOUDFLARE-WORKERS-1","base":"main","body":"修复Sentry问题并关闭#1"}}ounter(linenpmrundeploy
验证发现漏洞已修复,Sentry 不再报告新错误。
MCP 兑现了其承诺:只需为每个服务配置一次,而非构建两两之间的定制桥接。以包含三个组件(Cline 主机、Sentry 服务器、GitHub 服务器)的演示为例:
延迟MCP 在 LLM 与 API 间引入额外层,导致轻微延迟。尽管对多数场景影响可忽略,但在延迟敏感型应用或复杂工作流中需谨慎评估。
安全
令牌管理:不同服务器需独立认证(如 Sentry 的 OAuth、GitHub 的 API 令牌)。
权限范围:用户需为每个服务器单独授权,流程繁琐。
令牌刷新:支持 OAuth 刷新的客户端较少,当前依赖mcp-remote等包装器处理。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |