Skip to main content
Glama
safurrier

MCP Filesystem Server

move_file

Move or rename files and directories by specifying source and destination paths, with optional overwrite protection for existing files.

Instructions

Move or rename files and directories.

Args:
    source: Source path
    destination: Destination path
    overwrite: Whether to overwrite existing destination
    ctx: MCP context

Returns:
    Success or error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYes
destinationYes
overwriteNo

Implementation Reference

  • MCP tool handler for 'move_file'. This is the entry point decorated with @mcp.tool(), handling input parameters, error catching, and delegating to the core operations.move_file method.
    @mcp.tool()
    async def move_file(
        source: str,
        destination: str,
        ctx: Context,
        overwrite: bool = False,
    ) -> str:
        """Move or rename files and directories.
    
        Args:
            source: Source path
            destination: Destination path
            overwrite: Whether to overwrite existing destination
            ctx: MCP context
    
        Returns:
            Success or error message
        """
        try:
            components = get_components()
            await components["operations"].move_file(source, destination, overwrite)
            return f"Successfully moved {source} to {destination}"
        except Exception as e:
            return f"Error moving file: {str(e)}"
  • Core implementation of the move_file logic in FileOperations class. Validates paths using PathValidator, checks existence and overwrite conditions, then performs the move using shutil.move in a thread.
    async def move_file(
        self,
        source: Union[str, Path],
        destination: Union[str, Path],
        overwrite: bool = False,
    ) -> None:
        """Move or rename a file or directory.
    
        Args:
            source: Source path
            destination: Destination path
            overwrite: Whether to overwrite destination if it exists
    
        Raises:
            ValueError: If paths are outside allowed directories
            FileNotFoundError: If source does not exist
            FileExistsError: If destination exists and overwrite is False
        """
        source_path, source_allowed = await self.validator.validate_path(source)
        if not source_allowed:
            raise ValueError(f"Source path outside allowed directories: {source}")
    
        dest_path, dest_allowed = await self.validator.validate_path(destination)
        if not dest_allowed:
            raise ValueError(
                f"Destination path outside allowed directories: {destination}"
            )
    
        # Check if source exists
        if not source_path.exists():
            raise FileNotFoundError(f"Source does not exist: {source}")
    
        # Check if destination exists and we're not overwriting
        if dest_path.exists() and not overwrite:
            raise FileExistsError(f"Destination already exists: {destination}")
    
        try:
            # Use shutil.move which handles cross-filesystem moves
            await anyio.to_thread.run_sync(shutil.move, source_path, dest_path)
        except (PermissionError, shutil.Error) as e:
            raise ValueError(f"Cannot move file: {e}")

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/safurrier/mcp-filesystem'

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