库文档预配置工具

Context9是一款轻量级的MCP封装工具,用于预配置库ID和关键词,使LLM能够跳过解析库ID的步骤,直接查询文档。

  • by Cordtus
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 Cordtus/context9

使用说明

context9

Lightweight MCP wrapper for Context7 that lets you pre-configure library IDs so your LLM can skip the resolve-library-id step entirely.

Instead of the LLM resolving "react" every time, you tell context9 once which libraries you use, and it embeds them directly in the tool description -- including keywords so the LLM can pick the right one from your question.

Install

yarn global add context9
# or
npm install -g context9

Quick Start

Option A: Scan your project automatically

cd your-project
context9 init

This reads package.json, resolves every dependency against Context7, and writes a .context9.json config with IDs and keywords. Takes a few seconds for a typical project.

Option B: Add libraries one by one

context9 add react
context9 add next

Interactive search -- pick from Context7 results and set an alias.

Commands

context9 init

Scan package.json (CWD or parent directories), resolve all dependencies and devDependencies against Context7, and write the config.

  • Runs up to 5 concurrent API calls
  • Skips packages with no Context7 match
  • Merges with existing config (won't overwrite entries you've already added)
  • Extracts keywords automatically from library descriptions
$ context9 init
Found /home/user/my-app/package.json
Found 12 dependencies. Resolving against Context7...

Resolved:

  Y  react -> /facebook/react [hooks, components, jsx, state, virtual, dom]
  Y  next -> /vercel/next.js [ssr, routing, app, router, server, components]
  Y  zod -> /colinhacks/zod [typescript, schema, validation, parsing]

  N  @types/node (not found)
  N  typescript (not found)

Wrote 3 libraries to /home/user/my-app/.context9.json

context9 add <name>

Search Context7 for a library, pick from results interactively, and add it to config with auto-extracted keywords.

$ context9 add express
Searching Context7 for "express"...

Results:

  1. Express /expressjs/express (score: 85)
     Fast, unopinionated web framework for Node.js

Select (1-10): 1
Alias [express]: express

Added: express -> /expressjs/express

context9 list

Print all configured libraries with their Context7 IDs and keywords.

$ context9 list
Configured libraries:

  react -> /facebook/react [hooks, components, jsx, state, virtual, dom]
  next -> /vercel/next.js [ssr, routing, app, router, server, components]
  express -> /expressjs/express [web, node, http, middleware, rest, api]

context9 remove <alias>

Remove a library from the config.

context9 remove express

context9 serve

Start the MCP server on stdio. This is the default command -- running context9 with no arguments does the same thing.

MCP Integration

Add context9 to your MCP client config. For Claude Code (~/.claude.json):

{
  "mcpServers": {
    "context9": {
      "command": "context9",
      "args": ["--config", "/path/to/your/project/.context9.json"]
    }
  }
}

The server exposes two tools:

  • query-docs -- Fetch documentation. Accepts a pre-configured alias ("react") or a raw Context7 ID ("/vercel/next.js"). The tool description lists all configured libraries with their keywords, so the LLM knows what's available without extra round-trips.
  • resolve-library-id -- Fallback search for libraries not in your config.

Config Format

Config lives in .context9.json (searched from CWD upward).

{
  "libraries": {
    "react": {
      "id": "/facebook/react",
      "keywords": ["hooks", "components", "jsx", "state", "virtual", "dom"]
    },
    "next": {
      "id": "/vercel/next.js",
      "keywords": ["ssr", "routing", "app", "router", "server", "components"]
    }
  },
  "apiKey": "ctx7sk_..."
}

Plain string entries are also accepted for backward compatibility:

{
  "libraries": {
    "react": "/facebook/react"
  }
}

Keywords

Keywords are extracted automatically from Context7 library descriptions. They serve two purposes:

  1. LLM tool selection -- embedded in the query-docs tool description so the model can match your question to the right library without calling resolve-library-id.
  2. Human reference -- visible in context9 list output.

You can edit them manually in .context9.json if the auto-extracted ones aren't ideal.

Global Options

Flag Description
--config <path> Explicit path to .context9.json (skips CWD walk-up)
--version Print version
--help Print help

API Key

Optional. Without one, Context7's free tier rate limits apply. Set it via:

  • apiKey field in .context9.json
  • CONTEXT7_API_KEY environment variable

Get a key at context7.com/dashboard.

Development

git clone <repo-url>
cd context9
yarn install
yarn build
yarn typecheck

License

MIT