> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 工具

模型上下文协议 (MCP) 允许服务器暴露工具，以便由语言模型调用。工具使模型能够与外部系统交互，例如查询数据库、调用 API 或执行计算。每个工具都由一个名称唯一标识，并包含描述其模式的元数据。

## 用户交互模型

MCP 中的工具设计为**模型控制**，这意味着语言模型可以根据其上下文理解和用户的提示自动发现和调用工具。

然而，实现可以自由地通过任何适合其需求的界面模式暴露工具—协议本身不强制任何特定的用户交互模型。

<Warning>
  为了信任、安全和安保，**应该**始终有人类参与循环，能够拒绝工具调用。

  应用程序**应该**：

  * 提供 UI，明确哪些工具被暴露给 AI 模型
  * 在调用工具时插入清晰的视觉指示
  * 向用户呈现操作确认提示，以确保人类参与循环
</Warning>

## 能力

支持工具的服务器**必须**声明 `tools` 能力：

```json theme={null}
{
  "capabilities": {
    "tools": {
      "listChanged": true
    }
  }
}
```

`listChanged` 指示服务器是否会在可用工具列表更改时发出通知。

## 协议消息

### 列出工具

要发现可用工具，客户端发送 `tools/list` 请求。此操作支持 [分页](/specification/2025-03-26/server/utilities/pagination)。

**请求：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}
```

**响应：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "tools": [
      {
        "name": "get_weather",
        "description": "Get current weather information for a location",
        "inputSchema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "City name or zip code"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}
```

### 调用工具

要调用工具，客户端发送 `tools/call` 请求：

**请求：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "get_weather",
    "arguments": {
      "location": "New York"
    }
  }
}
```

**响应：**

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Current weather in New York:\nTemperature: 72°F\nConditions: Partly cloudy"
      }
    ],
    "isError": false
  }
}
```

### 列表更改通知

当可用工具列表更改时，声明了 `listChanged` 能力的服务器**应该**发送通知：

```json theme={null}
{
  "jsonrpc": "2.0",
  "method": "notifications/tools/list_changed"
}
```

## 消息流

```mermaid theme={null}
sequenceDiagram
    participant LLM
    participant Client
    participant Server

    Note over Client,Server: Discovery
    Client->>Server: tools/list
    Server-->>Client: List of tools

    Note over Client,LLM: Tool Selection
    LLM->>Client: Select tool to use

    Note over Client,Server: Invocation
    Client->>Server: tools/call
    Server-->>Client: Tool result
    Client->>LLM: Process result

    Note over Client,Server: Updates
    Server--)Client: tools/list_changed
    Client->>Server: tools/list
    Server-->>Client: Updated tools
```

## 数据类型

### 工具

工具定义包括：

* `name`: 工具的唯一标识符
* `description`: 功能的可读描述
* `inputSchema`: 定义预期参数的 JSON Schema
* `annotations`: 描述工具行为的可选属性

<Warning>
  为了信任、安全和安保，客户端**必须**认为工具注释是不可信的，除非它们来自受信任的服务器。
</Warning>

### 工具结果

工具结果可以包含不同类型的多个内容项：

#### 文本内容

```json theme={null}
{
  "type": "text",
  "text": "Tool result text"
}
```

#### 图像内容

```json theme={null}
{
  "type": "image",
  "data": "base64-encoded-data",
  "mimeType": "image/png"
}
```

#### 音频内容

```json theme={null}
{
  "type": "audio",
  "data": "base64-encoded-audio-data",
  "mimeType": "audio/wav"
}
```

#### 嵌入资源

[资源](/specification/2025-03-26/server/resources) **可以** 被嵌入，以提供额外的上下文或数据，位于一个 URI 之后，客户端可以订阅该 URI 或稍后再次获取：

```json theme={null}
{
  "type": "resource",
  "resource": {
    "uri": "resource://example",
    "mimeType": "text/plain",
    "text": "Resource content"
  }
}
```

## 错误处理

工具使用两种错误报告机制：

1. **协议错误**：标准 JSON-RPC 错误，用于此类问题：
   * 未知工具
   * 无效参数
   * 服务器错误

2. **工具执行错误**：在工具结果中报告，`isError: true`：
   * API 失败
   * 无效输入数据
   * 业务逻辑错误

协议错误示例：

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 3,
  "error": {
    "code": -32602,
    "message": "Unknown tool: invalid_tool_name"
  }
}
```

工具执行错误示例：

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": 4,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "Failed to fetch weather data: API rate limit exceeded"
      }
    ],
    "isError": true
  }
}
```

## 安全考量

1. 服务器**必须**：
   * 验证所有工具输入
   * 实施适当的访问控制
   * 限制工具调用速率
   * 清理工具输出

2. 客户端**应该**：
   * 在敏感操作上提示用户确认
   * 在调用服务器前向用户显示工具输入，以避免恶意或意外的数据泄露
   * 在传递给 LLM 之前验证工具结果
   * 为工具调用实施超时
   * 记录工具使用情况以用于审计目的
