read
Read file content from any storage service supported by Apache OpenDAL by providing a resource URI.
Instructions
Read file content from OpenDAL service
Args:
uri: resource URI, e.g. mys3://path/to/file
Returns:
File content or error informationInput Schema
| Name | Required | Description | Default |
|---|---|---|---|
| uri | Yes |
Implementation Reference
- src/mcp_server_opendal/server.py:112-130 (handler)The 'read' tool handler function registered via @mcp.tool(). Accepts a URI (e.g., mys3://path/to/file), parses it, and delegates to opendal_resource() which reads file content via OpenDAL.
@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)} - The read_path method on OpendalResource that actually calls await self.op.read(path) to read raw bytes from storage.
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) - The parse_uri helper that parses a URI string into an (OpendalResource, path) tuple 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) - src/mcp_server_opendal/server.py:46-80 (handler)The opendal_resource template resource handler (@mcp.resource) that the read tool delegates to. Reads file bytes via resource.read_path(), returns decoded text or base64-encoded binary content.
@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)} - src/mcp_server_opendal/server.py:112-113 (registration)The @mcp.tool() decorator registration for the 'read' tool on line 112.
@mcp.tool() async def read(uri: str) -> Dict[str, Any]: