Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ function useMcp(options: UseMcpOptions): UseMcpResult
| `autoReconnect` | `boolean \| number` | Auto reconnect if an established connection is lost, with delay in ms (default: 3000) |
| `transportType` | `'auto' \| 'http' \| 'sse'` | Transport type preference: 'auto' (HTTP with SSE fallback), 'http' (HTTP only), 'sse' (SSE only) (default: 'auto') |
| `preventAutoAuth` | `boolean` | Prevent automatic authentication popup on initial connection (default: false) |
| `onPopupWindow` | `(url: string, features: string, window: Window \| null) => void` | Callback invoked just after the authentication popup window is opened |

#### Return Value

Expand Down
8 changes: 8 additions & 0 deletions src/auth/browser-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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 ---
Expand Down Expand Up @@ -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.`,
Expand Down
6 changes: 6 additions & 0 deletions src/react/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export type UseMcpOptions = {
transportType?: 'auto' | 'http' | 'sse'
/** Prevent automatic authentication popup on initial connection (default: false) */
preventAutoAuth?: boolean
/**
* Callback function that is invoked just before the authentication popup window is opened.
* @param url The URL that will be opened in the popup.
* @param features The features string for the popup window.
*/
onPopupWindow?: (url: string, features: string, window: Window | null) => void
}

export type UseMcpResult = {
Expand Down
3 changes: 3 additions & 0 deletions src/react/useMcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
autoReconnect = DEFAULT_RECONNECT_DELAY,
transportType = 'auto',
preventAutoAuth = false,
onPopupWindow,
} = options

const [state, setState] = useState<UseMcpResult['state']>('discovering')
Expand Down Expand Up @@ -176,6 +177,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
clientName,
clientUri,
callbackUrl,
onPopupWindow,
})
addLog('debug', 'BrowserOAuthClientProvider initialized in connect.')
}
Expand Down Expand Up @@ -779,6 +781,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
clientName,
clientUri,
callbackUrl,
onPopupWindow,
})
addLog('debug', 'BrowserOAuthClientProvider initialized/updated on mount/option change.')
}
Expand Down
Loading