AgentSkillsCN

context7

通过Context7 MCP服务器查询最新版库文档与代码示例。需提供CONTEXT7_API_KEY。

SKILL.md
--- frontmatter
name: context7
description: Query up-to-date library documentation and code examples using the Context7 MCP server. Requires CONTEXT7_API_KEY.
homepage: https://context7.com
metadata: {"clawdbot":{"emoji":"📚","requires":{"env":["CONTEXT7_API_KEY"]}}}

Context7 - Up-to-date Library Documentation

Context7 provides real-time, version-specific documentation and code examples for programming libraries. It solves the problem of LLMs generating outdated code by fetching current documentation directly from the source.

When to Use

  • When you need accurate, up-to-date documentation for a library
  • When implementing features using unfamiliar APIs
  • When the user mentions "use context7" in their prompt
  • When you suspect training data may have outdated API information

Prerequisites

  1. Get your API key from https://context7.com
  2. Set up mcporter config with your API key:
bash
mkdir -p ./config
cat > ./config/mcporter.json << EOF
{
  "mcpServers": {
    "context7": {
      "url": "https://mcp.context7.com/mcp",
      "headers": {
        "CONTEXT7_API_KEY": "YOUR_API_KEY_HERE"
      }
    }
  }
}
EOF

Or if using opencode/cursor config, ensure the {env:CONTEXT7_API_KEY} is properly set in your environment.

Usage via mcporter (Recommended)

1. Resolve Library ID

First, find the Context7 library ID for the library you need:

bash
npx mcporter --config ./config/mcporter.json call 'context7.resolve-library-id(query: "How to create routes", libraryName: "express")'

Or using key=value syntax:

bash
npx mcporter --config ./config/mcporter.json call context7.resolve-library-id query="How to create routes" libraryName="express"

This returns matching libraries with their IDs (e.g., /expressjs/express).

2. Query Documentation

Once you have the library ID, query the documentation:

bash
npx mcporter --config ./config/mcporter.json call 'context7.query-docs(libraryId: "/expressjs/express", query: "How to set up middleware")'

Or using key=value syntax:

bash
npx mcporter --config ./config/mcporter.json call context7.query-docs libraryId="/expressjs/express" query="How to set up middleware"

Bundled CLI

This skill includes a pre-generated CLI at context7-cli.ts. Run it with bun:

bash
# Show help
bun run ./context7/context7-cli.ts

# Resolve library ID
bun run ./context7/context7-cli.ts resolve-library-id \
  --query "How to create a REST API" \
  --library-name "express"

# Query documentation
bun run ./context7/context7-cli.ts query-docs \
  --library-id "/expressjs/express" \
  --query "How to handle errors"

Note: The bundled CLI requires mcporter as a dependency. Install it first:

bash
npm install -g mcporter

Regenerating the CLI

To regenerate the CLI with the latest schema:

bash
cd /tmp && npx mcporter generate-cli --server context7
mv /tmp/context7.ts ./context7/context7-cli.ts

Available Tools

resolve-library-id

Resolves a library name to a Context7-compatible library ID.

Parameters:

  • query (required): The user's question or task - used to rank results by relevance
  • libraryName (required): Library name to search for

Returns: List of matching libraries with IDs, descriptions, and quality scores.

query-docs

Fetches up-to-date documentation for a specific library.

Parameters:

  • libraryId (required): Context7 library ID (e.g., /vercel/next.js, /mongodb/docs)
  • query (required): Specific question about the library

Returns: Relevant documentation snippets and code examples.

Common Library IDs

LibraryID
Next.js/vercel/next.js
React/facebook/react
Express/expressjs/express
MongoDB/mongodb/docs
Supabase/supabase/supabase
Prisma/prisma/prisma

Best Practices

  1. Always call resolve-library-id first unless you know the exact library ID
  2. Be specific in your queries - "How to set up JWT authentication" is better than "auth"
  3. Limit to 3 calls per question - use best available result after that
  4. Do not include sensitive information (API keys, credentials) in queries

Output Formats

Control output format with -o flag:

bash
npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o json    # JSON output
npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o markdown # Markdown output
npx mcporter --config ./config/mcporter.json call context7.query-docs ... -o text    # Plain text (default)