@@ -497,6 +497,16 @@ class StreamableHttpClientTransport implements Transport {
497
497
return ;
498
498
}
499
499
500
+ // Check for authentication first - if we need auth, handle it before proceeding
501
+ if (_authProvider != null ) {
502
+ final tokens = await _authProvider! .tokens ();
503
+ if (tokens == null ) {
504
+ // No tokens available - trigger authentication flow
505
+ await _authProvider! .redirectToAuthorization ();
506
+ throw UnauthorizedError ('Authentication required' );
507
+ }
508
+ }
509
+
500
510
final headers = await _commonHeaders ();
501
511
headers['content-type' ] = 'application/json' ;
502
512
headers['accept' ] = 'application/json, text/event-stream' ;
@@ -523,13 +533,9 @@ class StreamableHttpClientTransport implements Transport {
523
533
524
534
if (response.statusCode < 200 || response.statusCode >= 300 ) {
525
535
if (response.statusCode == 401 && _authProvider != null ) {
526
- final result = await auth (_authProvider! , serverUrl: _url);
527
- if (result != "AUTHORIZED" ) {
528
- throw UnauthorizedError ();
529
- }
530
-
531
- // Purposely _not_ awaited, so we don't call onerror twice
532
- return send (message);
536
+ // Authentication failed with the server - try to refresh or redirect
537
+ await _authProvider! .redirectToAuthorization ();
538
+ throw UnauthorizedError ('Authentication failed with the server' );
533
539
}
534
540
535
541
final text = await response.transform (utf8.decoder).join ();
0 commit comments