Skip to content

Commit 531370a

Browse files
committed
Simplification
1 parent 0aeec19 commit 531370a

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

samples/ProtectedMCPClient/BasicOAuthAuthorizationProvider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ public class BasicOAuthAuthorizationProvider(
2222
string clientId = "demo-client",
2323
string clientSecret = "",
2424
Uri? redirectUri = null,
25-
IEnumerable<string>? scopes = null,
26-
HttpClient? httpClient = null) : IMcpAuthorizationProvider
25+
IEnumerable<string>? scopes = null) : IMcpAuthorizationProvider
2726
{
2827
private readonly Uri _serverUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl));
2928
private readonly Uri _redirectUri = redirectUri ?? new Uri("http://localhost:8080/callback");
3029
private readonly List<string> _scopes = scopes?.ToList() ?? new List<string>();
31-
private readonly HttpClient _httpClient = httpClient ?? new HttpClient();
30+
private readonly HttpClient _httpClient = new HttpClient();
3231

3332
// Single token storage
3433
private TokenContainer? _token;

samples/ProtectedMCPClient/Program.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ static async Task Main(string[] args)
1313

1414
var serverUrl = "http://localhost:7071/sse";
1515

16-
var httpClient = new HttpClient();
17-
1816
var tokenProvider = new BasicOAuthAuthorizationProvider(
1917
new Uri(serverUrl),
2018
clientId: "6ad97b5f-7a7b-413f-8603-7a3517d4adb8",
2119
redirectUri: new Uri("http://localhost:1179/callback"),
22-
scopes: new List<string> { "api://167b4284-3f92-4436-92ed-38b38f83ae08/weather.read" },
23-
httpClient: httpClient);
20+
scopes: new List<string> { "api://167b4284-3f92-4436-92ed-38b38f83ae08/weather.read" }
21+
);
22+
23+
var authHandler = new AuthorizationDelegatingHandler(tokenProvider, "Bearer")
24+
{
25+
// 3. Set the inner handler (the handler that actually sends the request)
26+
InnerHandler = new HttpClientHandler()
27+
};
2428

25-
// Use the same HttpClient instance with the authentication provider
26-
var authenticatedClient = httpClient.UseMcpAuthorizationProvider(tokenProvider);
29+
var httpClient = new HttpClient(authHandler);
2730

2831
Console.WriteLine();
2932
Console.WriteLine($"Connecting to weather server at {serverUrl}...");
@@ -36,7 +39,8 @@ static async Task Main(string[] args)
3639
Name = "Secure Weather Client"
3740
};
3841

39-
var transport = new SseClientTransport(transportOptions, authenticatedClient);
42+
// Use the single, pre-configured HttpClient
43+
var transport = new SseClientTransport(transportOptions, httpClient);
4044

4145
var client = await McpClientFactory.CreateAsync(transport);
4246

src/ModelContextProtocol/Authentication/AuthorizationDelegatingHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ModelContextProtocol.Authentication;
55
/// <summary>
66
/// A delegating handler that adds authentication tokens to requests and handles 401 responses.
77
/// </summary>
8-
internal class AuthorizationDelegatingHandler : DelegatingHandler
8+
public class AuthorizationDelegatingHandler : DelegatingHandler
99
{
1010
private readonly IMcpAuthorizationProvider _tokenProvider;
1111
private readonly string _scheme;

0 commit comments

Comments
 (0)