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
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| start_line | No | ||
| end_line | No |
Implementation Reference
- src/mcp_ty/server.py:250-283 (handler)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 })