Sample MDC Files and Explanations
Explore a collection of sample MDC files and learn how to effectively use them with Cursor's AI.
Introduction
Below are several sample MDC (Markdown Domain Configuration) files designed for use with Cursor, an AI-powered coding assistant. Each file includes explanations of its content and how it is used to guide the AI's behavior. These examples demonstrate how to structure MDC files, use glob patterns, and write effective rules for different scenarios.
Sample MDC File 1: TypeScript Coding Standards
File Name: typescript-rules.mdc
Purpose: This file defines coding standards for TypeScript files, helping the AI enforce consistent practices like variable declarations and function syntax.
Content
---
description: TypeScript coding standards
globs: **/*.ts, **/*.tsx
---
# TypeScript Rules
- Use 'const' for variables that don't change
- Prefer arrow functions for callbacks
- Always specify return types for functions
- Use interfaces for complex types
Explanation
- Frontmatter:
- description: Provides a brief summary of the rule's purpose, which helps both the developer and the AI understand its intent.
- globs: Targets all .ts and .tsx files in the project, ensuring the rules apply to TypeScript and TypeScript React files.
- Rule Content:
- Written in markdown, this section includes a list of key TypeScript guidelines.
- The rules are concise and focus on essential practices, making it easy for the AI to apply them when suggesting code.
How It's Used: When editing a .ts or .tsx file, Cursor's AI will reference these rules to ensure suggestions align with the specified standards, such as using const for immutable variables or specifying return types for functions.
Sample MDC File 2: React Component Guidelines
File Name: react-rules.mdc
Purpose: This file provides guidelines for writing React components, ensuring consistency in component structure and best practices.
Content
---
description: React component guidelines
globs: src/components/*.jsx, src/components/*.tsx
---
# React Rules
- Use functional components over class components
- Implement proper prop types
- Follow the team's style guide @style-guide.md
- Use hooks for state management
Explanation
- Frontmatter:
- description: Clearly states the rule's focus on React components.
- globs: Targets .jsx and .tsx files specifically in the src/components directory, ensuring the rules apply only to component files.
- Rule Content:
- Includes best practices for React, such as preferring functional components and using hooks.
- References an external file (@style-guide.md) using the @ syntax to include additional context, which the AI can use for more detailed guidance.
How It's Used: When working on React components in the specified directory, the AI will apply these rules to suggest code that follows functional component patterns, implements prop types, and uses hooks for state management.
Sample MDC File 3: Testing Conventions
File Name: testing-rules.mdc
Purpose: This file outlines conventions for writing tests, ensuring the AI provides suggestions that align with the project's testing framework and coverage goals.
Content
---
description: Testing conventions
globs: **/*.test.ts
---
# Testing Rules
- Use Jest for unit tests
- Cover one module per test file
- Target 80%+ code coverage
- Follow TDD where possible
Explanation
- Frontmatter:
- description: Indicates the rule's focus on testing practices.
- globs: Applies to all files ending with .test.ts, which are typically test files in a TypeScript project.
- Rule Content:
- Specifies the testing framework (Jest) and key testing principles, such as test coverage and TDD (Test-Driven Development).
- The rules are clear and actionable, guiding the AI to suggest appropriate test structures.
How It's Used: When editing test files, the AI will reference these rules to ensure suggestions align with the project's testing standards, such as using Jest, organizing tests by module, and aiming for high code coverage.
Sample MDC File 4: General Project Conventions
File Name: project-conventions.mdc
Purpose: This file sets broad conventions that apply to the entire project, providing a baseline for coding practices across all files.
Content
---
description: General project conventions
globs: **/*
---
# Project Conventions
- Follow the project's naming conventions
- Use consistent indentation (4 spaces)
- Write clear and concise commit messages
- Document public APIs with JSDoc
Explanation
- Frontmatter:
- description: Indicates that these are general rules for the entire project.
- globs: Uses **/* to apply the rules to all files in the project.
- Rule Content:
- Includes high-level guidelines like naming conventions, indentation, and documentation practices.
- These rules are broad enough to be relevant across different file types and directories.
How It's Used: The AI will apply these rules universally, ensuring that suggestions for any file in the project adhere to fundamental practices like consistent indentation and clear commit messages.
How These MDC Files Are Used in Cursor
MDC files are stored in the .cursor/rules directory of your project and are written in markdown with frontmatter metadata. Cursor's AI uses them to provide context-aware coding assistance as follows:
- Context-Aware Assistance: When you edit a file, the AI automatically includes relevant MDC files in its context based on the file's path and the glob patterns defined in the frontmatter.
- For example, editing a file in src/components/App.tsx would trigger both react-rules.mdc (due to the glob src/components/*.tsx) and typescript-rules.mdc (due to **/*.tsx).
- Layered Rules: If multiple MDC files apply to a file, the AI considers all relevant rules, allowing for layered guidance (e.g., general project conventions plus specific language or framework rules).
- References to Other Files: Using @ to reference other files (like @style-guide.md) pulls in additional context, giving the AI more information to work with when making suggestions.
Best Practices for Creating MDC Files
- Be Specific with Globs: Use precise glob patterns to target the right files. Overly broad patterns (like **/*) may apply rules where they aren't needed, so reserve them for general guidelines.
- Keep Rules Concise: Focus on key guidelines to avoid overwhelming the AI with too much information. Use bullet points or short sentences for clarity.
- Use Descriptive Names: Name your MDC files clearly (e.g., react-rules.mdc) to make their purpose obvious to both developers and the AI.
- Leverage References: Use @ to link to other files for additional context, especially for detailed guidelines or style guides that don't fit in a single MDC file.
- Organize by Purpose: Create separate MDC files for different aspects of your project (e.g., language standards, framework guidelines, testing conventions) to keep rules manageable and easy to update.
Conclusion
These sample MDC files demonstrate how to guide Cursor's AI to provide tailored, context-aware coding assistance. By defining rules for specific file types (like TypeScript or test files), directories (like React components), or the entire project, you can ensure that the AI's suggestions align with your team's standards and best practices. Experiment with these examples and adapt them to your project's needs to maximize Cursor's effectiveness as a coding assistant.