Creating cursor rules programmatically?

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!

2 Likes

We dont have any formal feature for this yet, but auto-generating some Project Rules is something the team have discussed already!

What would really help Dan is for Cursor to be able to edit / create cursor rules. It fails when applying and gets into a real mess. I have to ask it to output the markdown and copy and paste in the changes like it was 2024.

1 Like

This is definitely on the horizon for being added in a future update!

it’s just a markdown file with yaml header inside “—” lines, dude

---
description: for testing things
globs: *.py
alwaysApply: false
---

# Your rule content

- You can @ files here
- You can use markdown but dont have to
1 Like

With 0.46.10 this is now possible

0.46.10: Fix for chat/apply not working with .github and other . prefixed folders, and small performance fixes.

You need to get VSCode to treat the rules as markdown and you will get the nice Accept / Reject UI

Cursor does not know where rules should be stored so you need to be very explicit but when you are it can update them.

{
    "workbench.editorAssociations": {
        "*.mdc": "default"
    },
    "files.associations": {
        "*.mdc": "markdown"
    }
}