Skip to main content
Glama

edit_file

Make line-based edits to text files by replacing specific line sequences with new content. Returns a git-style diff showing all changes made to the file.

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 switch case handler for the 'edit_file' tool. It reads the target file, applies sequential string replacements for each specified edit (oldText to newText), writes the modified content back to the file, generates a simple diff using generateDiff helper, and returns a success message with 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}`, }, ], }; }
  • The tool definition in the TOOLS array, including name, description, and inputSchema specifying path (string) and edits (array of {oldText, newText} objects). This schema is used for validation and returned by listTools.
    { 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, used exclusively in the edit_file handler to provide feedback on changes.
    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; }
  • src/index.ts:261-263 (registration)
    The listTools request handler that registers all tools, including edit_file, by returning the TOOLS array containing its definition.
    server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: TOOLS }; });

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