> ## Documentation index
> Fetch the complete documentation index at: https://upstash-context7-git-ctx7-4280-helm-registry-login-c44ca5.docs7.io/llms.txt
> Ask it with GET https://upstash-context7-git-ctx7-4280-helm-registry-login-c44ca5.docs7.io/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.
> Feature requests: https://upstash-context7-git-ctx7-4280-helm-registry-login-c44ca5.docs7.io/resources/feature-requests.md lists what users asked for and how to vote for a request or add one.
> Page rating: `GET https://upstash-context7-git-ctx7-4280-helm-registry-login-c44ca5.docs7.io/feedback?path=/clients/cli&helpful={true|false}&comment={comment}` records whether this page helped. The `comment` (URL-encoded, at most 500 characters) says what helped, or what was missing or wrong; a `helpful=false` rating needs it. It is anonymous and needs no key. Comments hold no code, secrets or personal data.

# CLI

> The ctx7 CLI — fetch library documentation and configure Context7 MCP from your terminal

The `ctx7` CLI is the command-line interface for Context7. It does two things:

- **Fetch library documentation** — search all libraries with one question, or resolve any library by name and query its up-to-date docs directly in your terminal, without opening a browser
- **Configure your AI coding agent** — set up the Context7 MCP server (or a CLI-based `find-docs` skill) for Claude Code, Cursor, OpenCode, and more with a single command

The CLI is useful both as a standalone tool (fetching docs while you code) and as a setup utility (wiring up Context7 for your AI coding agent).

## Installation

Requires Node.js 22.13 or later.

<Tabs>
  <Tab title="npx (no install)">
    Run ctx7 directly without installing anything. Useful for one-off commands or trying it out.

    ```bash
    npx ctx7 --help
    npx ctx7 library react
    ```

  </Tab>
  <Tab title="Global install">
    Install globally for faster access — no `npx` prefix needed on every command.

    ```bash
    npm install -g ctx7

    # Verify installation
    ctx7 --version
    ```

  </Tab>
</Tabs>

---

## Search Documentation

### ctx7 search

Takes a natural-language question and returns relevant code snippets and explanations in one step. Context7 picks the libraries for you, so no library ID is needed.

```bash
ctx7 search "How to stream an OpenAI response from a Next.js route"
ctx7 search "How to validate a request body with a schema"
```

If you know the library, version, or language, add them to narrow the results:

```bash
# Search specific libraries — a name or a library ID, repeat for up to four
ctx7 search "How to stream a response" --library Next.js --library /openai/openai-node

# Search a specific version
ctx7 search "How to cache a server function" --library Next.js --library-version 15.4.0

# Prefer results in the language you write in
ctx7 search "How to send an SMS" --library Twilio --language Ruby

# Output as structured JSON
ctx7 search "How to use hooks for state management" --library React --json
```

---

## Query Library Documentation

To query one library directly, fetching docs is a two-step process: first resolve the library name to get its Context7 ID, then use that ID to query documentation.

### Step 1 — ctx7 library

Searches the Context7 index by name and returns matching libraries. Pass a `query` describing what you're trying to do — this ranks results by relevance and helps when a library name is ambiguous or shared across multiple packages.

```bash
ctx7 library react "How to clean up useEffect with async operations"
ctx7 library nextjs "How to set up app router with a proxy"
ctx7 library prisma "How to define one-to-many relations with cascade delete"
```

Each result includes:

| Field                 | Description                                                                |
| --------------------- | -------------------------------------------------------------------------- |
| **Library ID**        | The identifier to pass to `ctx7 docs` (format: `/org/project`)             |
| **Code Snippets**     | Number of indexed code examples — higher means more documentation coverage |
| **Source Reputation** | Authority indicator: High, Medium, Low, or Unknown                         |
| **Benchmark Score**   | Quality score from 0 to 100                                                |
| **Versions**          | Version tags when available — pass a version with `--tag`                  |

When multiple results come back, the best match is usually the one with the closest name, highest snippet count, and strongest reputation. If you need docs for a specific version, pass the version your project uses with `--tag`, as written in your package file (`15.4.3`, `^15.4.3`, `14`). Context7 serves that version or the closest one it has.

```bash
# Fetch docs for a specific version
ctx7 docs /vercel/next.js "How to set up app router" --tag 16.2.9

# Output as JSON for scripting
ctx7 library react "How to use hooks for state management" --json | jq '.[0].id'
```

### Step 2 — ctx7 docs

Takes a library ID and a natural-language question, and returns relevant code snippets and explanations from the indexed documentation.

```bash
ctx7 docs /reactjs/react.dev "How to clean up useEffect with async operations"
ctx7 docs /vercel/next.js "How to add a proxy that redirects unauthenticated users"
ctx7 docs /prisma/prisma "How to define one-to-many relations with cascade delete"
```

<Note>
  Library IDs always start with `/`. Running `ctx7 docs react "hooks"` will fail — always use the
  full ID returned by `ctx7 library` in Step 1.
</Note>

Queries work best when they're specific. Describe what you're trying to accomplish rather than using single keywords — `"How to set up authentication with JWT in Express.js"` returns much better results than `"auth"`.

The output contains two types of content: **code snippets** (titled, with language-tagged blocks) and **info snippets** (prose explanations with breadcrumb context). Both are formatted for readability in the terminal.

```bash
# Rank code examples in the language you write in first
ctx7 docs /websites/twilio "How to send an SMS" --language Ruby

# Output as structured JSON
ctx7 docs /reactjs/react.dev "How to use hooks for state management" --json

# Pipe to other tools — output is clean when not in a TTY (no spinners or colors)
ctx7 docs /reactjs/react.dev "How to use hooks for state management" | head -50
ctx7 docs /vercel/next.js "How to add a proxy for route protection" | grep -A 10 "proxy"
```

### On-premise deployments

`ctx7 library` and `ctx7 docs` also query an on-premise deployment. Set the deployment's root URL in `CONTEXT7_BASE_URL` once, or pass `--base-url` on each command. When MCP authentication is enabled on the deployment, set `CONTEXT7_API_KEY` to a personal API key from the dashboard:

```bash
export CONTEXT7_BASE_URL=https://context7.internal.example
export CONTEXT7_API_KEY=ctx7op-...

ctx7 library react "How to clean up useEffect"
ctx7 docs /your-org/your-repo "How to configure the client"
```

`--base-url` wins when both are set. A custom deployment authenticates with `CONTEXT7_API_KEY` only: the CLI does not send your `ctx7 login` session to it. `ctx7 search`, `ctx7 code` and `ctx7 get-upgrade-guide` are available only on hosted Context7.

<Note>
  `CONTEXT7_BASE_URL` is the deployment's root URL, without `/api`. The MCP server reads a different variable, `CONTEXT7_API_URL`, which ends in `/api`.
</Note>

---

## Setup

Configure Context7 for your AI coding agent. On first run, prompts you to choose between two modes:

- **MCP server** — registers the Context7 MCP server in your agent's config so it can call `resolve-library-id` and `query-docs` tools natively
- **CLI + Skills** — installs a `find-docs` skill that guides your agent to fetch up-to-date library docs using `ctx7` CLI commands (no MCP required)

### ctx7 setup

```bash
# Interactive — prompts for mode, then agent/install target
ctx7 setup

# Skip the mode prompt
ctx7 setup --mcp            # MCP server mode
ctx7 setup --cli            # CLI + Skills mode

# Target a specific agent (MCP mode)
ctx7 setup --claude
ctx7 setup --cursor
ctx7 setup --opencode
ctx7 setup --pi

# Target a specific install location (CLI + Skills mode)
ctx7 setup --cli --claude       # Claude Code (~/.claude/skills)
ctx7 setup --cli --cursor       # Cursor (~/.cursor/skills)
ctx7 setup --cli --codex        # Codex (~/.agents/skills)
ctx7 setup --cli --opencode     # OpenCode (~/.agents/skills)
ctx7 setup --cli --antigravity  # Antigravity (~/.agent/skills)
ctx7 setup --cli --pi           # Pi (~/.agents/skills)

# Configure for current project only (default is global)
ctx7 setup --project

# Skip confirmation prompts
ctx7 setup --yes
```

**Authentication options:**

```bash
# Use an existing API key (works for both MCP and CLI + Skills mode)
ctx7 setup --api-key YOUR_API_KEY

# Use OAuth endpoint — MCP mode only (IDE handles the auth flow)
ctx7 setup --oauth
```

Without `--api-key` or `--oauth`, setup runs the OAuth device flow: it shows a verification link and short code that you open on any device to sign in, so it works the same locally or on a remote, headless, or SSH host. The device flow returns an API key that setup uses for authentication. `--oauth` is MCP-only — use it when an IDE handles the auth flow on your behalf. If your teamspace's API key policy refuses you a key (OAuth only, or keys for owners and admins only), MCP setup continues with OAuth instead, as with `--oauth`. This includes a key saved by an earlier login: setup checks it first, and signs you in again when the key is no longer valid. With `--stdio` it stops with an error, because the stdio server needs an API key.

If the teamspace you pick does not allow you an API key (its policy is **Owners and admins only** and you are a developer, or it is **OAuth only**), setup continues with OAuth and configures the MCP server without a key. `--stdio` and `ctx7` CLI commands need an API key, so they are not available for that teamspace. To use another teamspace, log in again and pick it. See [Manage API Keys](/howto/api-keys#teamspace-api-key-access).

### On-premise setup

Pass the root URL of your on-premise deployment with `--base-url`, or set it once in `CONTEXT7_BASE_URL`. Do not include `/mcp` or `/api`:

```bash
# MCP authentication disabled on the deployment
ctx7 setup --mcp --base-url https://context7.internal.example --codex

# MCP authentication enabled: the CLI requests the personal key with hidden input
ctx7 setup --mcp \
  --base-url https://context7.internal.example \
  --codex

# Non-interactive automation
CONTEXT7_API_KEY=ctx7op-... ctx7 setup --mcp --yes \
  --base-url https://context7.internal.example \
  --codex
```

The CLI checks `/api/auth/mcp` before writing configuration. It configures anonymous access, writes `Authorization: Bearer <personal-key>`, or, when the deployment uses [OAuth sign-in](/enterprise/security/mcp-oauth), writes no key so the client signs in through the browser. When the deployment needs a personal key and no key was provided, interactive setup requests the key with masked input. Use `--api-key` or `CONTEXT7_API_KEY` for non-interactive automation. On-premise setup uses Streamable HTTP and bundled rule/skill content.

When `--base-url` targets a custom deployment, setup skips the npm update check and telemetry and does not fetch rules or skills from GitHub or Context7 Cloud. After the CLI package is installed, the only network request made by setup is to the configured deployment. In a restricted environment, mirror `ctx7` and its dependencies in an internal npm registry and run the installed `ctx7` binary instead of `npx ctx7@latest`.

For a custom deployment, `ctx7 setup` supports MCP setup only. It cannot be combined with `--stdio` or `--cli`. `--oauth` works when the deployment offers OAuth sign-in.

**What gets written — MCP mode:**

| File                                                | Purpose                                                          |
| --------------------------------------------------- | ---------------------------------------------------------------- |
| `.mcp.json` / `.cursor/mcp.json` / `opencode.json`  | MCP server entry                                                 |
| Agent rules directory                               | Rule file — instructs the agent to use Context7 for library docs |
| Agent skills directory                              | `context7-mcp` skill                                             |

The rule file is the contents of [`rules/context7-mcp.md`](https://github.com/upstash/context7/blob/master/rules/context7-mcp.md) and the skill is [`skills/context7-mcp/SKILL.md`](https://github.com/upstash/context7/blob/master/skills/context7-mcp/SKILL.md). For Claude Code, `ctx7 setup --claude` writes the files below. With `--project`, the rule and skill go to `.claude/rules/` and `.claude/skills/` in the project instead; `CLAUDE_CONFIG_DIR` replaces `~/.claude` when it is set.

| File                                        | Purpose                                                          |
| ------------------------------------------- | ---------------------------------------------------------------- |
| `~/.claude.json` (or `.mcp.json` with `--project`) | MCP server entry named `context7`                        |
| `~/.claude/rules/context7.md`               | Rule file — instructs the agent to use Context7 for library docs |
| `~/.claude/skills/context7-mcp/SKILL.md`    | `context7-mcp` skill                                             |

**What gets written — CLI + Skills mode:**

| File                   | Purpose                                                                             |
| ---------------------- | ----------------------------------------------------------------------------------- |
| Agent skills directory | `find-docs` skill — guides the agent to use `ctx7 library` and `ctx7 docs` commands |
| Agent rules directory  | Rule file — instructs the agent to use the `ctx7` commands for library docs         |

The rule file is the contents of [`rules/context7-cli.md`](https://github.com/upstash/context7/blob/master/rules/context7-cli.md) and the skill is [`skills/find-docs/SKILL.md`](https://github.com/upstash/context7/blob/master/skills/find-docs/SKILL.md). For Claude Code, `ctx7 setup --cli --claude` writes `~/.claude/skills/find-docs/SKILL.md` and `~/.claude/rules/context7.md` (`.claude/` in the project with `--project`).

### ctx7 remove

Remove the setup written by `ctx7 setup`. By default this removes both MCP setup and CLI setup for the selected agent.

```bash
# Interactive
ctx7 remove

# Target specific agents
ctx7 remove --cursor
ctx7 remove --claude --project

# Remove both setup modes explicitly
ctx7 remove --cursor --all

# Remove only one setup mode
ctx7 remove --cursor --cli
ctx7 remove --claude --mcp
```

If you installed the CLI itself with `npm install -g ctx7`, remove that separately with `npm uninstall -g ctx7`. If you run Context7 with `npx ctx7`, there is no permanent CLI install to remove.

---

## Authentication

Most commands work without authentication. Log in to unlock higher rate limits on documentation commands.

### Commands

```bash
# Log in (opens browser for OAuth)
ctx7 login

# Log in without opening the browser (prints URL instead)
ctx7 login --no-browser

# Check current login status
ctx7 whoami

# Log out
ctx7 logout
```

### Session expiry

If your saved login can no longer be refreshed, the CLI clears it and asks whether to sign in again. Answering yes runs `ctx7 login` and continues the command with the new login. Answering no continues without an account.

No prompt is shown when output is piped or `--json` is passed — the CLI prints a notice to stderr and continues without an account, so parsed output stays clean. `ctx7 login`, `ctx7 whoami`, and `ctx7 setup` also skip the prompt. Network, rate-limit, and server errors keep your saved login, so the next command retries.

### API Key

Set an API key via environment variable to skip interactive login entirely — useful for CI or scripting:

```bash
export CONTEXT7_API_KEY=your_key
```

### When is authentication required?

| Feature                                                  | Required                                                                     |
| -------------------------------------------------------- | ---------------------------------------------------------------------------- |
| `ctx7 search` / `ctx7 library` / `ctx7 docs`             | No — login gives higher rate limits                                          |
| Hosted `ctx7 setup`                                      | Yes — unless `--api-key` is passed (`--oauth` also skips login for MCP mode) |
| On-premise `ctx7 setup --mcp --base-url ...`             | Only when MCP authentication is enabled on the deployment                    |

---

## Telemetry

The CLI collects anonymous usage data to help improve the product. To disable:

```bash
# For a single command
CTX7_TELEMETRY_DISABLED=1 ctx7 docs /reactjs/react.dev "useEffect examples"

# Permanently — add to ~/.bashrc or ~/.zshrc
export CTX7_TELEMETRY_DISABLED=1
```

Against hosted Context7, the CLI sends anonymous usage events to `context7.com` and checks the npm registry (`registry.npmjs.org`) for a newer `ctx7` version. The update check is cached for 24 hours, so it runs at most once a day, and is skipped for `--json`, `-v`/`--version` and `--base-url`. `CTX7_TELEMETRY_DISABLED` stops the events, not the update check. A custom `--base-url` deployment turns off both.

---

## Next Steps

<CardGroup cols={2}>
  <Card title="Claude Code" icon="terminal" href="/clients/claude-code">
    Set up Context7 in Claude Code
  </Card>
  <Card title="All Clients" icon="grid" href="/resources/all-clients">
    Installation for every supported editor
  </Card>
</CardGroup>
