Fix azureterraform_azapi_get to return 400 BadRequest for invalid resource type or API version#2657
Merged
Conversation
…AzApiDocsGetCommandTests
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the azureterraform_azapi_get tool’s error handling so invalid --resource-type / --api-version inputs return 400 BadRequest (instead of 500 InternalServerError), enabling callers (including AI agents) to treat the failure as a correctable input issue.
Changes:
- Normalize schema lookup failures in
AzApiDocsServiceto surface input-related failures asInvalidDataException. - Map
InvalidDataExceptionto400 BadRequestinAzApiDocsGetCommand. - Update unit test expectation and add an Azure MCP Server changelog entry documenting the behavior change.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tools/Azure.Mcp.Tools.AzureTerraform/tests/Azure.Mcp.Tools.AzureTerraform.Tests/AzApiDocsGetCommandTests.cs | Updates the failure-path test to expect 400 BadRequest when the docs service throws an InvalidDataException. |
| tools/Azure.Mcp.Tools.AzureTerraform/src/Services/AzApiDocsService.cs | Wraps schema lookup exceptions to normalize certain upstream failures into InvalidDataException. |
| tools/Azure.Mcp.Tools.AzureTerraform/src/Commands/AzApiDocsGetCommand.cs | Overrides status code mapping so InvalidDataException returns 400 BadRequest. |
| servers/Azure.Mcp.Server/changelog-entries/yunliu1-azapi-get-bad-request.yaml | Adds a changelog entry describing the corrected status code behavior for invalid inputs. |
vcolin7
reviewed
Jun 9, 2026
vcolin7
left a comment
Contributor
There was a problem hiding this comment.
Left a comment on how to handle the different kinds of exception thrown by ResourceVisitor :)
…to throw `InvalidDataException` for unknown resource types (#2840) Change `GetResourceApiVersions.ResourceVisitor` in the BicepSchema tools to throw `InvalidDataException` for unknown resource types `GetResourceApiVersions` threw a bare `Exception` (and an unhandled `KeyNotFoundException` when the provider existed but the resource type did not), which `AzApiDocsService` normalized via a broad catch-all that also misclassified real server-side faults as `400 BadRequest`. Throw `InvalidDataException` at the source instead, consistent with `LoadSingleResource`, and drop the catch-all so unexpected exceptions surface as `500`.
vcolin7
approved these changes
Jun 10, 2026
msalaman
approved these changes
Jun 15, 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.
What does this PR do?
When
azureterraform_azapi_getis called with an invalid resource type or API version, the tool previously returned500 InternalServerError. This caused AI agents to interpret the failure as a server-side problem rather than a correctable input error, preventing auto-correction.This PR fixes the issue by:
AzApiDocsService: Wraps the schema lookup in a try/catch that normalizes any non-InvalidDataExceptionfrom the BicepSchema layer (e.g., the genericExceptionthrown when a resource type isn't found and--api-versionis omitted) intoInvalidDataException, ensuring consistent exception surfacing.AzApiDocsGetCommand: OverridesGetStatusCodeto mapInvalidDataException → 400 BadRequest, scoping the change strictly to this tool without touching shared core infrastructure.With these changes, agents receive
400 BadRequestwith an actionable message (e.g.,"Resource type X does not have an apiVersion "Y". Available versions are: ...") and can retry with corrected input.GitHub issue number?
N/A
Pre-merge Checklist
servers/Azure.Mcp.Server/README.mdand/orservers/Fabric.Mcp.Server/README.mddocumentationREADME.mdchanges running the script./eng/scripts/Process-PackageReadMe.ps1. See Package READMEToolDescriptionEvaluatorand obtained a score of0.4or more and a top 3 ranking for all related test promptsconsolidated-tools.jsonbreaking-changelabelservers/Azure.Mcp.Server/docs/azmcp-commands.md./eng/scripts/Update-AzCommandsMetadata.ps1to update tool metadata inazmcp-commands.md(required for CI)servers/Azure.Mcp.Server/docs/e2eTestPrompts.mdcrypto mining, spam, data exfiltration, etc.)/azp run mcp - pullrequest - liveto run Live Test Pipeline