read_multiple_files
Read and retrieve contents of multiple files simultaneously from specified paths. Supports custom encoding and returns a dictionary mapping each file path to its content or error message.
Instructions
Read multiple files at once.
Args:
paths: List of file paths to read
encoding: File encoding (default: utf-8)
ctx: MCP context
Returns:
Dictionary mapping file paths to contents or error messages
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoding | No | utf-8 | |
| paths | Yes |
Implementation Reference
- mcp_filesystem/operations.py:206-237 (handler)Core handler function that implements the logic for reading multiple files asynchronously, validating paths, handling encoding, and returning a dictionary of file contents or exceptions.async def read_multiple_files( self, paths: List[Union[str, Path]], encoding: str = "utf-8" ) -> Dict[str, Union[str, Exception]]: """Read multiple files at once. Args: paths: List of file paths encoding: Text encoding (default: utf-8) Returns: Dictionary mapping file paths to contents or exceptions """ # Explicitly type-annotate the results to help mypy results: Dict[str, Union[str, Exception]] = {} for path in paths: try: abs_path, allowed = await self.validator.validate_path(path) if not allowed: # Create an error and store it error_msg = f"Path outside allowed directories: {path}" results[str(path)] = ValueError(error_msg) continue content = await anyio.to_thread.run_sync( partial(abs_path.read_text, encoding=encoding) ) results[str(path)] = content except Exception as e: results[str(path)] = e return results
- mcp_filesystem/server.py:113-142 (registration)MCP tool registration using @mcp.tool decorator, which defines the tool schema via parameters and docstring, and delegates to the operations component.@mcp.tool() async def read_multiple_files( paths: List[str], ctx: Context, encoding: str = "utf-8" ) -> Dict[str, str]: """Read multiple files at once. Args: paths: List of file paths to read encoding: File encoding (default: utf-8) ctx: MCP context Returns: Dictionary mapping file paths to contents or error messages """ try: components = get_components() results = await components["operations"].read_multiple_files(paths, encoding) # Convert exceptions to strings for JSON serialization formatted_results = {} for path, result in results.items(): if isinstance(result, Exception): formatted_results[path] = f"Error: {str(result)}" else: formatted_results[path] = result return formatted_results except Exception as e: return {"error": str(e)}