GeezerVison is showing github.com — fix(mcp): fix OAuth auto-connect failing on first connection by domdomegg · Pull Request #15547 · anomalyco/opencode
Conversation
domdomegg
commented
Mar 1, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Issue for this PR
Closes #15546
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
When OpenCode auto-connects to a remote MCP server requiring OAuth (e.g. on startup with a pre-configured server), the SDK transport gets a 401 and internally triggers the OAuth auth() flow. This flow calls provider.state() to get an OAuth state parameter — but state() was implemented as a reader that throws if no state was previously saved. Since saveState() is only called in the explicit startAuth() path (not the automatic connect path), state() always throws on first connect.
The thrown error is a plain Error, not UnauthorizedError, so OpenCode's create() catch block doesn't recognize it as auth-related. The server ends up with status: "failed" instead of status: "needs_auth".
Two fixes:
-
McpOAuthProvider.state() — generate and persist a new cryptographic state when none exists, instead of throwing. The SDK's OAuthClientProvider interface defines state?() as a generator that returns a state value, not a reader.
-
create() error handling — also treat errors containing "OAuth" from an auth-provider-enabled transport as auth-related, so they result in needs_auth status rather than failed. This is a defense-in-depth fix for any other plain Error the SDK might throw during the auth flow.
How did you verify your code works?
Added 3 new tests in test/mcp/oauth-auto-connect.test.ts:
first connect to OAuth server shows needs_auth instead of failed — verifies the full auto-connect path sets needs_auth status
state() generates a new state when none is saved — verifies state generation and persistence
state() returns existing state when one is saved — verifies pre-saved state is returned as-is
All MCP tests pass: bun test test/mcp/ --timeout 30000 → 9 pass, 0 fail.
Screenshots / recordings
N/A
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
mattzcarey reacted with thumbs up emoji
MessiasLima reacted with heart emoji
All reactions
-
👍
1 reaction
-
❤️
1 reaction
… MCP servers
state() threw when no state was pre-saved, but the SDK calls it as a
generator during automatic auth. Also harden create() to treat
auth-related errors from the SDK transport as needs_auth.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
After the fix:
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Mar 4, 2026
Uh oh!
There was an error while loading. Please reload this page.
Rwanbt
pushed a commit
to Rwanbt/unifia
that referenced
this pull request
May 5, 2026
AIALRA-0
pushed a commit
to AIALRA-0/opencode-turn-engine
that referenced
this pull request
Jun 10, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|