Skip to main content

adk_tool/mcp/manager/
status.rs

1//! Server status tracking.
2//!
3//! Defines the [`ServerStatus`] enum representing the current lifecycle state
4//! of a managed MCP server.
5
6use serde::{Deserialize, Serialize};
7
8/// Current lifecycle state of a managed MCP server.
9///
10/// Transitions:
11/// - `Stopped`, `Crashed`, `FailedToStart`, or `Restarting` → `Starting` (a start begins; the
12///   process is spawned and the MCP handshake runs in this state)
13/// - `Starting` → `Running` (the handshake completed)
14/// - `Starting` → `FailedToStart` (spawn or handshake failure, or the startup timeout)
15/// - `Starting` → `Stopped` (stopped while it was starting)
16/// - `Running` → `Stopped` (on manual stop or shutdown)
17/// - `Running` → `Crashed` (the health check found the connection closed)
18/// - `Running` → `Restarting` (on `restart_server`)
19/// - `Crashed` or `FailedToStart` → `Restarting` (on auto-restart, after the backoff)
20/// - `Crashed` → `FailedToStart` (the restart policy's attempts are exhausted)
21/// - any → `Disabled` (on `disable_server`, or a start of a server configured `disabled`);
22///   `Disabled` → `Stopped` (on `enable_server`)
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
24pub enum ServerStatus {
25    /// The server process has been spawned and its MCP handshake is in progress.
26    Starting,
27    /// The server process is running and the MCP connection is active.
28    Running,
29    /// The server has been stopped (manually or via shutdown).
30    Stopped,
31    /// The server process exited unexpectedly or a health check failed.
32    Crashed,
33    /// The server is in the process of being restarted.
34    Restarting,
35    /// The server configuration has `disabled: true`; it will not be started.
36    Disabled,
37    /// The server failed to start (spawn failure, handshake failure or timeout, or max
38    /// restarts exceeded).
39    FailedToStart,
40}
41
42/// One change of a managed server's [`ServerStatus`], published as `(id, from, to)` on the
43/// manager's status channel (spec 3.2 §10 T1).
44///
45/// [`McpServerManager::subscribe_status_transitions`](super::McpServerManager::subscribe_status_transitions)
46/// delivers one for every change the manager records, in the order they happen. `from` and
47/// `to` always differ.
48///
49/// # Example
50///
51/// ```rust
52/// use adk_tool::mcp::{ServerStatus, ServerStatusTransition};
53///
54/// let crashed = ServerStatusTransition {
55///     id: "email".to_string(),
56///     from: ServerStatus::Running,
57///     to: ServerStatus::Crashed,
58/// };
59/// assert_eq!(
60///     serde_json::to_value(&crashed).unwrap(),
61///     serde_json::json!({ "id": "email", "from": "Running", "to": "Crashed" })
62/// );
63/// ```
64#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(rename_all = "camelCase")]
66pub struct ServerStatusTransition {
67    /// ID of the server whose status changed.
68    pub id: String,
69    /// Status the server left.
70    pub from: ServerStatus,
71    /// Status the server entered.
72    pub to: ServerStatus,
73}
74
75/// Sending half of the manager's status channel.
76pub(crate) type StatusSender = tokio::sync::broadcast::Sender<ServerStatusTransition>;
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_server_status_clone() {
84        let status = ServerStatus::Running;
85        let cloned = status;
86        assert_eq!(status, cloned);
87    }
88
89    #[test]
90    fn test_server_status_debug() {
91        let status = ServerStatus::Crashed;
92        let debug_str = format!("{status:?}");
93        assert_eq!(debug_str, "Crashed");
94    }
95
96    #[test]
97    fn test_server_status_eq() {
98        assert_eq!(ServerStatus::Running, ServerStatus::Running);
99        assert_ne!(ServerStatus::Running, ServerStatus::Stopped);
100    }
101
102    #[test]
103    fn test_server_status_hash() {
104        use std::collections::HashSet;
105        let mut set = HashSet::new();
106        set.insert(ServerStatus::Running);
107        set.insert(ServerStatus::Stopped);
108        set.insert(ServerStatus::Running); // duplicate
109        assert_eq!(set.len(), 2);
110    }
111
112    #[test]
113    fn test_server_status_serde_round_trip() {
114        let statuses = [
115            ServerStatus::Starting,
116            ServerStatus::Running,
117            ServerStatus::Stopped,
118            ServerStatus::Crashed,
119            ServerStatus::Restarting,
120            ServerStatus::Disabled,
121            ServerStatus::FailedToStart,
122        ];
123        for status in &statuses {
124            let json = serde_json::to_string(status).unwrap();
125            let deserialized: ServerStatus = serde_json::from_str(&json).unwrap();
126            assert_eq!(*status, deserialized);
127        }
128    }
129}