@@ -159,6 +159,59 @@ export async function connectToMCPServer(
159
159
// Wait for connection attempt to complete
160
160
await page . waitForTimeout ( 1000 )
161
161
162
+ // Check for OAuth popup and handle it - try multiple times to catch different OAuth flows
163
+ let oauthHandled = false
164
+ for ( let popupAttempt = 0 ; popupAttempt < 5 && ! oauthHandled ; popupAttempt ++ ) {
165
+ try {
166
+ // Look for OAuth popup by checking for new pages
167
+ const context = page . context ( )
168
+ const initialPages = context . pages ( )
169
+
170
+ // Wait a bit to see if OAuth popup appears
171
+ await page . waitForTimeout ( 1000 )
172
+
173
+ const currentPages = context . pages ( )
174
+ if ( currentPages . length > initialPages . length ) {
175
+ // OAuth popup appeared, find it
176
+ const popup = currentPages . find ( ( p ) => p !== page && p . url ( ) . includes ( '/authorize' ) )
177
+ if ( popup ) {
178
+ console . log ( '🔐 OAuth popup detected, clicking Approve button...' )
179
+ // Wait for the approval form to load
180
+ await popup . waitForSelector ( 'button.approve' , { timeout : 5000 } )
181
+ // Click the Approve button
182
+ await popup . click ( 'button.approve' )
183
+ console . log ( '✅ Clicked Approve button' )
184
+ // Wait for popup to close
185
+ await popup . waitForEvent ( 'close' , { timeout : 10000 } )
186
+ console . log ( '🔒 OAuth popup closed' )
187
+ oauthHandled = true
188
+ break
189
+ }
190
+ }
191
+
192
+ // Also check if there's a popup that opened that we haven't detected yet
193
+ const allPages = context . pages ( )
194
+ for ( const possiblePopup of allPages ) {
195
+ if ( possiblePopup !== page && possiblePopup . url ( ) . includes ( '/authorize' ) ) {
196
+ console . log ( '🔐 Found OAuth popup on retry, clicking Approve button...' )
197
+ await possiblePopup . waitForSelector ( 'button.approve' , { timeout : 5000 } )
198
+ await possiblePopup . click ( 'button.approve' )
199
+ console . log ( '✅ Clicked Approve button' )
200
+ await possiblePopup . waitForEvent ( 'close' , { timeout : 10000 } )
201
+ console . log ( '🔒 OAuth popup closed' )
202
+ oauthHandled = true
203
+ break
204
+ }
205
+ }
206
+ } catch ( e ) {
207
+ console . log ( `ℹ️ OAuth popup attempt ${ popupAttempt + 1 } failed:` , e . message )
208
+ }
209
+ }
210
+
211
+ if ( ! oauthHandled ) {
212
+ console . log ( 'ℹ️ No OAuth popup detected after multiple attempts' )
213
+ }
214
+
162
215
// Check for connection status
163
216
let attempts = 0
164
217
const maxAttempts = 20
0 commit comments