rename_file
Rename or move files within directories by specifying old and new paths, with optional overwrite protection for existing files.
Instructions
Rename or move a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| old_path | Yes | Current file path | |
| new_path | Yes | New file path | |
| overwrite | No | Overwrite if destination exists |
Implementation Reference
- server.js:668-705 (handler)The core handler function implementing the rename_file tool. It destructures arguments, resolves full paths relative to working directory, validates source existence and handles destination overwrite option, ensures target directory exists, performs the rename using fs.rename(), and returns a success message or throws an MCP error on failure.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)Tool registration entry in the ListToolsRequestSchema handler's tools array. Defines the tool name, description, and input schema for MCP clients to discover and validate calls.{ 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)JSON Schema defining the input parameters for the rename_file tool: old_path (required string), new_path (required string), overwrite (optional boolean with default false).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)Dispatch case in the CallToolRequestSchema handler's switch statement that routes 'rename_file' tool calls to the handleRenameFile method.case 'rename_file': return await this.handleRenameFile(args);