IT评测·应用市场-qidao123.com技术社区

标题: MCP协议-焦点架构 [打印本页]

作者: 万有斥力    时间: 4 天前
标题: MCP协议-焦点架构
焦点架构

了解MCP怎样毗连客户端、服务端和LLM
模型上下文协议(MCP)基于一个机动、可扩展的架构,使LLM应用步伐和集成之间的通讯无缝衔接。本文档涵盖了焦点架构组件和概念。
概述

MCP遵循客户端-服务端架构,此中:

     焦点组件

协议层

协议层处置惩罚消息框架、哀求/响应链接和高级通讯模式。
Python 示例代码
  1. class Session(BaseSession[RequestT, NotificationT, ResultT]):
  2.     async def send_request(
  3.         self,
  4.         request: RequestT,
  5.         result_type: type[Result]
  6.     ) -> Result:
  7.         """
  8.         Send request and wait for response. Raises McpError if response contains error.
  9.         """
  10.         # Request handling implementation
  11.     async def send_notification(
  12.         self,
  13.         notification: NotificationT
  14.     ) -> None:
  15.         """Send one-way notification that doesn't expect response."""
  16.         # Notification handling implementation
  17.     async def _received_request(
  18.         self,
  19.         responder: RequestResponder[ReceiveRequestT, ResultT]
  20.     ) -> None:
  21.         """Handle incoming request from other side."""
  22.         # Request handling implementation
  23.     async def _received_notification(
  24.         self,
  25.         notification: ReceiveNotificationT
  26.     ) -> None:
  27.         """Handle incoming notification from other side."""
  28.         # Notification handling implementation
复制代码
关键类包括:

传输层

传输层处置惩罚客户端和服务端之间的现实通讯。MCP支持多种传输机制:
全部传输都利用JSON-RPC 2.0来互换消息。有关模型上下文协议消息格式的详细信息,请参阅规范。
消息范例

MCP有以下主要范例的消息:
  1.             interface Request {
  2.               method: string;
  3.               params?: { ... };
  4.             }
复制代码
  1.             interface Result {
  2.               [key: string]: unknown;
  3.             }
复制代码
  1.            interface Error {
  2.                code: number;
  3.                message: string;
  4.                data?: unknown;
  5.              }
复制代码
  1.             interface Notification {
  2.               method: string;
  3.               params?: { ... };
  4.             }
复制代码
毗连生命周期

1. 初始化


  1. 1.  客户端发送`initialize`请求,包含协议版本和能力
  2. 2.  服务端响应其协议版本和能力
  3. 3.  客户端发送`initialized`通知作为确认
  4. 4.  正常消息交换开始
复制代码
2. 消息互换

初始化后,支持以下模式:

3. 终止

任一方都可以终止毗连:

错误处置惩罚

MCP定义了这些标准错误代码:
  1.     enum ErrorCode {
  2.       // 标准JSON-RPC错误代码
  3.       ParseError = -32700,
  4.       InvalidRequest = -32600,
  5.       MethodNotFound = -32601,
  6.       InvalidParams = -32602,
  7.       InternalError = -32603
  8.     }
复制代码
SDK和应用步伐可以在-32000以上定义自己的错误代码。
错误通过以下方式传播:

实现示例

以下是一个实现MCP服务端的根本示例:
Python 示例
  1. import asyncio
  2. import mcp.types as types
  3. from mcp.server import Server
  4. from mcp.server.stdio import stdio_server
  5. app = Server("example-server")
  6. @app.list_resources()
  7. async def list_resources() -> list[types.Resource]:
  8.     return [
  9.         types.Resource(
  10.             uri="example://resource",
  11.             name="Example Resource"
  12.         )
  13.     ]
  14. async def main():
  15.     async with stdio_server() as streams:
  16.         await app.run(
  17.             streams[0],
  18.             streams[1],
  19.             app.create_initialization_options()
  20.         )
  21. if __name__ == "__main__":
  22.     asyncio.run(main)
复制代码
最佳实践

传输选择

消息处置惩罚

安全注意事项

调试和监控


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4