anda_engine 0.18.2

Agents engine for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
//! OAuth authorization for MCP servers.
//!
//! Configuration types for the Authorization Code (interactive) and Client
//! Credentials (headless) flows, the [`McpCredentialStore`] persistence seam
//! the consuming application implements, and the error classification that
//! tells the session layer an authorization problem from a lifecycle problem.
//! `anda_engine` is a library: it drives the OAuth protocol but never opens a
//! browser, runs a callback server, or decides where tokens live.

use anda_core::{BoxError, Json};
use async_trait::async_trait;
use parking_lot::RwLock;
use rmcp::{
    service::ClientInitializeError,
    transport::{
        AuthError, AuthorizationManager, ClientCredentialsConfig, CredentialStore,
        StoredCredentials,
        auth::{
            AuthorizationCallback, AuthorizationMetadataSource, AuthorizationRequest,
            AuthorizationSession, OAuthClientConfig, OAuthState, WWWAuthenticateParams,
        },
    },
};
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
    time::{Duration, Instant},
};

use super::McpServerConfig;
use super::session::McpTransportConfig;

/// How far ahead of a client-credentials token's expiry to re-establish the session.
///
/// Long-lived tokens reconnect before rmcp's proactive refresh threshold.
pub(crate) const CLIENT_CREDENTIALS_RENEW_BUFFER: Duration = Duration::from_secs(120);

/// Computes the reconnect deadline for a client-credentials token.
///
/// Long-lived tokens renew 120 seconds early. Shorter tokens keep half their useful
/// lifetime, matching rmcp 3.11's capped half-lifetime refresh buffer. A remote
/// authorization server controls `expires_in`, so `checked_add` turns an unrepresentable duration
/// into "no local deadline" instead of panicking the process.
pub(crate) fn client_credentials_deadline(now: Instant, ttl: Duration) -> Option<Instant> {
    let buffer = CLIENT_CREDENTIALS_RENEW_BUFFER.min(ttl / 2);
    now.checked_add(ttl.saturating_sub(buffer))
}

/// OAuth 2.1 authorization for a Streamable HTTP MCP server.
///
/// `anda_engine` is a library: it drives the OAuth *protocol* and exposes the
/// seams, but never opens a browser, runs a callback server, or decides where
/// tokens live. The consuming application owns those concerns (see
/// [`McpToolProvider::begin_authorization`](super::McpToolProvider::begin_authorization) and [`McpCredentialStore`]).
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(tag = "flow", rename_all = "snake_case")]
pub enum McpOAuthConfig {
    /// Interactive Authorization Code flow with PKCE. Requires a one-time,
    /// out-of-band browser authorization that persists credentials; afterwards
    /// sessions are established from the stored refresh token with no human in
    /// the loop.
    AuthorizationCode(OAuthAuthorizationCodeConfig),
    /// Server-to-server Client Credentials flow (SEP-1046). Fully headless:
    /// tokens are obtained at connection time with no human interaction.
    ClientCredentials(OAuthClientCredentialsConfig),
}

impl McpOAuthConfig {
    pub(crate) fn validate(&self) -> Result<(), BoxError> {
        match self {
            Self::AuthorizationCode(config) => config.validate(),
            Self::ClientCredentials(config) => config.validate(),
        }
    }
}

/// Configuration for the interactive OAuth Authorization Code flow.
#[derive(Clone, Default, Deserialize, Serialize)]
pub struct OAuthAuthorizationCodeConfig {
    /// Redirect URI registered/used for the authorization request. The consuming
    /// application decides how the redirect is received (loopback, a server
    /// route, or manual paste) and passes the resulting URL back through
    /// [`McpToolProvider::complete_authorization`](super::McpToolProvider::complete_authorization).
    pub redirect_uri: String,
    /// Requested OAuth scopes. When empty, scopes are selected from the server's
    /// `WWW-Authenticate` challenge or protected resource metadata (and
    /// `offline_access` is requested when advertised by the authorization server).
    #[serde(default)]
    pub scopes: Vec<String>,
    /// Client name advertised during dynamic client registration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_name: Option<String>,
    /// Pre-registered `client_id`. Takes priority over CIMD and RFC 7591 dynamic
    /// client registration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    /// Optional pre-registered `client_secret` for confidential clients.
    /// Requires [`client_id`](Self::client_id); redacted from [`Debug`] output.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_secret: Option<String>,
    /// Optional HTTPS URL of a Client ID Metadata Document (SEP-991). Used when
    /// no pre-registered [`client_id`](Self::client_id) is set and the
    /// authorization server advertises `client_id_metadata_document_supported`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_metadata_url: Option<String>,
    /// Optional OIDC Dynamic Client Registration `application_type` (SEP-837),
    /// e.g. `"native"` or `"web"`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub application_type: Option<String>,
}

impl std::fmt::Debug for OAuthAuthorizationCodeConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OAuthAuthorizationCodeConfig")
            .field("redirect_uri", &self.redirect_uri)
            .field("scopes", &self.scopes)
            .field("client_name", &self.client_name)
            .field("client_id", &self.client_id)
            .field(
                "client_secret",
                &self.client_secret.as_ref().map(|_| "[REDACTED]"),
            )
            .field("client_metadata_url", &self.client_metadata_url)
            .field("application_type", &self.application_type)
            .finish()
    }
}

impl OAuthAuthorizationCodeConfig {
    /// Creates an Authorization Code flow configuration for `redirect_uri`.
    pub fn new(redirect_uri: impl Into<String>) -> Self {
        Self {
            redirect_uri: redirect_uri.into(),
            ..Self::default()
        }
    }

    pub(crate) fn validate(&self) -> Result<(), BoxError> {
        if self.redirect_uri.trim().is_empty() {
            return Err("MCP OAuth authorization_code redirect_uri must not be empty".into());
        }
        if let Some(secret) = &self.client_secret {
            if self
                .client_id
                .as_deref()
                .is_none_or(|id| id.trim().is_empty())
            {
                return Err("MCP OAuth authorization_code client_secret requires client_id".into());
            }
            if secret.trim().is_empty() {
                return Err("MCP OAuth authorization_code client_secret must not be empty".into());
            }
        }
        if let Some(metadata_url) = &self.client_metadata_url {
            let parsed = reqwest::Url::parse(metadata_url).map_err(|err| {
                format!("MCP OAuth authorization_code client_metadata_url is invalid: {err}")
            })?;
            if parsed.scheme() != "https"
                || !parsed.username().is_empty()
                || parsed.password().is_some()
                || parsed.fragment().is_some()
                || parsed.path() == "/"
                || parsed.path().is_empty()
            {
                return Err(
                    "MCP OAuth authorization_code client_metadata_url must be an HTTPS URL \
                     with a non-root path and no user info or fragment"
                        .into(),
                );
            }
        }
        if let Some(application_type) = &self.application_type
            && application_type.trim().is_empty()
        {
            return Err("MCP OAuth authorization_code application_type must not be empty".into());
        }
        Ok(())
    }
}

/// Configuration for the headless OAuth Client Credentials flow.
#[derive(Clone, Deserialize, Serialize)]
pub struct OAuthClientCredentialsConfig {
    /// Confidential client id.
    pub client_id: String,
    /// Confidential client secret.
    pub client_secret: String,
    /// Requested OAuth scopes.
    #[serde(default)]
    pub scopes: Vec<String>,
    /// Optional explicit resource indicator (RFC 8707).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resource: Option<String>,
}

// Custom `Debug` to keep the client secret out of logs and error output, matching
// how `rmcp` redacts its own credential types.
impl std::fmt::Debug for OAuthClientCredentialsConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OAuthClientCredentialsConfig")
            .field("client_id", &self.client_id)
            .field("client_secret", &"[REDACTED]")
            .field("scopes", &self.scopes)
            .field("resource", &self.resource)
            .finish()
    }
}

impl OAuthClientCredentialsConfig {
    pub(crate) fn validate(&self) -> Result<(), BoxError> {
        if self.client_id.trim().is_empty() {
            return Err("MCP OAuth client_credentials client_id must not be empty".into());
        }
        if self.client_secret.trim().is_empty() {
            return Err("MCP OAuth client_credentials client_secret must not be empty".into());
        }
        Ok(())
    }
}

/// Outcome of [`McpToolProvider::discover_http_oauth`](super::McpToolProvider::discover_http_oauth): the OAuth capabilities
/// an HTTP MCP endpoint advertises.
#[derive(Debug, Clone)]
pub struct McpOAuthMetadata {
    /// Scopes the authorization server advertises (may be empty).
    pub scopes_supported: Vec<String>,
    /// Whether the server supports dynamic client registration (RFC 7591).
    pub registration_supported: bool,
    /// Whether the authorization server advertises Client ID Metadata Document
    /// support (SEP-991 `client_id_metadata_document_supported`).
    pub client_id_metadata_document_supported: bool,
}

/// Pluggable persistence for MCP OAuth credentials, keyed by server id.
///
/// The library never decides where tokens live; the consuming application
/// supplies an implementation (e.g. backed by an encrypted store) through
/// [`McpToolProviderBuilder::credential_store`](super::McpToolProviderBuilder::credential_store). Refresh tokens are secrets and
/// must be persisted securely.
#[async_trait]
pub trait McpCredentialStore: Send + Sync {
    /// Loads the stored credentials for `server_id`, if any.
    async fn load(&self, server_id: &str) -> Result<Option<StoredCredentials>, BoxError>;
    /// Persists credentials for `server_id`, replacing any previous value.
    async fn save(&self, server_id: &str, credentials: StoredCredentials) -> Result<(), BoxError>;
    /// Removes any stored credentials for `server_id`.
    async fn clear(&self, server_id: &str) -> Result<(), BoxError>;
    /// Optionally handle the authorization server's definitive `invalid_grant`
    /// rejection of a refresh token.
    ///
    /// Called with the credentials used for that refresh attempt while the
    /// refresh guard (if any) is still held. Implementations must not reacquire
    /// that guard or overwrite credentials that have since been replaced.
    async fn on_refresh_token_rejected(
        &self,
        _server_id: &str,
        _credentials: &StoredCredentials,
    ) -> Result<(), BoxError> {
        Ok(())
    }
    /// Optionally lock a complete refresh transaction (load, exchange, save).
    /// Store methods must not reacquire this lock. Shared persistent backends
    /// should coordinate every writer, including sign-out, using the same authority.
    async fn acquire_refresh_guard(
        &self,
        _server_id: &str,
    ) -> Result<Option<rmcp::transport::auth::CredentialRefreshGuard>, BoxError> {
        Ok(None)
    }
}

/// Default in-memory [`McpCredentialStore`]. Credentials do not survive a
/// process restart; supply a persistent implementation in production.
#[derive(Debug, Default)]
pub struct InMemoryMcpCredentialStore {
    credentials: RwLock<HashMap<String, StoredCredentials>>,
    refresh_locks: RwLock<HashMap<String, Arc<tokio::sync::Mutex<()>>>>,
}

impl InMemoryMcpCredentialStore {
    /// Creates an empty in-memory credential store.
    pub fn new() -> Self {
        Self::default()
    }
}

#[async_trait]
impl McpCredentialStore for InMemoryMcpCredentialStore {
    async fn acquire_refresh_guard(
        &self,
        server_id: &str,
    ) -> Result<Option<rmcp::transport::auth::CredentialRefreshGuard>, BoxError> {
        let lock = self
            .refresh_locks
            .write()
            .entry(server_id.to_string())
            .or_default()
            .clone();
        Ok(Some(rmcp::transport::auth::CredentialRefreshGuard::new(
            lock.lock_owned().await,
        )))
    }
    async fn load(&self, server_id: &str) -> Result<Option<StoredCredentials>, BoxError> {
        Ok(self.credentials.read().get(server_id).cloned())
    }

    async fn save(&self, server_id: &str, credentials: StoredCredentials) -> Result<(), BoxError> {
        self.credentials
            .write()
            .insert(server_id.to_string(), credentials);
        Ok(())
    }

    async fn clear(&self, server_id: &str) -> Result<(), BoxError> {
        self.credentials.write().remove(server_id);
        Ok(())
    }
}

/// Adapts a keyed [`McpCredentialStore`] to rmcp's per-manager (keyless)
/// `CredentialStore`, bound to a single server id.
pub(crate) struct ScopedCredentialStore {
    pub(crate) server_id: String,
    pub(crate) inner: Arc<dyn McpCredentialStore>,
}

#[async_trait]
impl CredentialStore for ScopedCredentialStore {
    async fn acquire_refresh_guard(
        &self,
    ) -> Result<Option<rmcp::transport::auth::CredentialRefreshGuard>, AuthError> {
        self.inner
            .acquire_refresh_guard(&self.server_id)
            .await
            .map_err(|err| AuthError::CredentialStoreError(err.to_string()))
    }
    async fn load(&self) -> Result<Option<StoredCredentials>, AuthError> {
        self.inner
            .load(&self.server_id)
            .await
            .map_err(|err| AuthError::CredentialStoreError(err.to_string()))
    }

    async fn save(&self, credentials: StoredCredentials) -> Result<(), AuthError> {
        self.inner
            .save(&self.server_id, credentials)
            .await
            .map_err(|err| AuthError::CredentialStoreError(err.to_string()))
    }

    async fn clear(&self) -> Result<(), AuthError> {
        self.inner
            .clear(&self.server_id)
            .await
            .map_err(|err| AuthError::CredentialStoreError(err.to_string()))
    }

    async fn on_refresh_token_rejected(
        &self,
        credentials: &StoredCredentials,
    ) -> Result<(), AuthError> {
        self.inner
            .on_refresh_token_rejected(&self.server_id, credentials)
            .await
            .map_err(|err| AuthError::CredentialStoreError(err.to_string()))
    }
}

/// Error returned when establishing a session for a server configured with the
/// OAuth Authorization Code flow, but no usable stored credentials exist yet.
///
/// The consuming application should catch this (via [`BoxError`] downcast) and
/// run [`McpToolProvider::begin_authorization`](super::McpToolProvider::begin_authorization) /
/// [`McpToolProvider::complete_authorization`](super::McpToolProvider::complete_authorization) before retrying.
#[derive(Debug, Clone)]
pub struct McpAuthorizationRequired {
    /// The MCP server id that needs interactive authorization.
    pub server_id: String,
}

impl std::fmt::Display for McpAuthorizationRequired {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "MCP server {} requires interactive OAuth authorization; \
             call begin_authorization/complete_authorization first",
            self.server_id
        )
    }
}

impl std::error::Error for McpAuthorizationRequired {}

/// Whether a failed connection attempt was about credentials rather than the
/// lifecycle, in which case retrying with a different opener cannot help.
pub(crate) fn is_authorization_error(err: &(dyn std::error::Error + 'static)) -> bool {
    use rmcp::transport::streamable_http_client::StreamableHttpError;
    if let Some(service_err) = err.downcast_ref::<rmcp::service::ServiceError>() {
        if service_err.is_authorization_required() {
            return true;
        }
        if let rmcp::service::ServiceError::TransportSend(transport) = service_err {
            return is_authorization_error(transport.error.as_ref());
        }
    }
    if let Some(error) = err.downcast_ref::<ClientInitializeError>() {
        if error.is_authorization_required() {
            return true;
        }
        match error {
            ClientInitializeError::TransportError { error, .. } => {
                return is_authorization_error(error.error.as_ref());
            }
            ClientInitializeError::LegacyFallbackFailed { fallback, .. } => {
                return is_authorization_error(fallback.as_ref());
            }
            _ => {}
        }
    }
    if let Some(error) = err.downcast_ref::<StreamableHttpError<super::http_client::HttpError>>() {
        return matches!(
            error,
            StreamableHttpError::AuthRequired(_)
                | StreamableHttpError::InsufficientScope(_)
                | StreamableHttpError::Client(super::http_client::HttpError::Status(401 | 403))
                | StreamableHttpError::Auth(
                    AuthError::AuthorizationRequired | AuthError::TokenRefreshRejected(_)
                )
        );
    }
    if err.is::<McpAuthorizationRequired>() {
        return true;
    }
    // Credential acquisition reports a revoked or unusable grant directly, before
    // any transport exists; the handshake reports it wrapped in a transport error.
    matches!(
        err.downcast_ref::<AuthError>(),
        Some(AuthError::AuthorizationRequired | AuthError::TokenRefreshRejected(_))
    )
}

/// Extracts the `WWW-Authenticate` header from a 401 or 403 transport/service
/// error so interactive OAuth can proceed reactively or step up scopes.
pub(crate) fn extract_auth_challenge(err: &(dyn std::error::Error + 'static)) -> Option<String> {
    use rmcp::transport::streamable_http_client::{
        AuthRequiredError, InsufficientScopeError, StreamableHttpError,
    };
    if let Some(service_err) = err.downcast_ref::<rmcp::service::ServiceError>() {
        if let Some(challenge) = service_err.auth_challenge() {
            return Some(challenge.to_string());
        }
        if let rmcp::service::ServiceError::TransportSend(transport) = service_err {
            return extract_auth_challenge(transport.error.as_ref());
        }
    }
    if let Some(init_err) = err.downcast_ref::<ClientInitializeError>() {
        if let Some(challenge) = init_err.auth_challenge() {
            return Some(challenge.to_string());
        }
        match init_err {
            ClientInitializeError::TransportError { error, .. } => {
                return extract_auth_challenge(error.error.as_ref());
            }
            ClientInitializeError::LegacyFallbackFailed { fallback, .. } => {
                return extract_auth_challenge(fallback.as_ref());
            }
            _ => {}
        }
    }
    if let Some(http_err) = err.downcast_ref::<StreamableHttpError<super::http_client::HttpError>>()
        && let Some(challenge) = http_err.auth_challenge()
    {
        return Some(challenge.to_string());
    }
    let mut source: Option<&(dyn std::error::Error + 'static)> = Some(err);
    while let Some(current) = source {
        if let Some(auth_required) = current.downcast_ref::<AuthRequiredError>() {
            return Some(auth_required.www_authenticate_header.clone());
        }
        if let Some(insufficient_scope) = current.downcast_ref::<InsufficientScopeError>() {
            return Some(insufficient_scope.www_authenticate_header.clone());
        }
        source = current.source();
    }
    None
}

/// Extracts the `scope` parameter from a 403 `InsufficientScope` error, if present.
pub(crate) fn extract_required_scope(err: &(dyn std::error::Error + 'static)) -> Option<String> {
    use rmcp::transport::streamable_http_client::{InsufficientScopeError, StreamableHttpError};
    if let Some(rmcp::service::ServiceError::TransportSend(transport)) =
        err.downcast_ref::<rmcp::service::ServiceError>()
    {
        return extract_required_scope(transport.error.as_ref());
    }
    if let Some(init_err) = err.downcast_ref::<ClientInitializeError>() {
        match init_err {
            ClientInitializeError::TransportError { error, .. } => {
                return extract_required_scope(error.error.as_ref());
            }
            ClientInitializeError::LegacyFallbackFailed { fallback, .. } => {
                return extract_required_scope(fallback.as_ref());
            }
            _ => {}
        }
    }
    if let Some(StreamableHttpError::InsufficientScope(scope_err)) =
        err.downcast_ref::<StreamableHttpError<super::http_client::HttpError>>()
    {
        return scope_err.required_scope.clone();
    }
    let mut source: Option<&(dyn std::error::Error + 'static)> = Some(err);
    while let Some(current) = source {
        if let Some(scope_err) = current.downcast_ref::<InsufficientScopeError>() {
            return scope_err.required_scope.clone();
        }
        source = current.source();
    }
    None
}

/// Re-labels a credential failure on an interactive server as
/// [`McpAuthorizationRequired`], the signal applications are told to act on.
///
/// [`authorize_from_store`] raises that error when nothing is stored, but a grant
/// the authorization server has since revoked only shows up later, as a rejected
/// refresh or a `401` during the handshake. Both mean the same thing to the
/// caller โ€” run the interactive flow again โ€” so both are reported the same way.
/// The underlying cause is logged rather than dropped silently.
///
/// [`authorize_from_store`]: McpToolProvider::authorize_from_store
pub(crate) fn authorization_required_hint(config: &McpServerConfig, err: BoxError) -> BoxError {
    if err.is::<McpAuthorizationRequired>() || !is_authorization_error(err.as_ref()) {
        return err;
    }
    let McpTransportConfig::StreamableHttp(http) = &config.transport else {
        return err;
    };
    if !matches!(&http.auth, Some(McpOAuthConfig::AuthorizationCode(_))) {
        return err;
    }

    log::warn!(
        "MCP server {}: stored authorization is no longer usable ({err}); interactive \
         authorization must run again",
        config.id
    );
    McpAuthorizationRequired {
        server_id: config.id.clone(),
    }
    .into()
}

/// Probes an HTTP MCP endpoint to determine whether it requires OAuth.
///
/// Performs RFC 9728 protected-resource / RFC 8414 authorization-server
/// discovery against `url`. Returns `None` when the endpoint advertises no
/// OAuth support (it uses a static bearer token or no auth), or `Some` with
/// the discovered metadata otherwise.
pub(crate) async fn discover_http_oauth(url: &str) -> Result<Option<McpOAuthMetadata>, BoxError> {
    let manager = AuthorizationManager::new(url).await?;
    let resolution = manager.resolve_metadata().await?;
    if resolution.source == AuthorizationMetadataSource::LegacyEndpointFallback {
        return Ok(None);
    }
    let metadata = resolution.metadata;
    let client_id_metadata_document_supported = metadata
        .additional_fields
        .get("client_id_metadata_document_supported")
        .and_then(Json::as_bool)
        .unwrap_or(false);
    Ok(Some(McpOAuthMetadata {
        scopes_supported: metadata.scopes_supported.unwrap_or_default(),
        registration_supported: metadata.registration_endpoint.is_some(),
        client_id_metadata_document_supported,
    }))
}

fn merge_unique_scopes(groups: impl IntoIterator<Item = Vec<String>>) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut merged = Vec::new();
    for group in groups {
        for scope in group {
            if !scope.is_empty() && seen.insert(scope.clone()) {
                merged.push(scope);
            }
        }
    }
    merged
}

/// Builds the manager for one interactive Authorization Code flow and returns
/// it with the authorization URL to open in a browser.
///
/// Resolves the server's OAuth metadata (seeded from `challenge` when present),
/// then follows MCP's registration priority via [`AuthorizationSession::new`]:
/// pre-registered client credentials, Client ID Metadata Documents (SEP-991),
/// and finally dynamic client registration (RFC 7591). On `insufficient_scope`
/// challenges (SEP-2350), previously granted scopes are preserved cumulatively.
/// The returned manager holds the PKCE/CSRF state consumed by
/// [`complete_authorization_exchange`].
pub(crate) async fn begin_authorization_manager(
    url: &str,
    ac: &OAuthAuthorizationCodeConfig,
    store: ScopedCredentialStore,
    challenge: Option<&str>,
) -> Result<(AuthorizationManager, String), BoxError> {
    let prior_scopes = store
        .load()
        .await?
        .map(|stored| stored.granted_scopes)
        .unwrap_or_default();

    let mut manager = AuthorizationManager::new(url).await?;
    manager.set_credential_store(store);
    let metadata = manager
        .resolve_metadata_from_challenge(challenge)
        .await?
        .metadata;
    manager.set_metadata(metadata);

    let mut request = AuthorizationRequest::new(ac.redirect_uri.clone())
        .with_client_name(ac.client_name.as_deref().unwrap_or("Anda Engine MCP Host"));
    if let Some(client_id) = &ac.client_id {
        request = request.with_preregistered_client(client_id.clone());
        if let Some(client_secret) = &ac.client_secret {
            request = request.with_client_secret(client_secret.clone());
        }
    }
    if let Some(client_metadata_url) = &ac.client_metadata_url {
        request = request.with_client_metadata_url(client_metadata_url.clone());
    }
    if let Some(application_type) = &ac.application_type {
        request = request.with_application_type(application_type.clone());
    }

    let challenge_params = challenge.and_then(|header| {
        reqwest::Url::parse(url)
            .ok()
            .map(|base_url| WWWAuthenticateParams::parse(header, &base_url))
    });
    let is_step_up = challenge_params
        .as_ref()
        .is_some_and(WWWAuthenticateParams::is_insufficient_scope);
    let challenged_scopes: Vec<String> = challenge_params
        .as_ref()
        .and_then(|params| params.scope.as_deref())
        .map(|scope| scope.split_whitespace().map(str::to_string).collect())
        .unwrap_or_default();

    if is_step_up {
        let merged = merge_unique_scopes([prior_scopes, ac.scopes.clone(), challenged_scopes]);
        if !merged.is_empty() {
            request = request.with_scopes(merged);
        }
    } else if !ac.scopes.is_empty() {
        request = request.with_scopes(ac.scopes.clone());
    } else {
        request = request.with_initial_scope_selection();
    }

    let session = AuthorizationSession::new(manager, request)
        .await
        .map_err(|(_, err)| BoxError::from(err))?;
    Ok((session.auth_manager, session.auth_url))
}

/// Completes the Authorization Code exchange using the full redirect URL the
/// authorization server sent back (carrying `code`, `state`, and optionally
/// RFC 9207 `iss`). On success the credentials are persisted through the
/// manager's credential store.
pub(crate) async fn complete_authorization_exchange(
    manager: AuthorizationManager,
    redirect_url: &str,
) -> Result<(), BoxError> {
    let callback = AuthorizationCallback::from_redirect_url(redirect_url)?;
    manager
        .exchange_code_for_token_with_issuer(
            &callback.code,
            &callback.csrf_token,
            callback.issuer.as_deref(),
        )
        .await?;
    Ok(())
}

/// Rebuilds an authorized manager from persisted Authorization Code
/// credentials, refreshing on demand. Errors with [`McpAuthorizationRequired`]
/// when no usable credentials exist yet, so the caller can trigger the
/// interactive flow.
pub(crate) async fn authorize_from_store(
    server_id: &str,
    url: &str,
    ac: &OAuthAuthorizationCodeConfig,
    store: ScopedCredentialStore,
    challenge: Option<&str>,
) -> Result<AuthorizationManager, BoxError> {
    let mut manager = AuthorizationManager::new(url).await?;
    manager.set_credential_store(store);
    if challenge.is_some() {
        let resolution = manager.resolve_metadata_from_challenge(challenge).await?;
        manager.set_metadata(resolution.metadata);
    }
    if !manager.initialize_from_store().await? {
        return Err(McpAuthorizationRequired {
            server_id: server_id.to_string(),
        }
        .into());
    }
    // `StoredCredentials` does not persist confidential client secrets; reapply
    // the configured secret so token refreshes authenticate properly.
    if let Some(client_secret) = &ac.client_secret {
        let client_id = match &ac.client_id {
            Some(client_id) => client_id.clone(),
            None => manager.get_credentials().await?.0,
        };
        let mut cfg = OAuthClientConfig::new(client_id, ac.redirect_uri.clone())
            .with_client_secret(client_secret.clone());
        if let Some(application_type) = &ac.application_type {
            cfg = cfg.with_application_type(application_type.clone());
        }
        manager.configure_client(cfg)?;
    }
    Ok(manager)
}

/// Obtains an authorized manager via the headless Client Credentials flow and
/// reports when the token expires.
///
/// RFC 6749 ยง4.4.3 says a client-credentials grant SHOULD NOT issue a refresh token, and
/// rmcp's only renewal path is `refresh_token`. Once the access token expires, every
/// request fails with `AuthorizationRequired` and nothing re-runs this exchange. The
/// returned deadline lets the session expire itself slightly early so the caller
/// reconnects and mints a fresh token instead of failing permanently.
pub(crate) async fn authorize_client_credentials(
    url: &str,
    config: &OAuthClientCredentialsConfig,
) -> Result<(AuthorizationManager, Option<Instant>), BoxError> {
    let mut state = OAuthState::new(url, None).await?;
    state
        .authenticate_client_credentials(ClientCredentialsConfig::ClientSecret {
            client_id: config.client_id.clone(),
            client_secret: config.client_secret.clone(),
            scopes: config.scopes.clone(),
            resource: config.resource.clone(),
        })
        .await?;

    // Renew before rmcp's proactive refresh would require a new grant. `expires_in` is
    // read through the token response's `Serialize` impl so this does not depend on
    // `oauth2` directly, which would risk a version skew with the one rmcp uses.
    let expires_at = match state.get_credentials().await {
        Ok((_, Some(token))) => serde_json::to_value(&token)
            .ok()
            .and_then(|token| token.get("expires_in").and_then(Json::as_u64))
            .and_then(|secs| {
                client_credentials_deadline(Instant::now(), Duration::from_secs(secs))
            }),
        _ => None,
    };

    let manager = state
        .into_authorization_manager()
        .ok_or("MCP client_credentials authorization did not complete")?;
    Ok((manager, expires_at))
}