Skip to content

Accept tenant when calling Azure Resource Graph queries#1945

Open
alzimmermsft wants to merge 1 commit intomicrosoft:mainfrom
alzimmermsft:UseTenantInBaseAzureResourceService
Open

Accept tenant when calling Azure Resource Graph queries#1945
alzimmermsft wants to merge 1 commit intomicrosoft:mainfrom
alzimmermsft:UseTenantInBaseAzureResourceService

Conversation

@alzimmermsft
Copy link
Contributor

What does this PR do?

Accept tenant when calling Azure Resource Graph queries to allow configuring both tenant and subscription.

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
    • Updated servers/Azure.Mcp.Server/CHANGELOG.md and/or servers/Fabric.Mcp.Server/CHANGELOG.md for product changes (features, bug fixes, UI/UX, updated dependencies)
  • 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 using script at eng/scripts/Process-PackageReadMe.ps1. See Package README
    • Updated command list in /servers/Azure.Mcp.Server/docs/azmcp-commands.md and/or /docs/fabric-commands.md
    • Run .\eng\scripts\Update-AzCommandsMetadata.ps1 to update tool metadata in azmcp-commands.md (required for CI)
    • 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 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 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
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 extends Azure Resource Graph (ARG) query execution to accept an optional tenant, enabling callers to scope queries by both tenant and subscription.

Changes:

  • Adds an optional tenant parameter to BaseAzureResourceService.ExecuteResourceQueryAsync and uses it when resolving the subscription/tenant context.
  • Refactors single-resource ARG querying to reuse the multi-result query path.
  • Plumbs tenant through ARG query calls in several tool services (Storage, Kusto, Grafana, Authorization, AppConfig, ACR).

Reviewed changes

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

Show a summary per file
File Description
tools/Azure.Mcp.Tools.Storage/src/Services/StorageService.cs Passes tenant into ARG list/single queries for storage accounts.
tools/Azure.Mcp.Tools.Kusto/src/Services/KustoService.cs Passes tenant into ARG list/single queries for Kusto clusters.
tools/Azure.Mcp.Tools.Grafana/src/Services/GrafanaService.cs Passes tenant into ARG workspace listing.
tools/Azure.Mcp.Tools.Authorization/src/Services/AuthorizationService.cs Passes tenantId into ARG role assignment listing.
tools/Azure.Mcp.Tools.AppConfig/src/Services/AppConfigService.cs Passes tenant into ARG store listing (but not yet into store lookup used by other operations).
tools/Azure.Mcp.Tools.Acr/src/Services/AcrService.cs Passes tenant into ARG registry list/single queries.
core/Azure.Mcp.Core/src/Services/Azure/BaseAzureResourceService.cs Adds tenant support to ARG query execution and refactors single-query path.

var subscriptionResource = await _subscriptionService.GetSubscription(subscription, null, retryPolicy, cancellationToken);
var tenantResource = await GetTenantResourceAsync(subscriptionResource.Data.TenantId, cancellationToken);
var subscriptionResource = await _subscriptionService.GetSubscription(subscription, tenant, retryPolicy, cancellationToken);
var tenantResource = await GetTenantResourceAsync(subscriptionResource!.Data.TenantId, cancellationToken);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

subscriptionResource is declared non-nullable by ISubscriptionService.GetSubscription(...) (it throws if it can't retrieve the subscription). Using the null-forgiving operator here is unnecessary and can mask future nullability issues; prefer removing ! (or, if null is actually possible, handle it explicitly with a clear exception before dereferencing .Data).

Suggested change
var tenantResource = await GetTenantResourceAsync(subscriptionResource!.Data.TenantId, cancellationToken);
var tenantResource = await GetTenantResourceAsync(subscriptionResource.Data.TenantId, cancellationToken);

Copilot uses AI. Check for mistakes.
Comment on lines +159 to 160
string? tenant = null,
CancellationToken cancellationToken = default) where T : class
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

ExecuteSingleResourceQueryAsync introduces tenant before cancellationToken, while ExecuteResourceQueryAsync appends tenant after cancellationToken. This inconsistent ordering makes the API easier to misuse and is source-breaking for any positional callers that previously passed only a cancellationToken. Consider standardizing both methods (ideally keeping cancellationToken last) and updating internal call sites to use named arguments as needed.

Suggested change
string? tenant = null,
CancellationToken cancellationToken = default) where T : class
CancellationToken cancellationToken = default,
string? tenant = null) where T : class

Copilot uses AI. Check for mistakes.
Comment on lines 143 to 160
/// <param name="resourceType">The Azure resource type to query for (e.g., "Microsoft.Sql/servers/databases")</param>
/// <param name="resourceGroup">The resource group name to filter by (null to query all resource groups)</param>
/// <param name="subscription">The subscription ID or name</param>
/// <param name="retryPolicy">Optional retry policy configuration</param>
/// <param name="converter">Function to convert JsonElement to the target type</param>
/// <param name="additionalFilter">Optional additional KQL filter conditions</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Single resource converted to the specified type, or null if not found</returns>
protected async Task<T?> ExecuteSingleResourceQueryAsync<T>(
string resourceType,
string? resourceGroup,
string subscription,
RetryPolicyOptions? retryPolicy,
Func<JsonElement, T> converter,
string? tableName = "resources",
string? additionalFilter = null,
string? tenant = null,
CancellationToken cancellationToken = default) where T : class
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The XML docs for ExecuteSingleResourceQueryAsync no longer match the signature: the method has a tableName parameter and now also has an optional tenant, but neither is documented. Please update the <param> entries so the docs reflect the actual parameters and intended usage.

Copilot uses AI. Check for mistakes.
Comment on lines 32 to 39
var accounts = await ExecuteResourceQueryAsync(
"Microsoft.AppConfiguration/configurationStores",
resourceGroup: null, // all resource groups
subscription,
retryPolicy,
ConvertToAppConfigurationAccountModel,
tenant: tenant,
cancellationToken: cancellationToken);
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

tenant is now propagated for GetAppConfigAccounts, but other AppConfig operations that accept a tenant (e.g., GetKeyValues via GetConfigurationClient/FindAppConfigStore) still discover the store endpoint via an ARG query that does not pass tenant. This means cross-tenant scenarios can still resolve the subscription/store in the default tenant. Consider threading tenant into FindAppConfigStore(...) and passing it to ExecuteSingleResourceQueryAsync(... tenant: tenant ...), then updating GetConfigurationClient to forward the tenant.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

2 participants