
MCP 协议解析
来源标题:MCP Explained
这篇文章解释 MCP(模型上下文协议)如何让 AI 客户端与外部工具服务器通信。内容涉及请求、结果、错误、通知等消息类型,以及 host、client、server 等角色分工。
以下正文由 AI 翻译自 Hacker News(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。
Log
(() => {
const $ = (s, r = document) => r.querySelector(s);
const el = (tag, attrs = {}, html) => { const e = document.createElement(tag); for (const k in attrs) e.setAttribute(k, attrs[k]); if (html != null) e.innerHTML = html; return e; };
const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&", "": ">", '"': """ })[c]);
const NS = "http://www.w3.org/2000/svg";
const svgEl = (tag, attrs = {}) => { const e = document.createElementNS(NS, tag); for (const k in attrs) e.setAttribute(k, attrs[k]); return e; };
const ICONS = {
host: '',
llm: '',
user: '',
client: '',
server: '',
key: '',
gateway: '',
code: '',
};
const KIND = { req: "Request", res: "Result", err: "Error", note: "Notification", http: "Transport", host: "In the host" };
const methodOf = (m) => { const j = m.json || {}; if (Array.isArray(j)) return "batch [" + j.length + "]"; if (m.kind === "req" && Array.isArray(j.parallel)) return "tools/call × " + j.parallel.length; if (j.method) return j.method === "tools/call" && j.params && j.params.name ? "tools/call " + j.params.name : j.method; if (m.kind === "res") return "result"; if (m.kind === "err") return j.error ? "error " + j.error.code : (j.status || "error").split(" ")[0]; if (m.kind === "http") return j.request ? j.request.split(" ")[0] : j.status ? j.status.split(" ")[0] : j.transport || "http"; if (j.batch) return "batch"; return j.type || j.host || j.tool || "host"; };
// ── 等距模型
const C30 = 0.866, S30 = 0.5;
const P = (x, y, z) => [(x - y) * C30, (x + y) * S30 - z];
const pts = (a) => a.map((p) => p[0].toFixed(1) + "," + p[1].toFixed(1)).join(" ");
const poly = (a, cls) => ; const mtx = (a, b, c, d, o) => `matrix(${a},${b},${c},${d},${o[0].toFixed(1)},${o[1].toFixed(1)})`; // 面:L = 左前方(y = y+d),R = 右前方(x = x+w),T = 顶面。局部坐标 (u, v):u 向右,v 向下。 const faceL = (b, inner) => `${inner}`; const faceR = (b, inner) => `${inner}`; const faceT = (b, inner) => `${inner}`; function box(x, y, z, w, d, h, extra = "") { const b = { x, y, z, w, d, h }; const t = [P(x, y, z + h), P(x + w, y, z + h), P(x + w, y + d, z + h), P(x, y + d, z + h)]; const r = [P(x + w, y, z), P(x + w, y + d, z), P(x + w, y + d, z + h), P(x + w, y, z + h)]; const l = [P(x, y + d, z), P(x + w, y + d, z), P(x + w, y + d, z + h), P(x, y + d, z + h)]; b.svg = `${poly(l, "fl")}${poly(r, "fr")}${poly(t, "ft")}`; return b; const shadow = (rx) => ;
const MODELS = {
server(a) {
const w = 46, n = 3, h = 13, g = 4; let o = shadow(46);
for (let i = 0; i ) + faceR(b, ``); return o; tile(a) { const w = 48, h = 12, g = 4; let o = shadow(46); for (let i = 0; i );
if (i === 1) o += faceT(b, ${esc(a.mono || "")});
return o;
host() {
let o = shadow(52);
const base = box(-28, -22, 0, 56, 44, 5);
o += base.svg + faceT(base, ); const scr = box(-28, -22, 5, 3, 44, 38); o += scr.svg + faceR(scr, );
return o;
client() {
let o = shadow(32);
const b = box(-17, -17, 0, 34, 34, 22);
o += b.svg + faceL(b, ) + faceR(b, );
o += box(-3, -10, 22, 5, 5, 11, "pin").svg + box(-3, 5, 22, 5, 5, 11, "pin").svg;
return o;
llm() {
let o = shadow(50);
const pins = [];
for (let i = -20; i b.x b.svg).join("");
const base = box(-26, -26, 0, 52, 52, 8);
o += base.svg;
o += pins.filter((b) => !(b.x b.svg).join("");
const die = box(-15, -15, 8, 30, 30, 6);
o += die.svg + faceT(die, ); return o; user() { return shadow(24) + ;
browser() {
let o = shadow(42);
const b = box(-32, -3, 10, 64, 4, 46);
o += b.svg + faceL(b, ); return o; key() { let o = shadow(42); const b = box(-24, -24, 0, 48, 48, 46); o += b.svg + faceL(b, ) + faceR(b, ); return o; gateway() { let o = shadow(60); const w = 66; for (let i = 0; i `); return o; code() { let o = shadow(52); const b = box(-29, -29, 0, 58, 58, 32); o += b.svg + faceT(b, ) + faceL(b, { ts }) + faceR(b, ``);
return o;
};
// 节点几何:连线用的模型中心 (ex, ey),标签在下方,区域用 bbox
const textW = (s, px, mono) => String(s).length * px * (mono ? 0.6 : 0.52);
function layoutActor(a) {
const kind = a.icon === "server" ? "server" : a.icon;
const mdl = (MODELS[kind] || MODELS.server)(a);
const K = 1.4;
const [front, top0] = { server: [30, -70], tile: [30, -52], host: [33, -68], client: [22, -40], llm: [32, -34], user: [16, -61], browser: [28, -74], key: [28, -70], gateway: [38, -69], code: [34, -61] }[kind] || [30, -70];
const half = K * ({ server: 41, tile: 43, host: 44, client: 31, llm: 52, user: 25, browser: 42, key: 42, gateway: 58, code: 51 }[kind] || 42);
const mh = (front - top0) * K, total = mh + 46;
const groundY = a.y - total / 2 - top0 * K;
const labelY = groundY + front * K + 22, subY = labelY + 19;
const lw = Math.max(half * 2, textW(a.label, 19), textW(a.sub, 13.5, true));
return Object.assign(a, { _k: K, _m: mdl, _gy: groundY, _ly: labelY, _sy: subY, ex: a.x, ey: groundY + (front + top0) / 2 * K, hw: half + 4, hh: mh / 2 + 4,
bx0: a.x - lw / 2, bx1: a.x + lw / 2, by0: a.y - total / 2, by1: subY + 6 });
// ── 状态
const st = { version: SPEC, lesson: LESSONS[0], idx: -1, playing: false, speed: 1, view: "network", group: "all", selected: null, done: new Set(), seen: new Set(), timer: null };
const vIdx = (v) => VERSIONS.findIndex(([x]) => x === v);
const atVersion = (l, ver) => {
if (ver === SPEC) return l;
const since = TM_SINCE[l.id];
if (since && vIdx(ver) { l = atVersion(l, st.version); const vs = l.variants || [], v = vs.find((x) => x.id === vid) || vs[0]; return v ? Object.assign({}, l, v, { id: l.id, vid: v.id }) : l; };
const variantOf = {};
// 攻击面:在每节课第一条消息处给出轻提示,标注该攻击可能从哪里开始。
const RISKS = {
poisoning: { title: "工具投毒", text: "工具描述会直接进入模型的上下文。恶意服务器可以在描述里藏指令,或者在你批准之后偷偷改掉它。" },
(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。