MCP:AI 应用怎么接上外部工具和数据MCP: how AI apps plug into tools and data
AI 应用想读文件、查数据库、看 Sentry。MCP 是统一插口,不用每个工具单独接。本文按 2026-07-28 版协议。
谁参与。宿主是 AI 应用,如 Claude Code、VS Code。它给每个服务端配一个客户端,管着对话,工具运行前问用户。服务端只见所需。
两层。内层数据层是消息:JSON-RPC 2.0,客户端发请求,服务端答。外层传输层是管子。
一个请求。每个请求都带协议版本和客户端能力,服务端不记会话。server/discover 查它支持什么,然后 tools/list、tools/call。
服务端提供。模型调用的工具、应用读取的资源、用户选用的提示词。
问用户。服务端不发请求,只回"需要输入";客户端问过用户后带答案重发。密码走网页,不走表单。
获知变化。客户端用 subscriptions/listen 选想听的,如"工具列表变了"。需订阅,尽力送达。
传输。本地走 stdio,客户端把它当子进程启动。远程走 Streamable HTTP:每条消息一个 POST,回 JSON 或 SSE 流。
登录可选,只用于 HTTP。服务端指明授权服务器,用户在浏览器同意,之后每个请求带令牌。
MCP 只管传上下文,怎么用归应用。
An AI app wants to read your files, query a database or check Sentry. MCP is one plug for all of them, so each tool doesn't need its own integration. This is protocol version 2026-07-28.
Who takes part. The host is the AI app, like Claude Code or VS Code. It makes one client per server, keeps the conversation and asks the user before a tool runs. Servers see only what they need.
Two layers. Inside, the data layer is the messages: JSON-RPC 2.0. The client sends requests, the server answers. Outside, the transport layer is the pipe.
One request. Every request carries the protocol version and the client's capabilities, so the server keeps no session. server/discover says what a server supports. Then tools/list and tools/call.
What servers offer. Tools the model calls, resources the app reads, prompts the user picks.
Asking the user. A server sends no requests. It answers "input required", the client asks the user, then sends the call again with the answers. Passwords go through a web page, never a form.
Hearing about changes. The client opens subscriptions/listen and picks what to hear, such as "the tool list changed". It's opt-in and best effort.
Transports. Local servers use stdio: the client starts them as child processes. Remote ones use Streamable HTTP: each message is a POST, the reply is JSON or an SSE stream.
Signing in is optional and only for HTTP. The server points to its auth server, the user approves in the browser, then every request carries the token.
MCP only moves context. How to use it is up to the app.
来源Source: modelcontextprotocol.io