| 12 | |
| 13 | |
| 14 | class FileWriteTool(Tool): |
| 15 | @property |
| 16 | def name(self) -> str: |
| 17 | return "write_file" |
| 18 | |
| 19 | @property |
| 20 | def description(self) -> str: |
| 21 | return "Write content to a file. Creates parent directories if they don't exist. Overwrites if the file already exists." |
| 22 | |
| 23 | @property |
| 24 | def input_schema(self) -> dict[str, Any]: |
| 25 | return { |
| 26 | "type": "object", |
| 27 | "properties": { |
| 28 | "path": {"type": "string", "description": "Absolute or relative path to the file."}, |
| 29 | "content": {"type": "string", "description": "The content to write."}, |
| 30 | }, |
| 31 | "required": ["path", "content"], |
| 32 | } |
| 33 | |
| 34 | def execute(self, params: dict[str, Any]) -> ToolResult: |
| 35 | filepath = Path(params["path"]).expanduser() |
| 36 | content = params.get("content", "") |
| 37 | try: |
| 38 | filepath.parent.mkdir(parents=True, exist_ok=True) |
| 39 | filepath.write_text(content) |
| 40 | return ToolResult(output=f"Wrote {len(content)} chars to {filepath}") |
| 41 | except Exception as exc: |
| 42 | return ToolResult(output=f"Error writing file: {exc}", is_error=True) |