mcp_host/protocol/
methods.rs

1//! MCP method names
2//!
3//! Defines all available MCP methods as type-safe constants
4
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8/// MCP method names
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum McpMethod {
12    // Core methods
13    Initialize,
14    Ping,
15
16    // Resource methods
17    #[serde(rename = "resources/list")]
18    ResourcesList,
19    #[serde(rename = "resources/templates/list")]
20    ResourcesTemplatesList,
21    #[serde(rename = "resources/read")]
22    ResourcesRead,
23    #[serde(rename = "resources/subscribe")]
24    ResourcesSubscribe,
25    #[serde(rename = "resources/unsubscribe")]
26    ResourcesUnsubscribe,
27
28    // Prompt methods
29    #[serde(rename = "prompts/list")]
30    PromptsList,
31    #[serde(rename = "prompts/get")]
32    PromptsGet,
33
34    // Tool methods
35    #[serde(rename = "tools/list")]
36    ToolsList,
37    #[serde(rename = "tools/call")]
38    ToolsCall,
39
40    // Task methods (async execution)
41    #[serde(rename = "tasks/get")]
42    TasksGet,
43    #[serde(rename = "tasks/result")]
44    TasksResult,
45    #[serde(rename = "tasks/list")]
46    TasksList,
47    #[serde(rename = "tasks/cancel")]
48    TasksCancel,
49
50    // Logging methods
51    #[serde(rename = "logging/setLevel")]
52    LoggingSetLevel,
53
54    // Completion methods
55    #[serde(rename = "completion/complete")]
56    CompletionComplete,
57
58    // Elicitation methods (server-initiated user input)
59    #[serde(rename = "elicitation/create")]
60    ElicitationCreate,
61
62    // Sampling methods (server-initiated LLM sampling)
63    #[serde(rename = "sampling/createMessage")]
64    SamplingCreateMessage,
65
66    // Roots methods (client workspace roots)
67    #[serde(rename = "roots/list")]
68    RootsList,
69
70    // Notification methods
71    #[serde(rename = "notifications/initialized")]
72    NotificationsInitialized,
73    #[serde(rename = "notifications/resources/list_changed")]
74    NotificationsResourcesListChanged,
75    #[serde(rename = "notifications/resources/updated")]
76    NotificationsResourcesUpdated,
77    #[serde(rename = "notifications/prompts/list_changed")]
78    NotificationsPromptsListChanged,
79    #[serde(rename = "notifications/tools/list_changed")]
80    NotificationsToolsListChanged,
81    #[serde(rename = "notifications/progress")]
82    NotificationsProgress,
83    #[serde(rename = "notifications/message")]
84    NotificationsMessage,
85    #[serde(rename = "notifications/cancelled")]
86    NotificationsCancelled,
87    #[serde(rename = "notifications/roots/list_changed")]
88    NotificationsRootsListChanged,
89    #[serde(rename = "notifications/tasks/status")]
90    NotificationsTasksStatus,
91    #[serde(rename = "notifications/elicitation/complete")]
92    NotificationsElicitationComplete,
93
94    // Custom/unknown method (for extensibility)
95    #[serde(untagged)]
96    Custom(String),
97}
98
99impl fmt::Display for McpMethod {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        match self {
102            McpMethod::Initialize => write!(f, "initialize"),
103            McpMethod::Ping => write!(f, "ping"),
104            McpMethod::ResourcesList => write!(f, "resources/list"),
105            McpMethod::ResourcesTemplatesList => write!(f, "resources/templates/list"),
106            McpMethod::ResourcesRead => write!(f, "resources/read"),
107            McpMethod::ResourcesSubscribe => write!(f, "resources/subscribe"),
108            McpMethod::ResourcesUnsubscribe => write!(f, "resources/unsubscribe"),
109            McpMethod::PromptsList => write!(f, "prompts/list"),
110            McpMethod::PromptsGet => write!(f, "prompts/get"),
111            McpMethod::ToolsList => write!(f, "tools/list"),
112            McpMethod::ToolsCall => write!(f, "tools/call"),
113            McpMethod::TasksGet => write!(f, "tasks/get"),
114            McpMethod::TasksResult => write!(f, "tasks/result"),
115            McpMethod::TasksList => write!(f, "tasks/list"),
116            McpMethod::TasksCancel => write!(f, "tasks/cancel"),
117            McpMethod::LoggingSetLevel => write!(f, "logging/setLevel"),
118            McpMethod::CompletionComplete => write!(f, "completion/complete"),
119            McpMethod::ElicitationCreate => write!(f, "elicitation/create"),
120            McpMethod::SamplingCreateMessage => write!(f, "sampling/createMessage"),
121            McpMethod::RootsList => write!(f, "roots/list"),
122            McpMethod::NotificationsInitialized => write!(f, "notifications/initialized"),
123            McpMethod::NotificationsResourcesListChanged => {
124                write!(f, "notifications/resources/list_changed")
125            }
126            McpMethod::NotificationsResourcesUpdated => {
127                write!(f, "notifications/resources/updated")
128            }
129            McpMethod::NotificationsPromptsListChanged => {
130                write!(f, "notifications/prompts/list_changed")
131            }
132            McpMethod::NotificationsToolsListChanged => {
133                write!(f, "notifications/tools/list_changed")
134            }
135            McpMethod::NotificationsProgress => write!(f, "notifications/progress"),
136            McpMethod::NotificationsMessage => write!(f, "notifications/message"),
137            McpMethod::NotificationsCancelled => write!(f, "notifications/cancelled"),
138            McpMethod::NotificationsRootsListChanged => {
139                write!(f, "notifications/roots/list_changed")
140            }
141            McpMethod::NotificationsTasksStatus => write!(f, "notifications/tasks/status"),
142            McpMethod::NotificationsElicitationComplete => {
143                write!(f, "notifications/elicitation/complete")
144            }
145            McpMethod::Custom(s) => write!(f, "{}", s),
146        }
147    }
148}
149
150impl From<String> for McpMethod {
151    fn from(s: String) -> Self {
152        match s.as_str() {
153            "initialize" => McpMethod::Initialize,
154            "ping" => McpMethod::Ping,
155            "resources/list" => McpMethod::ResourcesList,
156            "resources/templates/list" => McpMethod::ResourcesTemplatesList,
157            "resources/read" => McpMethod::ResourcesRead,
158            "resources/subscribe" => McpMethod::ResourcesSubscribe,
159            "resources/unsubscribe" => McpMethod::ResourcesUnsubscribe,
160            "prompts/list" => McpMethod::PromptsList,
161            "prompts/get" => McpMethod::PromptsGet,
162            "tools/list" => McpMethod::ToolsList,
163            "tools/call" => McpMethod::ToolsCall,
164            "tasks/get" => McpMethod::TasksGet,
165            "tasks/result" => McpMethod::TasksResult,
166            "tasks/list" => McpMethod::TasksList,
167            "tasks/cancel" => McpMethod::TasksCancel,
168            "logging/setLevel" => McpMethod::LoggingSetLevel,
169            "completion/complete" => McpMethod::CompletionComplete,
170            "elicitation/create" => McpMethod::ElicitationCreate,
171            "sampling/createMessage" => McpMethod::SamplingCreateMessage,
172            "roots/list" => McpMethod::RootsList,
173            "notifications/initialized" => McpMethod::NotificationsInitialized,
174            "notifications/resources/list_changed" => McpMethod::NotificationsResourcesListChanged,
175            "notifications/resources/updated" => McpMethod::NotificationsResourcesUpdated,
176            "notifications/prompts/list_changed" => McpMethod::NotificationsPromptsListChanged,
177            "notifications/tools/list_changed" => McpMethod::NotificationsToolsListChanged,
178            "notifications/progress" => McpMethod::NotificationsProgress,
179            "notifications/message" => McpMethod::NotificationsMessage,
180            "notifications/cancelled" => McpMethod::NotificationsCancelled,
181            "notifications/roots/list_changed" => McpMethod::NotificationsRootsListChanged,
182            "notifications/tasks/status" => McpMethod::NotificationsTasksStatus,
183            "notifications/elicitation/complete" => McpMethod::NotificationsElicitationComplete,
184            _ => McpMethod::Custom(s),
185        }
186    }
187}
188
189impl From<&str> for McpMethod {
190    fn from(s: &str) -> Self {
191        McpMethod::from(s.to_string())
192    }
193}
194
195#[cfg(test)]
196mod tests {
197    use super::*;
198
199    #[test]
200    fn test_method_display() {
201        assert_eq!(McpMethod::Initialize.to_string(), "initialize");
202        assert_eq!(McpMethod::ResourcesList.to_string(), "resources/list");
203        assert_eq!(McpMethod::ToolsCall.to_string(), "tools/call");
204    }
205
206    #[test]
207    fn test_method_from_string() {
208        assert_eq!(McpMethod::from("initialize"), McpMethod::Initialize);
209        assert_eq!(McpMethod::from("tools/call"), McpMethod::ToolsCall);
210        assert_eq!(
211            McpMethod::from("notifications/progress"),
212            McpMethod::NotificationsProgress
213        );
214    }
215
216    #[test]
217    fn test_custom_method() {
218        let method = McpMethod::from("custom/method");
219        assert!(matches!(method, McpMethod::Custom(_)));
220        assert_eq!(method.to_string(), "custom/method");
221    }
222}