fix(auth): check token expiry before scopes in withMcpAuth - #182
Open
anshusaurav wants to merge 1 commit into
Open
fix(auth): check token expiry before scopes in withMcpAuth#182anshusaurav wants to merge 1 commit into
anshusaurav wants to merge 1 commit into
Conversation
An expired token that also lacked a required scope was reported as 403 insufficient_scope instead of 401 invalid_token. Per RFC 6750 §3.1 an expired token is always invalid_token — this matters because clients use the error code to decide whether to refresh the token or surface a permissions error. Move the expiresAt check above the requiredScopes check so expiry is evaluated first. Add regression tests covering both orderings. Fixes vercel#181
Collaborator
allenzhou101
approved these changes
Aug 6, 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
401 invalid_tokeninstead of403 insufficient_scopeexpiresAtcheck above therequiredScopescheck inwithMcpAuthso expiry is always evaluated first, per RFC 6750 §3.1Fixes #181
Why it matters
401 invalid_tokentells an MCP client its token is no longer valid — the client should refresh or re-authenticate.403 insufficient_scopetells the client it has the wrong permissions — the client will not refresh and may surface a misleading "insufficient permissions" error. When both conditions are true, RFC 6750 requires the expiry to win.Test plan
invalid_token(was 403 before fix)insufficient_scope(unchanged)vitest run)