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
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
//! Core authentication traits for flexible OAuth/auth integration.
//!
//! This module provides a provider-agnostic authentication abstraction for MCP servers.
//! The core design principle is that **your MCP server code should never know about OAuth
//! providers, tokens, or authentication flows. It only sees `AuthContext`.**
//!
//! # Provider Agnosticism
//!
//! The authentication system supports multiple OAuth providers (Cognito, Entra, Google,
//! Okta, Auth0, etc.) through configuration, not code changes. See [`ClaimMappings`] for
//! how provider-specific claim names are translated to standard names.
//!
//! # Example
//!
//! ```rust
//! use pmcp::server::auth::AuthContext;
//!
//! fn handle_request(auth: &AuthContext) -> Result<String, &'static str> {
//!     // Require authentication
//!     auth.require_auth()?;
//!
//!     // Check scopes
//!     auth.require_scope("read:data")?;
//!
//!     // Access user info (provider-agnostic)
//!     let user_id = auth.user_id();
//!     let email = auth.email().unwrap_or("unknown");
//!
//!     Ok(format!("Hello, {} ({})", email, user_id))
//! }
//! ```

use crate::error::Result;
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::collections::HashMap;

/// Authentication context containing validated user information.
///
/// This is the **only** auth type your MCP code should interact with.
/// It provides a provider-agnostic view of the authenticated user, regardless
/// of whether the token came from Cognito, Entra, Google, Okta, or any other
/// OIDC provider.
///
/// # Provider-Agnostic Access
///
/// Use the helper methods like [`email()`](Self::email), [`tenant_id()`](Self::tenant_id),
/// and [`user_id()`](Self::user_id) instead of directly accessing claims. These methods
/// handle the different claim names used by various OAuth providers.
///
/// # Example
///
/// ```rust
/// use pmcp::server::auth::AuthContext;
///
/// fn get_user_greeting(auth: &AuthContext) -> String {
///     let name = auth.email().unwrap_or(auth.user_id());
///     format!("Welcome, {}!", name)
/// }
/// ```
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AuthContext {
    /// Subject identifier (user ID from the `sub` claim).
    pub subject: String,

    /// Granted scopes/permissions.
    pub scopes: Vec<String>,

    /// Additional claims from the token.
    /// Use the helper methods like [`email()`](Self::email) for common claims.
    pub claims: HashMap<String, serde_json::Value>,

    /// Original token if available (for forwarding to downstream services).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,

    /// Client ID that authenticated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,

    /// Token expiration timestamp (Unix epoch seconds).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires_at: Option<u64>,

    /// Whether this context represents an authenticated user.
    #[serde(default)]
    pub authenticated: bool,
}

impl AuthContext {
    /// Create a new authenticated context.
    pub fn new(subject: impl Into<String>) -> Self {
        Self {
            subject: subject.into(),
            authenticated: true,
            ..Default::default()
        }
    }

    /// Create an anonymous (unauthenticated) context.
    pub fn anonymous() -> Self {
        Self {
            subject: "anonymous".to_string(),
            authenticated: false,
            ..Default::default()
        }
    }

    /// Get the user ID (alias for subject).
    ///
    /// This is the standard user identifier, typically from the `sub` claim.
    #[inline]
    pub fn user_id(&self) -> &str {
        &self.subject
    }

    /// Get a typed claim value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pmcp::server::auth::AuthContext;
    ///
    /// let auth = AuthContext::new("user-123");
    /// let roles: Option<Vec<String>> = auth.claim("roles");
    /// ```
    pub fn claim<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
        self.claims
            .get(key)
            .and_then(|v| serde_json::from_value(v.clone()).ok())
    }

    /// Get the email address (handles different claim names across providers).
    ///
    /// This method checks common claim names used by different OAuth providers:
    /// - `email` (Cognito, Google, Okta, Auth0)
    /// - `preferred_username` (Entra ID)
    /// - `upn` (Entra ID UPN)
    pub fn email(&self) -> Option<&str> {
        self.claims
            .get("email")
            .or_else(|| self.claims.get("preferred_username"))
            .or_else(|| self.claims.get("upn"))
            .and_then(|v| v.as_str())
    }

    /// Get the display name.
    ///
    /// Checks common claim names for user's name:
    /// - `name` (most providers)
    /// - `given_name` + `family_name` fallback
    pub fn name(&self) -> Option<&str> {
        self.claims.get("name").and_then(|v| v.as_str())
    }

    /// Get the tenant ID (handles different claim names across providers).
    ///
    /// This method checks common claim names used by different OAuth providers:
    /// - `tenant_id` (normalized)
    /// - `tid` (Entra ID)
    /// - `custom:tenant_id` (Cognito custom attribute)
    /// - `custom:tenant` (Cognito custom attribute)
    /// - `org_id` (Auth0, Okta)
    pub fn tenant_id(&self) -> Option<&str> {
        self.claims
            .get("tenant_id")
            .or_else(|| self.claims.get("tid")) // Entra ID
            .or_else(|| self.claims.get("custom:tenant_id")) // Cognito
            .or_else(|| self.claims.get("custom:tenant")) // Cognito
            .or_else(|| self.claims.get("org_id")) // Auth0, Okta
            .and_then(|v| v.as_str())
    }

    /// Get groups/roles the user belongs to.
    ///
    /// Checks common claim names for group membership:
    /// - `groups` (Entra ID, Okta)
    /// - `cognito:groups` (Cognito)
    /// - `roles` (Auth0)
    pub fn groups(&self) -> Vec<String> {
        self.claims
            .get("groups")
            .or_else(|| self.claims.get("cognito:groups"))
            .or_else(|| self.claims.get("roles"))
            .and_then(|v| serde_json::from_value(v.clone()).ok())
            .unwrap_or_default()
    }

    /// Check if the context has a specific scope.
    pub fn has_scope(&self, scope: &str) -> bool {
        self.scopes.iter().any(|s| s == scope)
    }

    /// Check if the context has all specified scopes.
    pub fn has_all_scopes(&self, scopes: &[&str]) -> bool {
        scopes.iter().all(|scope| self.has_scope(scope))
    }

    /// Check if the context has any of the specified scopes.
    pub fn has_any_scope(&self, scopes: &[&str]) -> bool {
        scopes.iter().any(|scope| self.has_scope(scope))
    }

    /// Require a scope, returning an error message if missing.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pmcp::server::auth::AuthContext;
    ///
    /// fn protected_operation(auth: &AuthContext) -> Result<(), &'static str> {
    ///     auth.require_scope("write:data")?;
    ///     // ... perform operation
    ///     Ok(())
    /// }
    /// ```
    pub fn require_scope(&self, scope: &str) -> std::result::Result<(), &'static str> {
        if self.has_scope(scope) {
            Ok(())
        } else {
            Err("Insufficient scope")
        }
    }

    /// Require authentication, returning an error message if not authenticated.
    ///
    /// # Example
    ///
    /// ```rust
    /// use pmcp::server::auth::AuthContext;
    ///
    /// fn protected_operation(auth: &AuthContext) -> Result<&str, &'static str> {
    ///     auth.require_auth()?;
    ///     Ok(auth.user_id())
    /// }
    /// ```
    pub fn require_auth(&self) -> std::result::Result<(), &'static str> {
        if self.authenticated {
            Ok(())
        } else {
            Err("Authentication required")
        }
    }

    /// Check if the token is expired.
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();
            expires_at < now
        } else {
            false
        }
    }

    /// Check if the user is in a specific group.
    pub fn in_group(&self, group: &str) -> bool {
        self.groups().iter().any(|g| g == group)
    }
}

/// Claim mappings for translating provider-specific claims to standard names.
///
/// Different OAuth providers use different claim names for the same information.
/// This struct allows configuring the mapping from provider-specific names to
/// standard names used by `AuthContext`.
///
/// # Provider-Specific Claim Names
///
/// | Standard | Cognito | Entra ID | Google | Okta | Auth0 |
/// |----------|---------|----------|--------|------|-------|
/// | `user_id` | sub | oid | sub | uid | sub |
/// | `tenant_id` | `custom:tenant` | tid | N/A | `org_id` | `org_id` |
/// | email | email | `preferred_username` | email | email | email |
/// | groups | `cognito:groups` | groups | N/A | groups | roles |
///
/// # Example
///
/// ```rust
/// use pmcp::server::auth::ClaimMappings;
///
/// // Configure for Entra ID
/// let mappings = ClaimMappings::entra();
/// assert_eq!(mappings.user_id, "oid");
/// assert_eq!(mappings.tenant_id, Some("tid".to_string()));
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClaimMappings {
    /// Claim name for user ID (default: "sub").
    #[serde(default = "default_user_id_claim")]
    pub user_id: String,

    /// Claim name for tenant ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tenant_id: Option<String>,

    /// Claim name for email.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    /// Claim name for display name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Claim name for groups/roles.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub groups: Option<String>,

    /// Additional custom mappings.
    #[serde(flatten)]
    pub custom: HashMap<String, String>,
}

fn default_user_id_claim() -> String {
    "sub".to_string()
}

impl Default for ClaimMappings {
    fn default() -> Self {
        Self {
            user_id: default_user_id_claim(),
            tenant_id: None,
            email: Some("email".to_string()),
            name: Some("name".to_string()),
            groups: None,
            custom: HashMap::new(),
        }
    }
}

impl ClaimMappings {
    /// Create claim mappings for AWS Cognito.
    pub fn cognito() -> Self {
        Self {
            user_id: "sub".to_string(),
            tenant_id: Some("custom:tenant_id".to_string()),
            email: Some("email".to_string()),
            name: Some("name".to_string()),
            groups: Some("cognito:groups".to_string()),
            custom: HashMap::new(),
        }
    }

    /// Create claim mappings for Microsoft Entra ID (Azure AD).
    pub fn entra() -> Self {
        Self {
            user_id: "oid".to_string(),
            tenant_id: Some("tid".to_string()),
            email: Some("preferred_username".to_string()),
            name: Some("name".to_string()),
            groups: Some("groups".to_string()),
            custom: HashMap::new(),
        }
    }

    /// Create claim mappings for Google Identity.
    pub fn google() -> Self {
        Self {
            user_id: "sub".to_string(),
            tenant_id: None, // Google doesn't have tenant concept
            email: Some("email".to_string()),
            name: Some("name".to_string()),
            groups: None,
            custom: HashMap::new(),
        }
    }

    /// Create claim mappings for Okta.
    pub fn okta() -> Self {
        Self {
            user_id: "uid".to_string(),
            tenant_id: Some("org_id".to_string()),
            email: Some("email".to_string()),
            name: Some("name".to_string()),
            groups: Some("groups".to_string()),
            custom: HashMap::new(),
        }
    }

    /// Create claim mappings for Auth0.
    pub fn auth0() -> Self {
        Self {
            user_id: "sub".to_string(),
            tenant_id: Some("org_id".to_string()),
            email: Some("email".to_string()),
            name: Some("name".to_string()),
            groups: Some("roles".to_string()),
            custom: HashMap::new(),
        }
    }

    /// Apply these mappings to normalize claims from a token.
    ///
    /// This transforms provider-specific claims into standard names that
    /// `AuthContext` helper methods can find.
    pub fn normalize_claims(
        &self,
        claims: &serde_json::Value,
    ) -> HashMap<String, serde_json::Value> {
        let mut normalized = HashMap::new();

        if let Some(obj) = claims.as_object() {
            // Copy all original claims
            for (key, value) in obj {
                normalized.insert(key.clone(), value.clone());
            }

            // Add normalized mappings
            if let Some(value) = obj.get(&self.user_id) {
                normalized.insert("sub".to_string(), value.clone());
            }
            if let Some(ref tenant_claim) = self.tenant_id {
                if let Some(value) = obj.get(tenant_claim) {
                    normalized.insert("tenant_id".to_string(), value.clone());
                }
            }
            if let Some(ref email_claim) = self.email {
                if let Some(value) = obj.get(email_claim) {
                    normalized.insert("email".to_string(), value.clone());
                }
            }
            if let Some(ref name_claim) = self.name {
                if let Some(value) = obj.get(name_claim) {
                    normalized.insert("name".to_string(), value.clone());
                }
            }
            if let Some(ref groups_claim) = self.groups {
                if let Some(value) = obj.get(groups_claim) {
                    normalized.insert("groups".to_string(), value.clone());
                }
            }

            // Apply custom mappings
            for (standard_name, provider_name) in &self.custom {
                if let Some(value) = obj.get(provider_name) {
                    normalized.insert(standard_name.clone(), value.clone());
                }
            }
        }

        normalized
    }
}

/// Core authentication provider trait.
/// This is the main abstraction that MCP servers use for authentication.
#[async_trait]
pub trait AuthProvider: Send + Sync {
    /// Validate an incoming request and extract authentication context.
    ///
    /// This method receives the authorization header value and should:
    /// 1. Parse the authentication token (e.g., Bearer token)
    /// 2. Validate the token
    /// 3. Return the authentication context if valid
    ///
    /// The `authorization_header` parameter contains the value of the Authorization header,
    /// if present (e.g., "Bearer eyJhbGci...")
    async fn validate_request(
        &self,
        authorization_header: Option<&str>,
    ) -> Result<Option<AuthContext>>;

    /// Get the authentication scheme this provider uses (e.g., "Bearer", "Basic").
    fn auth_scheme(&self) -> &'static str {
        "Bearer"
    }

    /// Check if this provider requires authentication for all requests.
    fn is_required(&self) -> bool {
        true
    }
}

/// Token validator trait for validating access tokens.
#[async_trait]
pub trait TokenValidator: Send + Sync {
    /// Validate an access token and return token information.
    async fn validate(&self, token: &str) -> Result<AuthContext>;

    /// Optionally validate token with additional context (e.g., required scopes).
    async fn validate_with_context(
        &self,
        token: &str,
        required_scopes: Option<&[&str]>,
    ) -> Result<AuthContext> {
        let auth_context = self.validate(token).await?;

        // Check required scopes if specified
        if let Some(scopes) = required_scopes {
            if !auth_context.has_all_scopes(scopes) {
                return Err(crate::error::Error::protocol(
                    crate::error::ErrorCode::INVALID_REQUEST,
                    "Insufficient scopes",
                ));
            }
        }

        Ok(auth_context)
    }
}

/// Session management trait for stateful authentication.
#[async_trait]
pub trait SessionManager: Send + Sync {
    /// Create a new session and return the session ID.
    async fn create_session(&self, auth: AuthContext) -> Result<String>;

    /// Get session by ID.
    async fn get_session(&self, session_id: &str) -> Result<Option<AuthContext>>;

    /// Update an existing session.
    async fn update_session(&self, session_id: &str, auth: AuthContext) -> Result<()>;

    /// Invalidate a session.
    async fn invalidate_session(&self, session_id: &str) -> Result<()>;

    /// Clean up expired sessions (optional background task).
    async fn cleanup_expired(&self) -> Result<usize> {
        Ok(0) // Default no-op implementation
    }
}

/// Tool authorization trait for fine-grained access control.
#[async_trait]
pub trait ToolAuthorizer: Send + Sync {
    /// Check if the authenticated context can access a specific tool.
    async fn can_access_tool(&self, auth: &AuthContext, tool_name: &str) -> Result<bool>;

    /// Get required scopes for a tool.
    async fn required_scopes_for_tool(&self, tool_name: &str) -> Result<Vec<String>>;
}

/// Simple scope-based tool authorizer.
#[derive(Debug, Clone)]
pub struct ScopeBasedAuthorizer {
    tool_scopes: HashMap<String, Vec<String>>,
    default_scopes: Vec<String>,
}

impl ScopeBasedAuthorizer {
    /// Create a new scope-based authorizer.
    pub fn new() -> Self {
        Self {
            tool_scopes: HashMap::new(),
            default_scopes: vec!["mcp:tools:use".to_string()],
        }
    }

    /// Add required scopes for a tool.
    pub fn require_scopes<S, I>(mut self, tool_name: impl Into<String>, scopes: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let scopes_vec = scopes.into_iter().map(|s| s.as_ref().to_string()).collect();
        self.tool_scopes.insert(tool_name.into(), scopes_vec);
        self
    }

    /// Set default required scopes for all tools.
    pub fn default_scopes(mut self, scopes: Vec<String>) -> Self {
        self.default_scopes = scopes;
        self
    }
}

#[async_trait]
impl ToolAuthorizer for ScopeBasedAuthorizer {
    async fn can_access_tool(&self, auth: &AuthContext, tool_name: &str) -> Result<bool> {
        let required_scopes = self
            .tool_scopes
            .get(tool_name)
            .unwrap_or(&self.default_scopes);

        let scope_refs: Vec<&str> = required_scopes.iter().map(|s| s.as_str()).collect();
        Ok(auth.has_all_scopes(&scope_refs))
    }

    async fn required_scopes_for_tool(&self, tool_name: &str) -> Result<Vec<String>> {
        Ok(self
            .tool_scopes
            .get(tool_name)
            .unwrap_or(&self.default_scopes)
            .clone())
    }
}

impl Default for ScopeBasedAuthorizer {
    fn default() -> Self {
        Self::new()
    }
}