Context7 MCP

Context7Agent

Pre-built AI agent for documentation lookup workflows
2 min read

The Context7Agent class is a pre-configured AI agent that handles the complete documentation lookup workflow automatically. It combines both resolveLibraryId and queryDocs tools with an optimized system prompt.

Usage#

Configuration#

Parameters#

configContext7AgentConfig#

Configuration options for the agent.

Show properties
modelLanguageModel#

Language model to use. Must be a LanguageModel instance from an AI SDK provider.

Examples:

  • anthropic('claude-sonnet-4-20250514')
  • openai('gpt-5.2')
  • google('gemini-1.5-pro')
apiKeystring#

Context7 API key. If not provided, uses the CONTEXT7_API_KEY environment variable.

systemstring#

Custom system prompt. Overrides the default AGENT_PROMPT.

stopWhenStopCondition#

Condition for when the agent should stop. Defaults to stopping after 5 steps.

Default: "stepCountIs(5)"

Returns#

Context7Agent extends the AI SDK Agent class and provides generate() and stream() methods.

Agent Workflow#

The agent follows a structured multi-step workflow:

Step-by-Step#

  1. Extract library name - Identifies the library/framework from the user's query
  2. Resolve library - Calls resolveLibraryId to find the Context7 library ID
  3. Select best match - Analyzes results based on reputation, coverage, and relevance
  4. Fetch documentation - Calls queryDocs with the selected library ID and user's query
  5. Query if needed - Makes additional queries if initial context is insufficient
  6. Generate response - Provides an answer with code examples from the documentation

Examples#

Basic Usage#

With OpenAI#

Streaming Responses#

Custom Configuration#

Custom System Prompt#

Comparison: Agent vs Tools#

FeatureContext7AgentIndividual Tools
SetupSingle configurationConfigure each tool
WorkflowAutomatic multi-stepManual orchestration
System promptOptimized defaultYou provide
CustomizationLimitedFull control
Best forQuick integrationCustom workflows

When to Use the Agent#

  • Rapid prototyping
  • Standard documentation lookup use cases
  • When you want sensible defaults

When to Use Individual Tools#

  • Custom agentic workflows
  • Integration with other tools
  • Fine-grained control over the process
  • Custom system prompts with specific behavior