edit_block
Apply surgical text replacements to files using diff-based patches. Verify changes after application for precise file modifications.
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)Entry point handler for the 'edit_block' tool in the tool dispatch switch statement. Parses input, delegates to parseEditBlock and performSearchReplace, and returns success response.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/edit.ts:8-24 (helper)Core function that performs the search-and-replace edit on the target file by reading content, replacing the first occurrence of search text with replace text, and writing back.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); }
- src/tools/edit.ts:26-52 (helper)Parses the structured blockContent string to extract the target filePath and the search/replace pair 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/schemas.ts:68-70 (schema)Zod schema defining the input for edit_block tool: a single blockContent string containing the edit instructions.export const EditBlockArgsSchema = z.object({ blockContent: z.string(), });
- src/server.ts:198-204 (registration)Registration of the 'edit_block' tool in the server's tool list, providing name, description, and JSON schema for inputs.{ 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),