Preserve HTTP status codes across target frameworks - #1767
Open
PranavSenthilnathan wants to merge 1 commit into
Open
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9a49da98-c8d8-4b90-a6ed-50afe18ac981
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves cross-target consistency for HTTP error handling by preserving HTTP status codes on HttpRequestException even on targets that lack HttpRequestException.StatusCode (notably netstandard2.0). It does so by storing the status code in Exception.Data and providing helpers to create/read exceptions in a uniform way, including when AutoDetect wraps a failed SSE fallback.
Changes:
- Introduce
HttpRequestExceptionExtensionsto createHttpRequestExceptioninstances while persistingHttpStatusCodeinException.Data, and to retrieve the status viaStatusCodewhen available or the stored value otherwise. - Update AutoDetect’s SSE-fallback wrapping path to rethrow an
HttpRequestExceptioncreated via the helper, preserving the original Streamable HTTP status code across all TFMs. - Extend AutoDetect transport tests to assert status preservation via
Exception.Data(and viaStatusCodewhen available).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/ModelContextProtocol.Tests/Transport/HttpClientTransportAutoDetectTests.cs | Adds assertions that surfaced HttpRequestException instances preserve the original HTTP status code via Exception.Data (and StatusCode on #if NET). |
| src/ModelContextProtocol.Core/Client/AutoDetectingClientSessionTransport.cs | Switches the dual-failure (Streamable HTTP + SSE fallback) wrap to use the new helper and preserve status codes consistently. |
| src/Common/HttpResponseMessageExtensions.cs | Adds internal helpers to create/read HttpRequestException with status preserved via Exception.Data as a cross-target fallback. |
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.
Motivation and Context
HttpRequestException.StatusCodeis unavailable onnetstandard2.0, so HTTP status information is currently lost on that target. AutoDetect also recreates the original Streamable HTTP exception when SSE fallback fails, which must preserve the status for downstream handling.This prepares for #1765, where connect-time fallback needs to distinguish HTTP 400/404 responses consistently across all supported target frameworks.
Changes
HttpRequestExceptioninstances while preserving the HTTP status inException.Data.HttpRequestException.StatusCodewhere available, with the stored value as a cross-target fallback.net10.0,net9.0,net8.0, andnet472.No public API is added or changed.
How Has This Been Tested?
dotnet buildnet10.0,net9.0,net8.0, andnet472The ASP.NET conformance suite could not start locally because Node.js 21.6.2 does not provide
fs.globSync, which is required by the pinned conformance package.Breaking Changes
None.