Skip to content

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

feat: rerank libraries while listing

Abdusshh45062a2e3735
diff --git a/src/index.ts b/src/index.ts
index 940167f5..9bf65fe9 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -4,7 +4,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { fetchProjects, fetchLibraryDocumentation } from "./lib/api.js";
-import { formatProjectsList } from "./lib/utils.js";
+import { formatProjectsList, rerankProjects } from "./lib/utils.js";
// Create server instance
const server = new McpServer({
@@ -21,7 +21,13 @@ const server = new McpServer({
server.tool(
"list-available-docs",
"Lists all available library documentation from Context7. The library names can be used with 'get-library-documentation' to retrieve documentation.",
- async () => {
+ {
+ libraryName: z
+ .string()
+ .optional()
+ .describe("Optional library name to search for and rerank results based on"),
+ },
+ async ({ libraryName }) => {
const projects = await fetchProjects();
if (!projects) {
@@ -49,7 +55,12 @@ server.tool(
};
}
- const projectsText = formatProjectsList(finalizedProjects);
+ // Rerank projects if a library name is provided
+ const rankedProjects = libraryName
+ ? rerankProjects(finalizedProjects, libraryName)
+ : finalizedProjects;
+
+ const projectsText = formatProjectsList(rankedProjects);
return {
content: [
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index 23d402ea..3dbe560c 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -18,3 +18,46 @@ export function formatProjectsList(projects: Project[]): string {
const formattedProjects = projects.map(formatProject);
return `${formattedProjects.length} available documentation libraries:\n\n${formattedProjects.join("\n")}`;
}
+
+/**
+ * Rerank projects based on a search term
+ * @param projects List of projects to rerank
+ * @param searchTerm Term to rerank by (matches against project name and title)
+ * @returns Reranked list of projects
+ */
+export function rerankProjects(projects: Project[], searchTerm: string): Project[] {
+ if (!searchTerm) return projects;
+ const lowerSearchTerm = searchTerm.toLowerCase();
+ return [...projects].sort((a, b) => {
+ const aTitle = a.settings.title.toLowerCase();
+ const aProject = a.settings.project.toLowerCase();
+ const bTitle = b.settings.title.toLowerCase();
+ const bProject = b.settings.project.toLowerCase();
+
+ // Exact matches first
+ const aExactMatch = aProject === lowerSearchTerm || aTitle === lowerSearchTerm;
+ const bExactMatch = bProject === lowerSearchTerm || bTitle === lowerSearchTerm;
+
+ if (aExactMatch && !bExactMatch) return -1;
+ if (!aExactMatch && bExactMatch) return 1;
+
+ // Then starts-with matches
+ const aStartsWithMatch =
+ aProject.startsWith(lowerSearchTerm) || aTitle.startsWith(lowerSearchTerm);
+ const bStartsWithMatch =
+ bProject.startsWith(lowerSearchTerm) || bTitle.startsWith(lowerSearchTerm);
+
+ if (aStartsWithMatch && !bStartsWithMatch) return -1;
+ if (!aStartsWithMatch && bStartsWithMatch) return 1;
+
+ // Then contains matches
+ const aContainsMatch = aProject.includes(lowerSearchTerm) || aTitle.includes(lowerSearchTerm);
+ const bContainsMatch = bProject.includes(lowerSearchTerm) || bTitle.includes(lowerSearchTerm);
+
+ if (aContainsMatch && !bContainsMatch) return -1;
+ if (!aContainsMatch && bContainsMatch) return 1;
+
+ // Default to alphabetical by project name
+ return aProject.localeCompare(bProject);
+ });
+}