Skip to main content
Glama

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
NameRequiredDescriptionDefault
sourceYesSource path
destinationYesDestination path
overwriteNoOverwrite if exists

Implementation Reference

  • 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}`,
              }),
            },
          ],
        };
      }
    }
  • 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>;
  • 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);
      }
    );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mcp-tool-shop-org/mcp-file-forge'

If you have feedback or need assistance with the MCP directory API, please join our Discord server