fix(oauth): 401 on expired token at MCP transport (incl. tools/list)#135
Merged
Conversation
altinity-mcp issues no refresh tokens (#115). When claude.ai's access token expires it gets stuck showing a single stale tool instead of re-authorizing (anthropics/claude-code#46328), because the MCP auth middleware only checked the token was non-empty: an expired token sailed through tools/list (200 + stale tools) and only failed later inside tools/call as a ClickHouse error, never as a transport-level 401. Add an exp-only check in createMCPAuthInjector: decode the JWT payload without verifying the signature and reject when exp is in the past (with 60s clock skew), returning the existing 401 + WWW-Authenticate: Bearer error="invalid_token". This pushes the client to re-authorize on every method including tools/list. Signature/iss/aud/scope validation stays delegated to the CH-side ch-jwt-verify sidecar per query, so no JWKS hop is added to the hot path. Opaque/non-JWT tokens soft-pass unchanged (forward-mode safe). grant_types_supported already advertises only authorization_code, so no metadata change is needed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
altinity-mcp issues no refresh tokens (#115 — CIMD clients re-authorize on expiry). When claude.ai's access token expires it does not disconnect cleanly: it gets stuck showing a single stale tool instead of re-authenticating (suspected claude.ai bug, anthropics/claude-code#46328).
Root cause
The MCP auth middleware
createMCPAuthInjectoronly checked that the bearer token was non-empty — it never inspectedexp. So an expired token sailed throughtools/list(HTTP 200 + stale/static tools) and only failed later insidetools/call, surfacing as a ClickHouse error rather than a transport-level 401. The client never saw a signal to re-authorize.Fix
Add an exp-only check in the middleware: decode the JWT payload without verifying the signature and reject when
expis in the past (60s clock skew), returning the existing401+WWW-Authenticate: Bearer error="invalid_token", error_description="OAuth token expired". This fires on every MCP method, includingtools/list, pushing the client straight to re-auth.Design notes:
iss/aud/ scope validation stays delegated to the CH-sidech-jwt-verifysidecar per query — no JWKS hop added to the hot path, preserving the deliberate design in the prior middleware comment.grant_types_supportedalready advertises onlyauthorization_code(locked byTestOAuthASMetadataShape) — no metadata change needed.Changes
pkg/server/server_client.go—unverifiedExp(decode-only) +OAuthTokenExpiredmethod + 60s skew constant.cmd/altinity-mcp/oauth_server.go—createMCPAuthInjectorrejects expired tokens viawriteOAuthError(ErrOAuthTokenExpired).cmd/altinity-mcp/oauth_regression_test.go— invert the old "expired token reaches handler" assumption; add an expired→401+WWW-Authenticatetransport test (sends atools/listbody) plus opaque-soft-pass and unexpired-pass cases.pkg/server/server_client_test.go— unit tests forunverifiedExp/OAuthTokenExpired(expired, unexpired, clock-skew boundary, opaque, no-exp, float exp, malformed).Verification
go test ./...green.Deployed
fix-oauth-expired-token-401-1ee854fto the livegithub-mcp(broker:true, Google IdP) and verified againsthttps://mcp.demo.altinity.cloud/mcp/github:tools/list401+error="invalid_token","OAuth token expired"✅200(passes middleware) ✅200(soft-pass) ✅401(pre-existing) ✅🤖 Generated with Claude Code