Skip to main content
Glama
safurrier

MCP Filesystem Server

read_file

Retrieve file contents from the filesystem by specifying a file path and optional encoding. This tool reads complete file data for processing or analysis within the MCP Filesystem Server environment.

Instructions

Read the complete contents of a file.

Args:
    path: Path to the file
    encoding: File encoding (default: utf-8)
    ctx: MCP context

Returns:
    File contents as a string

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
encodingNoutf-8

Implementation Reference

  • MCP tool registration and handler for 'read_file'. This is the entry point that gets called by the MCP client, performs error handling, and delegates to the core FileOperations.read_file method.
    @mcp.tool()
    async def read_file(path: str, ctx: Context, encoding: str = "utf-8") -> str:
        """Read the complete contents of a file.
    
        Args:
            path: Path to the file
            encoding: File encoding (default: utf-8)
            ctx: MCP context
    
        Returns:
            File contents as a string
        """
        try:
            components = get_components()
            return await components["operations"].read_file(path, encoding)
        except Exception as e:
            return f"Error reading file: {str(e)}"
  • Core implementation of file reading logic in FileOperations class. Performs path validation using PathValidator and reads the file content asynchronously using anyio.
    async def read_file(self, path: Union[str, Path], encoding: str = "utf-8") -> str:
        """Read a text file.
    
        Args:
            path: Path to the file
            encoding: Text encoding (default: utf-8)
    
        Returns:
            File contents as string
    
        Raises:
            ValueError: If path is outside allowed directories
            FileNotFoundError: If file does not exist
            PermissionError: If file cannot be read
        """
        abs_path, allowed = await self.validator.validate_path(path)
        if not allowed:
            raise ValueError(f"Path outside allowed directories: {path}")
    
        try:
            return await anyio.to_thread.run_sync(
                partial(abs_path.read_text, encoding=encoding)
            )
        except UnicodeDecodeError:
            raise ValueError(f"Cannot decode file as {encoding}: {path}")
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden. It discloses the core behavior (reading complete contents) and mentions a default encoding, but doesn't address important behavioral aspects like error handling (e.g., what happens if file doesn't exist), performance implications for large files, or security constraints. It adds some context but leaves significant gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (purpose, args, returns) and uses only essential sentences. However, the 'ctx: MCP context' parameter mention is redundant since it's not in the actual input schema, slightly reducing efficiency. Overall, it's appropriately sized and front-loaded.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (file reading with encoding), no annotations, and no output schema, the description is minimally adequate. It covers basic purpose and parameters but lacks important context about return format details (e.g., string format for binary files), error scenarios, and performance considerations that would be needed for robust agent usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description must compensate. It successfully explains both parameters: 'path' as 'Path to the file' and 'encoding' as 'File encoding (default: utf-8)'. This adds essential meaning beyond the bare schema, though it doesn't elaborate on path format requirements or valid encoding values.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Read the complete contents') and resource ('of a file'), distinguishing it from siblings like read_file_lines (partial reading) or get_file_info (metadata only). The verb+resource combination is precise and unambiguous.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for reading entire file contents, but doesn't explicitly state when to choose this over alternatives like read_file_lines (for specific lines) or head_file/tail_file (for beginning/end). It provides clear context but lacks explicit sibling differentiation or exclusion guidance.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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