Skip to main content
Glama

new_file

Create a new text file at a specified path, automatically generating parent directories and selecting the first line for immediate editing.

Instructions

Creates a new file.

After creating new file, the first line is automatically selected for editing. Automatically creates parent directories if they don't exist.

Args: filepath (str): Path of the new file Returns: dict: Status message with selection info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes

Implementation Reference

  • The handler function for the 'new_file' tool. It sets the current file path, checks if the file exists and is empty, creates parent directories if needed, writes a header to the new file, selects the first line for editing, computes its ID, and returns a success response with selection details.
    async def new_file(filepath: str) -> Dict[str, Any]:
        """
        Creates a new file.
    
        After creating new file, the first line is automatically selected for editing.
        Automatically creates parent directories if they don't exist.
    
        Args:
            filepath (str): Path of the new file
        Returns:
            dict: Status message with selection info
    
        """
        self.current_file_path = filepath
    
        if (
            os.path.exists(self.current_file_path)
            and os.path.getsize(self.current_file_path) > 0
        ):
            return {
                "error": "Cannot create new file. Current file exists and is not empty."
            }
    
        try:
            # Create parent directories if they don't exist
            directory = os.path.dirname(self.current_file_path)
            if directory:
                os.makedirs(directory, exist_ok=True)
    
            text = "# NEW_FILE - REMOVE THIS HEADER"
            with open(self.current_file_path, "w", encoding="utf-8") as file:
                file.write(text)
    
            # Automatically select the first line for editing
            self.selected_start = 1
            self.selected_end = 1
            self.selected_id = calculate_id(text, 1, 1)
    
            result = {
                "status": "success",
                "text": text,
                "current_file_path": self.current_file_path,
                "id": self.selected_id,
                "selected_start": self.selected_start,
                "selected_end": self.selected_end,
                "message": "File created successfully. First line is now selected for editing.",
            }
    
            return result
        except Exception as e:
            return {"error": f"Error creating file: {str(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/danielpodrazka/editor-mcp'

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