edit_block
Perform precise text replacements in files using a diff-based format. Define search and replace blocks to update content, verify changes post-application, and ensure accuracy for small modifications (<20% file size).
Instructions
Apply surgical text replacements to files. Best for small changes (<20% of file size). Multiple blocks can be used for separate changes. Will verify changes after application. Format: filepath, then <<<<<<< SEARCH, content to find, =======, new content, >>>>>>> REPLACE.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| blockContent | Yes |
Implementation Reference
- src/server.ts:258-265 (handler)Main execution handler for the 'edit_block' tool. Parses the input arguments using the schema, extracts file path and search/replace instructions via parseEditBlock, applies the changes using performSearchReplace, and returns a success message.case "edit_block": { const parsed = EditBlockArgsSchema.parse(args); const { filePath, searchReplace } = await parseEditBlock(parsed.blockContent); await performSearchReplace(filePath, searchReplace); return { content: [{ type: "text", text: `Successfully applied edit to ${filePath}` }], }; }
- src/server.ts:198-205 (registration)Tool registration entry for 'edit_block' in the list of available tools, including name, description, and input schema reference.{ name: "edit_block", description: "Apply surgical text replacements to files. Best for small changes (<20% of file size). " + "Multiple blocks can be used for separate changes. Will verify changes after application. " + "Format: filepath, then <<<<<<< SEARCH, content to find, =======, new content, >>>>>>> REPLACE.", inputSchema: zodToJsonSchema(EditBlockArgsSchema), },
- src/tools/schemas.ts:68-70 (schema)Zod schema definition for 'edit_block' tool inputs, requiring a single 'blockContent' string field.export const EditBlockArgsSchema = z.object({ blockContent: z.string(), });
- src/tools/edit.ts:26-52 (helper)Helper function that parses the edit block content to extract the target file path and the search/replace strings based on the specified markers.export async function parseEditBlock(blockContent: string): Promise<{ filePath: string; searchReplace: SearchReplace; }> { const lines = blockContent.split('\n'); // First line should be the file path const filePath = lines[0].trim(); // Find the markers const searchStart = lines.indexOf('<<<<<<< SEARCH'); const divider = lines.indexOf('======='); const replaceEnd = lines.indexOf('>>>>>>> REPLACE'); if (searchStart === -1 || divider === -1 || replaceEnd === -1) { throw new Error('Invalid edit block format - missing markers'); } // Extract search and replace content const search = lines.slice(searchStart + 1, divider).join('\n'); const replace = lines.slice(divider + 1, replaceEnd).join('\n'); return { filePath, searchReplace: { search, replace } }; }
- src/tools/edit.ts:8-24 (helper)Helper function that reads the target file, finds the first occurrence of the search string, replaces it with the new content, and writes back to the file.export async function performSearchReplace(filePath: string, block: SearchReplace): Promise<void> { const content = await readFile(filePath); // Find first occurrence const searchIndex = content.indexOf(block.search); if (searchIndex === -1) { throw new Error(`Search content not found in ${filePath}`); } // Replace content const newContent = content.substring(0, searchIndex) + block.replace + content.substring(searchIndex + block.search.length); await writeFile(filePath, newContent); }