diff --git a/apps/mcp/src/auth.ts b/apps/mcp/src/auth.ts index 7bd3b946..ee0fa5a1 100644 --- a/apps/mcp/src/auth.ts +++ b/apps/mcp/src/auth.ts @@ -32,6 +32,7 @@ export async function validateApiKey( headers: { Authorization: `Bearer ${apiKey}`, }, + signal: AbortSignal.timeout(30_000), }) if (!sessionResponse.ok) { @@ -103,6 +104,7 @@ export async function validateOAuthToken( headers: { Authorization: `Bearer ${token}`, }, + signal: AbortSignal.timeout(30_000), }) if (!sessionResponse.ok) { diff --git a/apps/mcp/src/client.ts b/apps/mcp/src/client.ts index ee35fcf1..6018d593 100644 --- a/apps/mcp/src/client.ts +++ b/apps/mcp/src/client.ts @@ -142,6 +142,7 @@ export class SupermemoryClient { this.client = new Supermemory({ apiKey: bearerToken, baseURL: apiUrl, + timeout: 30_000, }) this.containerTag = containerTag || DEFAULT_PROJECT_ID } @@ -336,6 +337,7 @@ export class SupermemoryClient { Authorization: `Bearer ${this.bearerToken}`, "Content-Type": "application/json", }, + signal: AbortSignal.timeout(30_000), }) if (!response.ok) { @@ -374,6 +376,7 @@ export class SupermemoryClient { order: "desc", containerTags, }), + signal: AbortSignal.timeout(30_000), }) if (!response.ok) { throw Object.assign(new Error("Failed to fetch documents"), { @@ -387,6 +390,19 @@ export class SupermemoryClient { } private handleError(error: unknown): never { + // Handle request timeout errors (AbortError from AbortSignal.timeout or SDK TimeoutError) + if (error instanceof Error) { + if ( + error.name === "AbortError" || + error.name === "TimeoutError" || + error.message.toLowerCase().includes("timeout") + ) { + throw new Error( + "Request to Supermemory API timed out. Please try again.", + ) + } + } + // Handle network/fetch errors if (error instanceof TypeError) { if ( diff --git a/apps/mcp/src/index.ts b/apps/mcp/src/index.ts index 846064e1..45731982 100644 --- a/apps/mcp/src/index.ts +++ b/apps/mcp/src/index.ts @@ -89,6 +89,7 @@ app.get("/.well-known/oauth-authorization-server", async (c) => { // Fetch the authorization server metadata from the main API const response = await fetch( `${apiUrl}/.well-known/oauth-authorization-server`, + { signal: AbortSignal.timeout(30_000) }, ) if (!response.ok) {