Skip to content

Commit e73cad1

Browse files
Copilotcliffhall
andcommitted
Fix OAuth: use authorization_servers URL from resource metadata for auth server discovery
The discoverScopes function always used the MCP server URL to discover authorization server metadata, ignoring the authorization_servers field from the protected resource metadata. This caused failures when the authorization server is on a different domain than the MCP server. Now checks resourceMetadata.authorization_servers for a URL to use, falling back to the MCP server URL when not available. Fixes #675 Co-authored-by: cliffhall <[email protected]>
1 parent d9a20d8 commit e73cad1

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎client/src/lib/__tests__/auth.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ describe("discoverScopes", () => {
8585
},
8686
expected: "read",
8787
},
88+
{
89+
name: "uses authorization_servers URL from resource metadata for discovery",
90+
mockResolves: baseMetadata,
91+
serverUrl: "https://mcp-server.com",
92+
resourceMetadata: {
93+
resource: "https://mcp-server.com",
94+
authorization_servers: ["https://auth-server.com/"],
95+
},
96+
expected: "read write",
97+
expectedCallUrl: "https://auth-server.com/",
98+
},
99+
{
100+
name: "uses authorization_servers URL with path for discovery",
101+
mockResolves: baseMetadata,
102+
serverUrl: "https://mcp-server.com",
103+
resourceMetadata: {
104+
resource: "https://mcp-server.com",
105+
authorization_servers: ["https://auth-server.com/realms/my-realm/"],
106+
},
107+
expected: "read write",
108+
expectedCallUrl: "https://auth-server.com/realms/my-realm/",
109+
},
110+
{
111+
name: "falls back to serverUrl when authorization_servers is empty",
112+
mockResolves: baseMetadata,
113+
serverUrl: "https://mcp-server.com",
114+
resourceMetadata: {
115+
resource: "https://mcp-server.com",
116+
authorization_servers: [],
117+
},
118+
expected: "read write",
119+
expectedCallUrl: "https://mcp-server.com/",
120+
},
88121
];
89122

90123
const undefinedCases = [

‎client/src/lib/auth.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ export const discoverScopes = async (
2424
resourceMetadata?: OAuthProtectedResourceMetadata,
2525
): Promise<string | undefined> => {
2626
try {
27-
const metadata = await discoverAuthorizationServerMetadata(
28-
new URL("/", serverUrl),
29-
);
27+
// Use the authorization server URL from resource metadata if available,
28+
// otherwise fall back to the MCP server URL
29+
const authServerUrl = resourceMetadata?.authorization_servers?.length
30+
? new URL(resourceMetadata.authorization_servers[0])
31+
: new URL("/", serverUrl);
32+
const metadata = await discoverAuthorizationServerMetadata(authServerUrl);
3033

3134
// Prefer resource metadata scopes, but fall back to OAuth metadata if empty
3235
const resourceScopes = resourceMetadata?.scopes_supported;

0 commit comments

Comments
 (0)