Repository navigation
Conversation
… time Some servers (e.g. Google's Gmail/Drive/Calendar MCP endpoints) accept the initialize handshake without credentials and only return 401 on tool calls. The OAuth flow therefore never starts, mcp auth prints "Authentication successful!" with no tokens stored, and every tool call fails as Unauthorized. - Persist OAuth discovery state (RFC 9728 / RFC 8414) learned from 401 challenges via the provider's discoveryState/saveDiscoveryState hooks - startAuth() explicitly starts the authorization flow after a silent connect when the server has no usable tokens and there is evidence it requires OAuth (configured authorizationServerUrl or a stored challenge) - Tool-call 401s flip the server to needs_auth with a toast instead of dead-ending as Unauthorized - Add oauth.authorizationServerUrl config for servers that require OAuth but expose no discoverable metadata Fixes anomalyco#26195
Contributor
|
The following comment was made by an LLM, it may be inaccurate: |
Member
|
This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2. — from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊 |
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.
Issue for this PR
Closes #26195. The config escape hatch follows the approach proposed in #26912.
Type of change
What does this PR do?
Google's official MCP connectors (gmailmcp/drivemcp/calendarmcp.googleapis.com) accept
initialize— andpingandtools/list— without credentials, and only return 401 ontools/call. Since the OAuth flow only starts on a connect-time 401, it never runs:opencode mcp authprints "Authentication successful!" with zero tokens stored, and every tool call fails asUnauthorized.The fix has three parts:
discoveryState/saveDiscoveryStatehooks:WWW-Authenticateheader) are stored next to the other credentials inmcp-auth.jsonstartAuth()when the server connects without a 401:authorizationServerUrl, or a stored challengeauth()directly and returns the authorization URL, so the normal browser flow andfinishAuthrun unchangedneeds_authand the client is closed, with a toast pointing atopencode mcp auth <name>Unauthorizedwith no way outBehavior change:
authenticate()now starts the OAuth flow even if the server currently accepts unauthenticated connects:How did you verify your code works?
Four new tests in
test/mcp/oauth-auto-connect.test.tsusing a deferred-auth mock that mirrors Google's behavior (stateless, everything public excepttools/call). Also checked against the real gmailmcp endpoint:initialize/ping/tools/listreturn 200 unauthenticated,tools/callreturns 401 with valid RFC 9728 metadata in the challenge.bun run typecheckclean in opencode and core; full opencode test suite passes except a pre-existing flaky SSE timeout test that also fails ondevand passes 3/3 in isolation.Screenshots / recordings
N/A — no UI change beyond an existing-style toast.
Checklist