Skip to main content

poem_mcpserver/protocol/
initialize.rs

1//! Initialize request and response.
2
3use serde::{Deserialize, Serialize};
4use time::Date;
5
6/// The client capabilities.
7#[derive(Debug, Deserialize)]
8pub struct ClientCapabilities {}
9
10/// The client information.
11#[derive(Debug, Deserialize)]
12pub struct ClientInfo {
13    /// The client name.
14    pub name: String,
15    /// The client version.
16    pub version: String,
17}
18
19/// An initialize request.
20#[derive(Debug, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct InitializeRequest {
23    /// The protocol version.
24    #[serde(with = "protocol_version_serde")]
25    pub protocol_version: Date,
26    /// The client capabilities.
27    pub capabilities: ClientCapabilities,
28    /// The client information.
29    pub client_info: ClientInfo,
30}
31
32/// Prompts capability.
33#[derive(Debug, Serialize)]
34#[serde(rename_all = "camelCase")]
35pub struct PromptsCapability {
36    /// Indicates whether the server will emit notifications when the list of
37    /// available prompts changes.
38    pub list_changed: bool,
39}
40
41/// Resources capability.
42#[derive(Debug, Serialize)]
43#[serde(rename_all = "camelCase")]
44pub struct ResourcesCapability {
45    /// Indicates whether the server will emit notifications when the list of
46    /// available prompts changes.
47    pub list_changed: bool,
48    /// Whether the client can subscribe to be notified of changes to individual
49    /// resources.
50    pub subscribe: bool,
51}
52
53/// Tools capability.
54#[derive(Debug, Serialize)]
55#[serde(rename_all = "camelCase")]
56pub struct ToolsCapability {
57    /// Indicates whether the server will emit notifications when the list of
58    /// available prompts changes.
59    pub list_changed: bool,
60}
61
62/// The server capabilities.
63#[derive(Debug, Serialize)]
64#[serde(rename_all = "camelCase")]
65pub struct ServerCapabilities {
66    /// Prompts capability.
67    pub prompts: PromptsCapability,
68    /// Resources capability.
69    pub resources: ResourcesCapability,
70    /// Tools capability.
71    pub tools: ToolsCapability,
72}
73
74/// The server information.
75#[derive(Debug, Clone, Serialize)]
76pub struct ServerInfo {
77    /// The server name.
78    pub name: String,
79    /// The server version.
80    pub version: String,
81}
82
83/// An initialize response.
84#[derive(Debug, Serialize)]
85#[serde(rename_all = "camelCase")]
86pub struct InitializeResponse {
87    /// The protocol version.
88    #[serde(with = "protocol_version_serde")]
89    pub protocol_version: Date,
90    /// The server capabilities.
91    pub capabilities: ServerCapabilities,
92    /// The server information.
93    pub server_info: ServerInfo,
94    /// The server instructions.
95    pub instructions: Option<String>,
96}
97
98mod protocol_version_serde {
99    use serde::{Deserialize, Deserializer, Serializer, de::Error as _};
100    use time::{Date, format_description::BorrowedFormatItem};
101
102    const DESC: &[BorrowedFormatItem] = time::macros::format_description!("[year]-[month]-[day]");
103
104    pub(super) fn serialize<S>(date: &Date, serializer: S) -> Result<S::Ok, S::Error>
105    where
106        S: Serializer,
107    {
108        serializer.serialize_str(&date.format(DESC).unwrap())
109    }
110
111    pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Date, D::Error>
112    where
113        D: Deserializer<'de>,
114    {
115        let s = String::deserialize(deserializer)?;
116        Date::parse(&s, DESC).map_err(|err| D::Error::custom(err.to_string()))
117    }
118}