Skip to content

Commit ca0cdf3

Browse files
committed
Fix LINQ issue. Simplify best scheme match check
1 parent 26f44de commit ca0cdf3

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/ModelContextProtocol/Authentication/AuthorizationDelegatingHandler.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,25 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
5656
}
5757
else
5858
{
59-
// Try to find any matching scheme between server and provider
60-
bestSchemeMatch = serverSchemes.FirstOrDefault(scheme => supportedSchemes.Contains(scheme));
59+
// Try to find any matching scheme between server and provider using a HashSet for O(N) time complexity
60+
// Convert the supported schemes to a HashSet for O(1) lookups
61+
var supportedSchemesSet = new HashSet<string>(supportedSchemes, StringComparer.OrdinalIgnoreCase);
6162

62-
// If still no match, default to the provider's preferred scheme
63-
if (bestSchemeMatch == null && serverSchemes.Count > 0)
64-
{
65-
throw new AuthenticationSchemeMismatchException(
66-
$"No matching authentication scheme found. Server supports: [{string.Join(", ", serverSchemes)}], " +
67-
$"Provider supports: [{string.Join(", ", supportedSchemes)}].",
68-
serverSchemes,
69-
supportedSchemes);
70-
}
71-
else if (bestSchemeMatch == null)
63+
// Find the first server scheme that's in our supported set
64+
bestSchemeMatch = serverSchemes.FirstOrDefault(scheme => supportedSchemesSet.Contains(scheme));
65+
66+
// If no match was found, either throw an exception or use default
67+
if (bestSchemeMatch is null)
7268
{
69+
if (serverSchemes.Count > 0)
70+
{
71+
throw new AuthenticationSchemeMismatchException(
72+
$"No matching authentication scheme found. Server supports: [{string.Join(", ", serverSchemes)}], " +
73+
$"Provider supports: [{string.Join(", ", supportedSchemes)}].",
74+
serverSchemes,
75+
supportedSchemes);
76+
}
77+
7378
// If the server didn't specify any schemes, use the provider's default
7479
bestSchemeMatch = supportedSchemes.FirstOrDefault();
7580
}

0 commit comments

Comments
 (0)