update_file
Modify specific text within a file using search and replace operations. Supports regex, selective updates, and optional backups for precise file editing.
Instructions
Update specific parts of a file using search and replace
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| backup | No | Create backup before updating (.bak extension) | |
| path | Yes | File path relative to working directory | |
| updates | Yes | Array of search/replace operations |
Implementation Reference
- server.js:542-600 (handler)The handler function for the 'update_file' tool. It reads the file content, optionally creates a backup, applies multiple search-and-replace updates (supporting regex and all/first occurrence options), writes the updated content back, and returns a success message with update count.async handleUpdateFile(args) { const { path: filePath, updates, backup = true } = args; const fullPath = path.resolve(this.workingDirectory, filePath); try { // Read current content let content = await fs.readFile(fullPath, 'utf8'); const originalContent = content; // Create backup if requested if (backup) { await fs.writeFile(`${fullPath}.bak`, originalContent, 'utf8'); } // Apply updates let updateCount = 0; for (const update of updates) { const { search, replace, regex = false, all = true } = update; if (regex) { const flags = all ? 'g' : ''; const pattern = new RegExp(search, flags); const matches = content.match(pattern); if (matches) { content = content.replace(pattern, replace); updateCount += matches.length; } } else { if (all) { const parts = content.split(search); if (parts.length > 1) { content = parts.join(replace); updateCount += parts.length - 1; } } else { const index = content.indexOf(search); if (index !== -1) { content = content.substring(0, index) + replace + content.substring(index + search.length); updateCount++; } } } } // Write updated content await fs.writeFile(fullPath, content, 'utf8'); return { content: [ { type: 'text', text: `File updated successfully: ${filePath}\nReplacements made: ${updateCount}${backup ? '\nBackup created: ' + filePath + '.bak' : ''}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to update file: ${error.message}`); } }
- server.js:209-251 (schema)The JSON schema defining the input parameters for the 'update_file' tool, including path, array of updates (search/replace with options), and backup flag.inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, updates: { type: 'array', description: 'Array of search/replace operations', items: { type: 'object', properties: { search: { type: 'string', description: 'Text to search for (exact match)', }, replace: { type: 'string', description: 'Text to replace with', }, regex: { type: 'boolean', description: 'Use regex for search', default: false, }, all: { type: 'boolean', description: 'Replace all occurrences', default: true, }, }, required: ['search', 'replace'], }, }, backup: { type: 'boolean', description: 'Create backup before updating (.bak extension)', default: true, }, }, required: ['path', 'updates'], },
- server.js:476-477 (registration)The switch case in the tool dispatcher (CallToolRequestSchema handler) that routes 'update_file' calls to the handleUpdateFile method.case 'update_file': return await this.handleUpdateFile(args);
- server.js:206-252 (registration)The tool registration object in the tools list provided to ListToolsRequestSchema handler, including name, description, and inputSchema.{ name: 'update_file', description: 'Update specific parts of a file using search and replace', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, updates: { type: 'array', description: 'Array of search/replace operations', items: { type: 'object', properties: { search: { type: 'string', description: 'Text to search for (exact match)', }, replace: { type: 'string', description: 'Text to replace with', }, regex: { type: 'boolean', description: 'Use regex for search', default: false, }, all: { type: 'boolean', description: 'Replace all occurrences', default: true, }, }, required: ['search', 'replace'], }, }, backup: { type: 'boolean', description: 'Create backup before updating (.bak extension)', default: true, }, }, required: ['path', 'updates'], }, },
- server.js:744-747 (helper)Usage of handleUpdateFile within the batch_file_operations tool, with backup disabled for batch context.case 'update': result = await this.handleUpdateFile({ ...params, backup: false }); performedOperations.push({ type: 'update', path: params.path, backupPath: `${params.path}.batch-backup` }); break;