pmcp 2.2.0

High-quality Rust SDK for Model Context Protocol (MCP) with full TypeScript SDK compatibility
Documentation
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
//! JWT token validator with JWKS support.
//!
//! This module provides stateless JWT validation using JSON Web Key Sets (JWKS).
//! It supports all major OAuth providers through configuration.
//!
//! # Feature Flag
//!
//! This module requires the `jwt-auth` feature:
//!
//! ```toml
//! [dependencies]
//! pmcp = { version = "1.8", features = ["jwt-auth"] }
//! ```

use super::config::JwtValidatorConfig;
use super::traits::{AuthContext, ClaimMappings, TokenValidator};
use crate::error::{Error, ErrorCode, Result};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Cached JWKS keys with expiration.
struct CachedJwks {
    #[cfg(feature = "jwt-auth")]
    keys: HashMap<String, jsonwebtoken::DecodingKey>,
    #[cfg(not(feature = "jwt-auth"))]
    keys: HashMap<String, ()>,
    fetched_at: Instant,
    ttl: Duration,
}

impl std::fmt::Debug for CachedJwks {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CachedJwks")
            .field("keys_count", &self.keys.len())
            .field("fetched_at", &self.fetched_at)
            .field("ttl", &self.ttl)
            .finish()
    }
}

impl CachedJwks {
    fn new(ttl: Duration) -> Self {
        Self {
            keys: HashMap::new(),
            fetched_at: Instant::now(),
            ttl,
        }
    }

    #[cfg(all(feature = "jwt-auth", not(target_arch = "wasm32")))]
    fn is_expired(&self) -> bool {
        self.fetched_at.elapsed() > self.ttl
    }
}

/// JWT validator using JWKS for stateless token validation.
///
/// This validator fetches and caches the OAuth provider's public keys (JWKS)
/// and uses them to verify JWT signatures locally, without making a request
/// to the auth server for each token validation.
///
/// # Example
///
/// ```rust,ignore
/// use pmcp::server::auth::{JwtValidator, TokenValidator};
/// use pmcp::server::auth::config::JwtValidatorConfig;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Create validator for AWS Cognito
/// let config = JwtValidatorConfig::cognito("us-east-1", "us-east-1_xxxxx", "client-id");
/// let validator = JwtValidator::new(config).await?;
///
/// // Validate a token
/// let auth_context = validator.validate("eyJhbGci...").await?;
/// println!("User: {}", auth_context.user_id());
/// # Ok(())
/// # }
/// ```
///
/// # Provider Support
///
/// Works with any OIDC-compliant provider:
/// - AWS Cognito
/// - Microsoft Entra ID (Azure AD)
/// - Google Identity
/// - Okta
/// - Auth0
/// - Any provider with a JWKS endpoint
#[derive(Debug)]
pub struct JwtValidator {
    /// Validator configuration.
    #[allow(dead_code)]
    config: JwtValidatorConfig,
    /// Cached JWKS.
    #[allow(dead_code)]
    jwks: Arc<RwLock<CachedJwks>>,
    /// HTTP client for fetching JWKS.
    #[cfg(not(target_arch = "wasm32"))]
    #[allow(dead_code)]
    http_client: reqwest::Client,
}

impl JwtValidator {
    /// Create a new JWT validator with the given configuration.
    ///
    /// This will fetch the JWKS from the configured endpoint and cache it.
    ///
    /// # Errors
    ///
    /// Returns an error if the JWKS cannot be fetched or parsed.
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn new(config: JwtValidatorConfig) -> Result<Self> {
        let ttl = config.cache_ttl();
        let validator = Self {
            config,
            jwks: Arc::new(RwLock::new(CachedJwks::new(ttl))),
            http_client: reqwest::Client::builder()
                .timeout(Duration::from_secs(10))
                .build()
                .map_err(|e| Error::internal(format!("Failed to create HTTP client: {}", e)))?,
        };

        // Fetch JWKS on startup
        validator.refresh_keys_internal().await?;

        Ok(validator)
    }

    /// Create a validator from an existing reqwest client.
    #[cfg(not(target_arch = "wasm32"))]
    pub async fn with_client(
        config: JwtValidatorConfig,
        http_client: reqwest::Client,
    ) -> Result<Self> {
        let ttl = config.cache_ttl();
        let validator = Self {
            config,
            jwks: Arc::new(RwLock::new(CachedJwks::new(ttl))),
            http_client,
        };

        validator.refresh_keys_internal().await?;

        Ok(validator)
    }

    /// Get the issuer URL.
    pub fn issuer(&self) -> &str {
        &self.config.issuer
    }

    /// Get the expected audience.
    pub fn audience(&self) -> &str {
        &self.config.audience
    }

    /// Get the claim mappings.
    pub fn claim_mappings(&self) -> &ClaimMappings {
        &self.config.claim_mappings
    }

    /// Refresh the JWKS cache.
    #[cfg(not(target_arch = "wasm32"))]
    async fn refresh_keys_internal(&self) -> Result<()> {
        #[cfg(feature = "jwt-auth")]
        {
            let jwks_uri = self.config.jwks_uri();
            tracing::debug!("Fetching JWKS from {}", jwks_uri);

            let response = self
                .http_client
                .get(&jwks_uri)
                .send()
                .await
                .map_err(|e| Error::internal(format!("Failed to fetch JWKS: {}", e)))?;

            if !response.status().is_success() {
                return Err(Error::internal(format!(
                    "JWKS endpoint returned status {}",
                    response.status()
                )));
            }

            let jwks: JwksResponse = response
                .json()
                .await
                .map_err(|e| Error::internal(format!("Failed to parse JWKS: {}", e)))?;

            let mut keys = HashMap::new();
            for key in jwks.keys {
                if let (Some(kid), Some(n), Some(e)) = (&key.kid, &key.n, &key.e) {
                    match jsonwebtoken::DecodingKey::from_rsa_components(n, e) {
                        Ok(decoding_key) => {
                            keys.insert(kid.clone(), decoding_key);
                        },
                        Err(err) => {
                            tracing::warn!("Failed to parse key {}: {}", kid, err);
                        },
                    }
                }
            }

            if keys.is_empty() {
                return Err(Error::internal("No valid keys found in JWKS"));
            }

            tracing::info!("Loaded {} keys from JWKS", keys.len());

            let mut cache = self.jwks.write().await;
            cache.keys = keys;
            cache.fetched_at = Instant::now();

            Ok(())
        }

        #[cfg(not(feature = "jwt-auth"))]
        {
            Err(Error::protocol(
                ErrorCode::METHOD_NOT_FOUND,
                "JWT validation requires the 'jwt-auth' feature",
            ))
        }
    }

    /// Validate a JWT and extract the auth context.
    #[cfg(all(not(target_arch = "wasm32"), feature = "jwt-auth"))]
    async fn validate_jwt(&self, token: &str) -> Result<AuthContext> {
        use jsonwebtoken::{decode, decode_header, Algorithm, Validation};

        // Decode header to get key ID
        let header = decode_header(token).map_err(|e| {
            Error::protocol(
                ErrorCode::AUTHENTICATION_REQUIRED,
                format!("Invalid token header: {}", e),
            )
        })?;

        let kid = header.kid.ok_or_else(|| {
            Error::protocol(
                ErrorCode::AUTHENTICATION_REQUIRED,
                "Token missing key ID (kid)",
            )
        })?;

        // Get the key from cache
        let key = {
            let cache = self.jwks.read().await;

            // Refresh if expired
            if cache.is_expired() {
                drop(cache);
                self.refresh_keys_internal().await?;
                let cache = self.jwks.read().await;
                cache.keys.get(&kid).cloned()
            } else {
                cache.keys.get(&kid).cloned()
            }
        };

        let key = key.ok_or_else(|| {
            Error::protocol(
                ErrorCode::AUTHENTICATION_REQUIRED,
                format!("Unknown key ID: {}", kid),
            )
        })?;

        // Build validation
        let mut validation = Validation::new(Algorithm::RS256);
        validation.set_issuer(&[&self.config.issuer]);
        validation.set_audience(&[&self.config.audience]);
        validation.leeway = self.config.leeway_seconds;

        // Decode and verify token
        let token_data = decode::<serde_json::Value>(token, &key, &validation).map_err(|e| {
            let msg = match e.kind() {
                jsonwebtoken::errors::ErrorKind::ExpiredSignature => "Token expired",
                jsonwebtoken::errors::ErrorKind::InvalidIssuer => "Invalid issuer",
                jsonwebtoken::errors::ErrorKind::InvalidAudience => "Invalid audience",
                jsonwebtoken::errors::ErrorKind::InvalidSignature => "Invalid signature",
                _ => "Token validation failed",
            };
            Error::protocol(ErrorCode::AUTHENTICATION_REQUIRED, msg)
        })?;

        // Normalize claims using mappings
        let normalized_claims = self
            .config
            .claim_mappings
            .normalize_claims(&token_data.claims);

        // Extract subject
        let subject = normalized_claims
            .get("sub")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .to_string();

        // Extract scopes
        let scopes = parse_scopes(&token_data.claims);

        // Extract client ID
        let client_id = token_data
            .claims
            .get("azp")
            .or_else(|| token_data.claims.get("client_id"))
            .and_then(|v| v.as_str())
            .map(String::from);

        // Extract expiration
        let expires_at = token_data.claims.get("exp").and_then(|v| v.as_u64());

        Ok(AuthContext {
            subject,
            scopes,
            claims: normalized_claims,
            token: Some(token.to_string()),
            client_id,
            expires_at,
            authenticated: true,
        })
    }
}

#[async_trait]
impl TokenValidator for JwtValidator {
    #[cfg(all(not(target_arch = "wasm32"), feature = "jwt-auth"))]
    async fn validate(&self, token: &str) -> Result<AuthContext> {
        self.validate_jwt(token).await
    }

    #[cfg(any(target_arch = "wasm32", not(feature = "jwt-auth")))]
    async fn validate(&self, _token: &str) -> Result<AuthContext> {
        Err(Error::protocol(
            ErrorCode::METHOD_NOT_FOUND,
            "JWT validation requires the 'jwt-auth' feature and non-WASM target",
        ))
    }
}

/// Parse scopes from token claims.
///
/// Handles both space-separated string format and array format.
#[cfg(all(feature = "jwt-auth", not(target_arch = "wasm32")))]
fn parse_scopes(claims: &serde_json::Value) -> Vec<String> {
    if let Some(scope) = claims.get("scope") {
        if let Some(s) = scope.as_str() {
            return s.split_whitespace().map(String::from).collect();
        }
        if let Some(arr) = scope.as_array() {
            return arr
                .iter()
                .filter_map(|v| v.as_str())
                .map(String::from)
                .collect();
        }
    }

    if let Some(scp) = claims.get("scp") {
        if let Some(arr) = scp.as_array() {
            return arr
                .iter()
                .filter_map(|v| v.as_str())
                .map(String::from)
                .collect();
        }
    }

    Vec::new()
}

/// JWKS response structure.
#[cfg(feature = "jwt-auth")]
#[derive(Debug, serde::Deserialize)]
struct JwksResponse {
    keys: Vec<JwkKey>,
}

/// Individual JWK key structure.
#[cfg(feature = "jwt-auth")]
#[derive(Debug, serde::Deserialize)]
struct JwkKey {
    /// Key ID
    kid: Option<String>,
    /// Key type (e.g., "RSA")
    #[allow(dead_code)]
    kty: String,
    /// RSA modulus (base64url-encoded)
    n: Option<String>,
    /// RSA exponent (base64url-encoded)
    e: Option<String>,
    /// Algorithm (e.g., "RS256")
    #[allow(dead_code)]
    alg: Option<String>,
    /// Key use (e.g., "sig")
    #[serde(rename = "use")]
    #[allow(dead_code)]
    key_use: Option<String>,
}

#[cfg(all(test, feature = "jwt-auth", not(target_arch = "wasm32")))]
mod tests {
    use super::*;

    #[test]
    fn test_parse_scopes_string() {
        let claims = serde_json::json!({
            "scope": "read write admin"
        });
        let scopes = parse_scopes(&claims);
        assert_eq!(scopes, vec!["read", "write", "admin"]);
    }

    #[test]
    fn test_parse_scopes_array() {
        let claims = serde_json::json!({
            "scope": ["read", "write", "admin"]
        });
        let scopes = parse_scopes(&claims);
        assert_eq!(scopes, vec!["read", "write", "admin"]);
    }

    #[test]
    fn test_parse_scopes_scp() {
        let claims = serde_json::json!({
            "scp": ["User.Read", "User.Write"]
        });
        let scopes = parse_scopes(&claims);
        assert_eq!(scopes, vec!["User.Read", "User.Write"]);
    }

    #[test]
    fn test_parse_scopes_empty() {
        let claims = serde_json::json!({});
        let scopes = parse_scopes(&claims);
        assert!(scopes.is_empty());
    }
}