Repository navigation
fix(oauth): honour the oauth block for upstreams that authorise per method (#1271) - #1273
Merged
Merged
Conversation
…ethod (#1271) An upstream that answers initialize/tools/list anonymously but 401s tools/call (Google's Gmail MCP endpoint) could never be authenticated: the no-auth strategy "succeeded" on the anonymous handshake, the ladder stopped there, and the OAuth strategy — with a valid token already in the store — was never reached. Every tools/call then failed with "authorization required" while auth status reported the token as valid, and `mcpproxy auth login` refused with "server connected without OAuth". - Ladder: a configured `oauth` block (the documented "OAuth required" signal) drops the anonymous probe. The headers strategy draws the same inference, so it is kept only for a static Authorization credential; other static headers now ride on the OAuth transport instead of being dropped by it. MCPPROXY_DISABLE_OAUTH keeps the historical chain. - Manual login: the three login paths continue with the transport's own OAuth handler when initialize succeeds but OAuth is declared, instead of concluding "no authentication required"; oauth.ShouldUseOAuth no longer lets unrelated headers veto a declared oauth block. - SSE: trySSEOAuthAuth's Start()-error branch (now reachable for declared OAuth servers without a token) parks in PendingAuth like the HTTP path and clears the in-progress mark before starting a flow, instead of dead-ending in "OAuth authorization already in progress". - forceSSEOAuthFlowWithResult no longer shadows its result. - tests/oauthserver: `MCPPerMethodAuth` option (and `-per-method-auth` flag) reproduces the per-method behaviour; legacy `/sse` endpoint. Co-Authored-By: Claude Opus 5 <[email protected]>
- Ladder: an oauth block now yields OAuth only — a stale static Authorization header could still win the anonymous handshake and pin the connection exactly like no-auth did. Static headers ride on the OAuth transport where the token store owns Authorization (consistent with the GH #1172 projection). - tryOAuthAuth/trySSEOAuthAuth: oauthErr is now the named return, so the ErrOAuthPending and retry-with-stored-token exits reach the coordinator's EndFlow as failures instead of telling a concurrent waiter the flow succeeded. - forceSSEOAuthFlowWithResult: the initialize-triggered branch no longer shadows the outer result either. Co-Authored-By: Claude Opus 5 <[email protected]>
- OAuth strategy waiter: returning nil after another client's flow completed was read by runAuthStrategies as "connected", leaving a client with no transport marked ready (ghost connection). Return an OAuth-classified retry error instead. Pre-existing, but with an oauth block OAuth is now the only strategy so every same-name overlap hit it. - Transport: OAuth constructors refuse a brokered per-user credential outright and carry cfg.Headers (not effectiveHeaders), so spec 074 fail-closed holds at the transport too. - StartManualOAuthQuick: the reconnect watcher owned the 30-minute login context and cancelled it after ~2s whenever a completion was already on record (the re-login a declared-OAuth server now permits) or after 2 minutes; the browser callback then found nobody waiting. Completion is now detected as a token change, cancel only then or at the deadline. - ErrOAuthPending detail names the oauth block as the reason the anonymous probe was skipped. - Tests: concurrent same-name clients (ghost-connection invariant), brokered+OAuth refusal, manager-level re-login callback; SSE fixture shutdown ordering (5s -> 0.1s); short deadline on the PendingAuth test. Co-Authored-By: Claude Opus 5 <[email protected]>
…efore the flow Co-Authored-By: Claude Opus 5 <[email protected]>
Deploying mcpproxy-docs with
|
| Latest commit: |
0cee1b2
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://881b1c3b.mcpproxy-docs.pages.dev |
| Branch Preview URL: | https://fix-1271-oauth-block-per-met.mcpproxy-docs.pages.dev |
Contributor
📦 Build ArtifactsWorkflow Run: View Run Available Artifacts
How to DownloadOption 1: GitHub Web UI (easiest)
Option 2: GitHub CLI gh run download 34714770355 --repo smart-mcp-proxy/mcpproxy-go
|
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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.
Closes #1271
Problem
An upstream that answers
initialize/tools/listanonymously but 401stools/call(Google's Gmail MCP endpoint) could never be authenticated. The HTTP/SSE ladder washeaders → no-auth → OAuth;runAuthStrategiesstops at the first strategy that returns nil,tryNoAuthreturns nil on a successful anonymousinitialize, so the OAuth strategy — with a valid token already in the store — was never reached. Everytools/callthen failed withauthorization requiredwhileauth statusreportedtoken_valid: true, andauth loginrefused with HTTP 500server connected without OAuth - no authentication required(the three manual-login paths drew the same conclusion from a successfulinitialize).Fix
internal/upstream/core/connection_http.go): a configuredoauthblock — the documented "OAuth required" signal (config.ServerConfig.OAuth: keep even when empty to signal OAuth requirement) — makes OAuth the only strategy. The headers strategy draws the same "anonymous initialize succeeded" inference, so it is dropped too; static headers now ride on the OAuth transport, where the token store ownsAuthorization(consistent with the GH [Bug]: Server keeps showing "Token expired" after switching it from OAuth to a header token #1172 projection, which already treats anoauthblock as decisive over a staticAuthorizationheader). Brokered (spec 074) headers-only fail-closed still takes precedence;MCPPROXY_DISABLE_OAUTHkeeps the historical chain.internal/transport/http.go): both OAuth constructors carrycfg.Headers(they dropped them before) and refuse a brokered per-user credential outright.connection_oauth.go):forcedAuthorizationRequired()synthesises theOAuthAuthorizationRequiredErrorthe three login paths key on, from the live transport's handler, wheninitializesucceeds but OAuth is declared.oauth.ShouldUseOAuthno longer lets unrelated static headers veto a declaredoauthblock.trySSEOAuthAuth'sStart()-error branch (now reachable for declared-OAuth servers without a token) parks in PendingAuth under deferral and clears the in-progress mark before starting a flow — it used to dead-end inOAuth authorization already in progress.forceSSEOAuthFlowWithResultno longer shadows its result (both branches).runAuthStrategiesread as "connected" → a client with no transport marked ready (ghost connection); it now returns an OAuth-classified retry error.tryOAuthAuth/trySSEOAuthAuthuse a namedoauthErrreturn so every exit reachescoordinator.EndFlow.StartManualOAuthQuick's reconnect watcher owned the 30-minute login context and cancelled it after ~2 s whenever a completion was already on record (exactly the re-login this PR enables) or after 2 minutes — the browser callback then got 400; completion is now detected as a token change, cancel only then, at the deadline, or on manager shutdown.ErrOAuthPendingdetail names theoauthblock as the reason the anonymous probe was skipped; the "no authentication required" refusal (servers without a block) names the remedy.Behaviour change (documented in
docs/configuration.md)An
oauthblock now means "OAuth, period": a server carrying one no longer connects anonymously, and a staticAuthorizationheader is not used while the block is present — remove the block for a server that needs no sign-in. Health detail says so explicitly. Manual login on a declared-OAuth server always starts a fresh sign-in, even with a valid token stored.Test rig
tests/oauthservergainedOptions.MCPPerMethodAuth(-per-method-authon the standalone server):initialize/ping/tools/list/notifications/*anonymous,tools/call401 — the Gmail behaviour — plus a legacy/sseendpoint behind the same middleware.Tests (each proven to fail without its fix)
internal/upstream/core/auth_strategy_oauth_block_test.go— ladder shape (oauth block, headers, brokered, env gate),forcedAuthorizationRequired.internal/upstream/core/per_method_auth_e2e_test.go— realClientagainst the per-method rig: HTTP + SSE with a seeded token end on the OAuth strategy andtools/callsucceeds; no block stays anonymous (boundary); SSE with no token parks in PendingAuth fast; manual login proceeds with a valid token; six same-name clients connecting at once — no ghost connection.internal/upstream/manual_oauth_relogin_test.go— manager-level re-login: callback still finds the login waiting after the watcher's first tick.internal/transport/oauth_static_headers_test.go— static headers carried, token store winsAuthorization, brokered+OAuth refused.internal/oauth/should_use_oauth_block_test.go,tests/oauthserver/per_method_auth_test.go.Verification
go test -race ./internal/upstream/... ./internal/transport/... ./internal/oauth/... ./internal/runtime/... ./tests/oauthserver/...green;golangci-lint(CI config) 0 issues;./scripts/test-api-e2e.sh65/65.mcpproxy serveagainstoauthserver -per-method-auth— server parks in Sign-in required (was: falsely connected), headless login viaPOST /api/v1/servers/{name}/login→ callback 200 → Connected (2 tools),mcpproxy call tool-readsucceeds, re-login with a valid token returns 200 + auth URL and its callback lands (was: HTTP 500), survives a daemon restart.auth statusno longer contradicts itself.gpt-6-astra, 4 rounds (3 P2 + 1 P3 fixed, final VERDICT: CLEAN); independent adversarial pass (5 lenses, 15 confirmed items) — actionable ones fixed above, the rest are documented trade-offs or pre-existing residuals listed below.Known residuals (not introduced here)
tools/list(but accepts oninitialize), the server shows connected with 0 tools rather than a sign-in CTA — same as before this PR; atools/list-401 → sign-in signal would close it.client_id/client_secret/scopesas the issue reporter observed.🤖 Generated with Claude Code