Skip to main content
Glama

edit_file

Make line-based edits to text files by specifying original lines and their replacements. Returns a git-style diff showing changes made to files on your PC.

Instructions

Make line-based edits to a text file. Provide original lines and their replacements. Returns a git-style diff showing the changes made. Each edit replaces exact line sequences with new content.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe path to the file to edit
editsYesArray of edit operations to apply

Implementation Reference

  • The handler for the 'edit_file' tool. Reads the target file, applies each specified edit by replacing 'oldText' with 'newText' (ensuring 'oldText' exists first), overwrites the file with the modified content, generates a simple unified diff using the helper function, and returns the diff.
    case "edit_file": { const filePath = args.path as string; const edits = args.edits as Array<{ oldText: string; newText: string; }>; let content = await fs.readFile(filePath, "utf-8"); const originalContent = content; // Apply each edit for (const edit of edits) { if (!content.includes(edit.oldText)) { throw new Error( `Could not find text to replace: ${edit.oldText.substring(0, 100)}...` ); } content = content.replace(edit.oldText, edit.newText); } await fs.writeFile(filePath, content, "utf-8"); // Generate diff const diff = generateDiff(originalContent, content, filePath); return { content: [ { type: "text", text: `Successfully edited ${filePath}\n\n${diff}`, }, ], }; }
  • Input schema for the 'edit_file' tool, defining required parameters: 'path' (string) and 'edits' (array of objects with 'oldText' and 'newText').
    inputSchema: { type: "object", properties: { path: { type: "string", description: "The path to the file to edit", }, edits: { type: "array", description: "Array of edit operations to apply", items: { type: "object", properties: { oldText: { type: "string", description: "The exact text to search for (can be multiple lines)", }, newText: { type: "string", description: "The text to replace it with", }, }, required: ["oldText", "newText"], }, }, }, required: ["path", "edits"], },
  • src/index.ts:52-83 (registration)
    Registration of the 'edit_file' tool in the TOOLS array, which is returned by the listTools handler. Includes name, description, and input schema.
    { name: "edit_file", description: "Make line-based edits to a text file. Provide original lines and their replacements. Returns a git-style diff showing the changes made. Each edit replaces exact line sequences with new content.", inputSchema: { type: "object", properties: { path: { type: "string", description: "The path to the file to edit", }, edits: { type: "array", description: "Array of edit operations to apply", items: { type: "object", properties: { oldText: { type: "string", description: "The exact text to search for (can be multiple lines)", }, newText: { type: "string", description: "The text to replace it with", }, }, required: ["oldText", "newText"], }, }, }, required: ["path", "edits"], }, },
  • Helper function 'generateDiff' that creates a simple git-style unified diff between original and modified file contents. Called by the edit_file handler to include the diff in the response.
    function generateDiff( original: string, modified: string, filepath: string ): string { const originalLines = original.split("\n"); const modifiedLines = modified.split("\n"); let diff = `--- ${filepath}\n+++ ${filepath}\n`; let lineNum = 0; while ( lineNum < originalLines.length || lineNum < modifiedLines.length ) { if (originalLines[lineNum] !== modifiedLines[lineNum]) { diff += `@@ -${lineNum + 1} +${lineNum + 1} @@\n`; if (lineNum < originalLines.length) { diff += `- ${originalLines[lineNum]}\n`; } if (lineNum < modifiedLines.length) { diff += `+ ${modifiedLines[lineNum]}\n`; } } lineNum++; } return diff; }

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/koopatroopa787/first_mcp'

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