Skip to content

Commit 2e06f59

Browse files
committed
Update for consistency
1 parent 4073efd commit 2e06f59

File tree

5 files changed

+11
-31
lines changed

5 files changed

+11
-31
lines changed

samples/ProtectedMCPClient/Program.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,36 @@
11
using ModelContextProtocol.Authentication;
22
using ModelContextProtocol.Client;
33
using ModelContextProtocol.Protocol.Transport;
4-
using System.Diagnostics;
54

65
namespace ProtectedMCPClient;
76

87
class Program
98
{
109
static async Task Main(string[] args)
1110
{
12-
Console.WriteLine("MCP Secure Weather Client with Authentication");
13-
Console.WriteLine("==============================================");
11+
Console.WriteLine("Protected MCP Weather Server");
1412
Console.WriteLine();
1513

16-
// Create a standard HttpClient with authentication configured
17-
var serverUrl = "http://localhost:7071/sse"; // Default server URL
14+
var serverUrl = "http://localhost:7071/sse";
1815

19-
// Allow the user to specify a different server URL
20-
Console.WriteLine($"Server URL (press Enter for default: {serverUrl}):");
21-
var userInput = Console.ReadLine();
22-
if (!string.IsNullOrWhiteSpace(userInput))
23-
{
24-
serverUrl = userInput;
25-
}
26-
27-
// Create a single HttpClient with authentication configured
2816
var tokenProvider = new SimpleAccessTokenProvider(new Uri(serverUrl));
29-
var httpClient = new HttpClient().UseAuthenticationProvider(tokenProvider);
17+
var httpClient = new HttpClient().UseMcpAuthorizationProvider(tokenProvider);
3018

3119
Console.WriteLine();
3220
Console.WriteLine($"Connecting to weather server at {serverUrl}...");
33-
Console.WriteLine("When prompted for authorization, the challenge will be verified automatically.");
34-
Console.WriteLine("If required, you'll be guided through any necessary authentication steps.");
35-
Console.WriteLine();
3621

3722
try
3823
{
39-
// Create SseClientTransportOptions with the server URL
4024
var transportOptions = new SseClientTransportOptions
4125
{
4226
Endpoint = new Uri(serverUrl),
4327
Name = "Secure Weather Client"
4428
};
4529

46-
// Create SseClientTransport with our authenticated HTTP client
4730
var transport = new SseClientTransport(transportOptions, httpClient);
4831

49-
// Create an MCP client using the factory method with our transport
5032
var client = await McpClientFactory.CreateAsync(transport);
5133

52-
// Get the list of available tools
5334
var tools = await client.ListToolsAsync();
5435
if (tools.Count == 0)
5536
{
@@ -60,7 +41,6 @@ static async Task Main(string[] args)
6041
Console.WriteLine($"Found {tools.Count} tools on the server.");
6142
Console.WriteLine();
6243

63-
// Call the protected-data tool which requires authentication
6444
if (tools.Any(t => t.Name == "protected-data"))
6545
{
6646
Console.WriteLine("Calling protected-data tool...");

samples/ProtectedMCPClient/SimpleAccessTokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace ProtectedMCPClient;
1515
/// A simple implementation of IAccessTokenProvider that uses a fixed API key.
1616
/// This is just for demonstration purposes.
1717
/// </summary>
18-
public partial class SimpleAccessTokenProvider : ITokenProvider
18+
public partial class SimpleAccessTokenProvider : IMcpAuthorizationProvider
1919
{
2020
private readonly ConcurrentDictionary<string, TokenContainer> _tokenCache = new();
2121
private readonly Uri _serverUrl;
@@ -32,7 +32,7 @@ public SimpleAccessTokenProvider(Uri serverUrl, string clientId = "demo-client",
3232
}
3333

3434
/// <inheritdoc />
35-
public async Task<string?> GetTokenAsync(Uri resourceUri, CancellationToken cancellationToken = default)
35+
public async Task<string?> GetCredentialAsync(Uri resourceUri, CancellationToken cancellationToken = default)
3636
{
3737
Console.WriteLine($"Getting authentication token for {resourceUri}");
3838

src/ModelContextProtocol/Authentication/AuthorizationDelegatingHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace ModelContextProtocol.Authentication;
77
/// </summary>
88
internal class AuthorizationDelegatingHandler : DelegatingHandler
99
{
10-
private readonly ITokenProvider _tokenProvider;
10+
private readonly IMcpAuthorizationProvider _tokenProvider;
1111
private readonly string _scheme;
1212

1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="AuthorizationDelegatingHandler"/> class.
1515
/// </summary>
1616
/// <param name="tokenProvider">The provider that supplies authentication tokens.</param>
1717
/// <param name="scheme">The authentication scheme to use, e.g., "Bearer".</param>
18-
public AuthorizationDelegatingHandler(ITokenProvider tokenProvider, string scheme)
18+
public AuthorizationDelegatingHandler(IMcpAuthorizationProvider tokenProvider, string scheme)
1919
{
2020
_tokenProvider = tokenProvider ?? throw new ArgumentNullException(nameof(tokenProvider));
2121
_scheme = scheme ?? throw new ArgumentNullException(nameof(scheme));
@@ -66,7 +66,7 @@ private async Task AddAuthorizationHeaderAsync(HttpRequestMessage request, Cance
6666
{
6767
if (request.RequestUri != null)
6868
{
69-
var token = await _tokenProvider.GetTokenAsync(request.RequestUri, cancellationToken);
69+
var token = await _tokenProvider.GetCredentialAsync(request.RequestUri, cancellationToken);
7070
if (!string.IsNullOrEmpty(token))
7171
{
7272
request.Headers.Authorization = new AuthenticationHeaderValue(_scheme, token);

src/ModelContextProtocol/Authentication/HttpClientExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class HttpClientExtensions
2020
/// <item>Retries the request with the new token if token refresh is successful</item>
2121
/// </list>
2222
/// </remarks>
23-
public static HttpClient UseAuthenticationProvider(this HttpClient httpClient, ITokenProvider tokenProvider, string scheme = "Bearer")
23+
public static HttpClient UseMcpAuthorizationProvider(this HttpClient httpClient, IMcpAuthorizationProvider tokenProvider, string scheme = "Bearer")
2424
{
2525
if (httpClient == null)
2626
throw new ArgumentNullException(nameof(httpClient));

src/ModelContextProtocol/Authentication/ITokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ namespace ModelContextProtocol.Authentication;
44
/// Defines an interface for providing authentication for requests.
55
/// This is the main extensibility point for authentication in MCP clients.
66
/// </summary>
7-
public interface ITokenProvider
7+
public interface IMcpAuthorizationProvider
88
{
99
/// <summary>
1010
/// Gets an authentication token or credential for authenticating requests to a resource.
1111
/// </summary>
1212
/// <param name="resourceUri">The URI of the resource requiring authentication.</param>
1313
/// <param name="cancellationToken">A token to cancel the operation.</param>
1414
/// <returns>An authentication token string or null if no token could be obtained.</returns>
15-
Task<string?> GetTokenAsync(Uri resourceUri, CancellationToken cancellationToken = default);
15+
Task<string?> GetCredentialAsync(Uri resourceUri, CancellationToken cancellationToken = default);
1616

1717
/// <summary>
1818
/// Handles a 401 Unauthorized response from a resource.

0 commit comments

Comments
 (0)