Skip to main content

mcp_kit/server/
session.rs

1use uuid::Uuid;
2
3use crate::types::ClientInfo;
4
5#[cfg(feature = "auth")]
6use crate::auth::AuthenticatedIdentity;
7
8/// Unique session identifier
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub struct SessionId(pub String);
11
12impl SessionId {
13    pub fn new() -> Self {
14        Self(Uuid::new_v4().to_string())
15    }
16}
17
18impl Default for SessionId {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24impl std::fmt::Display for SessionId {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(f, "{}", self.0)
27    }
28}
29
30/// Per-connection session data
31#[derive(Debug, Clone)]
32pub struct Session {
33    pub id: SessionId,
34    pub client_info: Option<ClientInfo>,
35    pub protocol_version: Option<String>,
36    pub initialized: bool,
37    /// Populated by the transport layer after successful authentication.
38    /// `None` means the request was unauthenticated (or auth is not configured).
39    #[cfg(feature = "auth")]
40    pub identity: Option<AuthenticatedIdentity>,
41}
42
43impl Session {
44    pub fn new() -> Self {
45        Self {
46            id: SessionId::new(),
47            client_info: None,
48            protocol_version: None,
49            initialized: false,
50            #[cfg(feature = "auth")]
51            identity: None,
52        }
53    }
54}
55
56impl Default for Session {
57    fn default() -> Self {
58        Self::new()
59    }
60}