edit_file
Replace specific text in files by finding exact matches and substituting with new content. Use this tool to modify file contents through precise text replacement operations.
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 core handler function for the 'edit_file' tool. It resolves the file path, reads the content, verifies the old_text exists, replaces it with new_text using string.replace (all occurrences), writes the file back, and returns a success message.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:82-99 (schema)Input schema definition for the 'edit_file' tool, defining the required parameters: path, old_text, and new_text.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:79-100 (registration)Registration of the 'edit_file' tool in the ListTools response handler, providing name, description, and 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 (registration)Dispatch case in the CallToolRequestSchema handler that routes 'edit_file' calls to the editFile method.case 'edit_file': return await this.editFile(args.path, args.old_text, args.new_text);