Skip to main content

Overview

Rules allow you to automatically invoke Context7 for code-related questions without explicitly typing use context7 in every prompt.
Setting up a rule is the most effective way to integrate Context7 into your workflow.

Why Use Rules?

Without a rule, you need to remember to add use context7 to every prompt:
Create a Next.js middleware that checks for a valid JWT. use context7
With a rule configured, Context7 is automatically invoked when needed:
Create a Next.js middleware that checks for a valid JWT.
The AI assistant automatically detects that this is a code-related question and invokes Context7 behind the scenes.

Setting Up Rules

The setup process varies by AI coding assistant.

Cursor Rules Setup

1

Open Cursor Settings

Go to SettingsCursor SettingsRules
2

Add a new rule

Click Add new rule or edit your existing rules file.
3

Add the Context7 rule

Always use Context7 MCP when I need library/API documentation, code generation, setup or configuration steps without me having to explicitly ask.
4

Save and test

Save the rule and try asking a code-related question without use context7.
Cursor rules can be set globally (user-level) or per-project. Project-level rules are stored in .cursorrules in your project root.
Here are different rule variations you can use depending on your needs:
Always use Context7 MCP when I need library/API documentation, code generation, setup or configuration steps without me having to explicitly ask.
When to use: General development work across various technologies.

Specific Technology Focus

When working with web frameworks (Next.js, React, Vue), databases (Supabase, MongoDB), or cloud platforms (Vercel, Cloudflare), automatically use Context7 MCP to fetch current documentation and code examples.
When to use: When your project focuses on specific technologies.

Always-On Rule

Use Context7 MCP for any code-related question or task automatically.
When to use: When you want maximum automation.
Very broad rules may cause Context7 to be invoked for questions that don’t need it, potentially slowing down responses. The basic rule is usually the best balance.

Version-Specific Rule

Always use Context7 MCP for library documentation. When I mention a specific version (e.g., "Next.js 14"), ensure Context7 retrieves version-specific documentation.
When to use: When working with specific versions of libraries.

Advanced Rule Configuration

Project-Specific Rules

Combine Context7 rules with project-specific instructions:
.cursorrules
# Project: E-commerce Platform

## Technologies
- Next.js 14
- Supabase for auth and database
- Stripe for payments

## Context7 Usage
Always use Context7 MCP when working with Next.js, Supabase, or Stripe. 
Prefer official documentation and recent code examples.

## Code Style
- Use TypeScript strict mode
- Follow Airbnb style guide

Combining with Library IDs

You can specify preferred library IDs in your rules:
When working with:
- React: use library /facebook/react
- Next.js: use library /vercel/next.js
- Supabase: use library /supabase/supabase

Automatically invoke Context7 MCP for these libraries.

Using ctx7 Setup Command

The easiest way to set up both the MCP server and rules is with the ctx7 setup command:
ctx7 setup --cursor
This command:
  1. Authenticates via OAuth
  2. Generates an API key
  3. Configures the MCP server
  4. Sets up the recommended rule
See the CLI documentation for more setup options.

Testing Your Rule

After setting up a rule, test it with these prompts (without adding use context7):
How do I create a server action in Next.js?
Expected: Context7 should be invoked automatically.
Show me how to configure Supabase authentication.
Expected: Context7 should be invoked automatically.
How do I reverse a string in JavaScript?
Expected: Context7 may or may not be invoked (this is basic JavaScript that LLMs know well).
What's the weather like today?
Expected: Context7 should NOT be invoked.

Troubleshooting Rules

Solutions:
  1. Verify the rule is saved in Cursor Settings > Rules
  2. Try restarting Cursor
  3. Check if you have conflicting rules
  4. Ensure Context7 MCP is properly installed and enabled
  5. Try making the rule more explicit:
    You must use Context7 MCP for any question about libraries, APIs, or frameworks.
    
Solutions:
  1. Verify CLAUDE.md is in your project root
  2. Check the file name is exactly CLAUDE.md (all caps)
  3. Restart Claude Code
  4. Check Claude Code’s output logs for any parsing errors
Solutions:
  1. Make your rule more specific:
    Use Context7 MCP only when I ask about specific libraries, frameworks, or APIs.
    
  2. Add exclusions:
    Use Context7 MCP for library documentation, but not for basic programming concepts or general questions.
    
Solutions:
  1. Make your rule more aggressive:
    Always use Context7 MCP for any code-related question.
    
  2. Temporarily add use context7 to your prompts while debugging
  3. Check MCP server logs for errors

Best Practices

Start Simple

Begin with the basic recommended rule and adjust based on your experience.

Be Specific

Mention the technologies or libraries relevant to your project.

Iterate

Refine your rule over time based on what works best for your workflow.

Document

Keep your rules documented, especially in team projects.

Next Steps

Learn about specifying versions

Learn how to request specific library versions in your prompts.

Build docs developers (and LLMs) love