模型上下文协议 (MCP) 之基本原理
本文翻译自 MCP 官方文档。 核心架构 模型上下文协议 (MCP) 构建在一个灵活、可扩展的架构之上,旨在实现 LLM 应用和集成之间的无缝通信。本文档涵盖了其核心架构组件和概念。 概述 MCP 遵循客户端-服务器架构,其中: 主机 (Hosts) 是发起连接的 LLM 应用(例如 Claude Desktop 或 IDE)。 客户端 (Clients) 在主机应用内部,与服务器保持 1:1 连接。 服务器 (Servers) 向客户端提供上下文、工具和提示。 核心组件 协议层 协议层处理消息分帧、请求/响应关联以及高级通信模式。 class Session(BaseSession[RequestT, NotificationT, ResultT]): async def send_request( self, request: RequestT, result_type: type[Result] ) -> Result: """ Send request and wait for response. Raises McpError if response contains error. """ # Request handling implementation async def send_notification( self, notification: NotificationT ) -> None: """Send one-way notification that doesn't expect response....