How Auto-Invocation Rules Work in Context7 to Eliminate Manual Commands

Auto-invocation rules in Context7 automatically inject the "use context7" trigger into your prompts based on pattern matching, eliminating the need to manually type commands when querying library documentation.

Context7, developed by Upstash as an open-source Model-Context-Protocol (MCP) server, delivers up-to-date library documentation directly to AI coding assistants. By configuring auto-invocation rules in your MCP client (Cursor, Claude Code, Windsurf, or VS Code), you automate the triggering of Context7 without manually typing use context7 in every prompt.

What Are Auto-Invocation Rules in Context7?

Auto-invocation rules are client-side pattern definitions that tell your MCP client when to automatically trigger the Context7 server. Instead of requiring explicit manual commands, these rules evaluate your prompts against predefined patterns and inject the necessary trigger or directly call the Context7 tool.

According to the source code documentation in packages/mcp/README.md (lines 58-74), these rules are the recommended post-setup configuration to ensure "zero friction" when working with library documentation.

How Auto-Invocation Rules Work Internally

The auto-invocation mechanism operates through four distinct phases that transform a standard prompt into a Context7-powered query.

Rule Definition in MCP Clients

Users define rules using natural language instructions stored in client-specific configuration files. As documented in README.md (lines 177-186), a typical rule reads:

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

Different clients store these rules in specific locations:

  • Cursor: Cursor Settings > Rules or project-specific rule files
  • Windsurf: .windsurfrules file in project root (referenced in i18n/README.pt-BR.md line 882)
  • Claude Code: CLAUDE.md file in project root
  • VS Code: MCP server configuration settings

Client-Side Pattern Matching

Before forwarding your prompt to the LLM, the MCP client evaluates stored rules against the prompt content. When the client detects a match—such as questions about library APIs, setup steps, or code generation—it automatically injects the "use context7" trigger into the prompt or directly invokes the Context7 tool.

This pattern matching operates transparently, requiring no user intervention once rules are established.

Trigger Handling and Documentation Retrieval

When the trigger fires, the Context7 MCP server executes two primary functions as implemented in the source:

  1. resolve-library-id: Resolves the library name mentioned in your query to a Context7-compatible library ID
  2. query-docs: Retrieves the relevant documentation chunks based on your specific question

The server then returns the documentation as part of the model's context window, enabling the LLM to generate answers using up-to-date, version-specific code examples.

Setting Up Auto-Invocation Rules for Different MCP Clients

Cursor Rules Configuration

For Cursor, navigate to Settings > Rules and add the natural language rule. The Cursor plugin specifically supports an "always-on" mode as documented in plugins/cursor/context7/README.md, which maintains a persistent use-context7 rule without manual activation.

{
  "rules": [
    {
      "pattern": ".*(library|API|setup|configuration|docs).*",
      "action": "prepend",
      "text": "use context7"
    }
  ]
}

Windsurf (.windsurfrules)

Create a .windsurfrules file in your project root:


# .windsurfrules

Always use context7 when I ask about library APIs, code generation,
or configuration steps.

This file is automatically detected by Windsurf's MCP client integration.

Claude Code (CLAUDE.md)

Create a CLAUDE.md file in your project root with the rule:


# CLAUDE.md

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

Claude Code reads this file automatically and applies the rules to all subsequent prompts.

Summary

  • Auto-invocation rules eliminate manual use context7 commands by automatically triggering the MCP server based on pattern matching.
  • Rules are defined as natural language instructions in client-specific files (.windsurfrules, CLAUDE.md, Cursor Settings).
  • The client-side matching engine injects triggers or directly calls Context7 tools before sending prompts to the LLM.
  • Context7 then executes resolve-library-id and query-docs to fetch up-to-date documentation.
  • This architecture provides zero-friction access to current library documentation across all supported MCP clients.

Frequently Asked Questions

Do I need to manually type "use context7" with auto-invocation rules enabled?

No. Once you configure auto-invocation rules in your MCP client, the system automatically injects the trigger or directly calls the Context7 tool when it detects relevant patterns in your prompts. You only need to set up the rule once in your client's configuration file or settings.

Which MCP clients support Context7 auto-invocation rules?

Context7 supports auto-invocation rules in Cursor, Claude Code, Windsurf, and VS Code. Each client implements rules differently—Cursor uses a Settings UI, Windsurf uses .windsurfrules files, Claude Code uses CLAUDE.md, and VS Code uses MCP configuration settings.

How does the MCP client know when to trigger Context7?

The client evaluates your prompt against natural language rules you define, such as "Always use Context7 when I need library documentation." When the prompt contains keywords related to libraries, APIs, setup, or configuration, the client matches the pattern and automatically triggers Context7 before sending the request to the LLM.

What happens if the auto-invocation rule doesn't match my prompt?

If your prompt doesn't match the defined patterns, the MCP client sends the request to the LLM without triggering Context7. The LLM will answer based on its training data rather than up-to-date documentation. You can still manually append use context7 to any prompt to force a documentation lookup even with auto-invocation rules enabled.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →