@@ -24,8 +24,8 @@ public class BasicOAuthAuthorizationProvider : ITokenProvider
2424 private readonly HttpClient _httpClient ;
2525 private readonly AuthorizationHelpers _authorizationHelpers ;
2626
27- // Client name for IHttpClientFactory used by the BasicOAuthAuthorizationProvider
28- public const string HttpClientName = "ProtectedMCPClient.OAuth" ;
27+ // Lazy-initialized shared HttpClient for when no client is provided
28+ private static readonly Lazy < HttpClient > _defaultHttpClient = new ( ( ) => new HttpClient ( ) ) ;
2929
3030 private TokenContainer ? _token ;
3131 private AuthorizationServerMetadata ? _authServerMetadata ;
@@ -34,27 +34,24 @@ public class BasicOAuthAuthorizationProvider : ITokenProvider
3434 /// Initializes a new instance of the <see cref="BasicOAuthAuthorizationProvider"/> class.
3535 /// </summary>
3636 /// <param name="serverUrl">The MCP server URL.</param>
37- /// <param name="httpClientFactory ">The HTTP client factory to use for creating HTTP clients .</param>
37+ /// <param name="httpClient ">The HTTP client to use for OAuth requests. If null, a default HttpClient will be used .</param>
3838 /// <param name="authorizationHelpers">The authorization helpers.</param>
3939 /// <param name="clientId">OAuth client ID.</param>
4040 /// <param name="clientSecret">OAuth client secret.</param>
4141 /// <param name="redirectUri">OAuth redirect URI.</param>
4242 /// <param name="scopes">OAuth scopes.</param>
4343 public BasicOAuthAuthorizationProvider (
4444 Uri serverUrl ,
45- IHttpClientFactory httpClientFactory ,
46- AuthorizationHelpers authorizationHelpers ,
45+ HttpClient ? httpClient ,
46+ AuthorizationHelpers ? authorizationHelpers ,
4747 string clientId = "demo-client" ,
4848 string clientSecret = "" ,
4949 Uri ? redirectUri = null ,
5050 IEnumerable < string > ? scopes = null )
5151 {
5252 _serverUrl = serverUrl ?? throw new ArgumentNullException ( nameof ( serverUrl ) ) ;
53- if ( httpClientFactory == null ) throw new ArgumentNullException ( nameof ( httpClientFactory ) ) ;
54- _authorizationHelpers = authorizationHelpers ?? throw new ArgumentNullException ( nameof ( authorizationHelpers ) ) ;
55-
56- // Get the HttpClient once during construction instead of for each request
57- _httpClient = httpClientFactory . CreateClient ( HttpClientName ) ;
53+ _httpClient = httpClient ?? _defaultHttpClient . Value ;
54+ _authorizationHelpers = authorizationHelpers ?? new AuthorizationHelpers ( _httpClient ) ;
5855
5956 _redirectUri = redirectUri ?? new Uri ( "http://localhost:8080/callback" ) ;
6057 _scopes = scopes ? . ToList ( ) ?? [ ] ;
0 commit comments