Skip to content

Commit 2d7da73

Browse files
committed
Work on the basic OAuth implementation in sample
1 parent 2e06f59 commit 2d7da73

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

samples/ProtectedMCPClient/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static async Task Main(string[] args)
1313

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

16-
var tokenProvider = new SimpleAccessTokenProvider(new Uri(serverUrl));
16+
var tokenProvider = new BasicOAuthAuthorizationProvider(new Uri(serverUrl));
1717
var httpClient = new HttpClient().UseMcpAuthorizationProvider(tokenProvider);
1818

1919
Console.WriteLine();

samples/ProtectedMCPClient/SimpleAccessTokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ 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 : IMcpAuthorizationProvider
18+
public partial class BasicOAuthAuthorizationProvider : IMcpAuthorizationProvider
1919
{
2020
private readonly ConcurrentDictionary<string, TokenContainer> _tokenCache = new();
2121
private readonly Uri _serverUrl;
2222
private readonly string _clientId;
2323
private readonly string _clientSecret;
2424
private readonly Uri _redirectUri;
2525

26-
public SimpleAccessTokenProvider(Uri serverUrl, string clientId = "demo-client", string clientSecret = "", Uri? redirectUri = null)
26+
public BasicOAuthAuthorizationProvider(Uri serverUrl, string clientId = "demo-client", string clientSecret = "", Uri? redirectUri = null)
2727
{
2828
_serverUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl));
2929
_clientId = clientId;

samples/ProtectedMCPClient/Utils/AuthorizationServerUtils.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,26 @@ internal class AuthorizationServerUtils
1616
CancellationToken cancellationToken = default)
1717
{
1818
using var httpClient = new HttpClient();
19-
19+
20+
// Make sure the base URL ends with a slash to correctly append well-known paths
21+
string baseUrl = authorizationServerUrl.ToString();
22+
if (!baseUrl.EndsWith("/"))
23+
{
24+
baseUrl += "/";
25+
}
26+
2027
// Try OpenID Connect configuration endpoint first, then OAuth Authorization Server Metadata endpoint
21-
string[] wellKnownEndpoints = {
22-
"/.well-known/openid-configuration",
23-
"/.well-known/oauth-authorization-server"
24-
};
28+
string[] wellKnownPaths = {
29+
".well-known/openid-configuration",
30+
".well-known/oauth-authorization-server"
31+
};
2532

26-
foreach (var endpoint in wellKnownEndpoints)
33+
foreach (var path in wellKnownPaths)
2734
{
28-
var metadataUrl = new Uri(authorizationServerUrl, endpoint);
35+
// Simply combine the base URL (now with trailing slash) with the path (without leading slash)
36+
var metadataUrl = new Uri(baseUrl + path);
37+
Console.WriteLine($"Trying authorization server metadata endpoint: {metadataUrl}");
38+
2939
var metadata = await TryFetchMetadataAsync(httpClient, metadataUrl, cancellationToken);
3040
if (metadata != null)
3141
{
@@ -54,13 +64,17 @@ internal class AuthorizationServerUtils
5464
if (response.IsSuccessStatusCode)
5565
{
5666
var content = await response.Content.ReadAsStreamAsync();
57-
return await JsonSerializer.DeserializeAsync<AuthorizationServerMetadata>(content, new JsonSerializerOptions() { WriteIndented = true },
67+
return await JsonSerializer.DeserializeAsync<AuthorizationServerMetadata>(content,
68+
new JsonSerializerOptions
69+
{
70+
PropertyNameCaseInsensitive = true,
71+
},
5872
cancellationToken);
5973
}
6074
}
61-
catch (Exception)
75+
catch (Exception ex)
6276
{
63-
// Ignore exceptions and return null
77+
Console.WriteLine($"Error fetching metadata from {metadataUrl}: {ex.Message}");
6478
}
6579

6680
return null;

0 commit comments

Comments
 (0)