pulseengine_mcp_server/
context.rs1use pulseengine_mcp_protocol::Implementation;
4use std::collections::HashMap;
5use uuid::Uuid;
6
7#[derive(Debug, Clone)]
9pub struct RequestContext {
10 pub request_id: Uuid,
12 pub metadata: HashMap<String, String>,
14 pub client_info: Option<Implementation>,
16 pub authenticated_user: Option<String>,
18 pub roles: Vec<String>,
20}
21
22impl RequestContext {
23 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 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 pub fn with_client_info(mut self, client_info: Implementation) -> Self {
47 self.client_info = Some(client_info);
48 self
49 }
50
51 pub fn with_user(mut self, user: impl Into<String>) -> Self {
53 self.authenticated_user = Some(user.into());
54 self
55 }
56
57 pub fn with_role(mut self, role: impl Into<String>) -> Self {
59 self.roles.push(role.into());
60 self
61 }
62
63 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 pub fn get_metadata(&self, key: &str) -> Option<&String> {
71 self.metadata.get(key)
72 }
73
74 pub fn has_role(&self, role: &str) -> bool {
76 self.roles.contains(&role.to_string())
77 }
78
79 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}