Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
e6c95ea
Added configurable token endpoint auth method selection
RobotechUSA Apr 2, 2026
04f8e40
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Apr 2, 2026
b18ff50
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Apr 9, 2026
8728e04
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Apr 13, 2026
21371a8
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Apr 17, 2026
2ae03ee
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA May 2, 2026
9e9b8b1
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA May 18, 2026
84ee3df
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jun 2, 2026
14a90d7
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jun 5, 2026
df9d65b
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jun 16, 2026
16cf29d
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jun 25, 2026
3d2b595
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jul 6, 2026
0a47ebd
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jul 14, 2026
31647cb
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jul 21, 2026
fff35fd
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Jul 31, 2026
550059f
Merge branch 'main' into feature/oauth-token-endpoint-auth-selector
RobotechUSA Aug 5, 2026
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
14 changes: 14 additions & 0 deletions src/ModelContextProtocol.Core/Authentication/ClientOAuthOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ public sealed class ClientOAuthOptions
UrlFormat = ModelContextProtocol.Obsoletions.AuthorizationRedirectDelegate_Url)]
public AuthorizationRedirectDelegate? AuthorizationRedirectDelegate { get; set; }

/// <summary>
/// Gets or sets the token endpoint authentication method selector function.
/// </summary>
/// <remarks>
/// <para>
/// 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.
/// </para>
/// <para>
/// 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.
/// </para>
/// </remarks>
public Func<IReadOnlyList<string>?, string?>? TokenEndpointAuthMethodSelector { get; set; }

/// <summary>
/// Gets or sets the authorization server selector function.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal sealed partial class ClientOAuthProvider : McpHttpClient
private readonly string? _configuredScopes;
private readonly ScopeSelectorDelegate? _scopeSelector;
private readonly IDictionary<string, string> _additionalAuthorizationParameters;
private readonly Func<IReadOnlyList<string>?, string?> _tokenEndpointAuthMethodSelector;
private readonly Func<IReadOnlyList<Uri>, Uri?> _authServerSelector;
private readonly Func<AuthorizationCallbackContext, CancellationToken, Task<AuthorizationResult?>> _authorizationCallbackHandler;
private readonly bool _validateAuthorizationResponseState;
Expand Down Expand Up @@ -106,6 +107,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;

Expand Down Expand Up @@ -154,6 +158,13 @@ public ClientOAuthProvider(
_tokenCache = options.TokenCache ?? new InMemoryTokenCache();
}

/// <summary>
/// Default token endpoint authentication method selector that selects the first supported method from the authorization server metadata.
/// </summary>
/// <param name="tokenEndpointAuthMethodsSupported">The list of supported token endpoint authentication methods.</param>
/// <returns>The selected token endpoint authentication method, or null if none are available.</returns>
private static string? DefaultTokenEndpointAuthMethodSelector(IReadOnlyList<string>? tokenEndpointAuthMethodsSupported) => tokenEndpointAuthMethodsSupported?.FirstOrDefault();

/// <summary>
/// Default authorization server selection strategy that selects the first available server.
/// </summary>
Expand Down Expand Up @@ -475,7 +486,7 @@ private async Task<string> 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);

// Store auth server metadata for future refresh operations
_authServerMetadata = authServerMetadata;
Expand Down