@@ -14,7 +14,7 @@ use reqwest::{Client as HttpClient, IntoUrl, StatusCode, Url, header::AUTHORIZAT
14
14
use serde:: { Deserialize , Serialize } ;
15
15
use thiserror:: Error ;
16
16
use tokio:: sync:: { Mutex , RwLock } ;
17
- use tracing:: { debug, error} ;
17
+ use tracing:: { debug, error, warn } ;
18
18
19
19
const DEFAULT_EXCHANGE_URL : & str = "http://localhost" ;
20
20
@@ -102,7 +102,7 @@ pub enum AuthError {
102
102
pub struct AuthorizationMetadata {
103
103
pub authorization_endpoint : String ,
104
104
pub token_endpoint : String ,
105
- pub registration_endpoint : String ,
105
+ pub registration_endpoint : Option < String > ,
106
106
pub issuer : Option < String > ,
107
107
pub jwks_uri : Option < String > ,
108
108
pub scopes_supported : Option < Vec < String > > ,
@@ -273,7 +273,7 @@ impl AuthorizationManager {
273
273
return Ok ( metadata) ;
274
274
}
275
275
276
- debug ! ( "No valid .well-known endpoint found, falling back to default endpoints" ) ;
276
+ warn ! ( "No valid .well-known endpoint found, falling back to default endpoints" ) ;
277
277
278
278
// fallback to default endpoints
279
279
let mut auth_base = self . base_url . clone ( ) ;
@@ -290,7 +290,7 @@ impl AuthorizationManager {
290
290
Ok ( AuthorizationMetadata {
291
291
authorization_endpoint : create_endpoint ( "authorize" ) ,
292
292
token_endpoint : create_endpoint ( "token" ) ,
293
- registration_endpoint : create_endpoint ( "register" ) ,
293
+ registration_endpoint : None ,
294
294
issuer : None ,
295
295
jwks_uri : None ,
296
296
scopes_supported : None ,
@@ -323,12 +323,10 @@ impl AuthorizationManager {
323
323
let token_url = TokenUrl :: new ( metadata. token_endpoint . clone ( ) )
324
324
. map_err ( |e| AuthError :: OAuthError ( format ! ( "Invalid token URL: {}" , e) ) ) ?;
325
325
326
- // debug!("token url: {:?}", token_url);
327
326
let client_id = ClientId :: new ( config. client_id ) ;
328
327
let redirect_url = RedirectUrl :: new ( config. redirect_uri . clone ( ) )
329
328
. map_err ( |e| AuthError :: OAuthError ( format ! ( "Invalid re URL: {}" , e) ) ) ?;
330
329
331
- debug ! ( "client_id: {:?}" , client_id) ;
332
330
let mut client_builder = BasicClient :: new ( client_id. clone ( ) )
333
331
. set_auth_uri ( auth_url)
334
332
. set_token_uri ( token_url)
@@ -349,14 +347,16 @@ impl AuthorizationManager {
349
347
redirect_uri : & str ,
350
348
) -> Result < OAuthClientConfig , AuthError > {
351
349
if self . metadata . is_none ( ) {
352
- error ! ( "No authorization support detected" ) ;
353
350
return Err ( AuthError :: NoAuthorizationSupport ) ;
354
351
}
355
352
356
353
let metadata = self . metadata . as_ref ( ) . unwrap ( ) ;
357
- let registration_url = metadata. registration_endpoint . clone ( ) ;
354
+ let Some ( registration_url) = metadata. registration_endpoint . as_ref ( ) else {
355
+ return Err ( AuthError :: RegistrationFailed (
356
+ "Dynamic client registration not supported" . to_string ( ) ,
357
+ ) ) ;
358
+ } ;
358
359
359
- debug ! ( "registration url: {:?}" , registration_url) ;
360
360
// prepare registration request
361
361
let registration_request = ClientRegistrationRequest {
362
362
client_name : name. to_string ( ) ,
@@ -369,8 +369,6 @@ impl AuthorizationManager {
369
369
response_types : vec ! [ "code" . to_string( ) ] ,
370
370
} ;
371
371
372
- debug ! ( "registration request: {:?}" , registration_request) ;
373
-
374
372
let response = match self
375
373
. http_client
376
374
. post ( registration_url)
@@ -380,7 +378,6 @@ impl AuthorizationManager {
380
378
{
381
379
Ok ( response) => response,
382
380
Err ( e) => {
383
- error ! ( "Registration request failed: {}" , e) ;
384
381
return Err ( AuthError :: RegistrationFailed ( format ! (
385
382
"HTTP request error: {}" ,
386
383
e
@@ -395,7 +392,6 @@ impl AuthorizationManager {
395
392
Err ( _) => "cannot get error details" . to_string ( ) ,
396
393
} ;
397
394
398
- error ! ( "Registration failed: HTTP {} - {}" , status, error_text) ;
399
395
return Err ( AuthError :: RegistrationFailed ( format ! (
400
396
"HTTP {}: {}" ,
401
397
status, error_text
@@ -406,7 +402,6 @@ impl AuthorizationManager {
406
402
let reg_response = match response. json :: < ClientRegistrationResponse > ( ) . await {
407
403
Ok ( response) => response,
408
404
Err ( e) => {
409
- error ! ( "Failed to parse registration response: {}" , e) ;
410
405
return Err ( AuthError :: RegistrationFailed ( format ! (
411
406
"analyze response error: {}" ,
412
407
e
@@ -471,7 +466,6 @@ impl AuthorizationManager {
471
466
pkce_verifier,
472
467
csrf_token,
473
468
} ) ;
474
- debug ! ( "set authorization state: {:?}" , self . state. read( ) . await ) ;
475
469
476
470
Ok ( auth_url. to_string ( ) )
477
471
}
@@ -624,9 +618,9 @@ impl AuthorizationSession {
624
618
scopes : & [ & str ] ,
625
619
redirect_uri : & str ,
626
620
) -> Result < Self , AuthError > {
627
- // set redirect uri
621
+ // Defualt client config
628
622
let config = OAuthClientConfig {
629
- client_id : "mcp-client" . to_string ( ) , // temporary id, will be updated by dynamic registration
623
+ client_id : "mcp-client" . to_string ( ) ,
630
624
client_secret : None ,
631
625
scopes : scopes. iter ( ) . map ( |s| s. to_string ( ) ) . collect ( ) ,
632
626
redirect_uri : redirect_uri. to_string ( ) ,
@@ -639,7 +633,10 @@ impl AuthorizationSession {
639
633
{
640
634
Ok ( config) => config,
641
635
Err ( e) => {
642
- eprintln ! ( "Dynamic registration failed: {}" , e) ;
636
+ warn ! (
637
+ "Dynamic registration failed: {}, fallback to default config" ,
638
+ e
639
+ ) ;
643
640
// fallback to default config
644
641
config
645
642
}
0 commit comments