Skip to main content
Glama
safurrier

MCP Filesystem Server

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
NameRequiredDescriptionDefault
encodingNoutf-8
pathsYes

Implementation Reference

  • 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 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)}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/safurrier/mcp-filesystem'

If you have feedback or need assistance with the MCP directory API, please join our Discord server