create_file
Create files with automatic snake_case naming enforcement to maintain organizational coding standards and prevent policy violations.
Instructions
Create a new file (automatically applies snake_case naming convention)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Desired file path (will be normalized to snake_case) | |
| content | No | File content |
Implementation Reference
- server.py:144-169 (handler)The handler logic for the 'create_file' tool. Normalizes the filename to snake_case, resolves the path securely, checks if the file already exists, creates necessary directories, writes the provided content to the new file, and returns a success message with naming explanation.if name == "create_file": # Apply naming convention requested_path = arguments["path"] normalized_path, explanation = enforce_naming_convention(requested_path) path = resolve_path(normalized_path) content = arguments.get("content", "") # Check if file already exists if path.exists(): return [TextContent( type="text", text=f"Error: File '{normalized_path}' already exists. Use write_file to update it." )] # Create directory if needed path.parent.mkdir(parents=True, exist_ok=True) # Create the file with open(path, 'w', encoding='utf-8') as f: f.write(content) # Return success message with naming convention info message = f"{explanation}\nβ File created successfully: {normalized_path}" return [TextContent(type="text", text=message)]
- server.py:70-88 (registration)Tool registration for 'create_file' in the @app.list_tools() function, including metadata, description, and input schema definition.Tool( name="create_file", description="Create a new file (automatically applies snake_case naming convention)", inputSchema={ "type": "object", "properties": { "path": { "type": "string", "description": "Desired file path (will be normalized to snake_case)" }, "content": { "type": "string", "description": "File content", "default": "" } }, "required": ["path"] } ),
- server.py:13-65 (helper)Supporting helper function that enforces snake_case naming convention on the provided file path, returning the normalized path and an explanation message. Called by the create_file handler.def enforce_naming_convention(filename: str) -> tuple[str, str]: """ Enforce organizational naming convention: convert to snake_case. Returns: (normalized_filename, explanation) """ # Get the filename without path path_parts = filename.split('/') original_name = path_parts[-1] # Split name and extension if '.' in original_name: name_part, extension = original_name.rsplit('.', 1) else: name_part = original_name extension = '' # Convert to snake_case # 1. Replace spaces and hyphens with underscores normalized = name_part.replace(' ', '_').replace('-', '_') # 2. Insert underscore before capital letters (camelCase -> snake_case) normalized = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', normalized) normalized = re.sub('([a-z0-9])([A-Z])', r'\1_\2', normalized) # 3. Convert to lowercase normalized = normalized.lower() # 4. Remove consecutive underscores normalized = re.sub('_+', '_', normalized) # 5. Remove leading/trailing underscores normalized = normalized.strip('_') # Reconstruct full filename if extension: normalized_filename = f"{normalized}.{extension}" else: normalized_filename = normalized # Reconstruct full path if len(path_parts) > 1: path_parts[-1] = normalized_filename full_normalized = '/'.join(path_parts) else: full_normalized = normalized_filename # Create explanation if name was changed if original_name != normalized_filename: explanation = f"π Naming convention applied: '{original_name}' β '{normalized_filename}'" else: explanation = f"β Filename already follows convention: '{normalized_filename}'" return full_normalized, explanation