Skip to content

DI Refactor: Azure.Mcp.Tools.Aks#1883

Merged
conniey merged 4 commits intomainfrom
copilot/refactor-azure-mcp-tools-aks
Mar 9, 2026
Merged

DI Refactor: Azure.Mcp.Tools.Aks#1883
conniey merged 4 commits intomainfrom
copilot/refactor-azure-mcp-tools-aks

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 27, 2026

  • Understand the DI refactor pattern from example PR AppConfig: Remove usage of ServiceProvider in ExecuteAsync. #1815
  • Update ClusterGetCommand to accept IAksService via constructor injection
  • Update NodepoolGetCommand to accept IAksService via constructor injection
  • Update ClusterGetCommandTests to pass _aksService in constructor, remove from ServiceCollection
  • Update NodepoolGetCommandTests to pass _aksService in constructor, remove from ServiceCollection
  • Add changelog entry with correct PR number (1883)
  • Fix CommandFactoryHelpers.SetupCommonServices() to register ICacheService mock so AksService can be resolved in core unit tests
  • Verify ToolsListCommandTests (22/22 tests pass)

Invoking Livetests

Copilot submitted PRs are not trustworthy by default. Users with write access to the repo need to validate the contents of this PR before leaving a comment with the text /azp run mcp - pullrequest - live. This will trigger the necessary livetest workflows to complete required validation.

Original prompt

This section details on the original issue you should resolve

<issue_title>DI Refactor: Azure.Mcp.Tools.Aks</issue_title>
<issue_description>## Summary

Refactor Azure.Mcp.Tools.Aks to use constructor dependency injection instead of resolving services via context.GetService<T>() in ExecuteAsync.

This is a sub-issue of #158.

Instructions

Follow the algorithm described in tools/di-refactor-plan.md with {ToolArea} = Aks.

An example PR is: #1815</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor Azure.Mcp.Tools.Aks to use constructor dependency injection DI Refactor: Azure.Mcp.Tools.Aks Feb 27, 2026
Comment thread servers/Azure.Mcp.Server/changelog-entries/1772226177387.yaml Outdated
@conniey conniey marked this pull request as ready for review March 2, 2026 18:57
Copilot AI review requested due to automatic review settings March 2, 2026 18:57
@conniey conniey requested review from a team, gossion and jongio as code owners March 2, 2026 18:57
Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>
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

Refactors the AKS tool commands to use constructor injection for IAksService instead of resolving it from CommandContext during ExecuteAsync, aligning with the repo-wide DI refactor effort (#158) and improving testability.

Changes:

  • Inject IAksService via primary constructors in ClusterGetCommand and NodepoolGetCommand, removing context.GetService<IAksService>() usage.
  • Update unit tests to pass the IAksService mock directly to command constructors (no longer registering it in the ServiceCollection).
  • Add an Azure MCP Server changelog entry for the AKS DI refactor.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.Aks/src/Commands/Cluster/ClusterGetCommand.cs Switches to constructor-injected IAksService and uses the injected field in ExecuteAsync.
tools/Azure.Mcp.Tools.Aks/src/Commands/Nodepool/NodepoolGetCommand.cs Switches to constructor-injected IAksService and uses the injected field in ExecuteAsync.
tools/Azure.Mcp.Tools.Aks/tests/Azure.Mcp.Tools.Aks.UnitTests/Cluster/ClusterGetCommandTests.cs Updates test setup to pass _aksService directly into the command constructor.
tools/Azure.Mcp.Tools.Aks/tests/Azure.Mcp.Tools.Aks.UnitTests/Nodepool/NodepoolGetCommandTests.cs Updates test setup to pass _aksService directly into the command constructor.
servers/Azure.Mcp.Server/changelog-entries/1772226177387.yaml Adds a changelog entry documenting the AKS DI refactor.

Comment thread servers/Azure.Mcp.Server/changelog-entries/1772226177387.yaml
@conniey
Copy link
Copy Markdown
Member

conniey commented Mar 2, 2026

@copilot Azure.Mcp.Core unit tests are failing. For example:

Failed Azure.Mcp.Core.UnitTests.Areas.Tools.UnitTests.ToolsListCommandTests.ExecuteAsync_WithNameAndMultipleNamespaceOptions_FiltersAndReturnsNamesOnly [2 ms]
  Error Message:
   System.InvalidOperationException : Unable to resolve service for type 'Azure.Mcp.Core.Services.Caching.ICacheService' while attempting to activate 'Azure.Mcp.Tools.Aks.Services.AksService'.
  Stack Trace:
     at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateExact(ServiceDescriptor descriptor, ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateExact(ServiceDescriptor descriptor, ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Azure.Mcp.Tools.Aks.AksSetup.RegisterCommands(IServiceProvider serviceProvider) in /Users/runner/work/1/s/tools/Azure.Mcp.Tools.Aks/src/AksSetup.cs:line 38
   at Azure.Mcp.Core.Commands.CommandFactory.RegisterCommandGroup() in /Users/runner/work/1/s/core/Microsoft.Mcp.Core/src/Commands/CommandFactory.cs:line 142
   at Azure.Mcp.Core.Commands.CommandFactory.CreateRootCommand() in /Users/runner/work/1/s/core/Microsoft.Mcp.Core/src/Commands/CommandFactory.cs:line 250
   at Azure.Mcp.Core.Commands.CommandFactory..ctor(IServiceProvider serviceProvider, IEnumerable`1 serviceAreas, ITelemetryService telemetryService, IOptions`1 configurationOptions, ILogger`1 logger) in /Users/runner/work/1/s/core/Microsoft.Mcp.Core/src/Commands/CommandFactory.cs:line 67
   at Azure.Mcp.Core.UnitTests.Areas.Server.CommandFactoryHelpers.CreateCommandFactory(IServiceProvider serviceProvider) in /Users/runner/work/1/s/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Server/CommandFactoryHelpers.cs:line 110
   at Azure.Mcp.Core.UnitTests.Areas.Tools.UnitTests.ToolsListCommandTests..ctor() in /Users/runner/work/1/s/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Tools/UnitTests/ToolsListCommandTests.cs:line 39
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)

… failures

Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 2, 2026

@copilot Azure.Mcp.Core unit tests are failing. For example:

Failed Azure.Mcp.Core.UnitTests.Areas.Tools.UnitTests.ToolsListCommandTests.ExecuteAsync_WithNameAndMultipleNamespaceOptions_FiltersA...

Fixed in a06d7f6. Moving IAksService to the command constructor meant the DI container needed to resolve AksService (which depends on ICacheService) at command registration time rather than at ExecuteAsync call time. Added Substitute.For<ICacheService>() to CommandFactoryHelpers.SetupCommonServices() — all 22 ToolsListCommandTests now pass.

@conniey conniey merged commit caa0184 into main Mar 9, 2026
15 checks passed
@conniey conniey deleted the copilot/refactor-azure-mcp-tools-aks branch March 9, 2026 19:15
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in Azure MCP Server Mar 9, 2026
colbytimm pushed a commit to colbytimm/microsoft-mcp that referenced this pull request Apr 20, 2026
* Initial plan

* refactor: DI injection for Azure.Mcp.Tools.Aks commands

Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>

* fix: correct PR number in changelog entry

Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>

* fix: register ICacheService mock in CommandFactoryHelpers to fix test failures

Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: conniey <10136526+conniey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

DI Refactor: Azure.Mcp.Tools.Aks

5 participants