2025-04-17 01:03:45 +03:00
|
|
|
import { SearchResponse } from "./types.js";
|
2025-04-03 11:50:02 +03:00
|
|
|
|
2025-07-01 15:41:31 +03:00
|
|
|
const CONTEXT7_API_BASE_URL = "http://localhost:3000/api";
|
2025-04-17 01:03:45 +03:00
|
|
|
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
|
2025-07-01 15:41:31 +03:00
|
|
|
* @param apiKey Optional API key for authentication
|
2025-04-17 01:03:45 +03:00
|
|
|
* @returns Search results or null if the request fails
|
2025-04-03 11:50:02 +03:00
|
|
|
*/
|
2025-07-01 15:41:31 +03:00
|
|
|
export async function searchLibraries(query: string, apiKey?: string): Promise<SearchResponse> {
|
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);
|
2025-07-01 15:41:31 +03:00
|
|
|
|
|
|
|
|
const headers: Record<string, string> = {};
|
|
|
|
|
if (apiKey) {
|
2025-07-01 17:08:20 +03:00
|
|
|
headers["Authorization"] = `Bearer ${apiKey}`;
|
2025-07-01 15:41:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(url, { headers });
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorCode = response.status;
|
|
|
|
|
if (errorCode === 429) {
|
|
|
|
|
console.error(`Rate limited due to too many requests. Please try again later.`);
|
|
|
|
|
return {
|
|
|
|
|
results: [],
|
|
|
|
|
error: `Rate limited due to too many requests. Please try again later.`,
|
|
|
|
|
} as SearchResponse;
|
|
|
|
|
}
|
|
|
|
|
console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`);
|
|
|
|
|
return {
|
|
|
|
|
results: [],
|
|
|
|
|
error: `Failed to search libraries. Please try again later. Error code: ${errorCode}`,
|
|
|
|
|
} as SearchResponse;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
return await response.json();
|
|
|
|
|
} catch (error) {
|
2025-04-17 01:03:45 +03:00
|
|
|
console.error("Error searching libraries:", error);
|
2025-06-07 00:20:52 +03:00
|
|
|
return { results: [], error: `Error searching libraries: ${error}` } as SearchResponse;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
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-07-01 15:41:31 +03:00
|
|
|
* @param apiKey Optional API key for authentication
|
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-07-01 15:41:31 +03:00
|
|
|
} = {},
|
|
|
|
|
apiKey?: string
|
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);
|
2025-07-01 15:41:31 +03:00
|
|
|
|
|
|
|
|
const headers: Record<string, string> = {
|
|
|
|
|
"X-Context7-Source": "mcp-server",
|
|
|
|
|
};
|
|
|
|
|
if (apiKey) {
|
2025-07-01 17:08:20 +03:00
|
|
|
headers["Authorization"] = `Bearer ${apiKey}`;
|
2025-07-01 15:41:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch(url, { headers });
|
2025-04-03 11:50:02 +03:00
|
|
|
if (!response.ok) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorCode = response.status;
|
|
|
|
|
if (errorCode === 429) {
|
|
|
|
|
const errorMessage = `Rate limited due to too many requests. Please try again later.`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
|
|
|
|
}
|
|
|
|
|
const errorMessage = `Failed to fetch documentation. Please try again later. Error code: ${errorCode}`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
if (!text || text === "No content available" || text === "No context data available") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
} catch (error) {
|
2025-06-07 00:20:52 +03:00
|
|
|
const errorMessage = `Error fetching library documentation. Please try again later. ${error}`;
|
|
|
|
|
console.error(errorMessage);
|
|
|
|
|
return errorMessage;
|
2025-04-03 11:50:02 +03:00
|
|
|
}
|
|
|
|
|
}
|