[PR #7379] feat(mcp): add OAuth redirect URI configuration for MCP servers #12377

Closed
opened 2026-02-16 18:17:17 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7379

State: closed
Merged: Yes


Summary

This PR implements custom OAuth redirect URI configuration for MCP servers, allowing users to override the default callback URL to match their OAuth server's requirements.

Changes

  • Added optional redirectUri config parameter to MCP OAuth settings
  • Implemented parseRedirectUri() helper function to extract port and path
  • Dynamic callback server configuration based on custom redirect URI
  • Full test coverage for custom URI parsing and edge cases
  • Backward compatible - defaults to http://127.0.0.1:19876/mcp/oauth/callback when not specified

User Configuration

{
  "mcp": {
    "my-server": {
      "type": "remote",
      "url": "https://api.example.com",
      "oauth": {
        "clientId": "my-client-id",
        "redirectUri": "https://custom.example.com/callback"  // NEW
      }
    }
  }
}

Files Changed

  • packages/opencode/src/config/config.ts - Added redirectUri to OAuth config schema
  • packages/opencode/src/mcp/oauth-provider.ts - Implement parseRedirectUri() and use configured URI
  • packages/opencode/src/mcp/oauth-callback.ts - Dynamic server configuration with port/path parsing
  • packages/opencode/src/mcp/index.ts - Pass redirectUri through OAuth flow
  • packages/opencode/src/cli/cmd/mcp.ts - Pass redirectUri to OAuth provider in debug command
  • packages/opencode/test/mcp/oauth-callback.test.ts - Comprehensive test suite

Testing

  • Tests cover default behavior (no redirectUri specified)
  • Tests cover custom redirectUri with different ports/paths
  • Tests cover server reconfiguration when redirectUri changes
  • Tests cover invalid URI handling with fallback to defaults
  • All tests passing

Related

Closes #7377 - [FEATURE]: Allow custom OAuth redirect URI configuration for MCP servers

Design Rationale

OpenCode already supports overriding clientId for flexible OAuth client configuration. Since OAuth servers validate both client ID and redirect URI, supporting custom redirectUri is essential for feature completeness. This removes the asymmetry where users could point to any OAuth server/client but were constrained to a hardcoded callback URL.

GitHub OAuth:
image

Keycloak:
Image

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7379 **State:** closed **Merged:** Yes --- ## Summary This PR implements custom OAuth redirect URI configuration for MCP servers, allowing users to override the default callback URL to match their OAuth server's requirements. ## Changes - ✅ Added optional `redirectUri` config parameter to MCP OAuth settings - ✅ Implemented `parseRedirectUri()` helper function to extract port and path - ✅ Dynamic callback server configuration based on custom redirect URI - ✅ Full test coverage for custom URI parsing and edge cases - ✅ Backward compatible - defaults to `http://127.0.0.1:19876/mcp/oauth/callback` when not specified ## User Configuration ```jsonc { "mcp": { "my-server": { "type": "remote", "url": "https://api.example.com", "oauth": { "clientId": "my-client-id", "redirectUri": "https://custom.example.com/callback" // NEW } } } } ``` ## Files Changed - `packages/opencode/src/config/config.ts` - Added `redirectUri` to OAuth config schema - `packages/opencode/src/mcp/oauth-provider.ts` - Implement `parseRedirectUri()` and use configured URI - `packages/opencode/src/mcp/oauth-callback.ts` - Dynamic server configuration with port/path parsing - `packages/opencode/src/mcp/index.ts` - Pass `redirectUri` through OAuth flow - `packages/opencode/src/cli/cmd/mcp.ts` - Pass `redirectUri` to OAuth provider in debug command - `packages/opencode/test/mcp/oauth-callback.test.ts` - Comprehensive test suite ## Testing - Tests cover default behavior (no redirectUri specified) - Tests cover custom redirectUri with different ports/paths - Tests cover server reconfiguration when redirectUri changes - Tests cover invalid URI handling with fallback to defaults - All tests passing ## Related Closes #7377 - [FEATURE]: Allow custom OAuth redirect URI configuration for MCP servers ## Design Rationale OpenCode already supports overriding `clientId` for flexible OAuth client configuration. Since OAuth servers validate **both** client ID and redirect URI, supporting custom `redirectUri` is essential for feature completeness. This removes the asymmetry where users could point to any OAuth server/client but were constrained to a hardcoded callback URL. GitHub OAuth: <img width="1392" height="596" alt="image" src="https://github.com/user-attachments/assets/ffcec848-f3e1-4eaa-9bfc-fb7b588bae0d" /> Keycloak: <img width="1755" height="753" alt="Image" src="https://github.com/user-attachments/assets/f26558d4-c4f9-423b-8a39-3d2a37248c59" />
yindo added the pull-request label 2026-02-16 18:17:17 -05:00
yindo closed this issue 2026-02-16 18:17:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12377