Skip to main content
Glama

read

Extract specific lines from a text file by defining start and end positions to prepare content for editing operations.

Instructions

Read lines from the current file from start line to end line, returning them in a dictionary like {"lines":[[1,"text on first line"],[2,"text on second line"]]}. This makes it easier to find the precise lines to select for editing.

Args: start (int, optional): Start line number end (int, optional): End line number

Returns: dict: lines, start_line, end_line

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
startYes
endYes

Implementation Reference

  • Implementation of the 'read' tool handler. Reads a range of lines (start to end) from the currently set file, validates the range, formats lines with numbers, and returns them in a structured dictionary. Handles errors like no file set or invalid ranges.
    async def read(start: int, end: int) -> Dict[str, Any]:
        """
        Read lines from the current file from start line to end line, returning them in a dictionary
        like {"lines":[[1,"text on first line"],[2,"text on second line"]]}. This makes it easier to find the precise lines to select for editing.
    
        Args:
            start (int, optional): Start line number
            end (int, optional): End line number
    
        Returns:
            dict: lines, start_line, end_line
        """
        result = {}
    
        if self.current_file_path is None:
            return {"error": "No file path is set. Use set_file first."}
    
        try:
            with open(self.current_file_path, "r", encoding="utf-8") as file:
                lines = file.readlines()
    
            if start < 1:
                return {"error": "start must be at least 1"}
            if end > len(lines):
                end = len(lines)
            if start > end:
                return {
                    "error": f"{start=} cannot be greater than {end=}. {len(lines)=}"
                }
    
            selected_lines = lines[start - 1 : end]
    
            formatted_lines = []
            for i, line in enumerate(selected_lines, start):
                formatted_lines.append((i, line.rstrip()))
    
            result["lines"] = formatted_lines
            result["start_line"] = start
            result["end_line"] = end
    
            return result
    
        except Exception as e:
            return {"error": f"Error reading file: {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/danielpodrazka/editor-mcp'

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