Skip to main content
Glama
pwilkin

MCP File Editor Server

by pwilkin

read_file

Retrieve file contents efficiently, either in full or by specific line ranges, using a file path. Option to display line numbers for enhanced readability and precise file navigation.

Instructions

Read the contents of a file. You can read the entire file or specific line ranges.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
end_lineNoEnding line number (1-based). Cannot be used with full=true
file_pathYesAbsolute path to the file to read
fullNoRead the entire file. Cannot be used with start_line or end_line
show_line_numbersNoWhether to prefix each line with its line number
start_lineNoStarting line number (1-based). Cannot be used with full=true

Implementation Reference

  • src/index.ts:24-80 (registration)
    Registers the read_file tool using server.addTool, including name, description, parameters schema, and execute handler.
    server.addTool({
      name: 'read_file',
      description: 'Read the contents of a file. You can read the entire file or specific line ranges.',
      parameters: z.object({
        file_path: z.string().describe('Absolute path to the file to read'),
        show_line_numbers: z.boolean().optional().default(false).describe('Whether to prefix each line with its line number'),
        start_line: z.number().int().positive().optional().describe('Starting line number (1-based). Cannot be used with full=true'),
        end_line: z.number().int().positive().optional().describe('Ending line number (1-based). Cannot be used with full=true'),
        full: z.boolean().optional().describe('Read the entire file. Cannot be used with start_line or end_line')
      }),
      execute: async ({ file_path, show_line_numbers, start_line, end_line, full }) => {
        // Validate parameters
        if (full && (start_line || end_line)) {
          throw new UserError('Cannot use "full" parameter together with "start_line" or "end_line". Choose either full=true or specify line ranges.');
        }
        if ((start_line && !end_line) || (!start_line && end_line)) {
          throw new UserError('Both "start_line" and "end_line" must be provided together.');
        }
        if (start_line && end_line && start_line > end_line) {
          throw new UserError('"start_line" must be less than or equal to "end_line".');
        }
    
        const absolutePath = validateAbsolutePath(file_path, 'file_path');
        validateFileExists(absolutePath);
    
        try {
          const content = fs.readFileSync(absolutePath, 'utf-8');
          const lines = content.split('\n');
    
          let resultLines: string[];
    
          if (full) {
            resultLines = lines;
          } else if (start_line && end_line) {
            if (start_line > lines.length) {
              throw new UserError(`Start line ${start_line} is beyond the file length (${lines.length} lines).`);
            }
            if (end_line > lines.length) {
              throw new UserError(`End line ${end_line} is beyond the file length (${lines.length} lines).`);
            }
            resultLines = lines.slice(start_line - 1, end_line); // Convert to 0-based
          } else {
            resultLines = lines;
          }
    
          if (show_line_numbers) {
            const startLineNum = start_line || 1;
            return resultLines.map((line, index) => `${startLineNum + index} | ${line}`).join('\n');
          } else {
            return resultLines.join('\n');
          }
        } catch (error: any) {
          if (error instanceof UserError) throw error;
          throw new UserError(`Error reading file "${absolutePath}": ${error.message}`);
        }
      }
    });
  • The core handler function for the read_file tool. It validates input parameters, reads the file using fs.readFileSync, processes line ranges if specified, optionally adds line numbers, and returns the content.
    execute: async ({ file_path, show_line_numbers, start_line, end_line, full }) => {
      // Validate parameters
      if (full && (start_line || end_line)) {
        throw new UserError('Cannot use "full" parameter together with "start_line" or "end_line". Choose either full=true or specify line ranges.');
      }
      if ((start_line && !end_line) || (!start_line && end_line)) {
        throw new UserError('Both "start_line" and "end_line" must be provided together.');
      }
      if (start_line && end_line && start_line > end_line) {
        throw new UserError('"start_line" must be less than or equal to "end_line".');
      }
    
      const absolutePath = validateAbsolutePath(file_path, 'file_path');
      validateFileExists(absolutePath);
    
      try {
        const content = fs.readFileSync(absolutePath, 'utf-8');
        const lines = content.split('\n');
    
        let resultLines: string[];
    
        if (full) {
          resultLines = lines;
        } else if (start_line && end_line) {
          if (start_line > lines.length) {
            throw new UserError(`Start line ${start_line} is beyond the file length (${lines.length} lines).`);
          }
          if (end_line > lines.length) {
            throw new UserError(`End line ${end_line} is beyond the file length (${lines.length} lines).`);
          }
          resultLines = lines.slice(start_line - 1, end_line); // Convert to 0-based
        } else {
          resultLines = lines;
        }
    
        if (show_line_numbers) {
          const startLineNum = start_line || 1;
          return resultLines.map((line, index) => `${startLineNum + index} | ${line}`).join('\n');
        } else {
          return resultLines.join('\n');
        }
      } catch (error: any) {
        if (error instanceof UserError) throw error;
        throw new UserError(`Error reading file "${absolutePath}": ${error.message}`);
      }
    }
  • Zod schema defining the input parameters for the read_file tool, including file_path, optional line ranges, full read flag, and line number display option.
    parameters: z.object({
      file_path: z.string().describe('Absolute path to the file to read'),
      show_line_numbers: z.boolean().optional().default(false).describe('Whether to prefix each line with its line number'),
      start_line: z.number().int().positive().optional().describe('Starting line number (1-based). Cannot be used with full=true'),
      end_line: z.number().int().positive().optional().describe('Ending line number (1-based). Cannot be used with full=true'),
      full: z.boolean().optional().describe('Read the entire file. Cannot be used with start_line or end_line')
  • Helper function validateAbsolutePath used in read_file handler to ensure the file_path is absolute (called at line 46).
    export function validateAbsolutePath(filePath: string, parameterName: string = 'path'): string {
      if (!path.isAbsolute(filePath)) {
        throw new UserError(
          `The ${parameterName} must be an absolute path. You provided a relative path: "${filePath}". ` +
          `Please provide the full absolute path (e.g., "/home/user/file.txt" on Linux/Mac or "C:\\Users\\user\\file.txt" on Windows).`
        );
      }
      return filePath;
    }
  • Helper function validateFileExists used in read_file handler to check if the file exists and is readable (called at line 47).
    export function validateFileExists(filePath: string): void {
      try {
        const stats = fs.statSync(filePath);
        if (!stats.isFile()) {
          throw new UserError(
            `The path "${filePath}" exists but is not a file. Please ensure you're providing the path to a file, not a directory.`
          );
        }
      } catch (error: any) {
        if (error.code === 'ENOENT') {
          throw new UserError(
            `File not found: "${filePath}". Please verify that the file exists and the path is correct.`
          );
        } else if (error.code === 'EACCES') {
          throw new UserError(
            `Permission denied: Cannot access "${filePath}". Please check file permissions.`
          );
        } else {
          throw new UserError(
            `Error accessing file "${filePath}": ${error.message}`
          );
        }
      }
    }
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions reading capabilities but fails to disclose critical traits such as file size limits, encoding handling, error conditions (e.g., if the file doesn't exist or is unreadable), or performance implications. This leaves significant gaps in understanding the tool's behavior beyond basic functionality.

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

Conciseness5/5

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

The description is front-loaded with the core purpose in the first sentence and adds useful detail in the second sentence without redundancy. It is appropriately sized with zero waste, making it easy to scan and understand quickly.

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

Completeness2/5

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

Given the complexity of file operations and lack of annotations and output schema, the description is incomplete. It does not address error handling, return format (e.g., whether output is raw text or structured), or limitations (e.g., large files), which are crucial for an AI agent to use the tool effectively in varied contexts.

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

Parameters3/5

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

Schema description coverage is 100%, so the schema already documents all parameters thoroughly. The description adds minimal value by implying line-range functionality but does not provide additional semantics beyond what the schema specifies (e.g., it doesn't clarify format or constraints for 'file_path' or interactions between parameters). Baseline 3 is appropriate as the schema handles most parameter documentation.

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 verb ('Read') and resource ('contents of a file'), specifying the action and target. It distinguishes from siblings like 'delete_from_file', 'insert_into_file', and 'replace_in_file' by focusing on reading rather than modifying files, and from 'list_files' by reading file contents instead of listing files.

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 provides clear context for usage by mentioning 'entire file or specific line ranges', which helps understand when to use different parameter combinations. However, it does not explicitly state when to use this tool versus alternatives like 'search_file' for content searching or 'list_files' for file metadata, leaving some sibling differentiation implicit.

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

Related 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/pwilkin/mcp-file-edit'

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