How to Write Cursor Rules: .mdc Project Rules, AGENTS.md and Legacy .cursorrules
How to write Cursor project rules in .cursor/rules/*.mdc: frontmatter (description, globs, alwaysApply), the four rule types, AGENTS.md in root and subdirectories, and migrating from a legacy .cursorrules file.
Cursor rules are persistent instructions that Cursor's Agent reads alongside your request. A good set of rules stops you from correcting the same things over and over: the wrong framework pattern, the wrong library, the wrong file location.
This guide covers what Cursor's current documentation describes: project rules as .mdc files in .cursor/rules/, AGENTS.md files, and User and Team Rules. It also explains how to move off the older single-file .cursorrules format. All behavior described here comes from Cursor's Rules documentation (checked 2026-11-05). Cursor changes quickly, so check that page if something behaves differently in your version.
The Rule Sources Cursor Documents Today
Cursor's Rules page (checked 2026-11-05) describes four places instructions can come from:
| Source | Where it lives | Format | Scope |
|---|---|---|---|
| Project Rules | .cursor/rules/ (subfolders allowed), committed to the repo | .mdc files with frontmatter | Always, by file pattern, by relevance, or manually |
| AGENTS.md | Project root and any subdirectory | Plain Markdown, no metadata | The directory it sits in and its children |
| User Rules | Cursor's Customize → Rules | Free text | All your projects; used by Agent (Chat) |
| Team Rules | Cursor dashboard (Team and Enterprise plans) | Free text, optional glob patterns | Everyone on the team; admins can enforce them |
The docs give the order when sources conflict: Team Rules → Project Rules → User Rules. All applicable rules are merged, and the earlier source wins when guidance conflicts.
Two details from the docs are easy to miss:
- Project rules must use the
.mdcextension. Cursor says a plain.mdfile in.cursor/rulesis ignored by the rules system because it has no frontmatter. If you want plain Markdown, useAGENTS.mdinstead. - Rules don't affect Cursor Tab (autocomplete) or other AI features. They shape Agent behavior.
Project Rules: The .mdc Format
Each project rule is a Markdown file with a short frontmatter block. Three fields control when Cursor includes it: description, globs and alwaysApply. In the editor you normally pick a rule type from a dropdown, and Cursor sets the fields for you.
The four rule types
From Cursor's Rules documentation (checked 2026-11-05):
| Rule type | alwaysApply | description | globs | When it's included |
|---|---|---|---|---|
| Always Apply | true | — | — | Every chat session. Globs and description are ignored. |
| Apply to Specific Files | false | — | provided | When a file matching the pattern is in context |
| Apply Intelligently | false | provided | omitted | When Agent judges the description relevant |
| Apply Manually | false | omitted | omitted | Only when you @-mention it, e.g. @my-rule |
Use Apply to Specific Files for conventions tied to file types or folders. Use Apply Intelligently for topics like "how we write migrations" that don't map neatly to a path. Keep Always Apply for the few facts that really matter everywhere, because those rules use context in every session.
Glob syntax
Cursor's docs separate multiple patterns with commas in a single globs value, for example docs/**/*.md, docs/**/*.mdx. Earlier versions of this guide showed a YAML array. Use the comma-separated form the docs show.
Example: a scoped component rule
Illustrative example. It follows the frontmatter format in Cursor's docs, but we haven't measured its effect on output. Adapt it to your stack and test it in your own project.
.cursor/rules/react-components.mdc:
---
description: Conventions for React components in src/components and src/app
globs: src/components/**/*.tsx, src/app/**/*.tsx
alwaysApply: false
---
- Use function components with a TypeScript `interface` for props
- Use named exports (except Next.js `page.tsx` and `layout.tsx`, which need default exports)
- Style with Tailwind utility classes; no inline `style` props or CSS modules
- Use `next/image` for images, never a bare `<img>`
- Before creating a new component, read `@src/components/ui/button.tsx` for the house pattern
Here globs is set and alwaysApply is false, so this is an Apply to Specific Files rule. It's attached whenever a matching .tsx file is in context. The description is optional for this rule type, but it documents what the rule covers. If you pick the rule type in Cursor's dropdown, Cursor sets these fields for you, so check the saved file afterwards.
The last line uses a file reference. Cursor's docs say referenced files are not inlined into the prompt: Agent reads them with its tools when it needs them. Put anything that must always be in context directly in the rule body.
Example: a manual-only rule
---
alwaysApply: false
---
- Every database migration must include both `up` and `down` steps
- Never change a column type in place: add a new column, backfill, then drop the old one in a separate migration
- Read `@prisma/migrations/README.md` before writing a migration
With no description and no globs, this rule is included only when you type @ plus the rule's name in chat.
A typical layout
.cursor/
rules/
project-overview.mdc # Always Apply: stack, repo map (keep it short)
react-components.mdc # Apply to Specific Files: src/components/**/*.tsx
api-routes.mdc # Apply to Specific Files: src/app/api/**/*.ts
testing.mdc # Apply to Specific Files: **/*.test.ts, **/*.spec.ts
migrations.mdc # Apply Manually
Creating rules
Cursor documents two ways (checked 2026-11-05):
- Type
/create-rulein Agent chat and describe the rule. Agent writes the file with frontmatter into.cursor/rules. - Open Customize → Rules → Add Rule.
AGENTS.md: Plain Markdown, Root and Subdirectories
Cursor's docs state: "Cursor supports AGENTS.md in the project root and subdirectories" (Cursor Rules docs, checked 2026-11-05). AGENTS.md is plain Markdown with no frontmatter. Nested files apply when Agent works on files in that directory or its children. Instructions from nested files are combined with their parents', and the more specific file takes precedence.
project/
AGENTS.md # repo-wide instructions
frontend/
AGENTS.md # frontend-specific
components/
AGENTS.md # component-specific
backend/
AGENTS.md # backend-specific
When to use AGENTS.md instead of .mdc rules:
- You want simple, readable instructions without frontmatter.
- Your repo is also used with other agents. Several tools read
AGENTS.md. For example, Claude Code's memory docs say it readsAGENTS.mdwhen there's noCLAUDE.mdin the working directory or above it (checked 2026-11-05). Check each tool's docs for its exact behavior. - Directory scoping (a file per folder) is enough, and you don't need glob or relevance-based activation.
Cursor's CLI docs also say the CLI agent uses the same .cursor/rules system and reads AGENTS.md and CLAUDE.md at the project root (Using the Cursor CLI, checked 2026-11-05).
Migrating from a Legacy .cursorrules File
Older Cursor setups used one .cursorrules file at the project root. Cursor's current Rules documentation doesn't mention .cursorrules at all (checked 2026-11-05), so treat it as a legacy, undocumented format. Don't count on it for new projects. If you still have one, migrate:
- Inventory the file. Sort each instruction into: applies everywhere, applies to certain paths, or applies only to certain tasks.
- Move "everywhere" items into a short root
AGENTS.md, or into one.mdcrule withalwaysApply: true. Keep it brief. - Move path-specific items into
.mdcfiles withglobs(Apply to Specific Files). - Move task-specific items (migrations, release steps) into rules with a
description(Apply Intelligently) or no metadata (Apply Manually). - Delete or archive
.cursorrulesonce the new rules are in place, so the two sources don't drift apart. - Verify. Open a file covered by each glob, ask Agent for a small change, and check that it follows the rule. Cursor's Customize view also lists rules and their status.
Our Cursor Rules Generator produces a .cursor/rules/*.mdc file (with the rule type you choose) or an AGENTS.md. Earlier versions downloaded a single .cursorrules file; if you have one of those, move its sections into .mdc rules or AGENTS.md as described above. Treat generated output as a draft and review it before committing.
What to Put in Your Rules
The content advice applies whichever file you use.
1. Project context (short)
This is a SaaS app: Next.js (App Router), TypeScript strict mode, Tailwind CSS,
Prisma with PostgreSQL. Server Components by default.
Name versions only if they change what code the AI should write (for example, App Router vs Pages Router), and update them when you upgrade. Stale version numbers mislead.
2. Coding standards that differ from defaults
- Use async/await, not .then() chains
- Named exports (except Next.js pages and layouts)
- `interface` for object shapes, `type` for unions
- Wrap async operations in try/catch and return typed error objects
3. Naming and file organization
- Components: PascalCase files (UserProfile.tsx); hooks: useX.ts
- Server actions: src/actions/; DB queries: src/lib/db/; types: src/types/
- Tests colocated as *.test.ts
4. Preferred libraries
- Dates: date-fns. Validation: Zod. Forms: react-hook-form + Zod resolver
- HTTP: native fetch (no axios). Icons: lucide-react
5. Explicit "don'ts"
- No `any`; use proper types or `unknown`
- No new dependencies without asking
- Never edit generated files in dist/ or build/
- Never change the database schema without a Prisma migration
6. Patterns shown, not described
For complex patterns, reference a real file (@src/app/api/users/route.ts) instead of pasting code. Cursor's docs recommend referencing files because it keeps rules short and stops them going stale.
Before and After: What Rules Change
Illustrative example, not a measured result. AI output varies between runs and models. This shows the kind of drift that rules are meant to prevent.
Request: "Create a user profile component."
Without rules, an assistant may fall back to generic patterns: a class component, a separate CSS file, inline styles, a default export, no types.
With the react-components.mdc rule above in context, you'd expect output closer to this:
import Image from "next/image";
interface UserProfileProps {
name: string;
email: string;
avatarUrl?: string;
}
export function UserProfile({ name, email, avatarUrl }: UserProfileProps) {
return (
<div className="rounded-lg border bg-card p-6">
<div className="flex items-center gap-4">
{avatarUrl && (
<Image src={avatarUrl} alt={name} width={48} height={48} className="rounded-full" />
)}
<div>
<h2 className="text-lg font-semibold">{name}</h2>
<p className="text-sm text-muted-foreground">{email}</p>
</div>
</div>
</div>
);
}
To check your own rules, run the same request with the rule enabled and disabled, then compare the output against the rule's checklist.
Best Practices
- Keep each rule focused. Cursor's docs say to keep rules under 500 lines and split large ones into smaller, composable rules (checked 2026-11-05).
- Start small. Begin with your 10–20 most-corrected conventions, then add a rule each time Agent repeats a mistake. The docs recommend this approach too.
- Be specific. "Use early returns; keep functions under ~50 lines" beats "write clean code."
- Avoid contradictions. "Always use Server Components" plus "use useState for all state" can't both be followed.
- Review rules when your stack changes. Remove references to deprecated APIs and old versions.
- Commit
.cursor/rules/andAGENTS.mdso the whole team gets the same behavior, and review rule changes in PRs.
Templates and Examples on HyperPrompt
- Browse Cursor rules by framework, including Next.js, React, TypeScript and Python.
- Specific examples: Next.js App Router patterns, React hooks best practices, TypeScript strict mode configuration, FastAPI API design, Prisma ORM best practices.
- React Native / mobile: we don't have a dedicated React Native Cursor rule page. Start from the React rules and add mobile-specific conventions.
Community-submitted rules vary in format. Some are written for the older single-file style. Paste their content into an .mdc rule with the right frontmatter, or into AGENTS.md.
Other Tools' Instruction Files
The content of good rules carries over between tools. The file locations differ:
- Claude Code uses
CLAUDE.md(project root or.claude/CLAUDE.md), plus path-scoped rules in.claude/rules/(Claude Code memory docs, checked 2026-11-05). See Claude Code rules. - Windsurf: the Windsurf docs URL now redirects to Devin documentation. That documentation lists
.devin/rules/*.mdandAGENTS.md, and says legacy.windsurf/rules/*.mdand.windsurfrulesfiles are imported by default (Devin Local customizations, checked 2026-11-05). See Windsurf rules.
For a broader side-by-side, see Cursor vs Windsurf vs Claude Code.
Official References
- Cursor — Rules: https://cursor.com/docs/rules (checked 2026-11-05)
- Cursor — Using the CLI (rules, AGENTS.md, MCP in the CLI): https://cursor.com/docs/cli/using (checked 2026-11-05)
- Claude Code — memory and CLAUDE.md/AGENTS.md: https://code.claude.com/docs/en/memory (checked 2026-11-05)
- Devin Local (formerly Windsurf docs) — customizations: https://docs.devin.ai/desktop/devin-local/customizations (checked 2026-11-05)
Next: browse the rules directory, improve a prompt with the Prompt Grader, or read the prompt engineering guide.
Related Articles
AGENTS.md vs CLAUDE.md: Which AI Coding Tools Read Which Instruction File
A cross-tool guide to AGENTS.md: how Cursor, Claude Code, Gemini CLI, Kiro, Devin Desktop (Windsurf), GitHub Copilot and Antigravity read it, how it relates to CLAUDE.md, GEMINI.md and tool-specific rules, and a shared-file pattern with caveats.
Cursor vs Windsurf (Devin Desktop) vs Claude Code: Editors, Agents, Rules, MCP and Pricing Compared
A comparison of Cursor, Windsurf (now documented as Devin Desktop) and Claude Code based on their official docs: editor integrations, terminal and agent workflows, rules and instruction files, MCP setup and current pricing pages.
Agent Skills vs MCP Servers: What's the Difference?
Agent Skills (SKILL.md folders) teach an AI agent how to do a task. MCP servers give it tools and data. What each one is, how they work together, and when to use which, with current GitHub and filesystem MCP setup examples.