Skip to content

Cursor rules (.cursor/rules/*.mdc): format, examples and generator

Cursor project rules are .mdc files in .cursor/rules/. Each starts with YAML frontmatter (description, globs, alwaysApply) that controls when Cursor includes it: always, when matching files are in context, when the agent decides it is relevant, or only when you @-mention it. Splitting rules by concern keeps context small and relevant.

Generate a Cursor rules (.mdc) file

Where the file goes

How it works

Tips for a good Cursor rules (.mdc)

Example: Next.js (App Router) + Tailwind CSS v4 + shadcn/ui

.cursor/rules/project.mdc
---
description: "Core rules for this project"
alwaysApply: true
---

# this project

A Next.js (App Router) + Tailwind CSS v4 + shadcn/ui project.

Stack: Next.js (App Router), Tailwind CSS v4, shadcn/ui.

## Commands

- Dev server: `npm run dev`
- Production build (also type-checks): `npm run build`
- Lint: `npm run lint`
- Add a component: `npx shadcn@latest add <component>`

## How to work in this repo

- Read the relevant files before editing. Follow the patterns already in the codebase over these generic rules.
- Make the smallest change that solves the task. Do not refactor or reformat unrelated code.
- Before saying a task is done, run the type-check, lint and tests listed under Commands and fix what you broke.
- If requirements are ambiguous, ask one clear question instead of guessing.
- Never commit secrets, API keys or `.env` files. Use env vars and keep `.env.example` up to date.
- Do not add a new dependency when the standard library or an existing dependency already covers it.
- Ask before adding, removing or upgrading any dependency.
- Ask before changing database schemas, auth, payments or CI configuration.
- Never delete files, drop data or run destructive commands (`rm -rf`, `git push --force`, `DROP TABLE`) without explicit approval.
- Keep each change under ~200 changed lines where possible; split larger work into steps.
.cursor/rules/nextjs.mdc
---
description: "Next.js (App Router) conventions: Next.js App Router with React Server Components and TypeScript."
globs: app/**/*.tsx, app/**/*.ts, src/**/*.tsx, src/**/*.ts
alwaysApply: false
---

# Next.js (App Router)

- Check the Next.js major version in `package.json` before using an API; follow the docs for that version.
- Components are Server Components by default. Add `"use client"` only to the smallest leaf component that needs state, effects, or browser APIs.
- Fetch data in Server Components or Server Actions, not in `useEffect`.
- In Next.js 15+, `params`, `searchParams`, `cookies()` and `headers()` are async: always `await` them.
- Mutations go through Server Actions (`"use server"`) or `route.ts` handlers. Validate every input on the server and re-check auth inside the action.
- After a mutation, call `revalidatePath()` or `revalidateTag()` instead of forcing the whole route dynamic.
- Use `next/image` for images, `next/font` for fonts and `next/link` for internal links.
- Export `metadata` or `generateMetadata` from every page for title, description and Open Graph tags.
- Only variables prefixed `NEXT_PUBLIC_` reach the browser. Never put secrets in them.
- In Next.js 16, `middleware.ts` is renamed `proxy.ts`. Use whichever file the project already has.

## Avoid

- Do not add `"use client"` to layouts or pages just to silence an error; move the interactive part into a child component.
- Do not use the Pages Router (`pages/`) APIs such as `getServerSideProps` in an App Router project.

Docs: https://nextjs.org/docs
.cursor/rules/tailwind.mdc
---
description: "Tailwind CSS v4 conventions: Utility-first styling with Tailwind CSS v4."
globs: **/*.css, **/*.tsx, **/*.vue, **/*.svelte, **/*.astro, **/*.html, **/*.blade.php, **/*.erb
alwaysApply: false
---

# Tailwind CSS v4

- Tailwind v4 is configured in CSS: `@import "tailwindcss";` plus `@theme { ... }` for design tokens. Do not create a `tailwind.config.js` unless the project already has one.
- Use theme tokens (colors, spacing) instead of arbitrary values like `w-[37px]`.
- Build responsive layouts mobile-first with `sm:`, `md:`, `lg:` prefixes.
- Extract repeated class lists into components, not `@apply` chains.
- Support dark mode with the `dark:` variant when the project uses it.

Docs: https://tailwindcss.com/docs
.cursor/rules/shadcn.mdc
---
description: "shadcn/ui conventions: Copy-in React components built on Radix and Tailwind."
globs: components/ui/**/*.tsx, src/components/ui/**/*.tsx
alwaysApply: false
---

# shadcn/ui

- Before building a UI primitive, check whether a shadcn/ui component exists and add it with `npx shadcn@latest add <component>`.
- Components live in `components/ui/` and are owned code: customize them there rather than wrapping them in more layers.
- Merge class names with the `cn()` helper from `lib/utils`.
- Keep accessibility props from Radix (labels, `aria-*`, focus handling) intact.

Docs: https://ui.shadcn.com/docs

FAQ

What is the difference between .cursorrules and .cursor/rules?

.cursorrules is the legacy single file at the repo root. .cursor/rules/*.mdc is the current format, with multiple scoped rules and frontmatter.

Why is my Cursor rule ignored?

Common causes: the file ends in .md instead of .mdc, the frontmatter is malformed, or globs do not match the files in context.

Can I use AGENTS.md instead of Cursor rules?

Yes, Cursor reads AGENTS.md. Use .mdc rules when you want file-pattern scoping.

Cursor rules (.mdc) templates by stack

Official docs: Cursor rules docs. Last reviewed 2026-10-21.