feat: Add onPopupWindow callback and update README (#32)

* feat: Add onPopupWindow callback and update README

* feat: Pass window reference to onPopupWindow callback

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Glen Maddern 2025-07-24 17:23:52 +10:00 • committed by GitHub
commit 043efbcf12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,6 +15,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
readonly clientName: string
readonly clientUri: string
readonly callbackUrl: string
readonly onPopupWindow: ((url: string, features: string, window: Window | null) => void) | undefined
constructor(
serverUrl: string,
@ -23,6 +24,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
clientName?: string
clientUri?: string
callbackUrl?: string
onPopupWindow?: (url: string, features: string, window: Window | null) => void
} = {},
) {
this.serverUrl = serverUrl
@ -34,6 +36,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
options.callbackUrl ||
(typeof window !== 'undefined' ? new URL('/oauth/callback', window.location.origin).toString() : '/oauth/callback'),
)
this.onPopupWindow = options.onPopupWindow
}
// --- SDK Interface Methods ---
@ -169,6 +172,11 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
try {
const popup = window.open(sanitizedAuthUrl, `mcp_auth_${this.serverUrlHash}`, popupFeatures)
// If a callback is provided, invoke it after opening the popup
if (this.onPopupWindow) {
this.onPopupWindow(sanitizedAuthUrl, popupFeatures, popup)
}
if (!popup || popup.closed || typeof popup.closed === 'undefined') {
console.warn(
`[${this.storageKeyPrefix}] Popup likely blocked by browser. Manual navigation might be required using the stored URL.`,