mirror of
https://github.com/upstash/context7.git
synced 2025-12-08 13:29:03 +00:00
feat: add support for case-insensitive API key headers and improve error handling
This commit is contained in:
parent
e1fff9ac39
commit
1bf5f4fdeb
13
src/index.ts
13
src/index.ts
@ -295,9 +295,14 @@ async function main() {
|
|||||||
// Check headers in order of preference
|
// Check headers in order of preference
|
||||||
const apiKey =
|
const apiKey =
|
||||||
extractBearerToken(req.headers.authorization) ||
|
extractBearerToken(req.headers.authorization) ||
|
||||||
extractHeaderValue(req.headers["X-Context7-API-Key"]) || // Standard with x- prefix
|
extractHeaderValue(req.headers["Context7-API-Key"]) ||
|
||||||
extractHeaderValue(req.headers["Context7-API-Key"]) || // Without x- prefix
|
extractHeaderValue(req.headers["X-API-Key"]) ||
|
||||||
extractHeaderValue(req.headers["X-API-Key"]); // Generic API key header
|
extractHeaderValue(req.headers["context7-api-key"]) ||
|
||||||
|
extractHeaderValue(req.headers["x-api-key"]) ||
|
||||||
|
extractHeaderValue(req.headers["Context7_API_Key"]) ||
|
||||||
|
extractHeaderValue(req.headers["X_API_Key"]) ||
|
||||||
|
extractHeaderValue(req.headers["context7_api_key"]) ||
|
||||||
|
extractHeaderValue(req.headers["x_api_key"]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract client IP address using socket remote address (most reliable)
|
// Extract client IP address using socket remote address (most reliable)
|
||||||
@ -384,7 +389,7 @@ async function main() {
|
|||||||
startServer(initialPort);
|
startServer(initialPort);
|
||||||
} else {
|
} else {
|
||||||
// Stdio transport - this is already stateless by nature
|
// Stdio transport - this is already stateless by nature
|
||||||
const server = createServerInstance(cliOptions.apiKey);
|
const server = createServerInstance(undefined, cliOptions.apiKey);
|
||||||
const transport = new StdioServerTransport();
|
const transport = new StdioServerTransport();
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
console.error("Context7 Documentation MCP Server running on stdio");
|
console.error("Context7 Documentation MCP Server running on stdio");
|
||||||
|
|||||||
@ -26,10 +26,17 @@ export async function searchLibraries(
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorCode = response.status;
|
const errorCode = response.status;
|
||||||
if (errorCode === 429) {
|
if (errorCode === 429) {
|
||||||
console.error(`Rate limited due to too many requests. Please try again later.`);
|
console.error("Rate limited due to too many requests. Please try again later.");
|
||||||
return {
|
return {
|
||||||
results: [],
|
results: [],
|
||||||
error: `Rate limited due to too many requests. Please try again later.`,
|
error: "Rate limited due to too many requests. Please try again later.",
|
||||||
|
} as SearchResponse;
|
||||||
|
}
|
||||||
|
if (errorCode === 401) {
|
||||||
|
console.error("Unauthorized. Please check your API key.");
|
||||||
|
return {
|
||||||
|
results: [],
|
||||||
|
error: "Unauthorized. Please check your API key.",
|
||||||
} as SearchResponse;
|
} as SearchResponse;
|
||||||
}
|
}
|
||||||
console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`);
|
console.error(`Failed to search libraries. Please try again later. Error code: ${errorCode}`);
|
||||||
@ -77,7 +84,18 @@ export async function fetchLibraryDocumentation(
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorCode = response.status;
|
const errorCode = response.status;
|
||||||
if (errorCode === 429) {
|
if (errorCode === 429) {
|
||||||
const errorMessage = `Rate limited due to too many requests. Please try again later.`;
|
const errorMessage = "Rate limited due to too many requests. Please try again later.";
|
||||||
|
console.error(errorMessage);
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
if (errorCode === 404) {
|
||||||
|
const errorMessage =
|
||||||
|
"The library you are trying to access does not exist. Please try with a different library ID.";
|
||||||
|
console.error(errorMessage);
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
if (errorCode === 401) {
|
||||||
|
const errorMessage = "Unauthorized. Please check your API key.";
|
||||||
console.error(errorMessage);
|
console.error(errorMessage);
|
||||||
return errorMessage;
|
return errorMessage;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user