Skip to main content
Glama

read_context

Extract code segments from files by specifying a line number and surrounding context to analyze specific sections without reviewing entire files.

Instructions

Read code around a specific line with context.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
lineYes
contextNo

Implementation Reference

  • The implementation of the read_context tool, which reads code surrounding a target line within a file.
    @mcp.tool()
    async def read_context(
        file_path: str,
        line: int,
        context: int = 10
    ) -> str:
        """Read code around a specific line with context."""
        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)
    
            if line < 1 or line > total_lines:
                return _error(f"Line {line} out of range (1-{total_lines})")
    
            start = max(0, line - 1 - context)
            end = min(total_lines, line + context)
    
            selected = {i + 1: lines[i] for i in range(start, end)}
    
            return _ok({
                "file": path.name,
                "path": str(path),
                "target_line": line,
                "range": {"start": start + 1, "end": end},
                "total_lines": total_lines,
                "lines": selected
            })
    
        except UnicodeDecodeError:
            return _error(f"Cannot read file (not UTF-8): {file_path}")
        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/qinsehm1128/mcp-ty'

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