Skip to main content

mcp_kit/types/
prompt.rs

1use serde::{Deserialize, Serialize};
2
3use super::content::Content;
4
5/// A prompt template exposed by the server
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Prompt {
8    pub name: String,
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub description: Option<String>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub arguments: Option<Vec<PromptArgument>>,
13}
14
15impl Prompt {
16    pub fn new(name: impl Into<String>) -> Self {
17        Self {
18            name: name.into(),
19            description: None,
20            arguments: None,
21        }
22    }
23
24    pub fn with_description(mut self, description: impl Into<String>) -> Self {
25        self.description = Some(description.into());
26        self
27    }
28
29    pub fn with_arguments(mut self, args: Vec<PromptArgument>) -> Self {
30        self.arguments = Some(args);
31        self
32    }
33}
34
35/// An argument that a prompt accepts
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct PromptArgument {
38    pub name: String,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub description: Option<String>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub required: Option<bool>,
43}
44
45impl PromptArgument {
46    pub fn required(name: impl Into<String>) -> Self {
47        Self {
48            name: name.into(),
49            description: None,
50            required: Some(true),
51        }
52    }
53
54    pub fn optional(name: impl Into<String>) -> Self {
55        Self {
56            name: name.into(),
57            description: None,
58            required: Some(false),
59        }
60    }
61
62    pub fn with_description(mut self, description: impl Into<String>) -> Self {
63        self.description = Some(description.into());
64        self
65    }
66}
67
68/// Role in a conversation
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
70#[serde(rename_all = "lowercase")]
71pub enum PromptMessageRole {
72    User,
73    Assistant,
74}
75
76/// A single message in a prompt result
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct PromptMessage {
79    pub role: PromptMessageRole,
80    pub content: Content,
81}
82
83impl PromptMessage {
84    pub fn user(content: impl Into<Content>) -> Self {
85        Self {
86            role: PromptMessageRole::User,
87            content: content.into(),
88        }
89    }
90
91    pub fn assistant(content: impl Into<Content>) -> Self {
92        Self {
93            role: PromptMessageRole::Assistant,
94            content: content.into(),
95        }
96    }
97
98    pub fn user_text(text: impl Into<String>) -> Self {
99        Self::user(Content::text(text))
100    }
101
102    pub fn assistant_text(text: impl Into<String>) -> Self {
103        Self::assistant(Content::text(text))
104    }
105}
106
107/// Result of a prompts/get call
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct GetPromptResult {
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub description: Option<String>,
112    pub messages: Vec<PromptMessage>,
113}
114
115impl GetPromptResult {
116    pub fn new(messages: Vec<PromptMessage>) -> Self {
117        Self {
118            description: None,
119            messages,
120        }
121    }
122
123    pub fn with_description(mut self, description: impl Into<String>) -> Self {
124        self.description = Some(description.into());
125        self
126    }
127}