Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/mcp/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function validateOAuthToken(
headers: {
Authorization: `Bearer ${token}`,
},
signal: AbortSignal.timeout(30_000),
})

if (!sessionResponse.ok) {
Expand Down
17 changes: 16 additions & 1 deletion apps/mcp/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Supermemory from "supermemory"

const MAX_CHARS = 200000 // ~50k tokens (character-based limit)
const DEFAULT_PROJECT_ID = "sm_project_default"
const FETCH_TIMEOUT_MS = 30_000

interface MemoryRichFields {
metadata?: Record<string, unknown> | null
Expand Down Expand Up @@ -142,6 +143,7 @@ export class SupermemoryClient {
this.client = new Supermemory({
apiKey: bearerToken,
baseURL: apiUrl,
timeout: FETCH_TIMEOUT_MS,
})
this.containerTag = containerTag || DEFAULT_PROJECT_ID
}
Expand Down Expand Up @@ -328,14 +330,16 @@ export class SupermemoryClient {
}

// Get projects list
async getProjects(): Promise<string[]> {
async getProjects(options?: { signal?: AbortSignal }): Promise<string[]> {
try {
const signal = options?.signal ?? AbortSignal.timeout(FETCH_TIMEOUT_MS)
const response = await fetch(`${this.apiUrl}/v3/projects`, {
method: "GET",
headers: {
Authorization: `Bearer ${this.bearerToken}`,
"Content-Type": "application/json",
},
signal,
})

if (!response.ok) {
Expand All @@ -359,8 +363,10 @@ export class SupermemoryClient {
containerTags?: string[],
page = 1,
limit = 10,
options?: { signal?: AbortSignal },
): Promise<DocumentsApiResponse> {
try {
const signal = options?.signal ?? AbortSignal.timeout(FETCH_TIMEOUT_MS)
const response = await fetch(`${this.apiUrl}/v3/documents/documents`, {
method: "POST",
headers: {
Expand All @@ -374,6 +380,7 @@ export class SupermemoryClient {
order: "desc",
containerTags,
}),
signal,
})
if (!response.ok) {
throw Object.assign(new Error("Failed to fetch documents"), {
Expand All @@ -387,6 +394,14 @@ export class SupermemoryClient {
}

private handleError(error: unknown): never {
// Handle request timeout (AbortSignal.timeout or explicit abort)
if (
error instanceof Error &&
(error.name === "AbortError" || error.name === "TimeoutError")
) {
throw new Error("Request to Supermemory API timed out")
}

// Handle network/fetch errors
if (error instanceof TypeError) {
if (
Expand Down
1 change: 1 addition & 0 deletions apps/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading