GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mcp): fix OAuth auto-connect failing on first connection by domdomegg · Pull Request #15547 · anomalyco/opencode

Conversation

@domdomegg

@domdomegg domdomegg commented Mar 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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:

  1. 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.

  2. 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
@domdomegg
fix(mcp): fix OAuth auto-connect failing on first connection to OAuth…
a95bb7a
… 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.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 1, 2026
@github-actions

github-actions Bot commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@domdomegg

domdomegg commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

After the fix:

CleanShot 2026-03-01 at 08 52 13@2x CleanShot 2026-03-01 at 08 53 00@2x
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Mar 4, 2026
fix(mcp): show "needs auth" instead of "failed" for OAuth servers #15986
Closed
fix(mcp): generate OAuth state on-the-fly during initial connection #16248
Closed
fix(mcp): show auth-required status for streamable HTTP 401 #16315
Closed
@rekram1-node
Merge branch 'dev' into fix/oauth-auto-connect-state
2f53f2c
@rekram1-node
rekram1-node merged commit ff3bb74 into anomalyco:dev Mar 10, 2026
9 checks passed

Uh oh!

There was an error while loading. Please reload this page.

demostanis pushed a commit to demostanis/opencode that referenced this pull request Mar 20, 2026
@domdomegg @rekram1-node @demostanis
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
004cded
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
RobertWsp pushed a commit to RobertWsp/opencode that referenced this pull request Apr 16, 2026
@domdomegg @rekram1-node @RobertWsp
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
c7d9d60
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
@coleleavitt coleleavitt mentioned this pull request Apr 21, 2026
fix/memory 20695 coleleavitt/opencode#5
Merged
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request May 5, 2026
@domdomegg @rekram1-node
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
ac5f704
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
AIALRA-0 pushed a commit to AIALRA-0/opencode-turn-engine that referenced this pull request Jun 10, 2026
@domdomegg @rekram1-node
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
631e219
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
Rwanbt pushed a commit to Rwanbt/unifia that referenced this pull request Jun 14, 2026
@domdomegg @rekram1-node
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
ac82727
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
@rekram1-node rekram1-node mentioned this pull request Jun 24, 2026
Remote MCP OAuth: browser never opens for re-authentication after session loss #16893
Closed
MarsQiu007 referenced this pull request in MarsQiu007/openNovel Jul 31, 2026
@domdomegg @rekram1-node
fix(mcp): fix OAuth auto-connect failing on first connection (#15547)
9782b19
Co-authored-by: Aiden Cline <[email protected]>
meSingh pushed a commit to meSingh/opencode that referenced this pull request Sep 6, 2026
@domdomegg @rekram1-node
fix(mcp): fix OAuth auto-connect failing on first connection (anomaly…
6be8ff4
…co#15547)

Co-authored-by: Aiden Cline <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Remote MCP servers with OAuth fail with 'No OAuth state saved for MCP server'

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@domdomegg @rekram1-node
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.