mcp-probe-core 0.3.0

Core MCP (Model Context Protocol) types, traits, and transport implementations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Sampling-related message types for MCP LLM completion requests.
//!
//! This module provides types for:
//! - Server-to-client LLM completion requests
//! - Completion parameters (temperature, max tokens, etc.)
//! - Completion responses with generated content
//! - Model selection and configuration

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Request from server to client for LLM completion.
///
/// This allows MCP servers to request LLM completions from the client,
/// enabling servers to leverage the client's LLM capabilities.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompleteRequest {
    /// The completion argument
    pub argument: CompletionArgument,
}

/// Arguments for a completion request.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CompletionArgument {
    /// Messages for the completion
    pub messages: Vec<SamplingMessage>,

    /// Optional model selection
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model_preferences: Option<ModelPreferences>,

    /// System prompt for the completion
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_prompt: Option<String>,

    /// Include context about tools available to the model
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_context: Option<String>,

    /// Temperature for sampling (0.0 to 1.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,

    /// Maximum number of tokens to generate
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<i32>,

    /// Stop sequences for completion
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_sequences: Option<Vec<String>>,

    /// Additional metadata for the request
    #[serde(flatten)]
    pub metadata: HashMap<String, Value>,
}

impl CompletionArgument {
    /// Create a new completion argument with messages.
    pub fn new(messages: Vec<SamplingMessage>) -> Self {
        Self {
            messages,
            model_preferences: None,
            system_prompt: None,
            include_context: None,
            temperature: None,
            max_tokens: None,
            stop_sequences: None,
            metadata: HashMap::new(),
        }
    }

    /// Set model preferences.
    pub fn with_model_preferences(mut self, preferences: ModelPreferences) -> Self {
        self.model_preferences = Some(preferences);
        self
    }

    /// Set system prompt.
    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }

    /// Set temperature.
    pub fn with_temperature(mut self, temperature: f64) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set maximum tokens.
    pub fn with_max_tokens(mut self, max_tokens: i32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Add stop sequences.
    pub fn with_stop_sequences(mut self, sequences: Vec<String>) -> Self {
        self.stop_sequences = Some(sequences);
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
        self.metadata.insert(key.into(), value);
        self
    }
}

/// Model preferences for completion requests.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ModelPreferences {
    /// Preferred model names in order of preference
    #[serde(skip_serializing_if = "Option::is_none")]
    pub models: Option<Vec<String>>,

    /// Minimum cost tier acceptable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cost_priority: Option<CostPriority>,

    /// Minimum speed tier acceptable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub speed_priority: Option<SpeedPriority>,

    /// Minimum intelligence tier acceptable
    #[serde(skip_serializing_if = "Option::is_none")]
    pub intelligence_priority: Option<IntelligencePriority>,
}

impl ModelPreferences {
    /// Create new model preferences.
    pub fn new() -> Self {
        Self {
            models: None,
            cost_priority: None,
            speed_priority: None,
            intelligence_priority: None,
        }
    }

    /// Set preferred models.
    pub fn with_models(mut self, models: Vec<String>) -> Self {
        self.models = Some(models);
        self
    }

    /// Set cost priority.
    pub fn with_cost_priority(mut self, priority: CostPriority) -> Self {
        self.cost_priority = Some(priority);
        self
    }

    /// Set speed priority.
    pub fn with_speed_priority(mut self, priority: SpeedPriority) -> Self {
        self.speed_priority = Some(priority);
        self
    }

    /// Set intelligence priority.
    pub fn with_intelligence_priority(mut self, priority: IntelligencePriority) -> Self {
        self.intelligence_priority = Some(priority);
        self
    }
}

impl Default for ModelPreferences {
    fn default() -> Self {
        Self::new()
    }
}

/// Cost priority levels for model selection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CostPriority {
    /// Lowest cost models
    Low,
    /// Medium cost models
    Medium,
    /// High cost models acceptable
    High,
}

/// Speed priority levels for model selection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SpeedPriority {
    /// Slowest acceptable speed
    Low,
    /// Medium speed required
    Medium,
    /// High speed required
    High,
}

/// Intelligence priority levels for model selection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum IntelligencePriority {
    /// Basic intelligence level
    Low,
    /// Medium intelligence level
    Medium,
    /// High intelligence level required
    High,
}

/// A message in a sampling request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SamplingMessage {
    /// Role of the message
    pub role: MessageRole,

    /// Content of the message
    pub content: SamplingContent,
}

impl SamplingMessage {
    /// Create a new sampling message.
    pub fn new(role: MessageRole, content: SamplingContent) -> Self {
        Self { role, content }
    }

    /// Create a system message.
    pub fn system(content: impl Into<String>) -> Self {
        Self::new(MessageRole::System, SamplingContent::text(content))
    }

    /// Create a user message.
    pub fn user(content: impl Into<String>) -> Self {
        Self::new(MessageRole::User, SamplingContent::text(content))
    }

    /// Create an assistant message.
    pub fn assistant(content: impl Into<String>) -> Self {
        Self::new(MessageRole::Assistant, SamplingContent::text(content))
    }
}

/// Role of a message in sampling.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// System message
    System,
    /// User message
    User,
    /// Assistant message
    Assistant,
}

/// Content of a sampling message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SamplingContent {
    /// Text content
    #[serde(rename = "text")]
    Text {
        /// The text content
        text: String,
    },

    /// Image content
    #[serde(rename = "image")]
    Image {
        /// Image data (base64 or URL)
        data: String,

        /// MIME type of the image
        #[serde(rename = "mimeType")]
        mime_type: String,
    },
}

impl SamplingContent {
    /// Create text content.
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }

    /// Create image content.
    pub fn image(data: impl Into<String>, mime_type: impl Into<String>) -> Self {
        Self::Image {
            data: data.into(),
            mime_type: mime_type.into(),
        }
    }
}

/// Response to a completion request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompleteResponse {
    /// Completion result
    pub completion: CompletionResult,

    /// Model used for the completion
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,

    /// Stop reason for the completion
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<StopReason>,
}

/// Result of a completion request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum CompletionResult {
    /// Text completion result
    #[serde(rename = "text")]
    Text {
        /// Generated text
        text: String,
    },
}

impl CompletionResult {
    /// Create a text completion result.
    pub fn text(text: impl Into<String>) -> Self {
        Self::Text { text: text.into() }
    }
}

/// Reason why completion stopped.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopReason {
    /// Reached end of sequence naturally
    EndTurn,
    /// Hit maximum token limit
    MaxTokens,
    /// Encountered stop sequence
    StopSequence,
    /// Tool call was made
    ToolUse,
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_completion_argument_creation() {
        let messages = vec![
            SamplingMessage::system("You are a helpful assistant"),
            SamplingMessage::user("Hello, how are you?"),
        ];

        let arg = CompletionArgument::new(messages)
            .with_temperature(0.7)
            .with_max_tokens(1000)
            .with_system_prompt("Be helpful")
            .with_metadata("priority", json!("high"));

        assert_eq!(arg.temperature, Some(0.7));
        assert_eq!(arg.max_tokens, Some(1000));
        assert_eq!(arg.system_prompt, Some("Be helpful".to_string()));
        assert_eq!(arg.metadata.get("priority"), Some(&json!("high")));
    }

    #[test]
    fn test_model_preferences() {
        let prefs = ModelPreferences::new()
            .with_models(vec!["gpt-4".to_string(), "claude-3".to_string()])
            .with_cost_priority(CostPriority::Medium)
            .with_speed_priority(SpeedPriority::High)
            .with_intelligence_priority(IntelligencePriority::High);

        assert_eq!(
            prefs.models,
            Some(vec!["gpt-4".to_string(), "claude-3".to_string()])
        );
        assert_eq!(prefs.cost_priority, Some(CostPriority::Medium));
        assert_eq!(prefs.speed_priority, Some(SpeedPriority::High));
        assert_eq!(
            prefs.intelligence_priority,
            Some(IntelligencePriority::High)
        );
    }

    #[test]
    fn test_sampling_message_creation() {
        let system_msg = SamplingMessage::system("You are helpful");
        let user_msg = SamplingMessage::user("Hello");
        let assistant_msg = SamplingMessage::assistant("Hi there!");

        assert_eq!(system_msg.role, MessageRole::System);
        assert_eq!(user_msg.role, MessageRole::User);
        assert_eq!(assistant_msg.role, MessageRole::Assistant);
    }

    #[test]
    fn test_sampling_content_text() {
        let content = SamplingContent::text("Hello world");
        let json = serde_json::to_value(&content).unwrap();
        assert_eq!(json["type"], "text");
        assert_eq!(json["text"], "Hello world");
    }

    #[test]
    fn test_sampling_content_image() {
        let content = SamplingContent::image("base64data", "image/png");
        let json = serde_json::to_value(&content).unwrap();
        assert_eq!(json["type"], "image");
        assert_eq!(json["data"], "base64data");
        assert_eq!(json["mimeType"], "image/png");
    }

    #[test]
    fn test_completion_result() {
        let result = CompletionResult::text("Generated response");
        let json = serde_json::to_value(&result).unwrap();
        assert_eq!(json["type"], "text");
        assert_eq!(json["text"], "Generated response");
    }

    #[test]
    fn test_priority_serialization() {
        let cost = CostPriority::Low;
        let speed = SpeedPriority::Medium;
        let intel = IntelligencePriority::High;

        assert_eq!(serde_json::to_string(&cost).unwrap(), "\"low\"");
        assert_eq!(serde_json::to_string(&speed).unwrap(), "\"medium\"");
        assert_eq!(serde_json::to_string(&intel).unwrap(), "\"high\"");
    }

    #[test]
    fn test_stop_reason_serialization() {
        let reasons = [
            StopReason::EndTurn,
            StopReason::MaxTokens,
            StopReason::StopSequence,
            StopReason::ToolUse,
        ];

        let expected = [
            "\"end_turn\"",
            "\"max_tokens\"",
            "\"stop_sequence\"",
            "\"tool_use\"",
        ];

        for (reason, expected) in reasons.iter().zip(expected.iter()) {
            assert_eq!(serde_json::to_string(reason).unwrap(), *expected);
        }
    }
}