Skip to main content
Glama
StrawHatAI

Claude Desktop Commander MCP

by StrawHatAI

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
NameRequiredDescriptionDefault
blockContentYes

Implementation Reference

  • 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}` }], }; }
  • 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); }
  • 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 } }; }
  • 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),

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/StrawHatAI/claude-dev-tools'

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