Skip to content

[azsdk-cli] Refactor CLI command classes and remove output helper usage#12125

Merged
benbp merged 2 commits into
Azure:mainfrom
benbp:benbp/azsdk-cli-refactor
Sep 18, 2025
Merged

[azsdk-cli] Refactor CLI command classes and remove output helper usage#12125
benbp merged 2 commits into
Azure:mainfrom
benbp:benbp/azsdk-cli-refactor

Conversation

@benbp
Copy link
Copy Markdown
Member

@benbp benbp commented Sep 16, 2025

This is a somewhat large refactoring effort which adds a few key improvements:

  • Wrapped CLI command functions in the base class, enabling telemetry and finer control over output
  • Add GetCommands to allow a tool class to return an array of sub-commands, instead of being required to construct it oneself, for example:
    protected override List<Command> GetCommands() =>
    [
        new(AzureSubCommand, "Demonstrate Azure service integration") { tenantOption },
        new(DevOpsSubCommand, "Demonstrate DevOps service integration") { packageArgument, languageOption },
        new(GitHubSubCommand, "Demonstrate GitHub service integration")
    ];
    
  • Simplified tool command methods: you no longer need to call output.Output, SetFailure() or ctx.ExitCode = ExitCode. Instead it's based on the HandleCommand response.
    public override async Task<CommandResponse> HandleCommand(InvocationContext ctx, CancellationToken ct)
    {
        var commandName = ctx.ParseResult.CommandResult.Command.Name;
    
        CommandResponse result = commandName switch
        {
            AzureSubCommand => await DemonstrateAzureService(ctx.ParseResult.GetValueForOption(tenantOption), ct),
            DevOpsSubCommand => await DemonstrateDevOpsService(ctx.ParseResult.GetValueForArgument(packageArgument), ctx.ParseResult.GetValueForOption(languageOption), ct),
            GitHubSubCommand => await DemonstrateGitHubService(ct)
            _ => new ExampleServiceResponse { ResponseError = $"Unknown command: {commandName}" }
        };
    
        return result;
    }
    
  • Removed almost all usages of output helper across tools/tests

Additional minor improvements:

  • Migrated output recording functionality into the output helper from the test output helper (FYI @richardpark-msft)
  • Refactored tools returning string to return types implementing CommandResponse (FYI @praveenkuttappan)
  • General refactoring (use primary constructors, test cleanup, type renames)
  • Removed the contract project and moved all classes into the main cli project.

@github-actions github-actions Bot added the azsdk-cli Issues related to Azure/azure-sdk-tools::tools/azsdk-cli label Sep 16, 2025
@benbp benbp moved this from 🤔 Triage to 🔬 Dev in PR in Azure SDK EngSys 🚀🌒🧑‍🚀 Sep 16, 2025
@benbp benbp added the Central-EngSys This issue is owned by the Engineering System team. label Sep 16, 2025
@benbp benbp self-assigned this Sep 16, 2025
@benbp benbp marked this pull request as ready for review September 17, 2025 18:42
Copilot AI review requested due to automatic review settings September 17, 2025 18:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This is a large refactoring effort that modernizes the CLI command architecture by removing direct output helper usage from tools and introducing structured command response handling. The changes enable better telemetry collection and provide finer control over command execution flow.

  • Wrapped CLI command functions in the base class for consistent telemetry and output handling
  • Added GetCommands() method for multi-command tools and simplified command response handling
  • Migrated tools to return CommandResponse objects instead of using direct output calls

Reviewed Changes

Copilot reviewed 91 out of 91 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tools/azsdk-cli/docs/new-tool.md Updated documentation to reflect new MCPToolBase architecture and removed IOutputHelper references
tools/azsdk-cli/README.md Updated development guidelines to remove Console API restrictions
Multiple tool files Refactored to use new base classes (MCPTool/MCPMultiCommandTool) and return CommandResponse objects
Core architecture files Introduced new base classes MCPToolBase, MCPTool, MCPMultiCommandTool with instrumentation
Response model files Updated all response classes to inherit from CommandResponse instead of Response
Test files Updated to work with new architecture and removed IOutputHelper dependencies
Project files Removed Azure.Sdk.Tools.Cli.Contract project dependency
Comments suppressed due to low confidence (2)

tools/azsdk-cli/Azure.Sdk.Tools.Cli/Tools/ReleasePlan/SpecWorkFlowTool.cs:1

  • Missing comma after the first new(SubCommandName1...) statement. This will cause a compilation error.
// Copyright (c) Microsoft Corporation.

tools/azsdk-cli/Azure.Sdk.Tools.Cli/Tools/ReleasePlan/SpecWorkFlowTool.cs:1

  • The SetHandler method doesn't exist on List<Command>. The handler should be set on individual commands, not on the list itself.
// Copyright (c) Microsoft Corporation.

@benbp benbp merged commit 31deacb into Azure:main Sep 18, 2025
24 checks passed
@benbp benbp deleted the benbp/azsdk-cli-refactor branch September 18, 2025 19:37
@kurtzeborn kurtzeborn moved this from 🔬 Dev in PR to 🎊 Closed in Azure SDK EngSys 🚀🌒🧑‍🚀 Sep 18, 2025
@benbp
Copy link
Copy Markdown
Member Author

benbp commented Sep 22, 2025

Fixes #11686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

azsdk-cli Issues related to Azure/azure-sdk-tools::tools/azsdk-cli Central-EngSys This issue is owned by the Engineering System team.

Projects

Development

Successfully merging this pull request may close these issues.

5 participants