Claude 可以使用 Anthropic 定义的文本编辑器工具来查看和修改文本文件,帮助您调试、修复和改进代码或其他文本文档。这使 Claude 能够直接与您的文件交互,提供实际操作协助,而不仅仅是建议更改。
| 模型 | 工具版本 |
|---|---|
| Claude 4.x 模型 | text_editor_20250728 |
| Claude Sonnet 3.7 (已弃用) | text_editor_20250124 |
Claude 4 模型的 text_editor_20250728 工具不包括 undo_edit 命令。如果您需要此功能,您需要使用 Claude Sonnet 3.7 (已弃用)。
较旧的工具版本不保证与较新的模型向后兼容。始终使用与您的模型版本相对应的工具版本。
以下是一些使用文本编辑器工具的示例:
文本编辑器工具可以按以下方式使用:
向 Claude 提供文本编辑器工具和用户提示
Claude 使用该工具检查文件或目录
view 命令检查文件内容或列出目录内容view 命令的 tool_use 内容块执行 view 命令并返回结果
max_characters 参数,将文件内容截断到该长度tool_result 内容块的新 user 消息将结果返回给 ClaudeClaude 使用该工具修改文件
str_replace 等命令进行更改,或使用 insert 在特定行号处添加文本。str_replace 命令,Claude 会构造一个格式正确的工具使用请求,其中包含旧文本和要替换它的新文本执行编辑并返回结果
Claude 提供其分析和解释
文本编辑器工具支持多个用于查看和修改文件的命令:
view 命令允许 Claude 检查文件的内容或列出目录的内容。它可以读取整个文件或特定范围的行。
参数:
command:必须为 "view"path:要查看的文件或目录的路径view_range(可选):一个包含两个整数的数组,指定要查看的开始和结束行号。行号从 1 开始,结束行为 -1 表示读到文件末尾。此参数仅在查看文件时适用,不适用于目录。str_replace 命令允许 Claude 用新字符串替换文件中的特定字符串。这用于进行精确编辑。
参数:
command:必须为 "str_replace"path:要修改的文件的路径old_str:要替换的文本(必须完全匹配,包括空格和缩进)new_str:要插入以替换旧文本的新文本create 命令允许 Claude 创建具有指定内容的新文件。
参数:
command:必须为 "create"path:应创建新文件的路径file_text:要写入新文件的内容insert 命令允许 Claude 在文件中的特定位置插入文本。
参数:
command:必须为 "insert"path:要修改的文件的路径insert_line:要在其后插入文本的行号(0 表示文件开头)new_str:要插入的文本undo_edit 命令允许 Claude 撤销对文件所做的最后一次编辑。
此命令仅在 Claude Sonnet 3.7 (已弃用) 中可用。Claude 4 模型使用 text_editor_20250728 不支持此命令。
参数:
command:必须为 "undo_edit"path:应撤销其最后一次编辑的文件的路径文本编辑器工具作为无模式工具实现。使用此工具时,您无需像其他工具那样提供输入模式;该模式内置于Claude的模型中,无法修改。
工具类型取决于模型版本:
type: "text_editor_20250728"type: "text_editor_20250124"初始化您的编辑器实现
创建辅助函数来处理文件操作,如读取、写入和修改文件。考虑实现备份功能以从错误中恢复。
处理编辑器工具调用
创建一个函数来处理来自Claude的工具调用,基于命令类型:
def handle_editor_tool(tool_call, model_version):
input_params = tool_call.input
command = input_params.get('command', '')
file_path = input_params.get('path', '')
if command == 'view':
# Read and return file contents
pass
elif command == 'str_replace':
# Replace text in file
pass
elif command == 'create':
# Create new file
pass
elif command == 'insert':
# Insert text at location
pass
elif command == 'undo_edit':
# Check if it's a Claude 4 model
if 'str_replace_based_edit_tool' in model_version:
return {"error": "undo_edit command is not supported in Claude 4"}
# Restore from backup for Claude 3.7
pass实现安全措施
添加验证和安全检查:
处理Claude的响应
从Claude的响应中提取和处理工具调用:
# Process tool use in Claude's response
for content in response.content:
if content.type == "tool_use":
# Execute the tool based on command
result = handle_editor_tool(content)
# Return result to Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result
}实现文本编辑器工具时,请记住:
使用文本编辑器工具时,可能会出现各种错误。以下是如何处理它们的指导:
The text editor tool uses the same pricing structure as other tools used with Claude. It follows the standard input and output token pricing based on the Claude model you're using.
In addition to the base tokens, the following additional input tokens are needed for the text editor tool:
| Tool | Additional input tokens |
|---|---|
text_editor_20250429 (Claude 4.x) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7 (deprecated)) | 700 tokens |
有关工具定价的更多详细信息,请参阅工具使用定价。
文本编辑器工具可以与其他Claude工具一起使用。组合工具时,请确保:
| 日期 | 版本 | 更改 |
|---|---|---|
| 2025年7月28日 | text_editor_20250728 | 发布更新的文本编辑器工具,修复了一些问题并添加了可选的max_characters参数。除此之外,它与text_editor_20250429相同。 |
| 2025年4月29日 | text_editor_20250429 | 发布Claude 4的文本编辑器工具。此版本删除了undo_edit命令,但保持了所有其他功能。工具名称已更新以反映其基于str_replace的架构。 |
| 2025年3月13日 | text_editor_20250124 | 独立文本编辑器工具文档的介绍。此版本针对Claude Sonnet 3.7进行了优化,但具有与之前版本相同的功能。 |
| 2024年10月22日 | text_editor_20241022 | 文本编辑器工具与Claude Sonnet 3.5的初始版本发布(已停用)。通过view、create、str_replace、insert和undo_edit命令提供查看、创建和编辑文件的功能。 |
以下是如何以更便捷和强大的方式使用文本编辑器工具的一些想法:
当您使用文本编辑器工具构建应用程序时,我们很高兴看到您如何利用Claude的功能来增强您的开发工作流和生产力。