edit_block
Perform precise text replacements in files using a block-based format. Define search and replace content for targeted edits, with verification after application. Ideal for minor changes under 20% of 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)The main handler for the "edit_block" tool. Parses input arguments using EditBlockArgsSchema, extracts file path and search-replace pairs using parseEditBlock, applies the replacement with 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/tools/schemas.ts:68-70 (schema)Zod schema defining the input for the edit_block tool: a single blockContent string containing the edit instructions in the specified format.export const EditBlockArgsSchema = z.object({ blockContent: z.string(), });
- src/server.ts:198-205 (registration)Registration of the "edit_block" tool in the ListTools response, providing name, detailed description of usage and format, and the input schema.{ 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/edit.ts:26-52 (helper)Helper function that parses the blockContent string into filePath and a SearchReplace object by identifying the SEARCH/REPLACE 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 performs the actual search and replace in the target file by reading the content, finding the first match, replacing it, and writing back the updated content.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); }