Skip to content
Open
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
17 changes: 14 additions & 3 deletions lib/mcp/verifyApiKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ export async function verifyBearerToken(
bearerToken?: string,
): Promise<McpAuthInfo | undefined> {
if (!bearerToken) {
console.error("[MCP Auth] No bearer token provided");
return undefined;
}

// Try Privy JWT first
try {
const accountId = await getAccountIdByAuthToken(bearerToken);

console.log("[MCP Auth] Privy JWT validated, accountId:", accountId);
return {
token: bearerToken,
scopes: ["mcp:tools"],
Expand All @@ -41,18 +43,23 @@ export async function verifyBearerToken(
orgId: null,
},
};
} catch {
// Privy validation failed, try API key
} catch (privyError) {
console.log(
"[MCP Auth] Privy JWT validation failed:",
privyError instanceof Error ? privyError.message : privyError,
);
}

// Try API key validation
try {
const accountId = await getAccountIdByApiKey(bearerToken);

if (!accountId) {
console.error("[MCP Auth] API key validation returned no accountId");
return undefined;
}

console.log("[MCP Auth] API key validated, accountId:", accountId);
return {
token: bearerToken,
scopes: ["mcp:tools"],
Expand All @@ -62,7 +69,11 @@ export async function verifyBearerToken(
orgId: null,
},
};
} catch {
} catch (apiKeyError) {
console.error(
"[MCP Auth] API key validation failed:",
apiKeyError instanceof Error ? apiKeyError.message : apiKeyError,
);
return undefined;
}
}
Loading