How to Build a Harness

9. The `edit` Tool

Let Claude edit files with string replacement.

The blog posts converge on the same design: string replacement. Find oldText, replace with newText.

File: packages/coding-agent/src/core/tools/edit.ts L31-51

const replaceEditSchema = Type.Object({
  oldText: Type.String({
    description: "Exact text for one targeted replacement. Must be unique in the file.",
  }),
  newText: Type.String({ description: "Replacement text for this targeted edit." }),
});

const editSchema = Type.Object({
  path: Type.String({ description: "Path to the file to edit (relative or absolute)" }),
  edits: Type.Array(replaceEditSchema, {
    description: "One or more targeted replacements.",
  }),
});

pi’s edit tool supports batched edits (multiple replacements in one call), which is more powerful than the single-replacement version in the blog posts. The core logic is the same: read file → replace strings → write file.


Open this chapter inside the full course