edit_file
Replace specific text in local development files by matching exact content, enabling precise file modifications for coding tasks.
Instructions
Edit a file by replacing specific text. The old_text must match exactly (including whitespace).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file | |
| old_text | Yes | Text to find and replace (must match exactly) | |
| new_text | Yes | Text to replace it with |
Implementation Reference
- index.js:243-262 (handler)The handler function that implements the logic for the 'edit_file' tool: reads the file, checks if old_text exists, replaces it with new_text, and writes the file back.async editFile(filePath, oldText, newText) { const resolvedPath = this.resolvePath(filePath); let content = await fs.readFile(resolvedPath, 'utf-8'); if (!content.includes(oldText)) { throw new Error(`Text not found in file: ${oldText.substring(0, 50)}...`); } content = content.replace(oldText, newText); await fs.writeFile(resolvedPath, content, 'utf-8'); return { content: [ { type: 'text', text: `File edited successfully: ${resolvedPath}`, }, ], }; }
- index.js:79-100 (registration)Registration of the 'edit_file' tool in the ListTools response, including name, description, and input schema.{ name: 'edit_file', description: 'Edit a file by replacing specific text. The old_text must match exactly (including whitespace).', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file', }, old_text: { type: 'string', description: 'Text to find and replace (must match exactly)', }, new_text: { type: 'string', description: 'Text to replace it with', }, }, required: ['path', 'old_text', 'new_text'], }, },
- index.js:169-170 (handler)The switch case in the CallToolRequestHandler that dispatches 'edit_file' calls to the editFile method.case 'edit_file': return await this.editFile(args.path, args.old_text, args.new_text);
- index.js:82-99 (schema)The input schema defining the parameters for the 'edit_file' tool.inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file', }, old_text: { type: 'string', description: 'Text to find and replace (must match exactly)', }, new_text: { type: 'string', description: 'Text to replace it with', }, }, required: ['path', 'old_text', 'new_text'], },