Skip to content

Read default subscription from Azure CLI profile for all subscription-scoped commands#1974

Merged
ArthurMa1978 merged 11 commits intomainfrom
copilot/fix-issue-1079
Mar 20, 2026
Merged

Read default subscription from Azure CLI profile for all subscription-scoped commands#1974
ArthurMa1978 merged 11 commits intomainfrom
copilot/fix-issue-1079

Conversation

Copy link
Contributor

Copilot AI commented Mar 9, 2026

  • Analyze PR review comments (all 3 unresolved from live1206 were already addressed in prior commit)
  • Identify merge conflict in SubscriptionListCommand.cs and pipeline failures
  • Merge main into branch and resolve conflict (keep our SubscriptionInfo model)
  • Verify auto-merged SubscriptionService.cs correctly uses CacheDurations.Subscription
  • Fix caching: only cache CLI profile file read (Lazy), keep env var lookup fresh — fixes test failures
  • Build and test changes (857/858 pass; 1 unrelated network failure)
  • Run code review

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI and others added 2 commits March 10, 2026 00:01
…cription to guide AI agent behavior

The subscription_list tool now:
- Returns a SubscriptionInfo model with isDefault field indicating the default subscription
  (based on AZURE_SUBSCRIPTION_ID environment variable)
- Sorts subscriptions so the default appears first in the list
- Updated description instructs AI agents to prefer the default subscription or ask the
  user when no default is set and multiple subscriptions exist

Fixes #1079

Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue with mcp repository according to issue 1079 Add isDefault field to subscription_list response and guide AI subscription selection Mar 10, 2026
Copilot AI and others added 2 commits March 10, 2026 07:46
…cription detection

Read the default subscription from ~/.azure/azureProfile.json (set via 'az account set')
as the primary source. Fall back to AZURE_SUBSCRIPTION_ID environment variable only if
the profile is unavailable.

- Add GetDefaultSubscriptionId() to ISubscriptionService interface
- Implement ReadDefaultSubscriptionFromAzureProfile in SubscriptionService
- Update SubscriptionListCommand to use service method instead of env var directly
- Update tool description to reference 'az account set'
- Add 6 new profile-reading unit tests
- Update existing tests to use mocked GetDefaultSubscriptionId()

Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Copilot AI changed the title Add isDefault field to subscription_list response and guide AI subscription selection Read default subscription from Azure CLI profile instead of AZURE_SUBSCRIPTION_ID Mar 10, 2026
Copilot AI and others added 2 commits March 10, 2026 10:15
… into CommandHelper for all subscription-scoped commands

- Create AzureCliProfileHelper in Microsoft.Mcp.Core for reading default subscription from ~/.azure/azureProfile.json
- Update CommandHelper.GetSubscription() and HasSubscriptionAvailable() to use Azure CLI profile as primary fallback (before AZURE_SUBSCRIPTION_ID env var)
- Update SubscriptionService.GetDefaultSubscriptionId() to delegate to shared logic
- Update SubscriptionCommand comments to reflect new behavior
- Migrate SubscriptionServiceProfileTests to test AzureCliProfileHelper directly

Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Copilot AI changed the title Read default subscription from Azure CLI profile instead of AZURE_SUBSCRIPTION_ID Read default subscription from Azure CLI profile for all subscription-scoped commands Mar 10, 2026
@ArthurMa1978 ArthurMa1978 marked this pull request as ready for review March 11, 2026 03:28
@ArthurMa1978 ArthurMa1978 requested review from a team as code owners March 11, 2026 03:28
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

Updates subscription resolution across subscription-scoped commands to match Azure CLI behavior by reading the default subscription from ~/.azure/azureProfile.json (set via az account set) before falling back to AZURE_SUBSCRIPTION_ID, and surfaces that default selection in subscription list.

Changes:

  • Added AzureCliProfileHelper and CommandHelper.GetDefaultSubscription() to implement the default-subscription fallback chain.
  • Updated subscription option validation/resolution to use the shared default-subscription logic.
  • Updated subscription list to return an isDefault indicator and prioritize the default subscription in the output.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
servers/Azure.Mcp.Server/docs/azmcp-commands.md Updates CLI docs for subscription list to mention isDefault.
core/Microsoft.Mcp.Core/src/Helpers/CommandHelper.cs Adds GetDefaultSubscription() and routes subscription resolution/validation through it.
core/Microsoft.Mcp.Core/src/Helpers/AzureCliProfileHelper.cs New helper to parse Azure CLI profile JSON for the default subscription.
core/Azure.Mcp.Core/src/Services/Azure/Subscription/ISubscriptionService.cs Adds GetDefaultSubscriptionId() to expose shared default-subscription resolution.
core/Azure.Mcp.Core/src/Services/Azure/Subscription/SubscriptionService.cs Implements GetDefaultSubscriptionId() via CommandHelper.
core/Azure.Mcp.Core/src/Commands/Subscription/SubscriptionCommand.cs Updates validation comments to include the new fallback sources.
core/Azure.Mcp.Core/src/Areas/Subscription/Models/SubscriptionInfo.cs Introduces a model that includes isDefault for subscription list output.
core/Azure.Mcp.Core/src/Areas/Subscription/Commands/SubscriptionListCommand.cs Maps subscriptions to SubscriptionInfo, marks/sorts the default subscription, and updates description.
core/Azure.Mcp.Core/src/Areas/Subscription/Commands/SubscriptionJsonContext.cs Adds source-gen serialization metadata for the new output model.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/SubscriptionListCommandTests.cs Extends unit tests to cover default subscription marking/sorting and isDefault output.
core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Areas/Subscription/AzureCliProfileHelperTests.cs Adds unit tests for parsing the default subscription from Azure CLI profile JSON.

…e, update description

- Short-circuit HasSubscriptionAvailable when --subscription option is present (avoid unnecessary file IO)
- Only call GetDefaultSubscription() in GetSubscription when value is null/empty/placeholder (lazy evaluation)
- Guard GetAzureProfilePath() against empty user profile to avoid relative path reads
- Update SubscriptionListCommand description to mention AZURE_SUBSCRIPTION_ID env var fallback

Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Copy link

@live1206 live1206 left a comment

Choose a reason for hiding this comment

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

Well-structured PR that cleanly implements Azure CLI profile-based default subscription resolution. The fallback chain (CLI option → CLI profile → env var) is well-designed and the tests are thorough. A few concerns below:

…st fragility, add changelog

- Cache GetDefaultSubscription() with Lazy<string?> to avoid repeated file I/O
- Document intentional sync I/O trade-off in AzureCliProfileHelper
- Fix GetAzureProfilePath test to handle null user profile in containers
- Add changelog entry for subscription list breaking change

Co-authored-by: ArthurMa1978 <20514459+ArthurMa1978@users.noreply.github.com>
Copilot AI added 2 commits March 19, 2026 23:42
Only the Azure CLI profile file read (~/.azure/azureProfile.json) is cached
with Lazy<string?> since it involves file I/O. The AZURE_SUBSCRIPTION_ID
env var lookup is always fresh since it's trivially cheap. This fixes test
failures where cached env var values leaked across test cases.
@ArthurMa1978 ArthurMa1978 merged commit f91bf90 into main Mar 20, 2026
15 checks passed
@github-project-automation github-project-automation bot moved this from Untriaged to Done in Azure MCP Server Mar 20, 2026
@ArthurMa1978 ArthurMa1978 deleted the copilot/fix-issue-1079 branch March 20, 2026 03:30
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.

[BUG] If subscription_list tool called, copilot only uses first response instead of user prompting or other defaults

4 participants