Skip to content

资源(Resources)

协议版本:2025-06-18

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

1. 用户交互模型

MCP 中的资源被设计为应用程序驱动的,宿主应用程序根据其需求决定如何整合上下文。

例如,应用程序可以:

  • 通过 UI 元素暴露资源以供显式选择,采用树形或列表视图
  • 允许用户搜索和过滤可用资源
  • 基于启发式算法或 AI 模型的选择实现自动上下文包含

资源上下文选择器示例

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

2. 能力声明

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

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

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

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

subscribelistChanged 都是可选的——服务器可以不支持任何一个、支持其中一个或两个都支持:

json
{
  "capabilities": {
    "resources": {} // 两个功能都不支持
  }
}
json
{
  "capabilities": {
    "resources": {
      "subscribe": true // 仅支持订阅
    }
  }
}
json
{
  "capabilities": {
    "resources": {
      "listChanged": true // 仅支持列表变更通知
    }
  }
}

3. 协议消息

3.1 列出资源

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

请求:

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

响应:

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

3.2 读取资源

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

请求:

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

响应:

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

3.3 资源模板

资源模板允许服务器使用 RFC6570 URI 模板暴露参数化资源。参数可以通过 补全 API 进行自动补全。

请求:

json
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "resources/templates/list"
}

响应:

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

3.4 列表变更通知

当可用资源列表发生变化时,声明了 listChanged 能力的服务器应该发送通知:

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

3.5 订阅

协议支持对资源变更的可选订阅。客户端可以订阅特定资源并在其发生变化时接收通知:

订阅请求:

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

更新通知:

json
{
  "jsonrpc": "2.0",
  "method": "notifications/resources/updated",
  "params": {
    "uri": "file:///project/src/main.rs",
    "title": "Rust Software Application Main File"
  }
}

4. 消息流

5. 数据类型

5.1 资源

资源定义包括:

  • uri:资源的唯一标识符
  • name:资源的名称
  • title:用于显示目的的可选人类可读资源名称
  • description:可选描述
  • mimeType:可选的 MIME 类型
  • size:可选的字节大小

5.2 资源内容

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

文本内容

json
{
  "uri": "file:///example.txt",
  "name": "example.txt",
  "title": "Example Text File",
  "mimeType": "text/plain",
  "text": "Resource content"
}

二进制内容

json
{
  "uri": "file:///example.png",
  "name": "example.png",
  "title": "Example Image",
  "mimeType": "image/png",
  "blob": "base64-encoded-data"
}

5.3 注解

资源、资源模板和内容块支持可选注解,为客户端提供如何使用或显示资源的提示:

  • audience:一个数组,指示此资源的目标受众。有效值为 "user""assistant"。例如,["user", "assistant"] 表示对两者都有用的内容。
  • priority:从 0.0 到 1.0 的数字,表示此资源的重要性。值为 1 表示"最重要"(实际上是必需的),而 0 表示"最不重要"(完全可选)。
  • lastModified:ISO 8601 格式的时间戳,表示资源最后修改的时间(例如,"2025-01-12T15:00:58Z")。

带注解的资源示例:

json
{
  "uri": "file:///project/README.md",
  "name": "README.md",
  "title": "Project Documentation",
  "mimeType": "text/markdown",
  "annotations": {
    "audience": ["user"],
    "priority": 0.8,
    "lastModified": "2025-01-12T15:00:58Z"
  }
}

客户端可以使用这些注解来:

  • 根据目标受众过滤资源
  • 优先选择要包含在上下文中的资源
  • 显示修改时间或按最近修改时间排序

6. 常见 URI 方案

协议定义了几种标准的 URI 方案。此列表并非详尽无遗——实现总是可以自由使用额外的自定义 URI 方案。

6.1 https://

用于表示网络上可用的资源。

服务器应该仅在客户端能够直接从网络上自行获取和加载资源时使用此方案——也就是说,它不需要通过 MCP 服务器读取资源。 对于其他用例,服务器应该优先使用另一种 URI 方案,或定义自定义方案,即使服务器本身将通过互联网下载资源内容。

6.2 file://

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

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

6.3 git://

Git 版本控制集成。

6.4 自定义 URI 方案

自定义 URI 方案必须符合 RFC3986 并考虑上述指导原则。

7. 错误处理

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

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

错误示例:

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

8. 安全考虑

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