MCP Server 在编程智能体中的应用
MCP 是 Anthropic 提出的开放协议,标准化了 LLM 与外部工具的交互方式。
MCP 协议核心
MCP 定义三种原语:Tools(可执行函数)、Resources(可读数据源)、Prompts(预定义提示模板)。
typescript
import { Server } from "@modelcontextprotocol/sdk/server";
const server = new Server({ name: "code-analyzer", version: "1.0.0" });
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "analyze_complexity",
description: "分析代码圈复杂度",
inputSchema: { type: "object", properties: { file_path: { type: "string" } }, required: ["file_path"] }
}]
}));通信模式
- stdio:子进程通信,适合本地工具
- SSE:HTTP 服务,适合远程访问
模式选择
编程智能体推荐 stdio 模式,避免网络延迟。