From 9c603278aafcdbd7537bab99795a2c68e0885243 Mon Sep 17 00:00:00 2001 From: Alan Zimmer <48699787+alzimmermsft@users.noreply.github.com> Date: Wed, 25 Mar 2026 09:27:08 -0400 Subject: [PATCH] Update HttpRequestException handling to check for StatusCode (#2172) * Update HttpRequestException handling to check for StatusCode * Add CHANGELOG entries --- core/Microsoft.Mcp.Core/src/Commands/BaseCommand.cs | 1 + core/Microsoft.Mcp.Core/src/Commands/GlobalCommand.cs | 2 +- servers/Azure.Mcp.Server/changelog-entries/1774379055021.yaml | 3 +++ servers/Fabric.Mcp.Server/changelog-entries/1774379067978.yaml | 3 +++ 4 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 servers/Azure.Mcp.Server/changelog-entries/1774379055021.yaml create mode 100644 servers/Fabric.Mcp.Server/changelog-entries/1774379067978.yaml diff --git a/core/Microsoft.Mcp.Core/src/Commands/BaseCommand.cs b/core/Microsoft.Mcp.Core/src/Commands/BaseCommand.cs index 80a33df534..dca50c35b9 100644 --- a/core/Microsoft.Mcp.Core/src/Commands/BaseCommand.cs +++ b/core/Microsoft.Mcp.Core/src/Commands/BaseCommand.cs @@ -110,6 +110,7 @@ protected virtual void HandleException(CommandContext context, Exception ex) { ArgumentException => HttpStatusCode.BadRequest, // Bad Request for invalid arguments InvalidOperationException => HttpStatusCode.UnprocessableEntity, // Unprocessable Entity for configuration errors + HttpRequestException httpEx => httpEx.StatusCode ?? HttpStatusCode.ServiceUnavailable, _ => HttpStatusCode.InternalServerError // Internal Server Error for unexpected errors }; diff --git a/core/Microsoft.Mcp.Core/src/Commands/GlobalCommand.cs b/core/Microsoft.Mcp.Core/src/Commands/GlobalCommand.cs index c5a20680b5..881ac19108 100644 --- a/core/Microsoft.Mcp.Core/src/Commands/GlobalCommand.cs +++ b/core/Microsoft.Mcp.Core/src/Commands/GlobalCommand.cs @@ -121,7 +121,7 @@ protected override TOptions BindOptions(ParseResult parseResult) KeyNotFoundException => HttpStatusCode.NotFound, AuthenticationFailedException => HttpStatusCode.Unauthorized, RequestFailedException rfEx => (HttpStatusCode)rfEx.Status, - HttpRequestException => HttpStatusCode.ServiceUnavailable, + HttpRequestException httpEx => httpEx.StatusCode ?? HttpStatusCode.ServiceUnavailable, _ => HttpStatusCode.InternalServerError }; diff --git a/servers/Azure.Mcp.Server/changelog-entries/1774379055021.yaml b/servers/Azure.Mcp.Server/changelog-entries/1774379055021.yaml new file mode 100644 index 0000000000..959e060310 --- /dev/null +++ b/servers/Azure.Mcp.Server/changelog-entries/1774379055021.yaml @@ -0,0 +1,3 @@ +changes: + - section: "Other Changes" + description: "Update HttpRequestException to attempt to return a more specific status code for better troubleshooting" \ No newline at end of file diff --git a/servers/Fabric.Mcp.Server/changelog-entries/1774379067978.yaml b/servers/Fabric.Mcp.Server/changelog-entries/1774379067978.yaml new file mode 100644 index 0000000000..959e060310 --- /dev/null +++ b/servers/Fabric.Mcp.Server/changelog-entries/1774379067978.yaml @@ -0,0 +1,3 @@ +changes: + - section: "Other Changes" + description: "Update HttpRequestException to attempt to return a more specific status code for better troubleshooting" \ No newline at end of file