BrainBank
AI 课堂/MCPClaude Code Deep Dive

ReadMcpResourceTool:读取 MCP 资源

2026/8/2 18:24:06 · 来源

#mcp#tool#resource-reading#binary-resources

`ReadMcpResourceTool` enables remote reading of both text and binary resources via the Model Context Protocol (MCP) using an SDK-based request mechanism.

第 1 段,共 13 段

它是 MCP 世界里的远程 Read

ReadMcpResourceTool 的职责非常明确:
给定 server + uri,把远程 MCP 资源正文读回来。

关键源码

输入定义:

export const inputSchema = z.object({
  server: z.string().describe('The MCP server name'),
  uri: z.string().describe('The resource URI to read'),
})

实际读取走 MCP SDK:

const result = await connectedClient.client.request(
  {
    method: 'resources/read',
    params: { uri },
  },
  ReadResourceResultSchema,
)

二进制资源也能处理

源码里有一段很关键:

if (!('blob' in c) || typeof c.blob !== 'string') { ... }
const persisted = await persistBinaryContent(...)

这说明它不仅能读文本资源,也能读二进制 blob,并把内容保存到磁盘路径再返回。

调用链

模型已拿到 MCP 资源 URI > ReadMcpResourceTool > 确认 server 已连接且支持 resources

发起 resources/read 请求 > 返回 text 或 blob > 主线程继续理解内容

小结

ReadMcpResourceTool 把外部上下文读入主循环,是 MCP 集成真正落地的一环。

🎙 朗读音色: Serena

学习地图

Prerequisites

  • Basic understanding of Model Context Protocol (MCP)
  • Familiarity with HTTP requests and API usage

Core Concepts

  1. Resource URIs
    • How MCP resource paths are structured
  2. SDK Integration
    • Using connectedClient.client.request to access resources
  3. Text vs Binary Handling
    • Distinguishing between text and binary content in response payloads

Hands-On Practice

  1. Set up an MCP server connection with your client SDK.
  2. Execute a test request using the resource URI format.
  3. Analyze both text and binary responses from the API.
  4. Implement persistence for binary resources to disk.

动手实践——分步指南

  1. Install MCP-compatible SDK (npm install @mcp/sdk)
  2. Configure your client with server URL and credentials
  3. Write code invoking connectedClient.client.request({ method: 'resources/read', params: { uri: '/path/to/resource' } })
  4. Handle the response data as text or binary based on c.blob field
  5. Store binary content to disk using helper functions provided in SDK examples

三大推荐资源

  1. 1
    MCP Protocol Official Documentation

    Complete reference for resource handling, including text and binary formats.

    https://mcp-protocol.dev/docs/resources

  2. 2
    LangChain Tools & Agent Examples

    Practical examples of tool integrations with real-world protocols similar to MCP.

    https://github.com/langchain-ai/langchain-js/tree/main/tools

  3. 3
    Advanced Tool Development Guide

    Comprehensive coverage of custom tool creation and protocol integration strategies.

    https://example.com/advanced-tool-development

链接由 AI 推荐——使用前建议快速核实。