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 text: Option<String>,
243    pub data: Option<String>,
244}
245
246#[derive(Debug, Serialize, Deserialize)]
247#[serde(rename_all = "camelCase")]
248pub struct ResourceTemplate {
249    pub uri_template: String,
250    pub name: Option<String>,
251    pub description: Option<String>,
252}
253
254#[derive(Debug, Serialize, Deserialize)]
255#[serde(rename_all = "lowercase")]
256pub enum LoggingLevel {
257    Debug,
258    Info,
259    Warning,
260    Error,
261}
262
263// Client Notifications
264
265#[derive(Debug, Serialize)]
266#[serde(rename_all = "camelCase")]
267pub enum NotificationType {
268    Initialized,
269    Progress,
270}
271
272impl NotificationType {
273    pub fn as_str(&self) -> &'static str {
274        match self {
275            NotificationType::Initialized => "notifications/initialized",
276            NotificationType::Progress => "notifications/progress",
277        }
278    }
279}
280
281#[derive(Debug, Serialize)]
282#[serde(untagged)]
283pub enum ClientNotification {
284    Initialized,
285    Progress(ProgressParams),
286}
287
288#[derive(Debug, Serialize)]
289#[serde(rename_all = "camelCase")]
290pub struct ProgressParams {
291    pub progress_token: String,
292    pub progress: f64,
293    pub total: Option<f64>,
294}
295
296// Helper Types that don't map directly to the protocol
297
298pub enum CompletionTotal {
299    Exact(u32),
300    HasMore,
301    Unknown,
302}
303
304impl CompletionTotal {
305    pub fn from_options(has_more: Option<bool>, total: Option<u32>) -> Self {
306        match (has_more, total) {
307            (_, Some(count)) => CompletionTotal::Exact(count),
308            (Some(true), _) => CompletionTotal::HasMore,
309            _ => CompletionTotal::Unknown,
310        }
311    }
312}
313
314pub struct Completion {
315    pub values: Vec<String>,
316    pub total: CompletionTotal,
317}