types.rs

  1use collections::HashMap;
  2use serde::{Deserialize, Serialize};
  3use url::Url;
  4
  5#[derive(Debug, Serialize)]
  6#[serde(rename_all = "camelCase")]
  7pub enum RequestType {
  8    Initialize,
  9    CallTool,
 10    ResourcesUnsubscribe,
 11    ResourcesSubscribe,
 12    ResourcesRead,
 13    ResourcesList,
 14    LoggingSetLevel,
 15    PromptsGet,
 16    PromptsList,
 17    CompletionComplete,
 18}
 19
 20impl RequestType {
 21    pub fn as_str(&self) -> &'static str {
 22        match self {
 23            RequestType::Initialize => "initialize",
 24            RequestType::CallTool => "tools/call",
 25            RequestType::ResourcesUnsubscribe => "resources/unsubscribe",
 26            RequestType::ResourcesSubscribe => "resources/subscribe",
 27            RequestType::ResourcesRead => "resources/read",
 28            RequestType::ResourcesList => "resources/list",
 29            RequestType::LoggingSetLevel => "logging/setLevel",
 30            RequestType::PromptsGet => "prompts/get",
 31            RequestType::PromptsList => "prompts/list",
 32            RequestType::CompletionComplete => "completion/complete",
 33        }
 34    }
 35}
 36
 37#[derive(Debug, Serialize)]
 38#[serde(rename_all = "camelCase")]
 39pub struct InitializeParams {
 40    pub protocol_version: u32,
 41    pub capabilities: ClientCapabilities,
 42    pub client_info: EntityInfo,
 43}
 44
 45#[derive(Debug, Serialize)]
 46#[serde(rename_all = "camelCase")]
 47pub struct CallToolParams {
 48    pub name: String,
 49    pub arguments: Option<serde_json::Value>,
 50}
 51
 52#[derive(Debug, Serialize)]
 53#[serde(rename_all = "camelCase")]
 54pub struct ResourcesUnsubscribeParams {
 55    pub uri: Url,
 56}
 57
 58#[derive(Debug, Serialize)]
 59#[serde(rename_all = "camelCase")]
 60pub struct ResourcesSubscribeParams {
 61    pub uri: Url,
 62}
 63
 64#[derive(Debug, Serialize)]
 65#[serde(rename_all = "camelCase")]
 66pub struct ResourcesReadParams {
 67    pub uri: Url,
 68}
 69
 70#[derive(Debug, Serialize)]
 71#[serde(rename_all = "camelCase")]
 72pub struct LoggingSetLevelParams {
 73    pub level: LoggingLevel,
 74}
 75
 76#[derive(Debug, Serialize)]
 77#[serde(rename_all = "camelCase")]
 78pub struct PromptsGetParams {
 79    pub name: String,
 80    pub arguments: Option<HashMap<String, String>>,
 81}
 82
 83#[derive(Debug, Serialize)]
 84#[serde(rename_all = "camelCase")]
 85pub struct CompletionCompleteParams {
 86    pub r#ref: CompletionReference,
 87    pub argument: CompletionArgument,
 88}
 89
 90#[derive(Debug, Serialize)]
 91#[serde(untagged)]
 92pub enum CompletionReference {
 93    Prompt(PromptReference),
 94    Resource(ResourceReference),
 95}
 96
 97#[derive(Debug, Serialize)]
 98#[serde(rename_all = "camelCase")]
 99pub struct PromptReference {
100    pub r#type: PromptReferenceType,
101    pub name: String,
102}
103
104#[derive(Debug, Serialize)]
105#[serde(rename_all = "snake_case")]
106pub enum PromptReferenceType {
107    #[serde(rename = "ref/prompt")]
108    Prompt,
109    #[serde(rename = "ref/resource")]
110    Resource,
111}
112
113#[derive(Debug, Serialize)]
114#[serde(rename_all = "camelCase")]
115pub struct ResourceReference {
116    pub r#type: String,
117    pub uri: String,
118}
119
120#[derive(Debug, Serialize)]
121#[serde(rename_all = "camelCase")]
122pub struct CompletionArgument {
123    pub name: String,
124    pub value: String,
125}
126
127#[derive(Debug, Deserialize)]
128#[serde(rename_all = "camelCase")]
129pub struct InitializeResponse {
130    pub protocol_version: u32,
131    pub capabilities: ServerCapabilities,
132    pub server_info: EntityInfo,
133}
134
135#[derive(Debug, Deserialize)]
136#[serde(rename_all = "camelCase")]
137pub struct ResourcesReadResponse {
138    pub contents: Vec<ResourceContent>,
139}
140
141#[derive(Debug, Deserialize)]
142#[serde(rename_all = "camelCase")]
143pub struct ResourcesListResponse {
144    pub resource_templates: Option<Vec<ResourceTemplate>>,
145    pub resources: Vec<Resource>,
146}
147
148#[derive(Debug, Deserialize)]
149#[serde(rename_all = "camelCase")]
150pub struct PromptsGetResponse {
151    pub description: Option<String>,
152    pub prompt: String,
153}
154
155#[derive(Debug, Deserialize)]
156#[serde(rename_all = "camelCase")]
157pub struct PromptsListResponse {
158    pub prompts: Vec<PromptInfo>,
159}
160
161#[derive(Debug, Deserialize)]
162#[serde(rename_all = "camelCase")]
163pub struct CompletionCompleteResponse {
164    pub completion: CompletionResult,
165}
166
167#[derive(Debug, Deserialize)]
168#[serde(rename_all = "camelCase")]
169pub struct CompletionResult {
170    pub values: Vec<String>,
171    pub total: Option<u32>,
172    pub has_more: Option<bool>,
173}
174
175#[derive(Debug, Deserialize, Clone)]
176#[serde(rename_all = "camelCase")]
177pub struct PromptInfo {
178    pub name: String,
179    pub arguments: Option<Vec<PromptArgument>>,
180}
181
182#[derive(Debug, Deserialize, Clone)]
183#[serde(rename_all = "camelCase")]
184pub struct PromptArgument {
185    pub name: String,
186    pub description: Option<String>,
187    pub required: Option<bool>,
188}
189
190// Shared Types
191
192#[derive(Debug, Serialize, Deserialize)]
193#[serde(rename_all = "camelCase")]
194pub struct ClientCapabilities {
195    pub experimental: Option<HashMap<String, serde_json::Value>>,
196    pub sampling: Option<HashMap<String, serde_json::Value>>,
197}
198
199#[derive(Debug, Serialize, Deserialize)]
200#[serde(rename_all = "camelCase")]
201pub struct ServerCapabilities {
202    pub experimental: Option<HashMap<String, serde_json::Value>>,
203    pub logging: Option<HashMap<String, serde_json::Value>>,
204    pub prompts: Option<HashMap<String, serde_json::Value>>,
205    pub resources: Option<ResourcesCapabilities>,
206    pub tools: Option<HashMap<String, serde_json::Value>>,
207}
208
209#[derive(Debug, Serialize, Deserialize)]
210#[serde(rename_all = "camelCase")]
211pub struct ResourcesCapabilities {
212    pub subscribe: Option<bool>,
213}
214
215#[derive(Debug, Serialize, Deserialize)]
216#[serde(rename_all = "camelCase")]
217pub struct Tool {
218    pub name: String,
219    pub description: Option<String>,
220    pub input_schema: serde_json::Value,
221}
222
223#[derive(Debug, Serialize, Deserialize)]
224#[serde(rename_all = "camelCase")]
225pub struct EntityInfo {
226    pub name: String,
227    pub version: String,
228}
229
230#[derive(Debug, Serialize, Deserialize)]
231#[serde(rename_all = "camelCase")]
232pub struct Resource {
233    pub uri: Url,
234    pub mime_type: Option<String>,
235}
236
237#[derive(Debug, Serialize, Deserialize)]
238#[serde(rename_all = "camelCase")]
239pub struct ResourceContent {
240    pub uri: Url,
241    pub mime_type: Option<String>,
242    pub content_type: String,
243    pub text: Option<String>,
244    pub data: Option<String>,
245}
246
247#[derive(Debug, Serialize, Deserialize)]
248#[serde(rename_all = "camelCase")]
249pub struct ResourceTemplate {
250    pub uri_template: String,
251    pub name: Option<String>,
252    pub description: Option<String>,
253}
254
255#[derive(Debug, Serialize, Deserialize)]
256#[serde(rename_all = "lowercase")]
257pub enum LoggingLevel {
258    Debug,
259    Info,
260    Warning,
261    Error,
262}
263
264// Client Notifications
265
266#[derive(Debug, Serialize)]
267#[serde(rename_all = "camelCase")]
268pub enum NotificationType {
269    Initialized,
270    Progress,
271}
272
273impl NotificationType {
274    pub fn as_str(&self) -> &'static str {
275        match self {
276            NotificationType::Initialized => "notifications/initialized",
277            NotificationType::Progress => "notifications/progress",
278        }
279    }
280}
281
282#[derive(Debug, Serialize)]
283#[serde(untagged)]
284pub enum ClientNotification {
285    Initialized,
286    Progress(ProgressParams),
287}
288
289#[derive(Debug, Serialize)]
290#[serde(rename_all = "camelCase")]
291pub struct ProgressParams {
292    pub progress_token: String,
293    pub progress: f64,
294    pub total: Option<f64>,
295}
296
297// Helper Types that don't map directly to the protocol
298
299pub enum CompletionTotal {
300    Exact(u32),
301    HasMore,
302    Unknown,
303}
304
305impl CompletionTotal {
306    pub fn from_options(has_more: Option<bool>, total: Option<u32>) -> Self {
307        match (has_more, total) {
308            (_, Some(count)) => CompletionTotal::Exact(count),
309            (Some(true), _) => CompletionTotal::HasMore,
310            _ => CompletionTotal::Unknown,
311        }
312    }
313}
314
315pub struct Completion {
316    pub values: Vec<String>,
317    pub total: CompletionTotal,
318}