read
Retrieve file content from various storage services using a unified URI interface. Specify the resource location to access data across S3, Azure Blob Storage, and Google Cloud Storage.
Instructions
Read file content from OpenDAL service
Args:
uri: resource URI, e.g. mys3://path/to/file
Returns:
File content or error information
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| uri | Yes |
Implementation Reference
- src/mcp_server_opendal/server.py:112-131 (handler)The MCP tool handler for 'read'. It processes the URI input, parses it using parse_uri, and invokes the opendal_resource template to retrieve and format the file content as text or base64-encoded binary.@mcp.tool() async def read(uri: str) -> Dict[str, Any]: """ Read file content from OpenDAL service Args: uri: resource URI, e.g. mys3://path/to/file Returns: File content or error information """ logger.debug(f"Reading file content: {uri}") try: resource, path = parse_uri(uri) # Directly call the resource function to get content return await opendal_resource(resource.scheme, path) except Exception as e: logger.error(f"Failed to read file content: {e!s}") return {"error": str(e)}
- Key helper resource template (@mcp.resource) that performs the core file reading logic. Reads raw bytes with read_path, stats the file, detects text/binary, and formats the output dictionary used by the 'read' tool.@mcp.resource("{scheme}://{path}") async def opendal_resource(scheme: str, path: str) -> Dict[str, Any]: """ Access files in OpenDAL service Args: scheme: storage service scheme path: file path Returns: Dictionary containing file content and metadata """ logger.debug(f"Reading template resource content: {scheme}://{path}") try: resource = OpendalResource(scheme) data = await resource.read_path(path) metadata = await resource.stat(path) if resource.is_text_file(path): return { "content": data.decode("utf-8"), "mime_type": metadata.content_type or "text/plain", "size": metadata.content_length, "is_binary": False, } else: return { "content": base64.b64encode(data).decode("ascii"), "mime_type": metadata.content_type or "application/octet-stream", "size": metadata.content_length, "is_binary": True, } except Exception as e: logger.error(f"Failed to read resource: {e!s}") return {"error": str(e)}
- Low-level asynchronous method in OpendalResource class that executes the actual file read operation using the underlying OpenDAL operator (self.op.read). Called by opendal_resource.async def read_path(self, path: Union[str, os.PathLike]) -> bytes: """Read content from a specific path""" logger.debug(f"Reading path: {path}") return await self.op.read(path)
- Utility function to parse the input URI into scheme and path, instantiating the appropriate OpendalResource. Used by the 'read' tool.def parse_uri(uri: str) -> Tuple[OpendalResource, str]: """Parse a URI into a resource and path""" from urllib.parse import unquote, urlparse logger.debug(f"Parsing URI: {uri}") parsed = urlparse(uri) scheme = parsed.scheme path = parsed.netloc + parsed.path path = unquote(path) # Decode URL-encoded characters return (OpendalResource(scheme), path)