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
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| line | Yes | ||
| context | No |
Implementation Reference
- src/mcp_ty/server.py:291-328 (handler)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))