@@ -407,27 +407,52 @@ mcpTest.instance(
407407// altimate_change start — finishing an OAuth flow continues the call that began it: a disconnect
408408// or remove issued after startAuth is the later call, so finishAuth must not connect. (review)
409409mcpTest . instance (
410- "a disconnect after startAuth keeps finishAuth from exchanging the code or connecting " ,
410+ "a connect issued after startAuth wins: the late finishAuth exchanges nothing " ,
411411 ( ) =>
412412 MCP . Service . use ( ( mcp ) =>
413413 Effect . gen ( function * ( ) {
414414 yield * mcp . add ( "test-oauth-late-finish" , { type : "remote" , url : "https://example.com/mcp" } )
415415 const started = yield * mcp . startAuth ( "test-oauth-late-finish" )
416416 expect ( started . authorizationUrl ) . toBeTruthy ( )
417417
418- yield * mcp . disconnect ( "test-oauth-late-finish" )
418+ // A newer call that leaves the pending flow alone: the server connects without auth.
419419 simulateAuthFlow = false
420420 connectSucceedsImmediately = true
421- const result = yield * mcp . finishAuth ( "test-oauth-late-finish" , "code" )
421+ yield * mcp . connect ( "test-oauth-late-finish" )
422+ expect ( ( yield * mcp . status ( ) ) [ "test-oauth-late-finish" ] ?. status ) . toBe ( "connected" )
422423
423- expect ( result . status ) . not . toBe ( "connected" )
424+ const result = yield * mcp . finishAuth ( "test-oauth-late-finish" , "code" )
425+ expect ( result . status ) . toBe ( "connected" )
424426 expect ( tokenExchanges ) . toBe ( 0 )
425- expect ( ( yield * mcp . clients ( ) ) [ "test-oauth-late-finish" ] ) . toBeUndefined ( )
426427 } ) ,
427428 ) ,
428429 { config : config ( "test-oauth-late-finish" ) } ,
429430)
430431
432+ mcpTest . instance (
433+ "a disconnect drops a pending transport a connect left behind, so finishAuth has nothing to finish" ,
434+ ( ) =>
435+ MCP . Service . use ( ( mcp ) =>
436+ Effect . gen ( function * ( ) {
437+ // The connect finds the server needs auth and keeps its transport, with no flow recorded.
438+ const added = yield * mcp . add ( "test-oauth-connect-left" , { type : "remote" , url : "https://example.com/mcp" } )
439+ expect ( ( added . status as Record < string , { status : string } > ) [ "test-oauth-connect-left" ] ?. status ) . toBe ( "needs_auth" )
440+ yield * mcp . disconnect ( "test-oauth-connect-left" )
441+
442+ simulateAuthFlow = false
443+ connectSucceedsImmediately = true
444+ const error = yield * mcp . finishAuth ( "test-oauth-connect-left" , "code" ) . pipe (
445+ Effect . flip ,
446+ Effect . catchDefect ( ( defect ) => Effect . succeed ( defect ) ) ,
447+ )
448+ expect ( String ( error ) ) . toContain ( "No pending OAuth flow" )
449+ expect ( tokenExchanges ) . toBe ( 0 )
450+ expect ( ( yield * mcp . clients ( ) ) [ "test-oauth-connect-left" ] ) . toBeUndefined ( )
451+ } ) ,
452+ ) ,
453+ { config : config ( "test-oauth-connect-left" ) } ,
454+ )
455+
431456mcpTest . instance (
432457 "a remove after startAuth drops the pending flow, so finishAuth has nothing to finish" ,
433458 ( ) =>
0 commit comments