pulseengine_mcp_server/
context.rs

1//! Request context for MCP operations
2
3use pulseengine_mcp_protocol::Implementation;
4use std::collections::HashMap;
5use uuid::Uuid;
6
7/// Request context containing metadata and client information
8#[derive(Debug, Clone)]
9pub struct RequestContext {
10    /// Unique request ID
11    pub request_id: Uuid,
12    /// Request metadata
13    pub metadata: HashMap<String, String>,
14    /// Client information
15    pub client_info: Option<Implementation>,
16    /// Authentication information
17    pub authenticated_user: Option<String>,
18    /// Authorization roles
19    pub roles: Vec<String>,
20}
21
22impl RequestContext {
23    /// Create a new request context
24    pub fn new() -> Self {
25        Self {
26            request_id: Uuid::new_v4(),
27            metadata: HashMap::new(),
28            client_info: None,
29            authenticated_user: None,
30            roles: vec![],
31        }
32    }
33
34    /// Create a request context with specific ID
35    pub fn with_id(request_id: Uuid) -> Self {
36        Self {
37            request_id,
38            metadata: HashMap::new(),
39            client_info: None,
40            authenticated_user: None,
41            roles: vec![],
42        }
43    }
44
45    /// Set client information
46    pub fn with_client_info(mut self, client_info: Implementation) -> Self {
47        self.client_info = Some(client_info);
48        self
49    }
50
51    /// Set authenticated user
52    pub fn with_user(mut self, user: impl Into<String>) -> Self {
53        self.authenticated_user = Some(user.into());
54        self
55    }
56
57    /// Add a role
58    pub fn with_role(mut self, role: impl Into<String>) -> Self {
59        self.roles.push(role.into());
60        self
61    }
62
63    /// Add metadata
64    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
65        self.metadata.insert(key.into(), value.into());
66        self
67    }
68
69    /// Get metadata value
70    pub fn get_metadata(&self, key: &str) -> Option<&String> {
71        self.metadata.get(key)
72    }
73
74    /// Check if user has a specific role
75    pub fn has_role(&self, role: &str) -> bool {
76        self.roles.contains(&role.to_string())
77    }
78
79    /// Check if user is authenticated
80    pub fn is_authenticated(&self) -> bool {
81        self.authenticated_user.is_some()
82    }
83}
84
85impl Default for RequestContext {
86    fn default() -> Self {
87        Self::new()
88    }
89}