rename_file
Rename or move files by specifying old and new paths; optionally overwrite existing files. Part of the Enhanced Directory Context MCP Server for advanced file management operations.
Instructions
Rename or move a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| new_path | Yes | New file path | |
| old_path | Yes | Current file path | |
| overwrite | No | Overwrite if destination exists |
Implementation Reference
- server.js:668-705 (handler)The core handler function that performs the file rename/move operation using fs.rename, including existence checks, directory creation, and overwrite handling.async handleRenameFile(args) { const { old_path, new_path, overwrite = false } = args; const oldFullPath = path.resolve(this.workingDirectory, old_path); const newFullPath = path.resolve(this.workingDirectory, new_path); try { // Check if source exists await fs.access(oldFullPath); // Check if destination exists try { await fs.access(newFullPath); if (!overwrite) { throw new Error('Destination file already exists. Set overwrite=true to replace it.'); } } catch (error) { // Destination doesn't exist, which is fine } // Ensure destination directory exists const destDir = path.dirname(newFullPath); await fs.mkdir(destDir, { recursive: true }); // Rename/move file await fs.rename(oldFullPath, newFullPath); return { content: [ { type: 'text', text: `File renamed/moved: ${old_path} → ${new_path}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to rename file: ${error.message}`); } }
- server.js:295-317 (registration)Registers the 'rename_file' tool in the ListToolsRequestSchema response, including name, description, and input schema.{ name: 'rename_file', description: 'Rename or move a file', inputSchema: { type: 'object', properties: { old_path: { type: 'string', description: 'Current file path', }, new_path: { type: 'string', description: 'New file path', }, overwrite: { type: 'boolean', description: 'Overwrite if destination exists', default: false, }, }, required: ['old_path', 'new_path'], }, },
- server.js:298-316 (schema)Input schema defining parameters for the rename_file tool: old_path (required), new_path (required), overwrite (optional boolean).inputSchema: { type: 'object', properties: { old_path: { type: 'string', description: 'Current file path', }, new_path: { type: 'string', description: 'New file path', }, overwrite: { type: 'boolean', description: 'Overwrite if destination exists', default: false, }, }, required: ['old_path', 'new_path'], },
- server.js:485-486 (registration)Tool dispatch in the CallToolRequestSchema handler switch statement.case 'rename_file': return await this.handleRenameFile(args);
- server.js:760-760 (helper)Invocation of handleRenameFile within the batch_file_operations tool.result = await this.handleRenameFile(params);