-
Notifications
You must be signed in to change notification settings - Fork 23
fix(mcp): logged, single-flight, proactive OAuth refresh for MCP servers #1697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Aaronontheweb
wants to merge
5
commits into
netclaw-dev:dev
from
Aaronontheweb:fix/mcp-oauth-refresh
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29b6e4d
fix(mcp): logged, single-flight, proactive OAuth refresh for MCP servers
Aaronontheweb d56a03e
fix(mcp): treat invalid_client/unauthorized_client as terminal refres…
Aaronontheweb dbad913
Merge branch 'dev' into fix/mcp-oauth-refresh
Aaronontheweb eeb1dad
fix(mcp): make OAuth refresh ownership single-flight
Aaronontheweb 8d131d5
Merge branch 'dev' into fix/mcp-oauth-refresh
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,20 +288,77 @@ public async Task RefreshToken_Success_ReturnsNewTokenPreservingRefresh() | |
| Assert.Equal("old-refresh", result.RefreshToken!.Value); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("invalid_grant", HttpStatusCode.BadRequest)] | ||
| [InlineData("invalid_client", HttpStatusCode.Unauthorized)] | ||
| [InlineData("unauthorized_client", HttpStatusCode.BadRequest)] | ||
| public async Task RefreshToken_TerminalOAuthError_ThrowsRejectedWithErrorCode( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forcibly tests the types of error codes we get back on token exchange to ensure that there's a visible error that appears in the logs this time around |
||
| string errorCode, HttpStatusCode status) | ||
| { | ||
| var handler = new FakeHttpMessageHandler(_ => | ||
| JsonResponse(new { error = errorCode }, status)); | ||
|
|
||
| var service = new OAuthPkceService(new HttpClient(handler)); | ||
|
|
||
| var ex = await Assert.ThrowsAsync<OAuthTokenRefreshRejectedException>(() => | ||
| service.RefreshTokenAsync( | ||
| "https://auth.example.com/token", | ||
| "test-client", | ||
| new SensitiveString("expired-refresh"), ct: TestContext.Current.CancellationToken)); | ||
|
|
||
| Assert.Equal(errorCode, ex.ErrorCode); | ||
| Assert.Equal(status, ex.StatusCode); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RefreshToken_InvalidGrant_ReturnsNull() | ||
| public async Task RefreshToken_UnrecognizedOAuthError_ThrowsHttpRequestException() | ||
| { | ||
| // "invalid_request" is a 4xx OAuth error but NOT a terminal refresh | ||
| // code — it must stay transient (retryable), not clear credentials. | ||
| var handler = new FakeHttpMessageHandler(_ => | ||
| JsonResponse(new { error = "invalid_grant" }, HttpStatusCode.BadRequest)); | ||
| JsonResponse(new { error = "invalid_request" }, HttpStatusCode.BadRequest)); | ||
|
|
||
| var service = new OAuthPkceService(new HttpClient(handler)); | ||
|
|
||
| var result = await service.RefreshTokenAsync( | ||
| "https://auth.example.com/token", | ||
| "test-client", | ||
| new SensitiveString("expired-refresh"), ct: TestContext.Current.CancellationToken); | ||
| await Assert.ThrowsAsync<HttpRequestException>(() => | ||
| service.RefreshTokenAsync( | ||
| "https://auth.example.com/token", | ||
| "test-client", | ||
| new SensitiveString("expired-refresh"), ct: TestContext.Current.CancellationToken)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RefreshToken_UnparsableErrorBody_ThrowsHttpRequestException() | ||
| { | ||
| var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.BadRequest) | ||
| { | ||
| Content = new StringContent("<html>gateway error</html>"), | ||
| }); | ||
|
|
||
| var service = new OAuthPkceService(new HttpClient(handler)); | ||
|
|
||
| await Assert.ThrowsAsync<HttpRequestException>(() => | ||
| service.RefreshTokenAsync( | ||
| "https://auth.example.com/token", | ||
| "test-client", | ||
| new SensitiveString("expired-refresh"), ct: TestContext.Current.CancellationToken)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RefreshToken_ServerErrorWithTerminalShapedBody_ThrowsHttpRequestException() | ||
| { | ||
| // A 5xx is a server-side fault worth retrying even when its body | ||
| // happens to carry a terminal-looking error code. | ||
| var handler = new FakeHttpMessageHandler(_ => | ||
| JsonResponse(new { error = "invalid_grant" }, HttpStatusCode.ServiceUnavailable)); | ||
|
|
||
| var service = new OAuthPkceService(new HttpClient(handler)); | ||
|
|
||
| Assert.Null(result); | ||
| await Assert.ThrowsAsync<HttpRequestException>(() => | ||
| service.RefreshTokenAsync( | ||
| "https://auth.example.com/token", | ||
| "test-client", | ||
| new SensitiveString("expired-refresh"), ct: TestContext.Current.CancellationToken)); | ||
| } | ||
|
|
||
| [Fact] | ||
|
|
||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM