Skip to content

Context7 Platform -- Up-to-date docs for AI agents

enable paging and update readme

enesakare276bfc2aadc
diff --git a/.gitignore b/.gitignore
index cdf189f6..a978d008 100644
--- a/.gitignore
+++ b/.gitignore
@@ -176,3 +176,7 @@ dist
package-lock.json
prompt.txt
+
+reports
+reports-old
+src/test/questions*
diff --git a/README.md b/README.md
index d7191cb4..17396a9a 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@ LLMs rely on outdated or generic information about the libraries you use. You ge
Context7 MCP pulls up-to-date, version-specific documentation and code examples straight from the source — and places them directly into your prompt.
-Add `use context7` to your prompt in Cursor:
+Add `use context7` to your prompt (or [set up a rule](#️-installation) to auto-invoke):
```txt
Create a Next.js middleware that checks for a valid JWT in cookies
@@ -35,7 +35,7 @@ JSON API responses for five minutes. use context7
Context7 fetches up-to-date code examples and documentation right into your LLM's context.
- 1️⃣ Write your prompt naturally
-- 2️⃣ Tell the LLM to `use context7`
+- 2️⃣ Tell the LLM to `use context7` (or [set up a rule](#️-installation) once)
- 3️⃣ Get working code answers
No tab-switching, no hallucinated APIs that don't exist, no outdated code generation.
@@ -55,6 +55,26 @@ Check out our [project addition guide](https://context7.com/docs/adding-librarie
- Cursor, Claude Code, VSCode, Windsurf or another MCP Client
- Context7 API Key (Optional) for higher rate limits and private repositories (Get yours by creating an account at [context7.com/dashboard](https://context7.com/dashboard))
+> [!TIP]
+> **Recommended: Add a Rule to Auto-Invoke Context7**
+>
+> If you don't want to add `use context7` to every prompt, you can define a simple rule in your MCP client's rule section to automatically invoke Context7 on any code question:
+>
+> - For Windsurf, in `.windsurfrules` file
+> - For Cursor, from `Cursor Settings > Rules` section
+> - For Claude Code, in `CLAUDE.md` file
+> - Or the equivalent in your MCP client
+>
+> **Example Rule:**
+>
+> ```txt
+> Always use context7 when I need code generation, setup or configuration steps, or
+> library/API documentation. This means you should automatically use the Context7 MCP
+> tools to resolve library id and get library docs without me having to explicitly ask.
+> ```
+>
+> From then on, you'll get Context7's docs in any related conversation without typing anything extra. You can alter the rule to match your use cases.
+
<details>
<summary><b>Installing via Smithery</b></summary>
@@ -1179,23 +1199,7 @@ Context7 MCP provides the following tools that LLMs can use:
### Add a Rule
-If you don’t want to add `use context7` to every prompt, you can define a simple rule in your MCP client's rule section:
-
-- For Windsurf, in `.windsurfrules` file
-- For Cursor, from `Cursor Settings > Rules` section
-- For Claude Code, in `CLAUDE.md` file
-
-Or the equivalent in your MCP client to auto-invoke Context7 on any code question.
-
-#### Example Rule
-
-```txt
-Always use context7 when I need code generation, setup or configuration steps, or
-library/API documentation. This means you should automatically use the Context7 MCP
-tools to resolve library id and get library docs without me having to explicitly ask.
-```
-
-From then on, you’ll get Context7’s docs in any related conversation without typing anything extra. You can alter the rule to match your use cases.
+To avoid typing `use context7` in every prompt, you can add a rule to your MCP client that automatically invokes Context7 for code-related questions. See the [recommended setup in the Installation section](#️-installation) for detailed instructions and example rules.
### Use Library Id
diff --git a/docs/installation.mdx b/docs/installation.mdx
index d638e043..979b9c20 100644
--- a/docs/installation.mdx
+++ b/docs/installation.mdx
@@ -19,6 +19,27 @@ The Server-Sent Events (SSE) transport protocol has been removed and is no longe
</Warning>
+<Tip>
+**Recommended: Add a Rule to Auto-Invoke Context7**
+
+If you don't want to add `use context7` to every prompt, you can define a simple rule in your MCP client's rule section to automatically invoke Context7 on any code question:
+
+- For Windsurf, in `.windsurfrules` file
+- For Cursor, from `Cursor Settings > Rules` section
+- For Claude Code, in `CLAUDE.md` file
+- Or the equivalent in your MCP client
+
+**Example Rule:**
+```txt
+Always use context7 when I need code generation, setup or configuration steps, or
+library/API documentation. This means you should automatically use the Context7 MCP
+tools to resolve library id and get library docs without me having to explicitly ask.
+```
+
+From then on, you'll get Context7's docs in any related conversation without typing anything extra. You can alter the rule to match your use cases.
+
+</Tip>
+
## Recommended Setup
For most users, we recommend:
diff --git a/src/index.ts b/src/index.ts
index ca11d40c..ee91fdec 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -11,10 +11,8 @@ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/
import { Command } from "commander";
import { AsyncLocalStorage } from "async_hooks";
-/** Minimum allowed tokens for documentation retrieval */
-const MINIMUM_TOKENS = 1000;
-/** Default tokens when none specified */
-const DEFAULT_TOKENS = 5000;
+/** Default number of results to return per page */
+const DEFAULT_RESULTS_LIMIT = 10;
/** Default HTTP server port */
const DEFAULT_PORT = 3000;
@@ -71,6 +69,9 @@ const requestContext = new AsyncLocalStorage<{
apiKey?: string;
}>();
+// Store API key globally for stdio mode (where requestContext may not be available in tool handlers)
+let globalApiKey: string | undefined;
+
function getClientIp(req: express.Request): string | undefined {
const forwardedFor = req.headers["x-forwarded-for"] || req.headers["X-Forwarded-For"];
@@ -172,7 +173,7 @@ Each result includes:
- Benchmark Score: Quality indicator (100 is the highest score)
- Versions: List of versions if available. Use one of those versions if the user provides a version in their query. The format of the version is /org/project/version.
-For best results, select libraries based on name match, source reputation, snippet coverage, and relevance to your use case.
+For best results, select libraries based on name match, source reputation, snippet coverage, benchmark score, and relevance to your use case.
----------
@@ -205,25 +206,28 @@ server.registerTool(
.string()
.optional()
.describe("Topic to focus documentation on (e.g., 'hooks', 'routing')."),
- tokens: z
- .preprocess((val) => (typeof val === "string" ? Number(val) : val), z.number())
- .transform((val) => (val < MINIMUM_TOKENS ? MINIMUM_TOKENS : val))
- .optional()
+ page: z
+ .number()
+ .int()
+ .min(1)
+ .max(10)
.describe(
- `Maximum number of tokens of documentation to retrieve (default: ${DEFAULT_TOKENS}). Higher values provide more context but consume more tokens.`
+ "Page number for pagination (start: 1). If the context is not sufficient, try page=2, page=3, page=4, etc. with the same topic."
),
},
},
- async ({ context7CompatibleLibraryID, tokens = DEFAULT_TOKENS, topic = "" }) => {
+ async ({ context7CompatibleLibraryID, page = 1, topic = "" }) => {
const ctx = requestContext.getStore();
+ const apiKey = ctx?.apiKey || globalApiKey;
const fetchDocsResponse = await fetchLibraryDocumentation(
context7CompatibleLibraryID,
{
- tokens,
+ page,
+ limit: DEFAULT_RESULTS_LIMIT,
topic,
},
ctx?.clientIp,
- ctx?.apiKey
+ apiKey
);
if (!fetchDocsResponse) {
@@ -368,6 +372,7 @@ async function main() {
startServer(initialPort);
} else {
const apiKey = cliOptions.apiKey || process.env.CONTEXT7_API_KEY;
+ globalApiKey = apiKey; // Store globally for tool handlers in stdio mode
const transport = new StdioServerTransport();
await requestContext.run({ apiKey }, async () => {
diff --git a/src/lib/api.ts b/src/lib/api.ts
index 40a52afe..0de71f5b 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -97,7 +97,8 @@ export async function searchLibraries(
export async function fetchLibraryDocumentation(
libraryId: string,
options: {
- tokens?: number;
+ page?: number;
+ limit?: number;
topic?: string;
} = {},
clientIp?: string,
@@ -107,8 +108,9 @@ export async function fetchLibraryDocumentation(
if (libraryId.startsWith("/")) {
libraryId = libraryId.slice(1);
}
- const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/${libraryId}`);
- if (options.tokens) url.searchParams.set("tokens", options.tokens.toString());
+ const url = new URL(`${CONTEXT7_API_BASE_URL}/v2/docs/code/${libraryId}`);
+ if (options.page) url.searchParams.set("page", options.page.toString());
+ if (options.limit) url.searchParams.set("limit", options.limit.toString());
if (options.topic) url.searchParams.set("topic", options.topic);
url.searchParams.set("type", DEFAULT_TYPE);