Skip to content

Commit e868f8f

Browse files
feat: Add onPopupWindow callback and update README
1 parent c3ee0e9 commit e868f8f

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ function useMcp(options: UseMcpOptions): UseMcpResult
227227
| `autoReconnect` | `boolean \| number` | Auto reconnect if an established connection is lost, with delay in ms (default: 3000) |
228228
| `transportType` | `'auto' \| 'http' \| 'sse'` | Transport type preference: 'auto' (HTTP with SSE fallback), 'http' (HTTP only), 'sse' (SSE only) (default: 'auto') |
229229
| `preventAutoAuth` | `boolean` | Prevent automatic authentication popup on initial connection (default: false) |
230+
| `onPopupWindow` | `(url: string, features: string) => void` | Callback invoked just before the authentication popup window is opened |
230231
231232
#### Return Value
232233

src/auth/browser-provider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
1515
readonly clientName: string
1616
readonly clientUri: string
1717
readonly callbackUrl: string
18+
readonly onPopupWindow: ((url: string, features: string) => void) | undefined
1819

1920
constructor(
2021
serverUrl: string,
@@ -23,6 +24,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
2324
clientName?: string
2425
clientUri?: string
2526
callbackUrl?: string
27+
onPopupWindow?: (url: string, features: string) => void
2628
} = {},
2729
) {
2830
this.serverUrl = serverUrl
@@ -34,6 +36,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
3436
options.callbackUrl ||
3537
(typeof window !== 'undefined' ? new URL('/oauth/callback', window.location.origin).toString() : '/oauth/callback'),
3638
)
39+
this.onPopupWindow = options.onPopupWindow
3740
}
3841

3942
// --- SDK Interface Methods ---
@@ -167,6 +170,10 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
167170
// Attempt to open the popup
168171
const popupFeatures = 'width=600,height=700,resizable=yes,scrollbars=yes,status=yes' // Make configurable if needed
169172
try {
173+
// If a callback is provided, invoke it before opening the popup
174+
if (this.onPopupWindow) {
175+
this.onPopupWindow(sanitizedAuthUrl, popupFeatures)
176+
}
170177
const popup = window.open(sanitizedAuthUrl, `mcp_auth_${this.serverUrlHash}`, popupFeatures)
171178

172179
if (!popup || popup.closed || typeof popup.closed === 'undefined') {

src/react/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export type UseMcpOptions = {
3030
transportType?: 'auto' | 'http' | 'sse'
3131
/** Prevent automatic authentication popup on initial connection (default: false) */
3232
preventAutoAuth?: boolean
33+
/**
34+
* Callback function that is invoked just before the authentication popup window is opened.
35+
* @param url The URL that will be opened in the popup.
36+
* @param features The features string for the popup window.
37+
*/
38+
onPopupWindow?: (url: string, features: string) => void
3339
}
3440

3541
export type UseMcpResult = {

src/react/useMcp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
4747
autoReconnect = DEFAULT_RECONNECT_DELAY,
4848
transportType = 'auto',
4949
preventAutoAuth = false,
50+
onPopupWindow,
5051
} = options
5152

5253
const [state, setState] = useState<UseMcpResult['state']>('discovering')
@@ -176,6 +177,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
176177
clientName,
177178
clientUri,
178179
callbackUrl,
180+
onPopupWindow,
179181
})
180182
addLog('debug', 'BrowserOAuthClientProvider initialized in connect.')
181183
}
@@ -779,6 +781,7 @@ export function useMcp(options: UseMcpOptions): UseMcpResult {
779781
clientName,
780782
clientUri,
781783
callbackUrl,
784+
onPopupWindow,
782785
})
783786
addLog('debug', 'BrowserOAuthClientProvider initialized/updated on mount/option change.')
784787
}

0 commit comments

Comments
 (0)