mcp-protocol-sdk 0.5.1

Production-ready Rust SDK for the Model Context Protocol (MCP) with multiple transport support
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Prompt system for MCP servers
//!
//! This module provides the abstraction for implementing and managing prompts in MCP servers.
//! Prompts are templates that can be used to generate messages for language models.

use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;

use crate::core::error::{McpError, McpResult};
use crate::protocol::types::{
    Content, GetPromptResult as PromptResult, Prompt as PromptInfo, PromptArgument, PromptMessage,
    Role,
};

/// Trait for implementing prompt handlers
#[async_trait]
pub trait PromptHandler: Send + Sync {
    /// Generate prompt messages with the given arguments
    ///
    /// # Arguments
    /// * `arguments` - Prompt arguments as key-value pairs
    ///
    /// # Returns
    /// Result containing the generated prompt messages or an error
    async fn get(&self, arguments: HashMap<String, Value>) -> McpResult<PromptResult>;
}

/// A registered prompt with its handler
pub struct Prompt {
    /// Information about the prompt
    pub info: PromptInfo,
    /// Handler that implements the prompt's functionality
    pub handler: Box<dyn PromptHandler>,
    /// Whether the prompt is currently enabled
    pub enabled: bool,
}

impl Prompt {
    /// Create a new prompt with the given information and handler
    ///
    /// # Arguments
    /// * `info` - Information about the prompt
    /// * `handler` - Implementation of the prompt's functionality
    pub fn new<H>(info: PromptInfo, handler: H) -> Self
    where
        H: PromptHandler + 'static,
    {
        Self {
            info,
            handler: Box::new(handler),
            enabled: true,
        }
    }

    /// Enable the prompt
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Disable the prompt
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Check if the prompt is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Execute the prompt if it's enabled
    ///
    /// # Arguments
    /// * `arguments` - Prompt arguments as key-value pairs
    ///
    /// # Returns
    /// Result containing the prompt result or an error
    pub async fn get(&self, arguments: HashMap<String, Value>) -> McpResult<PromptResult> {
        if !self.enabled {
            return Err(McpError::validation(format!(
                "Prompt '{}' is disabled",
                self.info.name
            )));
        }

        // Validate required arguments
        if let Some(ref args) = self.info.arguments {
            for arg in args {
                if arg.required.unwrap_or(false) && !arguments.contains_key(&arg.name) {
                    return Err(McpError::validation(format!(
                        "Required argument '{}' missing for prompt '{}'",
                        arg.name, self.info.name
                    )));
                }
            }
        }

        self.handler.get(arguments).await
    }
}

impl std::fmt::Debug for Prompt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Prompt")
            .field("info", &self.info)
            .field("enabled", &self.enabled)
            .finish()
    }
}

impl PromptMessage {
    /// Create a system message
    pub fn system<S: Into<String>>(content: S) -> Self {
        Self {
            role: Role::User, // Note: 2025-06-18 only has User and Assistant roles
            content: Content::text(content.into()),
        }
    }

    /// Create a user message
    pub fn user<S: Into<String>>(content: S) -> Self {
        Self {
            role: Role::User,
            content: Content::text(content.into()),
        }
    }

    /// Create an assistant message
    pub fn assistant<S: Into<String>>(content: S) -> Self {
        Self {
            role: Role::Assistant,
            content: Content::text(content.into()),
        }
    }

    /// Create a message with custom role
    pub fn with_role(role: Role, content: Content) -> Self {
        Self { role, content }
    }
}

// Common prompt implementations

/// Simple greeting prompt
pub struct GreetingPrompt;

#[async_trait]
impl PromptHandler for GreetingPrompt {
    async fn get(&self, arguments: HashMap<String, Value>) -> McpResult<PromptResult> {
        let name = arguments
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("World");

        Ok(PromptResult {
            description: Some("A friendly greeting".to_string()),
            messages: vec![
                PromptMessage::system("You are a friendly assistant."),
                PromptMessage::user(format!("Hello, {name}!")),
            ],
            meta: None,
        })
    }
}

/// Code review prompt
pub struct CodeReviewPrompt;

#[async_trait]
impl PromptHandler for CodeReviewPrompt {
    async fn get(&self, arguments: HashMap<String, Value>) -> McpResult<PromptResult> {
        let code = arguments
            .get("code")
            .and_then(|v| v.as_str())
            .ok_or_else(|| McpError::validation("Missing 'code' argument"))?;

        let language = arguments
            .get("language")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown");

        let focus = arguments
            .get("focus")
            .and_then(|v| v.as_str())
            .unwrap_or("general");

        let system_prompt = format!(
            "You are an expert code reviewer. Focus on {focus} aspects of the code. \
             Provide constructive feedback and suggestions for improvement."
        );

        let user_prompt =
            format!("Please review this {language} code:\n\n```{language}\n{code}\n```");

        Ok(PromptResult {
            description: Some("Code review prompt".to_string()),
            messages: vec![
                PromptMessage::system(system_prompt),
                PromptMessage::user(user_prompt),
            ],
            meta: None,
        })
    }
}

/// SQL query generation prompt
pub struct SqlQueryPrompt;

#[async_trait]
impl PromptHandler for SqlQueryPrompt {
    async fn get(&self, arguments: HashMap<String, Value>) -> McpResult<PromptResult> {
        let request = arguments
            .get("request")
            .and_then(|v| v.as_str())
            .ok_or_else(|| McpError::validation("Missing 'request' argument"))?;

        let schema = arguments
            .get("schema")
            .and_then(|v| v.as_str())
            .unwrap_or("No schema provided");

        let dialect = arguments
            .get("dialect")
            .and_then(|v| v.as_str())
            .unwrap_or("PostgreSQL");

        let system_prompt = format!(
            "You are an expert SQL developer. Generate efficient and safe {dialect} queries. \
             Always use proper escaping and avoid SQL injection vulnerabilities."
        );

        let user_prompt = format!(
            "Database Schema:\n{schema}\n\nRequest: {request}\n\nPlease generate a {dialect} query for this request."
        );

        Ok(PromptResult {
            description: Some("SQL query generation prompt".to_string()),
            messages: vec![
                PromptMessage::system(system_prompt),
                PromptMessage::user(user_prompt),
            ],
            meta: None,
        })
    }
}

/// Builder for creating prompts with fluent API
pub struct PromptBuilder {
    name: String,
    description: Option<String>,
    arguments: Vec<PromptArgument>,
}

impl PromptBuilder {
    /// Create a new prompt builder with the given name
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            name: name.into(),
            description: None,
            arguments: Vec::new(),
        }
    }

    /// Set the prompt description
    pub fn description<S: Into<String>>(mut self, description: S) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Add a required argument
    pub fn required_arg<S: Into<String>>(mut self, name: S, description: Option<S>) -> Self {
        self.arguments.push(PromptArgument {
            name: name.into(),
            description: description.map(|d| d.into()),
            required: Some(true),
            title: None,
        });
        self
    }

    /// Add an optional argument
    pub fn optional_arg<S: Into<String>>(mut self, name: S, description: Option<S>) -> Self {
        self.arguments.push(PromptArgument {
            name: name.into(),
            description: description.map(|d| d.into()),
            required: Some(false),
            title: None,
        });
        self
    }

    /// Build the prompt with the given handler
    pub fn build<H>(self, handler: H) -> Prompt
    where
        H: PromptHandler + 'static,
    {
        let info = PromptInfo {
            name: self.name,
            description: self.description,
            arguments: if self.arguments.is_empty() {
                None
            } else {
                Some(self.arguments)
            },
            title: None,
            meta: None,
        };

        Prompt::new(info, handler)
    }
}

/// Utility for creating prompt arguments
pub fn required_arg<S: Into<String>>(name: S, description: Option<S>) -> PromptArgument {
    PromptArgument {
        name: name.into(),
        description: description.map(|d| d.into()),
        required: Some(true),
        title: None,
    }
}

/// Utility for creating optional prompt arguments
pub fn optional_arg<S: Into<String>>(name: S, description: Option<S>) -> PromptArgument {
    PromptArgument {
        name: name.into(),
        description: description.map(|d| d.into()),
        required: Some(false),
        title: None,
    }
}

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

    #[tokio::test]
    async fn test_greeting_prompt() {
        let prompt = GreetingPrompt;
        let mut args = HashMap::new();
        args.insert("name".to_string(), json!("Alice"));

        let result = prompt.get(args).await.unwrap();
        assert_eq!(result.messages.len(), 2);
        assert_eq!(result.messages[0].role, Role::User);
        assert_eq!(result.messages[1].role, Role::User);

        match &result.messages[1].content {
            Content::Text { text, .. } => assert!(text.contains("Alice")),
            _ => panic!("Expected text content"),
        }
    }

    #[tokio::test]
    async fn test_code_review_prompt() {
        let prompt = CodeReviewPrompt;
        let mut args = HashMap::new();
        args.insert(
            "code".to_string(),
            json!("function hello() { console.log('Hello'); }"),
        );
        args.insert("language".to_string(), json!("javascript"));
        args.insert("focus".to_string(), json!("performance"));

        let result = prompt.get(args).await.unwrap();
        assert_eq!(result.messages.len(), 2);

        match &result.messages[1].content {
            Content::Text { text, .. } => {
                assert!(text.contains("javascript"));
                assert!(text.contains("console.log"));
            }
            _ => panic!("Expected text content"),
        }
    }

    #[test]
    fn test_prompt_creation() {
        let info = PromptInfo {
            name: "test_prompt".to_string(),
            description: Some("Test prompt".to_string()),
            arguments: Some(vec![PromptArgument {
                name: "arg1".to_string(),
                description: Some("First argument".to_string()),
                required: Some(true),
                title: None,
            }]),
            title: None,
            meta: None,
        };

        let prompt = Prompt::new(info.clone(), GreetingPrompt);
        assert_eq!(prompt.info, info);
        assert!(prompt.is_enabled());
    }

    #[tokio::test]
    async fn test_prompt_validation() {
        let info = PromptInfo {
            name: "test_prompt".to_string(),
            description: None,
            arguments: Some(vec![PromptArgument {
                name: "required_arg".to_string(),
                description: None,
                required: Some(true),
                title: None,
            }]),
            title: None,
            meta: None,
        };

        let prompt = Prompt::new(info, GreetingPrompt);

        // Test missing required argument
        let result = prompt.get(HashMap::new()).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            McpError::Validation(msg) => assert!(msg.contains("required_arg")),
            _ => panic!("Expected validation error"),
        }
    }

    #[test]
    fn test_prompt_builder() {
        let prompt = PromptBuilder::new("test")
            .description("A test prompt")
            .required_arg("input", Some("Input text"))
            .optional_arg("format", Some("Output format"))
            .build(GreetingPrompt);

        assert_eq!(prompt.info.name, "test");
        assert_eq!(prompt.info.description, Some("A test prompt".to_string()));

        let args = prompt.info.arguments.unwrap();
        assert_eq!(args.len(), 2);
        assert_eq!(args[0].name, "input");
        assert_eq!(args[0].required, Some(true));
        assert_eq!(args[1].name, "format");
        assert_eq!(args[1].required, Some(false));
    }

    #[test]
    fn test_prompt_message_creation() {
        let system_msg = PromptMessage::system("You are a helpful assistant");
        assert_eq!(system_msg.role, Role::User);

        let user_msg = PromptMessage::user("Hello!");
        assert_eq!(user_msg.role, Role::User);

        let assistant_msg = PromptMessage::assistant("Hi there!");
        assert_eq!(assistant_msg.role, Role::Assistant);
    }

    #[test]
    fn test_prompt_content_creation() {
        let text_content = Content::text("Hello, world!");
        match text_content {
            Content::Text { text, .. } => {
                assert_eq!(text, "Hello, world!");
            }
            _ => panic!("Expected text content"),
        }

        let image_content = Content::image("base64data", "image/png");
        match image_content {
            Content::Image {
                data, mime_type, ..
            } => {
                assert_eq!(data, "base64data");
                assert_eq!(mime_type, "image/png");
            }
            _ => panic!("Expected image content"),
        }
    }
}