new_file
Creates a new file at the specified path, automatically generates parent directories if missing, and selects the first line for immediate editing using the editor-mcp server API.
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
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes |
Implementation Reference
- src/text_editor/server.py:802-853 (handler)The main 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 by setting selection state and computing its ID, and returns success details including the selection info.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)}"}