I’m wondering if it’s possible to create cursor rules programmatically?
Claude gave me this code to create .mdc files:
import os
from dataclasses import dataclass
from pathlib import Path
from typing import List
@dataclass
class CursorRule:
name: str
description: str
globs: List[str]
class CursorRuleGenerator:
@staticmethod
def create_rule(rule: CursorRule, directory: str = ".cursor/rules") -> None:
"""
Create a .mdc file with the specified rule details.
Args:
rule: CursorRule object containing the rule details
directory: Target directory for the .mdc file
"""
# Ensure the directory exists
Path(directory).mkdir(parents=True, exist_ok=True)
# Create the content for the .mdc file
content = f"""Rule Name: {rule.name}
Description: {rule.description}
Globs:
{os.linesep.join(f"- {glob}" for glob in rule.globs)}"""
# Create the file path
file_path = Path(directory) / f"{rule.name}.mdc"
# Write the content to the file
file_path.write_text(content, encoding="utf-8")
# Example usage
if __name__ == "__main__":
# Single rule example
pizza_rule = CursorRule(
name="pizza-rule",
description="Make sure the word pizza is in the code",
globs=["*.swift", "*.js"],
)
CursorRuleGenerator.create_rule(pizza_rule)
# Multiple rules example
rules = [
CursorRule(
name="naming-convention",
description="Ensure proper naming conventions are followed",
globs=["*.swift"],
),
CursorRule(
name="documentation-check",
description="Check if public functions are documented",
globs=["*.swift", "*.h"],
),
]
for rule in rules:
CursorRuleGenerator.create_rule(rule)
The problem is it doesn’t fill out the description and globs fields in the .mdc. Does anyone know how to achieve this? Thank you for any help!