refactor: use library instead of package

This commit is contained in:
Abdusshh 2025-04-04 18:20:07 +03:00
parent f2db8d4be0
commit 39a43d5b1d
4 changed files with 31 additions and 30 deletions

View File

@ -1,12 +1,12 @@
# Context7 MCP Server # Context7 MCP Server
In this repository, we provide an MCP Server for [Context7](https://context7.com), which offers access to high-quality documentation for popular packages and libraries. In this repository, we provide an MCP Server for [Context7](https://context7.com), which offers access to high-quality documentation for popular libraries.
This lets you use Cursor, Windsurf, Claude Desktop, or any MCP Client, to use natural language to search and access documentation for packages, e.g.: This lets you use Cursor, Windsurf, Claude Desktop, or any MCP Client, to use natural language to search and access documentation for libraries, e.g.:
- "What are the main features of React hooks?" - "What are the main features of React hooks?"
- "How do I implement authentication with Next.js?" - "How do I implement authentication with Next.js?"
- "Show me documentation for Upstash Redis" - "Rate limiting with Redis"
- "Get examples of using React Query" - "Get examples of using React Query"
## Usage ## Usage
@ -46,11 +46,11 @@ Add this to your Windsurf MCP config file. For more info, check the [Windsurf MC
### Tools ### Tools
- `list-available-packages`: Lists all available documentation packages from Context7 - `list-available-docs`: Lists all available documentation libraries from Context7
- `get-package-documentation`: Retrieves documentation for a specific package with options for: - `get-library-documentation`: Retrieves documentation for a specific library with options for:
- `packageName`: Name of the package to retrieve docs for - `libraryName`: Name of the library to retrieve docs for
- `topic`: Specific topic within the package to focus the documentation on - `topic`: Specific topic within the library
- `tokens`: Maximum number of tokens to retrieve (default: 5000) - `tokens`: Maximum tokens to retrieve (default: 5000)
## Development ## Development

View File

@ -3,13 +3,13 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod"; import { z } from "zod";
import { fetchProjects, fetchPackageDocumentation } from "./lib/api.js"; import { fetchProjects, fetchLibraryDocumentation } from "./lib/api.js";
import { formatProjectsList } from "./lib/utils.js"; import { formatProjectsList } from "./lib/utils.js";
// Create server instance // Create server instance
const server = new McpServer({ const server = new McpServer({
name: "Context7", name: "Context7",
description: "Retrieves documentation for packages.", description: "Retrieves documentation and code examples for software libraries.",
version: "1.0.0", version: "1.0.0",
capabilities: { capabilities: {
resources: {}, resources: {},
@ -19,8 +19,8 @@ const server = new McpServer({
// Register Context7 tools // Register Context7 tools
server.tool( server.tool(
"list-available-packages", "list-available-docs",
"Lists all packages from Context7. The package names can be used with 'get-package-documentation' to retrieve documentation.", "Lists all available library documentation from Context7. The library names can be used with 'get-library-documentation' to retrieve documentation.",
async () => { async () => {
const projects = await fetchProjects(); const projects = await fetchProjects();
@ -29,7 +29,7 @@ server.tool(
content: [ content: [
{ {
type: "text", type: "text",
text: "Failed to retrieve packages data from Context7", text: "Failed to retrieve library documentation data from Context7",
}, },
], ],
}; };
@ -43,7 +43,7 @@ server.tool(
content: [ content: [
{ {
type: "text", type: "text",
text: "No finalized documentation packages available", text: "No finalized documentation libraries available",
}, },
], ],
}; };
@ -63,18 +63,19 @@ server.tool(
); );
server.tool( server.tool(
"get-package-documentation", "get-library-documentation",
"Retrieves documentation for a specific package from Context7. Use 'list-available-packages' first to see what's available.", "Retrieves documentation for a specific library from Context7. Use 'list-available-docs' first to see what's available.",
{ {
packageName: z libraryName: z
.string() .string()
.describe( .describe(
"Name of the package/library to retrieve documentation for (e.g., 'upstash-redis', 'nextjs'). Must match exactly a package name from 'list-available-packages'." "Name of the library to retrieve documentation for (e.g., 'upstash-redis', 'nextjs'). Must match exactly a library name from 'list-available-docs'."
), ),
topic: z topic: z
.string() .string()
.optional()
.describe( .describe(
"Specific topic within the package to focus the documentation on (e.g., 'hooks', 'routing')." "Specific topic within the library to focus the documentation on (e.g., 'hooks', 'routing')."
), ),
tokens: z tokens: z
.number() .number()
@ -84,15 +85,15 @@ server.tool(
"Maximum number of tokens of documentation to retrieve (default: 5000).Higher values provide more comprehensive documentation but use more context window." "Maximum number of tokens of documentation to retrieve (default: 5000).Higher values provide more comprehensive documentation but use more context window."
), ),
}, },
async ({ packageName, tokens = 5000, topic = "" }) => { async ({ libraryName, tokens = 5000, topic = "" }) => {
const documentationText = await fetchPackageDocumentation(packageName, tokens, topic); const documentationText = await fetchLibraryDocumentation(libraryName, tokens, topic);
if (!documentationText) { if (!documentationText) {
return { return {
content: [ content: [
{ {
type: "text", type: "text",
text: "Documentation not found or not finalized for this package. Verify you've provided a valid package name exactly as listed by the 'list-available-packages' tool.", text: "Documentation not found or not finalized for this library. Verify you've provided a valid library name exactly as listed by the 'list-available-docs' tool.",
}, },
], ],
}; };

View File

@ -21,19 +21,19 @@ export async function fetchProjects(): Promise<Project[] | null> {
} }
/** /**
* Fetches documentation context for a specific package * Fetches documentation context for a specific library
* @param packageName The package name to fetch documentation for * @param libraryName The library name to fetch documentation for
* @param tokens Number of tokens to retrieve (default: 5000) * @param tokens Number of tokens to retrieve (default: 5000)
* @param topic Optional topic to rerank context for * @param topic Optional topic to rerank context for
* @returns The documentation text or null if the request fails * @returns The documentation text or null if the request fails
*/ */
export async function fetchPackageDocumentation( export async function fetchLibraryDocumentation(
packageName: string, libraryName: string,
tokens: number = 5000, tokens: number = 5000,
topic: string = "" topic: string = ""
): Promise<string | null> { ): Promise<string | null> {
try { try {
let contextURL = `${CONTEXT7_BASE_URL}/${packageName}/llm.txt`; let contextURL = `${CONTEXT7_BASE_URL}/${libraryName}/llm.txt`;
const params = []; const params = [];
if (tokens) { if (tokens) {
@ -62,7 +62,7 @@ export async function fetchPackageDocumentation(
return text; return text;
} catch (error) { } catch (error) {
console.error("Error fetching package documentation:", error); console.error("Error fetching library documentation:", error);
return null; return null;
} }
} }

View File

@ -6,7 +6,7 @@ import { Project } from "./types.js";
* @returns Formatted project string * @returns Formatted project string
*/ */
export function formatProject(project: Project): string { export function formatProject(project: Project): string {
return `Title: ${project.settings.title}\nPackage name: ${project.settings.project}\n`; return `Title: ${project.settings.title}\nLibrary name: ${project.settings.project}\n`;
} }
/** /**
@ -16,5 +16,5 @@ export function formatProject(project: Project): string {
*/ */
export function formatProjectsList(projects: Project[]): string { export function formatProjectsList(projects: Project[]): string {
const formattedProjects = projects.map(formatProject); const formattedProjects = projects.map(formatProject);
return `${formattedProjects.length} available documentation packages:\n\n${formattedProjects.join("\n")}`; return `${formattedProjects.length} available documentation libraries:\n\n${formattedProjects.join("\n")}`;
} }