Skip to main content
Glama

read_code

Read file content from specified paths, optionally extracting specific line ranges to access code sections directly for analysis or review.

Instructions

Read file content, optionally by line range (1-based).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
start_lineNo
end_lineNo

Implementation Reference

  • The `read_code` tool handler function implementation, which reads a file and optionally returns a range of lines.
    async def read_code(
        file_path: str,
        start_line: int | None = None,
        end_line: int | None = None
    ) -> str:
        """Read file content, optionally by line range (1-based)."""
        path = Path(file_path).resolve()
        if not path.exists():
            return _error(f"File not found: {file_path}")
    
        try:
            content = path.read_text(encoding="utf-8")
            lines = content.splitlines()
            total_lines = len(lines)
    
            start = (start_line or 1) - 1
            end = end_line or total_lines
    
            if start < 0:
                start = 0
            if end > total_lines:
                end = total_lines
            if start >= total_lines:
                return _error(f"start_line {start_line} exceeds file length {total_lines}")
    
            selected = {i + 1: lines[i] for i in range(start, end)}
    
            return _ok({
                "file": path.name,
                "path": str(path),
                "range": {"start": start + 1, "end": end},
                "total_lines": total_lines,
                "lines": selected
            })

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/qinsehm1128/mcp-ty'

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