From d888b2c0e6f7c02df561f4b99fe2abca54430ea7 Mon Sep 17 00:00:00 2001 From: xlyoung Date: Thu, 4 Jun 2026 14:55:03 +0800 Subject: [PATCH] feat: add append mode to file_write tool Add optional 'mode' parameter to file_write tool that allows appending content to existing files instead of always overwriting. - mode='w' (default): overwrite file (existing behavior) - mode='a': append content to existing file The documentation at strandsagents.com claims append mode is supported, but the implementation hardcoded open(path, 'w'). This fix aligns the implementation with the documented behavior. Fixes #344 --- src/strands_tools/file_write.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/strands_tools/file_write.py b/src/strands_tools/file_write.py index b142479e..fde6aaf3 100644 --- a/src/strands_tools/file_write.py +++ b/src/strands_tools/file_write.py @@ -82,6 +82,12 @@ "type": "string", "description": "The content to write to the file", }, + "mode": { + "type": "string", + "description": "Write mode: 'w' (default) to overwrite, 'a' to append to existing content", + "enum": ["w", "a"], + "default": "w", + }, }, "required": ["path", "content"], } @@ -168,6 +174,7 @@ def file_write(tool: ToolUse, **kwargs: Any) -> ToolResult: - path: The path to the file to write. User paths with tilde (~) are automatically expanded. - content: The content to write to the file. + - mode: Write mode - 'w' (default) to overwrite, 'a' to append. **kwargs: Additional keyword arguments (not used currently) Returns: @@ -257,10 +264,12 @@ def file_write(tool: ToolUse, **kwargs: Any) -> ToolResult: ) # Write the file - with open(path, "w") as file: + mode = tool.get("input", {}).get("mode", "w") + with open(path, mode) as file: file.write(content) - success_message = f"File written successfully to {path}" + action = "appended to" if mode == "a" else "written successfully to" + success_message = f"Content {action} {path}" success_panel = Panel( Text(success_message, style="bold green"), title="[bold green]Write Successful",