2025-04-17 01:03:45 +03:00
|
|
|
import { SearchResponse } from "./types.js";
|
2025-04-03 11:50:02 +03:00
|
|
|
|
2025-04-17 01:03:45 +03:00
|
|
|
const CONTEXT7_API_BASE_URL = "https://context7.com/api";
|
|
|
|
|
const DEFAULT_TYPE = "txt";
|
2025-04-03 11:50:02 +03:00
|
|
|
|
|
|
|
|
/**
|
2025-04-17 01:03:45 +03:00
|
|
|
* Searches for libraries matching the given query
|
|
|
|
|
* @param query The search query
|
|
|
|
|
* @returns Search results or null if the request fails
|
2025-04-03 11:50:02 +03:00
|
|
|
*/
|
2025-04-17 01:03:45 +03:00
|
|
|
export async function searchLibraries(query: string): Promise<SearchResponse | null> {
|
2025-04-03 11:50:02 +03:00
|
|
|
try {
|
2025-04-17 01:03:45 +03:00
|
|
|
const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/search`);
|
|
|
|
|
url.searchParams.set("query", query);
|
|
|
|
|
const response = await fetch(url);
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
2025-04-17 01:03:45 +03:00
|
|
|
console.error(`Failed to search libraries: ${response.status}`);
|
2025-04-03 11:50:02 +03:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (error) {
|
2025-04-17 01:03:45 +03:00
|
|
|
console.error("Error searching libraries:", error);
|
2025-04-03 11:50:02 +03:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-04 18:20:07 +03:00
|
|
|
* Fetches documentation context for a specific library
|
2025-04-17 01:03:45 +03:00
|
|
|
* @param libraryId The library ID to fetch documentation for
|
|
|
|
|
* @param options Options for the request
|
2025-04-03 11:50:02 +03:00
|
|
|
* @returns The documentation text or null if the request fails
|
|
|
|
|
*/
|
2025-04-04 18:20:07 +03:00
|
|
|
export async function fetchLibraryDocumentation(
|
2025-04-17 01:03:45 +03:00
|
|
|
libraryId: string,
|
|
|
|
|
options: {
|
|
|
|
|
tokens?: number;
|
|
|
|
|
topic?: string;
|
2025-05-24 19:33:15 +03:00
|
|
|
userQuery?: string;
|
|
|
|
|
} = { userQuery: "" }
|
2025-04-03 11:50:02 +03:00
|
|
|
): Promise<string | null> {
|
|
|
|
|
try {
|
2025-04-17 01:03:45 +03:00
|
|
|
if (libraryId.startsWith("/")) {
|
|
|
|
|
libraryId = libraryId.slice(1);
|
2025-04-08 20:29:04 +03:00
|
|
|
}
|
2025-04-17 01:03:45 +03:00
|
|
|
const url = new URL(`${CONTEXT7_API_BASE_URL}/v1/${libraryId}`);
|
|
|
|
|
if (options.tokens) url.searchParams.set("tokens", options.tokens.toString());
|
|
|
|
|
if (options.topic) url.searchParams.set("topic", options.topic);
|
|
|
|
|
url.searchParams.set("type", DEFAULT_TYPE);
|
|
|
|
|
const response = await fetch(url, {
|
2025-04-13 19:26:58 +03:00
|
|
|
headers: {
|
|
|
|
|
"X-Context7-Source": "mcp-server",
|
2025-05-24 19:33:15 +03:00
|
|
|
"X-Context7-User-Query": options.userQuery?.trim().toLowerCase() || "",
|
2025-04-13 19:26:58 +03:00
|
|
|
},
|
|
|
|
|
});
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
|
|
|
|
console.error(`Failed to fetch documentation: ${response.status}`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
if (!text || text === "No content available" || text === "No context data available") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
} catch (error) {
|
2025-04-04 18:20:07 +03:00
|
|
|
console.error("Error fetching library documentation:", error);
|
2025-04-03 11:50:02 +03:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|