Skip to content

Commit f657ead

Browse files
authored
fix: retry next endpoint on CORS error during auth server discovery (#827)
1 parent 0551cc5 commit f657ead

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/client/auth.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@ describe("OAuth Authorization", () => {
899899
"MCP-Protocol-Version": "2025-01-01"
900900
});
901901
});
902+
903+
it("returns undefined when all URLs fail with CORS errors", async () => {
904+
// All fetch attempts fail with CORS errors (TypeError)
905+
mockFetch.mockImplementation(() => Promise.reject(new TypeError("CORS error")));
906+
907+
const metadata = await discoverAuthorizationServerMetadata("https://auth.example.com/tenant1");
908+
909+
expect(metadata).toBeUndefined();
910+
911+
// Verify that all discovery URLs were attempted
912+
expect(mockFetch).toHaveBeenCalledTimes(8); // 4 URLs × 2 attempts each (with and without headers)
913+
});
902914
});
903915

904916
describe("startAuthorization", () => {

src/client/auth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,11 @@ export async function discoverAuthorizationServerMetadata(
758758
const response = await fetchWithCorsRetry(endpointUrl, headers, fetchFn);
759759

760760
if (!response) {
761-
throw new Error(`CORS error trying to load ${type === 'oauth' ? 'OAuth' : 'OpenID provider'} metadata from ${endpointUrl}`);
761+
/**
762+
* CORS error occurred - don't throw as the endpoint may not allow CORS,
763+
* continue trying other possible endpoints
764+
*/
765+
continue;
762766
}
763767

764768
if (!response.ok) {

0 commit comments

Comments
 (0)