ReadMcpResourceTool:读取 MCP 资源
2026/8/2 18:24:06 · 来源
`ReadMcpResourceTool` enables remote reading of both text and binary resources via the Model Context Protocol (MCP) using an SDK-based request mechanism.
它是 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
- Resource URIs
- How MCP resource paths are structured
- SDK Integration
- Using
connectedClient.client.requestto access resources
- Using
- Text vs Binary Handling
- Distinguishing between text and binary content in response payloads
Hands-On Practice
- Set up an MCP server connection with your client SDK.
- Execute a test request using the resource URI format.
- Analyze both text and binary responses from the API.
- Implement persistence for binary resources to disk.
动手实践——分步指南
- Install MCP-compatible SDK (
npm install @mcp/sdk) - Configure your client with server URL and credentials
- Write code invoking
connectedClient.client.request({ method: 'resources/read', params: { uri: '/path/to/resource' } }) - Handle the response data as text or binary based on
c.blobfield - Store binary content to disk using helper functions provided in SDK examples
三大推荐资源
- 1MCP Protocol Official Documentation
Complete reference for resource handling, including text and binary formats.
https://mcp-protocol.dev/docs/resources
- 2LangChain 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
- 3Advanced Tool Development Guide
Comprehensive coverage of custom tool creation and protocol integration strategies.
https://example.com/advanced-tool-development
链接由 AI 推荐——使用前建议快速核实。