Skip to content

Commit d6fcb49

Browse files
jeffhandleyCopilot
andcommitted
Avoid exceptions for control flow in auth server metadata fallback
Refactor GetAuthServerMetadataAsync to accept an allowDefaultFallback parameter and return BuildDefaultAuthServerMetadata directly instead of throwing and catching McpException at the call site. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e517f14 commit d6fcb49

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,7 @@ private async Task<string> GetAccessTokenAsync(HttpResponseMessage response, boo
270270
LogSelectedAuthorizationServer(selectedAuthServer, availableAuthorizationServers.Count);
271271

272272
// Get auth server metadata
273-
AuthorizationServerMetadata authServerMetadata;
274-
try
275-
{
276-
authServerMetadata = await GetAuthServerMetadataAsync(selectedAuthServer, cancellationToken).ConfigureAwait(false);
277-
}
278-
catch (McpException) when (protectedResourceMetadata.Resource is null)
279-
{
280-
// 2025-03-26 backcompat: when PRM is unavailable and auth server metadata discovery
281-
// also fails, fall back to default endpoint paths per the 2025-03-26 spec.
282-
authServerMetadata = BuildDefaultAuthServerMetadata(selectedAuthServer);
283-
}
273+
var authServerMetadata = await GetAuthServerMetadataAsync(selectedAuthServer, protectedResourceMetadata.Resource, cancellationToken).ConfigureAwait(false);
284274

285275
// The existing access token must be invalid to have resulted in a 401 response, but refresh might still work.
286276
var resourceUri = GetResourceUri(protectedResourceMetadata);
@@ -342,7 +332,7 @@ static bool IsValidClientMetadataDocumentUri(Uri uri)
342332
&& uri.AbsolutePath.Length > 1; // AbsolutePath always starts with "/"
343333
}
344334

345-
private async Task<AuthorizationServerMetadata> GetAuthServerMetadataAsync(Uri authServerUri, CancellationToken cancellationToken)
335+
private async Task<AuthorizationServerMetadata> GetAuthServerMetadataAsync(Uri authServerUri, string? resourceUri, CancellationToken cancellationToken)
346336
{
347337
foreach (var wellKnownEndpoint in GetWellKnownAuthorizationServerMetadataUris(authServerUri))
348338
{
@@ -386,6 +376,13 @@ private async Task<AuthorizationServerMetadata> GetAuthServerMetadataAsync(Uri a
386376
}
387377
}
388378

379+
if (resourceUri is null)
380+
{
381+
// 2025-03-26 backcompat: when PRM is unavailable and auth server metadata discovery
382+
// also fails, fall back to default endpoint paths per the 2025-03-26 spec.
383+
return BuildDefaultAuthServerMetadata(authServerUri);
384+
}
385+
389386
throw new McpException($"Failed to find .well-known/openid-configuration or .well-known/oauth-authorization-server metadata for authorization server: '{authServerUri}'");
390387
}
391388

0 commit comments

Comments
 (0)