资源

协议版本:2025-03-26

模型上下文协议 (MCP) 提供了一种标准化的方式,让服务器向客户端公开资源。资源允许服务器共享为语言模型提供上下文的数据,例如文件、数据库架构或应用程序特定信息。每个资源由 URI 唯一标识。

用户交互模型

MCP 中的资源设计为应用程序驱动,主机应用程序根据其需要确定如何合并上下文。

例如,应用程序可以:

  • 通过 UI 元素(在树或列表视图中)公开资源以供明确选择
  • 允许用户搜索和过滤可用资源
  • 实现自动上下文包含,基于启发式或 AI 模型的选择

资源上下文选择器示例

然而,实现可以自由地通过适合其需求的任何接口模式公开资源——协议本身并不强制要求任何特定的用户交互模型。

功能

支持资源的服务器必须声明 resources 功能:

{
  "capabilities": {
    "resources": {
      "subscribe": true,
      "listChanged": true
    }
  }
}

该功能支持两个可选功能:

  • subscribe:客户端是否可以订阅以接收单个资源更改的通知。
  • listChanged:当可用资源列表更改时,服务器是否会发出通知。

subscribe 和 listChanged 都是可选的——服务器可以都不支持、支持其中一个或两者都支持:

{
  "capabilities": {
    "resources": {} // 都不支持
  }
}
{
  "capabilities": {
    "resources": {
      "subscribe": true // 仅支持订阅
    }
  }
}
{
  "capabilities": {
    "resources": {
      "listChanged": true // 仅支持列表更改通知
    }
  }
}

协议消息

列出资源

要发现可用的资源,客户端发送 resources/list 请求。此操作支持分页。

请求:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "resources/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}

响应:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "resources": [
      {
        "uri": "file:///project/src/main.rs",
        "name": "main.rs",
        "description": "Primary application entry point",
        "mimeType": "text/x-rust"
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}

读取资源

要检索资源内容,客户端发送 resources/read 请求:

请求:

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "resources/read",
  "params": {
    "uri": "file:///project/src/main.rs"
  }
}

响应:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "contents": [
      {
        "uri": "file:///project/src/main.rs",
        "mimeType": "text/x-rust",
        "text": "fn main() {\n    println!(\"Hello world!\");\n}"
      }
    ]
  }
}

资源模板

资源模板允许服务器使用 URI 模板公开参数化资源。参数可以通过完成 API自动完成。此操作支持分页。

请求:

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "resources/templates/list",
  "params": {
    "cursor": "optional-cursor-value"
  }
}

响应:

{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "resourceTemplates": [
      {
        "uriTemplate": "file:///{path}",
        "name": "Project Files",
        "description": "Access files in the project directory",
        "mimeType": "application/octet-stream"
      }
    ],
    "nextCursor": "next-page-cursor"
  }
}

列表更改通知

当可用资源列表更改时,声明了 listChanged 功能的服务器应该发送通知:

{
  "jsonrpc": "2.0",
  "method": "notifications/resources/list_changed"
}

订阅

该协议支持对资源更改的可选订阅。客户端可以订阅特定资源并在它们更改时接收通知:

订阅请求:

{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "resources/subscribe",
  "params": {
    "uri": "file:///project/src/main.rs"
  }
}

更新通知:

{
  "jsonrpc": "2.0",
  "method": "notifications/resources/updated",
  "params": {
    "uri": "file:///project/src/main.rs"
  }
}

消息流

  sequenceDiagram
    participant Client
    participant Server

    Note over Client,Server: 资源发现
    Client->>Server: resources/list
    Server-->>Client: 资源列表

    Note over Client,Server: 资源模板发现
    Client->>Server: resources/templates/list
    Server-->>Client: 资源模板列表

    Note over Client,Server: 资源访问
    Client->>Server: resources/read
    Server-->>Client: 资源内容

    Note over Client,Server: 订阅
    Client->>Server: resources/subscribe
    Server-->>Client: 订阅确认

    Note over Client,Server: 更新
    Server--)Client: notifications/resources/updated
    Client->>Server: resources/read
    Server-->>Client: 更新的内容

数据类型

资源

资源定义包括:

  • uri:资源的唯一标识符
  • name:人类可读名称
  • description:可选描述
  • mimeType:可选 MIME 类型
  • size:可选大小(以字节为单位)

资源内容

资源可以包含文本或二进制数据:

文本内容

{
  "uri": "file:///example.txt",
  "mimeType": "text/plain",
  "text": "Resource content"
}

二进制内容

{
  "uri": "file:///example.png",
  "mimeType": "image/png",
  "blob": "base64-encoded-data"
}

常见 URI 方案

该协议定义了几个标准 URI 方案。此列表并不详尽——实现始终可以自由使用额外的、自定义的 URI 方案。

https://

用于表示 Web 上可用的资源。

服务器应该仅在客户端能够自行从 Web 直接获取和加载资源时使用此方案——也就是说,它不需要通过 MCP 服务器读取资源。

对于其他用例,服务器应该更喜欢使用其他 URI 方案,或定义自定义方案,即使服务器本身将通过 Internet 下载资源内容。

file://

用于标识行为像文件系统的资源。但是,资源不需要映射到实际的物理文件系统。

MCP 服务器可以使用 XDG MIME 类型(如 inode/directory)来标识 file:// 资源,以表示否则没有标准 MIME 类型的非常规文件(如目录)。

git://

Git 版本控制集成。

错误处理

服务器应该为常见失败情况返回标准 JSON-RPC 错误:

  • 资源未找到:-32002
  • 内部错误:-32603

示例错误:

{
  "jsonrpc": "2.0",
  "id": 5,
  "error": {
    "code": -32002,
    "message": "Resource not found",
    "data": {
      "uri": "file:///nonexistent.txt"
    }
  }
}

安全注意事项

  1. 服务器必须验证所有资源 URI
  2. 应该为敏感资源实施访问控制
  3. 二进制数据必须正确编码
  4. 应该在操作之前检查资源权限