rename_file
Rename or move files and directories in a single operation. Works across directories for easy relocation. Fails if destination already exists to prevent overwrites. Both paths must be within allowed directories.
Instructions
Rename or move a file or directory. Can move files between directories and rename them in a single operation. If the destination exists, the operation will fail. Works across different directories and can be used for simple renaming within the same directory. Both source and destination must be within allowed directories.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| oldPath | Yes | Current path of the file or directory to rename | |
| newPath | Yes | New path for the file or directory |
Implementation Reference
- src/rename-file.ts:32-106 (handler)The main handler function that executes the rename_file tool logic. It resolves paths, validates they are within allowed directories, checks source exists and destination doesn't, creates destination parent directory if needed, then performs the rename/move using fs.renameSync.
export async function handleRenameFile(args: any, allowedDirectories: string[]) { const { oldPath, newPath } = args; if (!oldPath || !newPath) { throw new McpError(ErrorCode.InvalidParams, "Both oldPath and newPath are required"); } // Resolve to absolute paths const resolvedOldPath = path.resolve(oldPath); const resolvedNewPath = path.resolve(newPath); // Check if source is within allowed directories const isOldPathAllowed = allowedDirectories.some(dir => { const normalizedDir = path.resolve(dir).replace(/\\/g, '/'); const normalizedOldPath = resolvedOldPath.replace(/\\/g, '/'); return normalizedOldPath === normalizedDir || normalizedOldPath.startsWith(normalizedDir + '/'); }); // Check if destination is within allowed directories const isNewPathAllowed = allowedDirectories.some(dir => { const normalizedDir = path.resolve(dir).replace(/\\/g, '/'); const normalizedNewPath = resolvedNewPath.replace(/\\/g, '/'); return normalizedNewPath === normalizedDir || normalizedNewPath.startsWith(normalizedDir + '/'); }); if (!isOldPathAllowed) { throw new McpError(ErrorCode.InvalidParams, `Source path "${resolvedOldPath}" is not within allowed directories`); } if (!isNewPathAllowed) { throw new McpError(ErrorCode.InvalidParams, `Destination path "${resolvedNewPath}" is not within allowed directories`); } try { // Check if source exists if (!fs.existsSync(resolvedOldPath)) { throw new McpError(ErrorCode.InvalidParams, `Source path "${resolvedOldPath}" does not exist`); } // Check if destination already exists if (fs.existsSync(resolvedNewPath)) { throw new McpError(ErrorCode.InvalidParams, `Destination path "${resolvedNewPath}" already exists`); } // Ensure destination directory exists const destDir = path.dirname(resolvedNewPath); if (!fs.existsSync(destDir)) { fs.mkdirSync(destDir, { recursive: true }); } // Perform the rename/move operation fs.renameSync(resolvedOldPath, resolvedNewPath); const message = `# File/Directory Rename/Move Successful\n\nā Successfully renamed/moved "${oldPath}" to "${newPath}"\n\n**Details:**\n- Source: ${resolvedOldPath}\n- Destination: ${resolvedNewPath}\n- Operation: ${path.dirname(resolvedOldPath) === path.dirname(resolvedNewPath) ? 'Rename' : 'Move'}`; return { content: [ { type: "text", text: message } ] }; } catch (error: any) { if (error instanceof McpError) { throw error; } throw new McpError( ErrorCode.InternalError, `Failed to rename file: ${error.message}` ); } } - src/rename-file.ts:12-30 (schema)The tool definition including name ('rename_file'), description, and inputSchema (requires oldPath and newPath strings).
export const renameFileTool = { name: "rename_file", description: "Rename or move a file or directory. Can move files between directories and rename them in a single operation. If the destination exists, the operation will fail. Works across different directories and can be used for simple renaming within the same directory. Both source and destination must be within allowed directories.", inputSchema: { type: "object", properties: { oldPath: { type: "string", description: "Current path of the file or directory to rename" }, newPath: { type: "string", description: "New path for the file or directory" } }, required: ["oldPath", "newPath"], additionalProperties: false } }; - src/index.ts:33-44 (registration)The tool is registered in the ListToolsRequestSchema handler, returning renameFileTool in the tools array.
server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ exploreProjectTool, listAllowedTool, searchTool, renameFileTool, deleteFileTool, checkOutdatedTool ] }; }); - src/index.ts:62-63 (registration)The tool handler routing in CallToolRequestSchema: when the tool name is 'rename_file', it calls handleRenameFile(args, ALLOWED_DIRECTORIES).
case "rename_file": return await handleRenameFile(args, ALLOWED_DIRECTORIES); - src/rename-file.ts:5-10 (helper)The RenameResult interface defining the return shape for a rename operation.
export interface RenameResult { success: boolean; oldPath: string; newPath: string; message: string; }