Skip to main content
Glama

edit_block

Apply precise text replacements to files using a structured format. Specify search patterns and new content to make verified changes, ideal for targeted 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
NameRequiredDescriptionDefault
blockContentYes

Implementation Reference

  • The main handler for the 'edit_block' tool. It parses the input arguments using EditBlockArgsSchema, extracts filePath and searchReplace pairs by calling parseEditBlock, applies the replacement 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}` }], }; }
  • Zod schema defining the input for the edit_block tool: a string containing the edit block with filepath and SEARCH/REPLACE markers.
    export const EditBlockArgsSchema = z.object({ blockContent: z.string(), });
  • src/server.ts:198-205 (registration)
    Registration of the 'edit_block' tool in the MCP server's tool list, 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), },
  • Helper function that parses the blockContent string into filePath and a single 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 } }; }
  • Helper function that performs the actual search and replace in the target file by reading the file, finding the first match, replacing it, 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); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MrGNSS/ClaudeDesktopCommander'

If you have feedback or need assistance with the MCP directory API, please join our Discord server