Skip to content

Commit ab38317

Browse files
feat: Pass window reference to onPopupWindow callback
1 parent e868f8f commit ab38317

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +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 |
230+
| `onPopupWindow` | `(url: string, features: string, window: Window \| null) => void` | Callback invoked just after the authentication popup window is opened |
231231
232232
#### Return Value
233233

src/auth/browser-provider.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +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
18+
readonly onPopupWindow: ((url: string, features: string, window: Window | null) => void) | undefined
1919

2020
constructor(
2121
serverUrl: string,
@@ -24,7 +24,7 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
2424
clientName?: string
2525
clientUri?: string
2626
callbackUrl?: string
27-
onPopupWindow?: (url: string, features: string) => void
27+
onPopupWindow?: (url: string, features: string, window: Window | null) => void
2828
} = {},
2929
) {
3030
this.serverUrl = serverUrl
@@ -170,11 +170,12 @@ export class BrowserOAuthClientProvider implements OAuthClientProvider {
170170
// Attempt to open the popup
171171
const popupFeatures = 'width=600,height=700,resizable=yes,scrollbars=yes,status=yes' // Make configurable if needed
172172
try {
173-
// If a callback is provided, invoke it before opening the popup
173+
const popup = window.open(sanitizedAuthUrl, `mcp_auth_${this.serverUrlHash}`, popupFeatures)
174+
175+
// If a callback is provided, invoke it after opening the popup
174176
if (this.onPopupWindow) {
175-
this.onPopupWindow(sanitizedAuthUrl, popupFeatures)
177+
this.onPopupWindow(sanitizedAuthUrl, popupFeatures, popup)
176178
}
177-
const popup = window.open(sanitizedAuthUrl, `mcp_auth_${this.serverUrlHash}`, popupFeatures)
178179

179180
if (!popup || popup.closed || typeof popup.closed === 'undefined') {
180181
console.warn(

src/react/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type UseMcpOptions = {
3535
* @param url The URL that will be opened in the popup.
3636
* @param features The features string for the popup window.
3737
*/
38-
onPopupWindow?: (url: string, features: string) => void
38+
onPopupWindow?: (url: string, features: string, window: Window | null) => void
3939
}
4040

4141
export type UseMcpResult = {

0 commit comments

Comments
 (0)