diff --git a/src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs b/src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs index 9cd109187..bf297d9e3 100644 --- a/src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs +++ b/src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs @@ -122,6 +122,20 @@ public sealed class ClientOAuthOptions UrlFormat = ModelContextProtocol.Obsoletions.AuthorizationRedirectDelegate_Url)] public AuthorizationRedirectDelegate? AuthorizationRedirectDelegate { get; set; } + /// + /// Gets or sets the token endpoint authentication method selector function. + /// + /// + /// + /// This function is used to select which token endpoint authentication method to use when multiple methods are available. + /// If not specified, the first available method will be selected. + /// + /// + /// The function receives a list of supported authentication methods from the authorization server metadata and should return the selected method, or null if no suitable method is found. + /// + /// + public Func?, string?>? TokenEndpointAuthMethodSelector { get; set; } + /// /// Gets or sets the authorization server selector function. /// diff --git a/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs b/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs index 9644e29b5..5a89e4b5a 100644 --- a/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs +++ b/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs @@ -30,6 +30,7 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient private readonly string? _configuredScopes; private readonly ScopeSelectorDelegate? _scopeSelector; private readonly IDictionary _additionalAuthorizationParameters; + private readonly Func?, string?> _tokenEndpointAuthMethodSelector; private readonly Func, Uri?> _authServerSelector; private readonly Func> _authorizationCallbackHandler; private readonly bool _validateAuthorizationResponseState; @@ -107,6 +108,9 @@ public ClientOAuthProvider( _additionalAuthorizationParameters = options.AdditionalAuthorizationParameters; _clientMetadataDocumentUri = options.ClientMetadataDocumentUri; + // Set up token endpoint authentication method selector (use default if not provided) + _tokenEndpointAuthMethodSelector = options.TokenEndpointAuthMethodSelector ?? DefaultTokenEndpointAuthMethodSelector; + // Set up authorization server selection strategy _authServerSelector = options.AuthServerSelector ?? DefaultAuthServerSelector; @@ -155,6 +159,13 @@ public ClientOAuthProvider( _tokenCache = options.TokenCache ?? new InMemoryTokenCache(); } + /// + /// Default token endpoint authentication method selector that selects the first supported method from the authorization server metadata. + /// + /// The list of supported token endpoint authentication methods. + /// The selected token endpoint authentication method, or null if none are available. + private static string? DefaultTokenEndpointAuthMethodSelector(IReadOnlyList? tokenEndpointAuthMethodsSupported) => tokenEndpointAuthMethodsSupported?.FirstOrDefault(); + /// /// Default authorization server selection strategy that selects the first available server. /// @@ -487,7 +498,7 @@ private async Task GetAccessTokenCoreAsync(HttpResponseMessage response, _clientCredentialsAuthorizationServer = selectedAuthServer.OriginalString; // Determine the token endpoint auth method from server metadata if not already set by DCR. - _tokenEndpointAuthMethod ??= authServerMetadata.TokenEndpointAuthMethodsSupported?.FirstOrDefault(); + _tokenEndpointAuthMethod ??= _tokenEndpointAuthMethodSelector(authServerMetadata.TokenEndpointAuthMethodsSupported); // Enable silent refresh only after client registration is ready. // A successful cold-start refresh initializes this state before its early return.