looking-up-docs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Documentation Lookup with Context7

借助Context7查找文档

Context7 provides up-to-date, version-specific documentation and code examples directly from source libraries.
Context7可直接从源库提供最新的、特定版本的文档和代码示例。

Why Context7

为什么选择Context7

  • Current APIs: No hallucinated or outdated patterns
  • Version-specific: Gets docs for exact library versions
  • Code examples: Real, working code from actual documentation
  • 实时API: 不会出现幻觉内容或过时用法
  • 特定版本适配: 获取对应库精确版本的文档
  • 真实代码示例: 来自官方文档的可运行代码

Workflow

工作流程

  1. Resolve library ID:
    mcp__context7__resolve-library-id
    with
    libraryName
  2. Get documentation:
    mcp__context7__query-docs
    with
    context7CompatibleLibraryID
    and
    topic
  1. 解析库ID:调用
    mcp__context7__resolve-library-id
    并传入
    libraryName
    参数
  2. 获取文档:调用
    mcp__context7__query-docs
    并传入
    context7CompatibleLibraryID
    topic
    参数

Modes

模式

ModeUse For
code
API references, code examples (default)
info
Conceptual guides, architecture, tutorials
模式适用场景
code
API参考、代码示例(默认模式)
info
概念指南、架构说明、教程

Examples

示例

undefined
undefined

React hooks

React hooks

resolve-library-id: "react" get-library-docs: context7CompatibleLibraryID="/facebook/react", topic="hooks", mode="code"
resolve-library-id: "react" get-library-docs: context7CompatibleLibraryID="/facebook/react", topic="hooks", mode="code"

Next.js middleware

Next.js middleware

resolve-library-id: "next.js" get-library-docs: context7CompatibleLibraryID="/vercel/next.js", topic="middleware"
resolve-library-id: "next.js" get-library-docs: context7CompatibleLibraryID="/vercel/next.js", topic="middleware"

Go net/http

Go net/http

resolve-library-id: "go net/http" get-library-docs: context7CompatibleLibraryID="/golang/go", topic="http server"
resolve-library-id: "go net/http" get-library-docs: context7CompatibleLibraryID="/golang/go", topic="http server"

Kubernetes API

Kubernetes API

resolve-library-id: "kubernetes" get-library-docs: context7CompatibleLibraryID="/kubernetes/kubernetes", topic="deployment"
undefined
resolve-library-id: "kubernetes" get-library-docs: context7CompatibleLibraryID="/kubernetes/kubernetes", topic="deployment"
undefined

Tips for Better Results

获取更优结果的技巧

Be specific with queries:
  • BAD:
    topic="hooks"
    → returns everything hook-related
  • GOOD:
    topic="useEffect cleanup function"
    → precise results
Filter strategies:
  • Use
    topic
    with function/method names:
    topic="json.Unmarshal"
  • Include version when relevant:
    libraryName="react 18"
  • Combine with feature context:
    topic="middleware error handling"
When results are too broad:
  1. Narrow the
    topic
    parameter
  2. Try
    mode="code"
    to focus on examples
  3. Paginate:
    page=2
    ,
    page=3
    for additional results
  4. Re-resolve library ID with more specific name
Quality check:
  • Verify code examples match your library version
  • Cross-reference with official docs if uncertain
查询请具体化:
  • ❌ 不佳示例:
    topic="hooks"
    → 返回所有与hooks相关的内容
  • ✅ 优质示例:
    topic="useEffect cleanup function"
    → 返回精准结果
过滤策略:
  • 结合函数/方法名使用
    topic
    topic="json.Unmarshal"
  • 相关时指定版本:
    libraryName="react 18"
  • 搭配功能上下文:
    topic="middleware error handling"
结果范围过宽时:
  1. 缩小
    topic
    参数的范围
  2. 尝试
    mode="code"
    聚焦代码示例
  3. 分页查询:使用
    page=2
    ,
    page=3
    获取更多结果
  4. 用更具体的名称重新解析库ID
质量校验:
  • 验证代码示例是否匹配你的库版本
  • 不确定时交叉参考官方文档

Fallback: Empty or Missing Results

回退方案:无结果或结果缺失时

When Context7 returns no results or doesn't have the library:
当Context7无返回结果或未收录目标库时:

Decision Tree

决策树

Context7 query returns empty?
├── Try broader query (e.g., "hooks" instead of "useCallback")
├── Still empty?
│   ├── Re-resolve library ID with alternative name
│   │   (e.g., "nextjs" → "next.js", "golang" → "go")
│   └── Still empty?
│       ├── Library not indexed → Use fallbacks below
│       └── Very niche library → WebSearch for official docs
Context7查询无结果?
├── 尝试更宽泛的查询(例如:用"hooks"替代"useCallback")
├── 仍无结果?
│   ├── 用替代名称重新解析库ID
│   │   (例如:"nextjs" → "next.js", "golang" → "go")
│   └── 仍无结果?
│       ├── 库未被收录 → 使用下方回退方案
│       └── 小众库 → 网页搜索官方文档

Fallback Strategies

回退策略

  1. Alternative library ID: Try variations
    # If "fastapi" fails, try:
    resolve-library-id: "starlette"  # FastAPI's underlying framework
    resolve-library-id: "pydantic"   # Often used with FastAPI
  2. WebSearch for official docs:
    WebSearch: "<library> official documentation <feature>"
    WebFetch: Official docs URL → extract relevant info
  3. Source code exploration (for open-source):
    # Clone and explore
    git clone --depth=1 <repo>
    Grep: "function <name>" --type=<lang>
  4. Perplexity for recent/niche libraries:
    mcp__perplexity-ask__perplexity_ask: "How to use <feature> in <library> 2024"
  1. 尝试替代库ID:使用变体名称
    # 如果"fastapi"失败,尝试:
    resolve-library-id: "starlette"  # FastAPI的底层框架
    resolve-library-id: "pydantic"   # 常与FastAPI搭配使用
  2. 网页搜索官方文档:
    WebSearch: "<库名> 官方文档 <功能点>"
    WebFetch: 官方文档URL → 提取相关信息
  3. 探索源代码(针对开源库):
    # 克隆并探索
    git clone --depth=1 <仓库地址>
    Grep: "function <函数名>" --type=<语言>
  4. 针对新/小众库使用Perplexity:
    mcp__perplexity-ask__perplexity_ask: "2024年如何在<库名>中使用<功能点>"

Libraries Commonly Not in Context7

Context7通常未收录的库类型

LibraryFallback
Internal/proprietarySource code + README
Very new (<6 months)WebSearch + official docs
Niche/specializedPerplexity or GitHub issues
Language stdlibUse language docs directly
库类型回退方案
内部/专有库源代码 + README
极新库(发布不足6个月)网页搜索 + 官方文档
小众/专业库Perplexity或GitHub Issues
语言标准库直接使用语言官方文档

Example Fallback Flow

回退流程示例

undefined
undefined

Initial attempt fails

初始尝试失败

resolve-library-id: "htmx" → No results
resolve-library-id: "htmx" → 无结果

Fallback 1: WebSearch

回退方案1:网页搜索

WebSearch: "htmx documentation hx-swap" → Found: https://htmx.org/docs/
WebSearch: "htmx documentation hx-swap" → 找到:https://htmx.org/docs/

Fallback 2: Fetch docs

回退方案2:获取文档内容

WebFetch: url="https://htmx.org/docs/", prompt="Explain hx-swap attribute" → Returns relevant documentation
undefined
WebFetch: url="https://htmx.org/docs/", prompt="解释hx-swap属性" → 返回相关文档内容
undefined

When to Skip Context7 Entirely

完全跳过Context7的场景

  • Asking about breaking changes between versions → WebSearch release notes
  • Debugging specific error messages → WebSearch + StackOverflow
  • Comparing libraries → Perplexity for analysis
  • Very recent features → WebSearch for latest docs
  • 查询版本间的破坏性变更 → 网页搜索发布说明
  • 调试特定错误信息 → 网页搜索 + StackOverflow
  • 对比不同库 → 使用Perplexity进行分析
  • 极新功能 → 网页搜索最新文档