move_file
Move or rename files and directories by specifying source and destination paths, with optional overwrite capability for existing items.
Instructions
Move or rename a file or directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source | Yes | Source path | |
| destination | Yes | Destination path | |
| overwrite | No | Overwrite if exists |
Implementation Reference
- src/tools/write.ts:333-429 (handler)The handler function that executes the logic for the `move_file` tool.
async function moveFileImpl(input: MoveFileInput): Promise<ToolResult> { try { const srcPath = path.resolve(input.source); const destPath = path.resolve(input.destination); // Check if source exists await fs.stat(srcPath); // Check if destination exists let destExists = false; try { await fs.access(destPath); destExists = true; } catch { // Destination doesn't exist } if (destExists && !input.overwrite) { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'ALREADY_EXISTS', message: `Destination already exists and overwrite is false: ${input.destination}`, }), }, ], }; } // Ensure parent directory exists await fs.mkdir(path.dirname(destPath), { recursive: true }); // Move/rename await fs.rename(srcPath, destPath); return { content: [ { type: 'text', text: JSON.stringify({ success: true, source: srcPath, destination: destPath, }), }, ], }; } catch (error) { const err = error as NodeJS.ErrnoException; if (err.code === 'ENOENT') { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'FILE_NOT_FOUND', message: `Source not found: ${input.source}`, }), }, ], }; } if (err.code === 'EACCES') { return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'PERMISSION_DENIED', message: `Permission denied`, }), }, ], }; } return { isError: true, content: [ { type: 'text', text: JSON.stringify({ code: 'UNKNOWN_ERROR', message: `Error moving: ${err.message}`, }), }, ], }; } } - src/types.ts:203-209 (schema)Input schema and type definitions for the `move_file` tool.
export const MoveFileInputSchema = z.object({ source: z.string().describe('Source path'), destination: z.string().describe('Destination path'), overwrite: z.boolean().default(false).describe('Overwrite if exists'), }); export type MoveFileInput = z.infer<typeof MoveFileInputSchema>; - src/tools/write.ts:592-605 (registration)Registration of the `move_file` tool with the MCP server.
// move_file tool server.tool( 'move_file', 'Move or rename a file or directory', { source: z.string().describe('Source path'), destination: z.string().describe('Destination path'), overwrite: z.boolean().optional().describe('Overwrite if exists'), }, async (args) => { const input = MoveFileInputSchema.parse(args); return await moveFileImpl(input); } );