Skip to content

Removes usage of IServiceProvider from Azure.Mcp.Tools.Cosmos, Device Registry and Cosmos#2478

Merged
conniey merged 4 commits intomicrosoft:mainfrom
conniey:compute
May 1, 2026
Merged

Removes usage of IServiceProvider from Azure.Mcp.Tools.Cosmos, Device Registry and Cosmos#2478
conniey merged 4 commits intomicrosoft:mainfrom
conniey:compute

Conversation

@conniey
Copy link
Copy Markdown
Member

@conniey conniey commented Apr 23, 2026

What does this PR do?

Removes usage of IServiceProvider from Azure.Mcp.Tools.Cosmos, Device Registry and Cosmos

GitHub issue number?

[Link to the GitHub issue this PR addresses]

Pre-merge Checklist

  • Required for All PRs
    • Read contribution guidelines
    • PR title clearly describes the change
    • Commit history is clean with descriptive messages (cleanup guide)
    • Added comprehensive tests for new/modified functionality
    • Created a changelog entry if the change falls among the following: new feature, bug fix, UI/UX update, breaking change, or updated dependencies. Follow the changelog entry guide
  • For MCP tool changes:
    • One tool per PR: This PR adds or modifies only one MCP tool for faster review cycles
    • Updated servers/Azure.Mcp.Server/README.md and/or servers/Fabric.Mcp.Server/README.md documentation
    • Validate README.md changes running the script ./eng/scripts/Process-PackageReadMe.ps1. See Package README
    • For new or modified tool descriptions, ran ToolDescriptionEvaluator and obtained a score of 0.4 or more and a top 3 ranking for all related test prompts
    • For tools with new names, including new tools or renamed tools, update consolidated-tools.json
    • For renamed tools, follow the Tool Rename Checklist and tag the PR with the breaking-change label
    • For new tools associated with Azure services or publicly available tools/APIs/products, add URL to documentation in the PR description
  • Extra steps for Azure MCP Server tool changes:
    • Updated command list in servers/Azure.Mcp.Server/docs/azmcp-commands.md
    • Ran ./eng/scripts/Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • Updated test prompts in servers/Azure.Mcp.Server/docs/e2eTestPrompts.md
    • 👉 For Community (non-Microsoft team member) PRs:
      • Security review: Reviewed code for security vulnerabilities, malicious code, or suspicious activities before running tests (crypto mining, spam, data exfiltration, etc.)
      • Manual tests run: added comment /azp run mcp - pullrequest - live to run Live Test Pipeline
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 PR refactors several MCP tool commands to stop resolving their tool services via CommandContext/IServiceProvider at execution time, instead using constructor injection for the relevant service interfaces.

Changes:

  • Updated Cosmos and Device Registry commands to accept their service dependencies via constructor injection and call them directly.
  • Updated multiple Compute commands to accept IComputeService via constructor injection and (mostly) use the injected instance.
  • Updated a Device Registry unit test to construct the command with the injected service.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tools/Azure.Mcp.Tools.DeviceRegistry/tests/Azure.Mcp.Tools.DeviceRegistry.UnitTests/Namespace/NamespaceListCommandTests.cs Updates test construction to pass IDeviceRegistryService into the command constructor.
tools/Azure.Mcp.Tools.DeviceRegistry/src/Commands/Namespace/NamespaceListCommand.cs Switches from context.GetService<IDeviceRegistryService>() to constructor-injected IDeviceRegistryService.
tools/Azure.Mcp.Tools.Cosmos/src/Commands/ItemQueryCommand.cs Switches from context.GetService<ICosmosService>() to constructor-injected ICosmosService.
tools/Azure.Mcp.Tools.Cosmos/src/Commands/CosmosListCommand.cs Switches from context.GetService<ICosmosService>() to constructor-injected ICosmosService.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vmss/VmssUpdateCommand.cs Introduces constructor injection of IComputeService and uses it for updates.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vmss/VmssDeleteCommand.cs Introduces constructor injection of IComputeService and uses it for deletes.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vmss/VmssCreateCommand.cs Introduces constructor injection of IComputeService and uses it for creates.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vm/VmUpdateCommand.cs Introduces constructor injection of IComputeService and uses it for updates.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vm/VmDeleteCommand.cs Introduces constructor injection of IComputeService and uses it for deletes.
tools/Azure.Mcp.Tools.Compute/src/Commands/Vm/VmCreateCommand.cs Introduces constructor injection of IComputeService (but currently still resolves via context.GetService in ExecuteAsync).
tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskUpdateCommand.cs Introduces constructor injection of IComputeService and uses it for disk updates.
tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskGetCommand.cs Introduces constructor injection of IComputeService and uses it for disk retrieval/listing.
tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskDeleteCommand.cs Introduces constructor injection of IComputeService and uses it for disk deletion.
tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskCreateCommand.cs Introduces constructor injection of IComputeService and uses it for disk creation.
Comment thread tools/Azure.Mcp.Tools.Compute/src/Commands/Vm/VmCreateCommand.cs Outdated
Comment thread tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskUpdateCommand.cs Outdated
Comment thread tools/Azure.Mcp.Tools.Compute/src/Commands/Disk/DiskCreateCommand.cs Outdated
Comment thread tools/Azure.Mcp.Tools.Compute/src/Commands/Vm/VmCreateCommand.cs Outdated
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

The direction is right - constructor injection instead of service locator. This aligns with how VmGetCommand and VmssGetCommand were already refactored.

The main issue: constructor signatures changed in 12 source files but only 1 test file (NamespaceListCommandTests) was updated. The remaining 9 test files still construct commands with just _logger, which won't compile against the new 2-param constructors. See inline comments for specifics.

A couple other things:

  • Title says "Cosmos, Device Registry and Cosmos" (Cosmos listed twice). The majority of changes are actually in Compute (10/14 files), which isn't mentioned. Consider updating to: "Remove IServiceProvider usage from Compute, Cosmos, and DeviceRegistry tools."
  • PR has merge conflicts against main that need resolving.
Comment thread tools/Azure.Mcp.Tools.Cosmos/src/Commands/CosmosListCommand.cs
@conniey conniey requested a review from a team as a code owner April 30, 2026 12:22
Copy link
Copy Markdown
Contributor

@jongio jongio left a comment

Choose a reason for hiding this comment

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

Two of three items from my previous review are addressed - tests compile via the CommandUnitTestsBase migration, and the Cosmos disposal null check looks good.

Two things still need attention:

  1. VmCreateCommand still calls context.GetService<IComputeService>() and its constructor only takes ILogger. This is the same service locator pattern the PR is removing from all other Compute commands. It should get the same treatment - add IComputeService to the constructor and use the injected field.

  2. Null guards on the service parameter aren't consistent. Seven commands use ?? throw new ArgumentNullException(nameof(computeService)) but five don't. See inline comments.

@conniey conniey merged commit 94ec2a4 into microsoft:main May 1, 2026
16 checks passed
@github-project-automation github-project-automation Bot moved this from Untriaged to Done in Azure MCP Server May 1, 2026
@conniey conniey deleted the compute branch May 1, 2026 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment