-
Notifications
You must be signed in to change notification settings - Fork 11
Refactor MCP token provider selection and fallback logic #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1fe6011
Refactor MCP token provider selection and fallback logic
MattB-msft c98cefa
Potential fix for pull request finding 'Missed 'readonly' opportunity'
MattB-msft de28355
Potential fix for pull request finding
MattB-msft 755830e
Merge branch 'main' into users/mbarbour/updateToolsDisco
MattB-msft 3117080
Refactor token handling and fix typos
MattB-msft e7dfdea
Addressing Per changes requested
MattB-msft daa2935
Refactor tests and update project visibility
MattB-msft 21a15ff
Refactor token provider usage in tests
MattB-msft 07938df
Merge branch 'main' into users/mbarbour/updateToolsDisco
gwharris7 399bda1
Resolve auth token for tool discovery in all scenarios
MattB-msft d131797
Refactor token provider selection with ternary logic
MattB-msft 323a78c
Refactor token provider initialization logic
MattB-msft 70659e7
Merge branch 'main' into users/mbarbour/updateToolsDisco
gwharris7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
MattB-msft marked this conversation as resolved.
|
||
|
|
||
| using Microsoft.Agents.A365.Tooling.Models; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.Agents.A365.Tooling.Services | ||
| { | ||
| internal class TokenProviderCollection : IMcpTokenProvider | ||
| { | ||
| readonly SortedDictionary<int, IMcpTokenProvider> _providers; | ||
| readonly ILogger _logger; | ||
|
|
||
| public TokenProviderCollection( | ||
| ILogger logger, | ||
| params IMcpTokenProvider[] providers) | ||
| { | ||
| _logger = logger; | ||
| _providers = new SortedDictionary<int, IMcpTokenProvider>(); | ||
| for (int i = 0; i < providers.Length; i++) | ||
| { | ||
| _providers.Add(i, providers[i]); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public async Task<string> GetTokenAsync(MCPServerConfig server, CancellationToken cancellationToken = default) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if ( _providers != null && _providers.Count == 0) | ||
| { | ||
| throw new InvalidOperationException("No token providers are configured."); | ||
| } | ||
|
|
||
| if (_providers!.Values.Any(p => p == null)) | ||
| { | ||
| throw new InvalidOperationException("One or more token providers are null."); | ||
| } | ||
|
|
||
| List<Exception> exceptions = new List<Exception>(); | ||
| // Try each provider in turn, then return the first successful token. If no providers can return a token, throw an exception. | ||
| foreach (var provider in _providers.Values) | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
MattB-msft marked this conversation as resolved.
Dismissed
|
||
| { | ||
| try | ||
| { | ||
| var token = await provider.GetTokenAsync(server, cancellationToken); | ||
| if (!string.IsNullOrWhiteSpace(token)) | ||
| { | ||
| return token; | ||
| } | ||
| else | ||
| { | ||
| _logger.LogDebug("Provider {ProviderName} returned an empty token.", provider.GetType().Name); | ||
| } | ||
| } | ||
| catch(Exception ex) | ||
|
gwharris7 marked this conversation as resolved.
|
||
| { | ||
| exceptions.Add(new Exception($"Provider {provider.GetType().Name} failed to obtain a token." , ex)); | ||
| _logger.LogDebug(ex, "Token provider {ProviderType} failed to obtain a token for server '{ServerName}'.", provider.GetType().Name, server.mcpServerName); | ||
| } | ||
|
github-code-quality[bot] marked this conversation as resolved.
Fixed
github-code-quality[bot] marked this conversation as resolved.
Fixed
github-code-quality[bot] marked this conversation as resolved.
Fixed
MattB-msft marked this conversation as resolved.
MattB-msft marked this conversation as resolved.
Dismissed
|
||
| } | ||
| throw new AggregateException("No valid token could be obtained from any provider.", exceptions); | ||
|
MattB-msft marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.