Skip to main content

file_read

Read the contents of a file. Supports reading specific line ranges.

Parameters

ParameterTypeRequiredDescription
pathstringYesPath to the file (relative to working directory)
offsetintegerNoLine number to start reading from (0-based)
limitintegerNoMaximum number of lines to read

Example

Reading a file with a line range:
{
  "path": "src/main.rs",
  "offset": 10,
  "limit": 20
}
This reads lines 11-30 of src/main.rs.

file_write

Create or overwrite a file. Automatically creates parent directories if they do not exist.

Parameters

ParameterTypeRequiredDescription
pathstringYesPath to the file
contentstringYesContent to write

Example

{
  "path": "src/utils.rs",
  "content": "pub fn add(a: i32, b: i32) -> i32 {\n    a + b\n}\n"
}
file_write overwrites the entire file. For targeted edits, use file_edit or apply_patch instead.

file_edit

Edit a file by replacing an exact string match. The old_string must match exactly once in the file.

Parameters

ParameterTypeRequiredDescription
pathstringYesPath to the file
old_stringstringYesExact text to find (must be unique in the file)
new_stringstringYesReplacement text

Example

{
  "path": "src/main.rs",
  "old_string": "fn main() {\n    println!(\"Hello\");\n}",
  "new_string": "fn main() {\n    println!(\"Hello, world!\");\n}"
}
If old_string matches multiple locations or is not found, the edit fails with an error.

apply_patch

Apply a unified diff patch. Supports Claude Code-style patch format with fuzzy matching for context lines.

Patch Format

*** Begin Patch
*** Add File: path/to/new_file.rs
+line 1
+line 2
*** Update File: path/to/existing.rs
 context line
-old line
+new line
 context line
*** Delete File: path/to/old_file.rs
*** End Patch

Patch Sections

SectionDescription
*** Add FileCreate a new file with the specified content
*** Update FileModify an existing file using diff hunks
*** Delete FileRemove a file

Fuzzy Matching

When applying updates, the patch tool uses fuzzy matching for context lines. If the exact context is not found, it searches within a 3-line window to find the best match. This makes patches more resilient to minor whitespace or formatting differences.

Example

*** Begin Patch
*** Update File: src/lib.rs
 use std::io;
 
-pub fn read_input() -> String {
+pub fn read_input() -> Result<String, io::Error> {
     let mut input = String::new();
-    std::io::stdin().read_line(&mut input).unwrap();
-    input
+    std::io::stdin().read_line(&mut input)?;
+    Ok(input)
 }
*** End Patch