-
Notifications
You must be signed in to change notification settings - Fork 545
Migrate Best Practices tools to new tool design #2951
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,13 @@ | ||||||||||||||
| // Copyright (c) Microsoft Corporation. | ||||||||||||||
| // Licensed under the MIT License. | ||||||||||||||
|
|
||||||||||||||
| using System.Collections.Concurrent; | ||||||||||||||
| using System.Net; | ||||||||||||||
| using System.Reflection; | ||||||||||||||
| using System.Text; | ||||||||||||||
| using Azure.Mcp.Tools.AzureBestPractices.Options; | ||||||||||||||
| using Microsoft.Extensions.Logging; | ||||||||||||||
| using Microsoft.Mcp.Core.Commands; | ||||||||||||||
| using Microsoft.Mcp.Core.Extensions; | ||||||||||||||
| using Microsoft.Mcp.Core.Helpers; | ||||||||||||||
| using Microsoft.Mcp.Core.Models.Command; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -32,70 +32,48 @@ it belongs to the Azure Best Practices category. | |||||||||||||
| ReadOnly = true, | ||||||||||||||
| Secret = false, | ||||||||||||||
| LocalRequired = false)] | ||||||||||||||
| public sealed class BestPracticesCommand(ILogger<BestPracticesCommand> logger) : BaseCommand<BestPracticesOptions> | ||||||||||||||
| public sealed class BestPracticesCommand(ILogger<BestPracticesCommand> logger) : BaseCommand<BestPracticesOptions, List<string>> | ||||||||||||||
| { | ||||||||||||||
| private readonly ILogger<BestPracticesCommand> _logger = logger; | ||||||||||||||
| private static readonly Dictionary<string, string> s_bestPracticesCache = new(); | ||||||||||||||
| private static readonly ConcurrentDictionary<string, string> s_bestPracticesCache = []; | ||||||||||||||
|
|
||||||||||||||
| protected override void RegisterOptions(Command command) | ||||||||||||||
| public override void ValidateOptions(BestPracticesOptions options, ValidationResult validationResult) | ||||||||||||||
| { | ||||||||||||||
| command.Options.Add(BestPracticesOptionDefinitions.Resource); | ||||||||||||||
| command.Options.Add(BestPracticesOptionDefinitions.Action); | ||||||||||||||
| command.Validators.Add(commandResult => | ||||||||||||||
| base.ValidateOptions(options, validationResult); | ||||||||||||||
|
|
||||||||||||||
| if (string.IsNullOrWhiteSpace(options.Resource) || string.IsNullOrWhiteSpace(options.Action)) | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't ValidateOptions already have this check?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The base So the null case here is indeed redundant for "missing" options. The whitespace case ( |
||||||||||||||
| { | ||||||||||||||
| validationResult.Errors.Add("Both resource and action parameters are required."); | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| commandResult.TryGetValue(BestPracticesOptionDefinitions.Resource, out string? resource); | ||||||||||||||
| commandResult.TryGetValue(BestPracticesOptionDefinitions.Action, out string? action); | ||||||||||||||
| bool validResource = options.Resource == "general" || options.Resource == "azurefunctions" || options.Resource == "static-web-app" || options.Resource == "coding-agent"; | ||||||||||||||
| bool validAction = options.Action == "all" || options.Action == "code-generation" || options.Action == "deployment"; | ||||||||||||||
|
|
||||||||||||||
| if (string.IsNullOrWhiteSpace(resource) || string.IsNullOrWhiteSpace(action)) | ||||||||||||||
| if (!validResource) | ||||||||||||||
| { | ||||||||||||||
| commandResult.AddError("Both resource and action parameters are required."); | ||||||||||||||
| validationResult.Errors.Add("Invalid resource. Must be 'general', 'azurefunctions', 'static-web-app', or 'coding-agent'."); | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| if (!validAction) | ||||||||||||||
| { | ||||||||||||||
| bool validResource = resource == "general" || resource == "azurefunctions" || resource == "static-web-app" || resource == "coding-agent"; | ||||||||||||||
| bool validAction = action == "all" || action == "code-generation" || action == "deployment"; | ||||||||||||||
|
|
||||||||||||||
| if (!validResource) | ||||||||||||||
| { | ||||||||||||||
| commandResult.AddError("Invalid resource. Must be 'general', 'azurefunctions', 'static-web-app', or 'coding-agent'."); | ||||||||||||||
| } | ||||||||||||||
| if (!validAction) | ||||||||||||||
| { | ||||||||||||||
| commandResult.AddError("Invalid action. Must be 'all', 'code-generation' or 'deployment'."); | ||||||||||||||
| } | ||||||||||||||
| if (resource == "static-web-app" && action != "all") | ||||||||||||||
| { | ||||||||||||||
| commandResult.AddError("The 'static-web-app' resource only supports 'all' action."); | ||||||||||||||
| } | ||||||||||||||
| if (resource == "coding-agent" && action != "all") | ||||||||||||||
| { | ||||||||||||||
| commandResult.AddError("The 'coding-agent' resource only supports 'all' action."); | ||||||||||||||
| } | ||||||||||||||
| validationResult.Errors.Add("Invalid action. Must be 'all', 'code-generation' or 'deployment'."); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| protected override BestPracticesOptions BindOptions(ParseResult parseResult) | ||||||||||||||
| { | ||||||||||||||
| return new BestPracticesOptions | ||||||||||||||
| { | ||||||||||||||
| Resource = parseResult.GetValueOrDefault<string>(BestPracticesOptionDefinitions.Resource.Name), | ||||||||||||||
| Action = parseResult.GetValueOrDefault<string>(BestPracticesOptionDefinitions.Action.Name) | ||||||||||||||
| }; | ||||||||||||||
| if (options.Resource == "static-web-app" && options.Action != "all") | ||||||||||||||
| { | ||||||||||||||
| validationResult.Errors.Add("The 'static-web-app' resource only supports 'all' action."); | ||||||||||||||
| } | ||||||||||||||
| if (options.Resource == "coding-agent" && options.Action != "all") | ||||||||||||||
| { | ||||||||||||||
| validationResult.Errors.Add("The 'coding-agent' resource only supports 'all' action."); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| public override Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken) | ||||||||||||||
| public override Task<CommandResponse> ExecuteAsync(CommandContext context, BestPracticesOptions options, CancellationToken cancellationToken) | ||||||||||||||
| { | ||||||||||||||
| if (!Validate(parseResult.CommandResult, context.Response).IsValid) | ||||||||||||||
| { | ||||||||||||||
| return Task.FromResult(context.Response); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var options = BindOptions(parseResult); | ||||||||||||||
|
|
||||||||||||||
| try | ||||||||||||||
| { | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you upgraded to ConcurrentDictionary, you can simplify this to use GetOrAdd for atomic check-and-load:
Suggested change
|
||||||||||||||
| var resourceFileName = GetResourceFileName(options.Resource!, options.Action!); | ||||||||||||||
| var resourceFileName = GetResourceFileName(options.Resource, options.Action); | ||||||||||||||
| var bestPractices = GetBestPracticesText(resourceFileName); | ||||||||||||||
|
|
||||||||||||||
| context.Response.Status = HttpStatusCode.OK; | ||||||||||||||
|
|
||||||||||||||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,15 @@ | ||||||
| // Copyright (c) Microsoft Corporation. | ||||||
| // Licensed under the MIT License. | ||||||
|
|
||||||
| using Microsoft.Mcp.Core.Options; | ||||||
|
|
||||||
| namespace Azure.Mcp.Tools.AzureBestPractices.Options; | ||||||
|
|
||||||
| public sealed class BestPracticesOptions | ||||||
| { | ||||||
| public string? Resource { get; set; } | ||||||
| public string? Action { get; set; } | ||||||
| [Option(Description = "The Azure resource type for which to get best practices. Options: 'general' (general Azure), 'azurefunctions' (Azure Functions), 'static-web-app' (Azure Static Web Apps), 'coding-agent' (Coding Agent).")] | ||||||
| public required string Resource { get; set; } | ||||||
|
|
||||||
| [Option(Description = "The action type for the best practices. Options: 'all', 'code-generation', 'deployment'. Note: 'static-web-app' and 'coding-agent' resources only supports 'all'.")] | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grammar:
Suggested change
|
||||||
| public required string Action { get; set; } | ||||||
| } | ||||||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.