Read file content.
| 61 | |
| 62 | |
| 63 | class ReadTool(Tool): |
| 64 | """Read file content.""" |
| 65 | |
| 66 | def __init__(self, workspace_dir: str = "."): |
| 67 | """Initialize ReadTool with workspace directory. |
| 68 | |
| 69 | Args: |
| 70 | workspace_dir: Base directory for resolving relative paths |
| 71 | """ |
| 72 | self.workspace_dir = Path(workspace_dir).absolute() |
| 73 | |
| 74 | @property |
| 75 | def name(self) -> str: |
| 76 | return "read_file" |
| 77 | |
| 78 | @property |
| 79 | def description(self) -> str: |
| 80 | return ( |
| 81 | "Read file contents from the filesystem. Output always includes line numbers " |
| 82 | "in format 'LINE_NUMBER|LINE_CONTENT' (1-indexed). Supports reading partial content " |
| 83 | "by specifying line offset and limit for large files. " |
| 84 | "You can call this tool multiple times in parallel to read different files simultaneously." |
| 85 | ) |
| 86 | |
| 87 | @property |
| 88 | def parameters(self) -> dict[str, Any]: |
| 89 | return { |
| 90 | "type": "object", |
| 91 | "properties": { |
| 92 | "path": { |
| 93 | "type": "string", |
| 94 | "description": "Absolute or relative path to the file", |
| 95 | }, |
| 96 | "offset": { |
| 97 | "type": "integer", |
| 98 | "description": "Starting line number (1-indexed). Use for large files to read from specific line", |
| 99 | }, |
| 100 | "limit": { |
| 101 | "type": "integer", |
| 102 | "description": "Number of lines to read. Use with offset for large files to read in chunks", |
| 103 | }, |
| 104 | }, |
| 105 | "required": ["path"], |
| 106 | } |
| 107 | |
| 108 | async def execute(self, path: str, offset: int | None = None, limit: int | None = None) -> ToolResult: |
| 109 | """Execute read file.""" |
| 110 | try: |
| 111 | file_path = Path(path) |
| 112 | # Resolve relative paths relative to workspace_dir |
| 113 | if not file_path.is_absolute(): |
| 114 | file_path = self.workspace_dir / file_path |
| 115 | |
| 116 | if not file_path.exists(): |
| 117 | return ToolResult( |
| 118 | success=False, |
| 119 | content="", |
| 120 | error=f"File not found: {path}", |
no outgoing calls