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 — feat: implement MCP OAuth remote authorization flow by vincenthcui · Pull Request #21704 · anomalyco/opencode

Conversation

@vincenthcui

@vincenthcui vincenthcui commented Apr 9, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Issue for this PR

Closes #7377

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Implements MCP OAuth remote authorization flow via the main server's /oauth/callback route. Instead of starting a separate callback server on 127.0.0.1:19876, OAuth callbacks are now handled by the main OpenCode server, making it work better in constrained environments (WSL2, devcontainer, agent sandbox) where binding to 0.0.0.0 or additional ports may not be feasible.

How did you verify your code works?

  • Manually tested OAuth flow end-to-end in TUI web mode with a remote MCP server
  • Unit tests pass for all new functionality
image image

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.

All reactions
@vincenthcui
feat: implement MCP OAuth remote authorization flow
18c3fed
- Implement MCP OAuth remote authorization flow
- Add unit tests for MCP OAuth remote authorization flow
- Show MCP OAuth auth prompts inline below each MCP item
- Show toast with auth URL when browser open fails in TUI MCP toggle
- Fix use correct protocol for OAuth callback URL and show toast on browser open failure
@vincenthcui
vincenthcui requested a review from adamdotdevin as a code owner April 9, 2026 15:38
@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 Apr 9, 2026
@github-actions

github-actions Bot commented Apr 9, 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.

vincenthcui added 2 commits April 9, 2026 23:47
@vincenthcui
Merge branch 'dev' into dev
327602e
@vincenthcui
chore(mcp): remove old callback server startup
194b402
The OAuth callback is now handled by the main server's /oauth/callback route.
No longer need to start a separate callback server on 127.0.0.1:19876.
@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 Apr 9, 2026
@github-actions

github-actions Bot commented Apr 9, 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.

vincenthcui added 3 commits April 10, 2026 00:41
@vincenthcui
Merge branch 'dev' into dev
fcbcad3
@vincenthcui
Merge branch 'dev' of upstream/dev into dev
8bea33d
@vincenthcui
Merge branch 'dev' into dev
dc2d1db
@egze

egze commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Looks really nice

All reactions

Sorry, something went wrong.

Uh oh!

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

vincenthcui added 2 commits April 11, 2026 02:13
@vincenthcui
Merge branch 'dev' into dev
7930a5b
@vincenthcui
fix-test
ddee629
vincenthcui
vincenthcui commented Apr 11, 2026
View reviewed changes

@vincenthcui vincenthcui left a comment

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: MCP OAuth Remote Authorization Flow

Summary

This PR implements MCP OAuth remote authorization flow by moving the callback handling from a separate local server (127.0.0.1:19876) to the main OpenCode server. This addresses authentication issues in constrained environments like WSL2, devcontainers, and agent sandboxes.

Key Changes

  • packages/opencode/src/mcp/oauth-callback.ts: New callback handler for OAuth
  • packages/opencode/src/server/routes/mcp.ts: Server route integration (+97 lines)
  • packages/opencode/src/mcp/oauth-provider.ts: OAuth provider updates
  • Comprehensive test coverage added

Questions/Comments

  1. The route is now handled server-side - is there a security consideration for how state is validated between the initial redirect and callback?
  2. How does this handle the race condition between starting the auth flow and the callback arriving?
  3. Should we consider adding rate limiting to the callback endpoint?

Suggestion

This looks good overall. @egze since you've been actively working on OAuth-related fixes recently, can you take a look? Also @adamdotdevin who is already requested.

Would also appreciate a review from anyone familiar with the MCP auth flow to verify the state management approach.

Sorry, something went wrong.

Uh oh!

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

All reactions
vincenthcui
vincenthcui commented Apr 11, 2026
View reviewed changes

@vincenthcui vincenthcui left a comment •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey team, this PR moves OAuth auth from server-side to client-side to fix authentication issues in WSL2/devcontainer/sandbox environments.

@egze @adamdotdevin I have seen your work on OAuth-related fixes and would really appreciate your review on this. The main changes are:

  • oauth callback handling moved from standalone port to main server
  • added oauth-callback.ts for callback processing
  • full test coverage included

This solves real issues for users in constrained environments. Would mean a lot to get your feedback!'

Sorry, something went wrong.

Uh oh!

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

All reactions
@egze

egze commented Apr 11, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Great work on this PR! The approach of routing OAuth callbacks through the main server is exactly what's needed for constrained environments.

I want to highlight an important use case and a blocker I found that affects both this PR and #21385 (already merged).

The architecture: Consider a deployment where a frontend web app runs in one pod (public-facing), and OpenCode runs in another pod (opencode serve) that is only reachable from the
internal network. The user interacts with the frontend in the browser, and the frontend communicates with OpenCode over REST.

The intended OAuth flow:

  1. Frontend calls POST /mcp/:name/auth → gets { authorizationUrl, oauthState }
  2. Frontend opens authorizationUrl in the user's browser
  3. User authorizes → OAuth provider redirects to the frontend's public URL (configured via redirectUri from feat(mcp): add OAuth redirect URI configuration for MCP servers #21385)
  4. Frontend receives the callback, verifies state === oauthState
  5. Frontend calls POST /mcp/:name/auth/callback with { code } → OpenCode completes the token exchange

The endpoints for this flow already exist on dev. The redirectUri config from #21385 tells the OAuth provider where to redirect.

The blocker: startAuth unconditionally calls McpOAuthCallback.ensureRunning(oauthConfig?.redirectUri) (index.ts:727), which tries to start a local HTTP server on the port parsed
from the redirect URI. When redirectUri is something like https://frontend.example.com/oauth/callback, parseRedirectUri returns port 443, and server.listen(443) fails with EACCES
on any non-root process. This crashes startAuth before the OAuth flow even begins.

Neither #21385 nor this PR modifies ensureRunning to handle external redirect URIs.

The fix is small — skip the local callback server when the redirect host isn't localhost:

  export async function ensureRunning(redirectUri?: string): Promise<void> {
      if (redirectUri) {
        try {
          const url = new URL(redirectUri)
          if (url.hostname !== "127.0.0.1" && url.hostname !== "localhost") {
            log.info("redirect URI is external, skipping local callback server", { redirectUri })
            return
          }
        } catch {}
      }

      const { port, path } = parseRedirectUri(redirectUri)
      // ... rest unchanged
  }

@lumincui would you be able to include this fix in this PR? It should be a small addition to ensureRunning in oauth-callback.ts.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vincenthcui
fix(mcp): skip local callback server for external redirect URIs
7037359
Handle external redirect URIs in ensureRunning by checking if the
hostname is not localhost/127.0.0.1. This fixes EACCES errors when
redirectUri points to an external server (e.g., https://frontend.example.com/oauth/callback).
@vincenthcui

vincenthcui commented Apr 13, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Author

Thanks for the suggestion and feedback! I've updated the ensureRunning function to skip the local callback server when the redirectUri points to an external server.

In our architecture, the callback is handled by the frontend server, so no local callback server is needed.

@egze

All reactions

Sorry, something went wrong.

Uh oh!

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

@vincenthcui

vincenthcui commented Apr 16, 2026

Copy link
Copy Markdown
Author

Hey @rekram1-node, we noticed you have experience with MCP and OAuth flows. This PR implements MCP OAuth remote authorization flow by moving callback handling from a separate local server to the main OpenCode server — this fixes real issues users face in WSL2, devcontainers, and agent sandbox environments where binding to additional ports isn't feasible.

This is an important architectural change that would benefit greatly from your expertise. We would really appreciate your review on this. Thank you!

All reactions

Sorry, something went wrong.

Uh oh!

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

@opencode-agent
opencode-agent Bot force-pushed the dev branch from 2f2ed11 to 9f201d6 Compare April 16, 2026 21:55
@rekram1-node

rekram1-node commented May 15, 2026

Copy link
Copy Markdown
Member

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rekram1-node rekram1-node closed this May 15, 2026
@rekram1-node rekram1-node added the automated-pr-cleanup label May 15, 2026
@Skeptomenos Skeptomenos mentioned this pull request Oct 15, 2026
[FEATURE]: MCP OAuth paste-code fallback for headless/SSH sessions #54495
Closed
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adamdotdevin adamdotdevin Awaiting requested review from adamdotdevin

Uh oh!

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

Assignees

No one assigned

Labels

automated-pr-cleanup

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Allow custom OAuth redirect URI configuration for MCP servers

Uh oh!

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

3 participants

@vincenthcui @egze @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.