Skip to content

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

Bound Context7 API calls with an abort signal (#3018)

Fahreddin Özcan67528f2450a6
diff --git a/.changeset/mcp-api-fetch-timeout.md b/.changeset/mcp-api-fetch-timeout.md
new file mode 100644
index 00000000..68e9c83c
--- /dev/null
+++ b/.changeset/mcp-api-fetch-timeout.md
@@ -0,0 +1,5 @@
+---
+"@upstash/context7-mcp": patch
+---
+
+Add a 60s `AbortSignal.timeout()` to both Context7 API calls in `lib/api.ts`. Without a signal a stalled backend call rides undici's ~300s default before failing. 60s is generous: these are vector queries with p99.9 ~3.2s, and no request exceeded 30s across a full day of production traffic.
diff --git a/packages/mcp/src/lib/api.ts b/packages/mcp/src/lib/api.ts
index 6bcd4502..d8d70239 100644
--- a/packages/mcp/src/lib/api.ts
+++ b/packages/mcp/src/lib/api.ts
@@ -5,6 +5,14 @@ import { CONTEXT7_API_BASE_URL } from "./constants.js";
import { readFileSync } from "fs";
import tls from "tls";
+/**
+ * Ceiling on a single Context7 API call. Without a signal a stalled backend
+ * call rides undici's ~300s default before failing. 60s is generous for these
+ * vector queries: p99.9 is ~3.2s, and no request exceeded 30s across a full
+ * day of production traffic.
+ */
+const API_TIMEOUT_MS = 60_000;
+
/**
* Parses error response from the Context7 API
* Extracts the server's error message, falling back to status-based messages if parsing fails
@@ -119,7 +127,7 @@ export async function searchLibraries(
const headers = generateHeaders(context);
- const response = await fetch(url, { headers });
+ const response = await fetch(url, { headers, signal: AbortSignal.timeout(API_TIMEOUT_MS) });
readPromptSignal(response, context);
if (!response.ok) {
const errorMessage = await parseErrorResponse(response, context.apiKey);
@@ -152,7 +160,7 @@ export async function fetchLibraryContext(
const headers = generateHeaders(context);
- const response = await fetch(url, { headers });
+ const response = await fetch(url, { headers, signal: AbortSignal.timeout(API_TIMEOUT_MS) });
readPromptSignal(response, context);
if (!response.ok) {
const errorMessage = await parseErrorResponse(response, context.apiKey);