opencode
opencode copied to clipboard
feat(mcp): add OAuth redirect URI configuration for MCP servers
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
redirectUriconfig 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/callbackwhen 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- AddedredirectUrito OAuth config schema -
packages/opencode/src/mcp/oauth-provider.ts- ImplementparseRedirectUri()and use configured URI -
packages/opencode/src/mcp/oauth-callback.ts- Dynamic server configuration with port/path parsing -
packages/opencode/src/mcp/index.ts- PassredirectUrithrough OAuth flow -
packages/opencode/src/cli/cmd/mcp.ts- PassredirectUrito 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.