一、核心概念

\wechat_2025-05-17_221052_917.png

  • 理解MCP核心是理解图中的Transport Layer

  • MCP 支持多种传输机制:

  • 第一种是:Stdio transport 标准运输,使用标准输入/输出进行通信,本地进程的理想选择

  • 第二种是:HTTP with SSE transport 使用 SSE 传输的 HTTP

1.Uses Server-Sent Events for server-to-client messages
2. HTTP POST for client-to-server messages
  • 所有传输都使用 JSON-RPC 2.0 来交换消息,格式说明如下:

一个典型的 JSON-RPC 请求对象通常包含以下成员:

jsonrpc: 一个指定 JSON-RPC 协议版本的字符串,例如 "2.0"。
method: 一个包含要调用的方法名称的字符串。
params: 一个结构化值(对象或数组),包含要传递给方法的参数值。如果没有参数,可以省略此成员。
id: 一个由客户端建立的标识符,可以是字符串或数字。服务器端的响应必须包含相同的 id 值。如果请求是通知(不需要响应),则不应包含此成员。

二、连接生命周期

  • 包含三个阶段:

    1. Initialization 初始化

1.Client sends initialize request with protocol version and capabilities
客户端发送具有协议版本和功能的初始化请求

2.Server responds with its protocol version and capabilities
Server 使用其协议版本和功能进行响应

3.Client sends initialized notification as acknowledgment
客户端发送初始化通知作为确认

4.Normal message exchange begins
正常消息交换开始
    1. Message exchange 消息交换

1.Request-Response: Client or server sends requests, the other responds
请求-响应 :客户端或服务器发送请求,另一个响应

2.Notifications: Either party sends one-way messages
通知 :任一方发送单向消息
    1. Termination 终止

任何一方都可以终止连接:
1.Clean shutdown via close()
通过 close() 进行干净关闭

2.Transport disconnection  传输断开
Error conditions  错误条件

三、Transport通信机制

1、MCP 使用 JSON-RPC 2.0 作为其传输格式。传输层负责将 MCP 协议消息转换为 JSON-RPC 格式进行传输,并将收到的 JSON-RPC 消息转换回 MCP 协议消息。

There are three types of JSON-RPC messages used:

Requests

{
  jsonrpc: "2.0",
  id: number | string,
  method: string,
  params?: object
}

Responses
{
  jsonrpc: "2.0",
  id: number | string,
  result?: object,
  error?: {
    code: number,
    message: string,
    data?: unknown
  }
}

Notifications
{
  jsonrpc: "2.0",
  method: string,
  params?: object
}

四、Tools 工具

  • 工具是模型上下文协议 (MCP) 中一个强大的原语,它使服务器能够向客户端公开可执行功能。通过工具,LLM 可以与外部系统交互、执行计算并在现实世界中采取行动。

  • 另外两种Resources、Promts都相对来讲没有那么常用

  • Tools的关键包括三个方面

1.Discovery: Clients can list available tools through the tools/list endpoint
发现 :客户端可以通过 tools/list 端点列出可用工具

2.nvocation: Tools are called using the tools/call endpoint, where servers perform the requested operation and return results
Invocation:使用 tools/call 端点调用工具,其中服务器执行请求的作并返回结果

3.Flexibility: Tools can range from simple calculations to complex API interactions
灵活性 :工具范围从简单的计算到复杂的 API 交互
  • Tools通过下面的结构来进行定义

{
  name: string;          // Unique identifier for the tool
  description?: string;  // Human-readable description
  inputSchema: {         // JSON Schema for the tool's parameters
    type: "object",
    properties: { ... }  // Tool-specific parameters
  },
  annotations?: {        // Optional hints about tool behavior
    title?: string;      // Human-readable title for the tool
    readOnlyHint?: boolean;    // If true, the tool does not modify its environment
    destructiveHint?: boolean; // If true, the tool may perform destructive updates
    idempotentHint?: boolean;  // If true, repeated calls with same args have no additional effect
    openWorldHint?: boolean;   // If true, tool interacts with external entities
  }
}

mcp

登陆发表评论