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
5 changes: 5 additions & 0 deletions .changeset/non-json-oauth-discovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/client': patch
---

Continue authorization server metadata discovery when a candidate well-known endpoint returns HTTP 200 with a non-JSON body, allowing fallback to the next OAuth/OIDC discovery URL.
12 changes: 9 additions & 3 deletions packages/client/src/client/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1262,10 +1262,16 @@ export async function discoverAuthorizationServerMetadata(
);
}

let metadata: unknown;
try {
metadata = await response.json();
} catch {
await response.body?.cancel().catch(() => {});
continue;
}

// Parse and validate based on type
return type === 'oauth'
? OAuthMetadataSchema.parse(await response.json())
: OpenIdProviderDiscoveryMetadataSchema.parse(await response.json());
return type === 'oauth' ? OAuthMetadataSchema.parse(metadata) : OpenIdProviderDiscoveryMetadataSchema.parse(metadata);
}

return undefined;
Expand Down
12 changes: 12 additions & 0 deletions packages/client/test/client/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,18 @@ describe('OAuth Authorization', () => {
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it('continues when an authorization metadata endpoint returns non-JSON', async () => {
mockFetch.mockResolvedValueOnce(new Response('<html>not metadata</html>', { status: 200 }));
mockFetch.mockResolvedValueOnce(Response.json(validOpenIdMetadata, { status: 200 }));

const metadata = await discoverAuthorizationServerMetadata('https://auth.example.com/tenant1');

expect(metadata).toEqual(validOpenIdMetadata);
expect(mockFetch).toHaveBeenCalledTimes(2);
expect(mockFetch.mock.calls[0]![0].toString()).toBe('https://auth.example.com/.well-known/oauth-authorization-server/tenant1');
expect(mockFetch.mock.calls[1]![0].toString()).toBe('https://auth.example.com/.well-known/openid-configuration/tenant1');
});

it('throws on non-502 5xx errors', async () => {
mockFetch.mockResolvedValueOnce({
ok: false,
Expand Down
Loading