fix: Use KeyNotFoundException for not-found conditions in core Azure services#2703
Merged
Merged
Conversation
…services Replace plain Exception and ArgumentException with KeyNotFoundException in SubscriptionService, TenantService, and ResourceGroupService so that not-found conditions return HTTP 404 instead of 500/400. Fixes: microsoft/mcp-pr#396 Fixes: microsoft/mcp-pr#403 Fixes: microsoft/mcp-pr#404 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Updates core Azure services to treat “not found” conditions as KeyNotFoundException so the command layer maps them to HTTP 404 (instead of 500/400), aligning behavior with existing patterns elsewhere in the repo.
Changes:
- Switched not-found throws in
SubscriptionServiceandTenantServicetoKeyNotFoundException. - Switched resource-group not-found throw in
ResourceGroupServicetoKeyNotFoundException. - Added an Azure server changelog entry describing the HTTP status code fix.
Show a summary per file
| File | Description |
|---|---|
| servers/Azure.Mcp.Server/changelog-entries/1779392230582.yaml | Adds changelog note for the not-found → 404 behavior change. |
| core/Azure.Mcp.Core/src/Services/Azure/Tenant/TenantService.cs | Uses KeyNotFoundException for tenant not-found lookups so callers map to 404. |
| core/Azure.Mcp.Core/src/Services/Azure/Subscription/SubscriptionService.cs | Uses KeyNotFoundException for subscription not-found lookups so callers map to 404. |
| core/Azure.Mcp.Core/src/Services/Azure/ResourceGroup/ResourceGroupService.cs | Uses KeyNotFoundException when resource group is missing during generic resource enumeration. |
Copilot's findings
- Files reviewed: 4/4 changed files
- Comments generated: 2
alzimmermsft
approved these changes
May 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace plain
ExceptionandArgumentExceptionwithKeyNotFoundExceptioninSubscriptionService,TenantService, andResourceGroupServiceso that not-found conditions return HTTP 404 instead of 500/400.Changes
SubscriptionService.csException(→500)KeyNotFoundException(→404)SubscriptionService.csArgumentException(→400)KeyNotFoundException(→404)SubscriptionService.csArgumentException(→400)KeyNotFoundException(→404)TenantService.csException(→500)KeyNotFoundException(→404)TenantService.csException(→500)KeyNotFoundException(→404)ResourceGroupService.csException(→500)KeyNotFoundException(→404)Why KeyNotFoundException?
AuthenticatedCommand.GetStatusCodealready mapsKeyNotFoundException→HttpStatusCode.NotFound(404). This is the same pattern used byBaseAzureResourceServiceandAcrServicefor not-found conditions.Validation
Fixes microsoft/mcp-pr#396
Fixes microsoft/mcp-pr#403
Fixes microsoft/mcp-pr#404
Invoking Livetests
Copilot submitted PRs are not trustworthy by default. Users with
writeaccess 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.