Skip to main content

turul_mcp_protocol_2026_07_28/
initialize.rs

1//! Shared capability and implementation identity types for MCP 2026-07-28.
2//!
3//! 2026-07-28 is stateless (SEP-2567, SEP-2575) — there is no
4//! `initialize`/`notifications/initialized` handshake. These types
5//! ([`Implementation`], [`ClientCapabilities`], [`ServerCapabilities`])
6//! survive because they are referenced by
7//! [`crate::meta::RequestMetaObject`] (per-request negotiation) and
8//! [`crate::discover::DiscoverResult`].
9
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12use std::collections::HashMap;
13
14/// Describes the name and version of an MCP implementation.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct Implementation {
18    /// Name intended for programmatic use; clients display it when `title` is absent.
19    pub name: String,
20    /// Version string (e.g., "1.0.0").
21    pub version: String,
22    /// Optional human-friendly display title.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub title: Option<String>,
25    /// Optional human-readable description of this implementation.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub description: Option<String>,
28    /// Optional URL for the implementation's website.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub website_url: Option<String>,
31    /// Optional icons for display. Most implementations do not need icons.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub icons: Option<Vec<crate::icons::Icon>>,
34}
35
36impl Implementation {
37    /// Creates an implementation description from its required name and version, leaving all optional fields unset.
38    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
39        Self {
40            name: name.into(),
41            version: version.into(),
42            title: None,
43            description: None,
44            website_url: None,
45            icons: None,
46        }
47    }
48
49    /// Sets the human-readable display title.
50    pub fn with_title(mut self, title: impl Into<String>) -> Self {
51        self.title = Some(title.into());
52        self
53    }
54
55    /// Sets the human-readable description of the implementation.
56    pub fn with_description(mut self, description: impl Into<String>) -> Self {
57        self.description = Some(description.into());
58        self
59    }
60
61    /// Sets the URL of the implementation's website.
62    pub fn with_website_url(mut self, url: impl Into<String>) -> Self {
63        self.website_url = Some(url.into());
64        self
65    }
66
67    /// Sets the icons offered for display.
68    pub fn with_icons(mut self, icons: Vec<crate::icons::Icon>) -> Self {
69        self.icons = Some(icons);
70        self
71    }
72}
73
74/// Capabilities related to root listing support.
75///
76/// Schema: `ClientCapabilities.roots?: {}` — an empty object; presence alone
77/// means the client supports listing roots. This revision has no
78/// `notifications/roots/list_changed`, so there is no `listChanged` sub-field.
79///
80/// **Deprecated** per SEP-2577 alongside the Roots feature.
81#[deprecated(
82    since = "0.4.0",
83    note = "Deprecated per SEP-2577 (2026-07-28). \
84            Replacement: pass directories or files via tool parameters, resource URIs, or server configuration. \
85            Earliest removal: first release on/after 2027-07-28."
86)]
87#[derive(Debug, Clone, Serialize, Deserialize, Default)]
88#[serde(rename_all = "camelCase")]
89pub struct RootsCapabilities {}
90
91/// Capabilities related to sampling support.
92///
93/// 2026-07-28 adds two named sub-capabilities:
94/// - `context` — client supports `includeContext` parameter (soft-deprecated)
95/// - `tools`   — client supports `tools` and `toolChoice` parameters
96///
97/// Presence of the parent `sampling` field indicates baseline sampling support;
98/// presence of a sub-field declares the specific sub-capability. Empty `{}` is valid.
99///
100/// **Deprecated** per SEP-2577 alongside the Sampling feature.
101#[deprecated(
102    since = "0.4.0",
103    note = "Deprecated per SEP-2577 (2026-07-28). \
104            Replacement: integrate directly with LLM provider APIs. \
105            Earliest removal: first release on/after 2027-07-28."
106)]
107#[derive(Debug, Clone, Serialize, Deserialize, Default)]
108#[serde(rename_all = "camelCase")]
109pub struct SamplingCapabilities {
110    /// Whether the client supports context inclusion via `includeContext`.
111    /// Server MAY use `includeContext: "thisServer"`/`"allServers"` only if this
112    /// is declared.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
115    pub context: Option<HashMap<String, Value>>,
116
117    /// Whether the client supports tool use in sampling.
118    /// Server MUST get an error if it sends `tools`/`toolChoice` without this
119    /// declared.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
122    pub tools: Option<HashMap<String, Value>>,
123
124    /// Additional forward-compatible capability data.
125    #[serde(flatten)]
126    #[serde(serialize_with = "crate::serde_maps::sorted")]
127    pub extra: HashMap<String, Value>,
128}
129
130/// Capabilities related to elicitation support.
131///
132/// 2026-07-28 adds two named sub-capabilities:
133/// - `form` — client supports form-mode elicitation
134/// - `url`  — client supports URL-mode elicitation
135///
136/// Presence of the parent `elicitation` field indicates baseline elicitation
137/// support. Empty `{}` is valid (implicit form mode).
138#[derive(Debug, Clone, Serialize, Deserialize, Default)]
139#[serde(rename_all = "camelCase")]
140pub struct ElicitationCapabilities {
141    /// Form-mode elicitation support.
142    #[serde(skip_serializing_if = "Option::is_none")]
143    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
144    pub form: Option<HashMap<String, Value>>,
145
146    /// URL-mode elicitation support.
147    #[serde(skip_serializing_if = "Option::is_none")]
148    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
149    pub url: Option<HashMap<String, Value>>,
150
151    /// Additional forward-compatible capability data.
152    #[serde(flatten)]
153    #[serde(serialize_with = "crate::serde_maps::sorted")]
154    pub extra: HashMap<String, Value>,
155}
156
157/// Capabilities that a client may support.
158///
159/// 2026-07-28 shape:
160/// - `experimental?: { [k]: JSONObject }`
161/// - `roots?: {}`                — presence means client supports listing roots
162/// - `sampling?: { context?, tools? }`
163/// - `elicitation?: { form?, url? }`
164/// - `extensions?: { [k]: JSONObject }`  — reverse-DNS keyed extension capability map
165///
166/// Note: there is no `tasks` field — tasks are an extension in 2026-07-28
167/// (SEP-2663). Advertise tasks support via
168/// `extensions["io.modelcontextprotocol/tasks"]`.
169#[derive(Debug, Clone, Serialize, Deserialize, Default)]
170#[serde(rename_all = "camelCase")]
171#[allow(deprecated)]
172pub struct ClientCapabilities {
173    /// Root directory capabilities. **Deprecated** per SEP-2577.
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub roots: Option<RootsCapabilities>,
176    /// Sampling capabilities (client can handle sampling requests from server).
177    /// **Deprecated** per SEP-2577.
178    #[serde(skip_serializing_if = "Option::is_none")]
179    pub sampling: Option<SamplingCapabilities>,
180    /// Elicitation capabilities (client can handle elicitation requests from server).
181    #[serde(skip_serializing_if = "Option::is_none")]
182    pub elicitation: Option<ElicitationCapabilities>,
183    /// Experimental capabilities.
184    #[serde(skip_serializing_if = "Option::is_none")]
185    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
186    pub experimental: Option<HashMap<String, Value>>,
187    /// MCP extensions the client supports.
188    /// Keys are reverse-DNS extension identifiers (e.g.
189    /// `"io.modelcontextprotocol/oauth-client-credentials"`); values are
190    /// per-extension settings. Empty `{}` means support without settings.
191    #[serde(skip_serializing_if = "Option::is_none")]
192    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
193    pub extensions: Option<HashMap<String, Value>>,
194}
195
196/// Capabilities for prompts provided by the server.
197#[derive(Debug, Clone, Serialize, Deserialize, Default)]
198#[serde(rename_all = "camelCase")]
199pub struct PromptsCapabilities {
200    /// Whether the server supports prompt list change notifications
201    #[serde(skip_serializing_if = "Option::is_none")]
202    pub list_changed: Option<bool>,
203}
204
205/// Capabilities for tools provided by the server.
206#[derive(Debug, Clone, Serialize, Deserialize, Default)]
207#[serde(rename_all = "camelCase")]
208pub struct ToolsCapabilities {
209    /// Whether the server supports tool list change notifications
210    #[serde(skip_serializing_if = "Option::is_none")]
211    pub list_changed: Option<bool>,
212}
213
214// Tasks capabilities live in the tasks extension (SEP-2663). Advertise via
215// `extensions["io.modelcontextprotocol/tasks"]` on `ServerCapabilities`.
216
217/// Capabilities for resources provided by the server.
218#[derive(Debug, Clone, Serialize, Deserialize, Default)]
219#[serde(rename_all = "camelCase")]
220pub struct ResourcesCapabilities {
221    /// Whether the server supports resource subscriptions
222    #[serde(skip_serializing_if = "Option::is_none")]
223    pub subscribe: Option<bool>,
224    /// Whether the server supports resource list change notifications
225    #[serde(skip_serializing_if = "Option::is_none")]
226    pub list_changed: Option<bool>,
227}
228
229/// Capabilities for logging provided by the server.
230///
231/// Wire shape: an opaque `JSONObject` — PRESENCE means the server can send
232/// `notifications/message`; the object carries no defined keys.
233#[derive(Debug, Clone, Serialize, Deserialize, Default)]
234pub struct LoggingCapabilities {
235    /// Opaque content per the schema's `JSONObject` (usually empty).
236    #[serde(flatten)]
237    #[serde(serialize_with = "crate::serde_maps::sorted")]
238    pub extra: HashMap<String, Value>,
239}
240
241/// Capabilities for completions provided by the server.
242#[derive(Debug, Clone, Serialize, Deserialize, Default)]
243#[serde(rename_all = "camelCase")]
244pub struct CompletionsCapabilities {
245    /// Opaque content per the schema's `JSONObject` (usually empty) —
246    /// PRESENCE means `completion/complete` is supported.
247    #[serde(flatten)]
248    #[serde(serialize_with = "crate::serde_maps::sorted")]
249    pub extra: HashMap<String, Value>,
250}
251
252/// Capabilities that a server may support.
253///
254/// 2026-07-28 shape:
255/// - `experimental?: { [k]: JSONObject }`
256/// - `logging?: JSONObject`            — opaque object; presence means server can send `notifications/message`
257/// - `completions?: JSONObject`        — opaque object; presence means `completion/complete` is supported
258/// - `prompts?: { listChanged? }`
259/// - `resources?: { subscribe?, listChanged? }`
260/// - `tools?: { listChanged? }`
261/// - `extensions?: { [k]: JSONObject }`  — reverse-DNS keyed extension capability map
262#[derive(Debug, Clone, Serialize, Deserialize, Default)]
263#[serde(rename_all = "camelCase")]
264pub struct ServerCapabilities {
265    /// Logging capabilities.
266    #[serde(skip_serializing_if = "Option::is_none")]
267    #[deprecated(
268        since = "0.4.0",
269        note = "Deprecated per SEP-2577 (2026-07-28) — the Logging capability is being \
270                phased out. Earliest removal: first release on/after 2027-07-28."
271    )]
272    pub logging: Option<LoggingCapabilities>,
273    /// Completion capabilities.
274    #[serde(skip_serializing_if = "Option::is_none")]
275    pub completions: Option<CompletionsCapabilities>,
276    /// Prompt capabilities.
277    #[serde(skip_serializing_if = "Option::is_none")]
278    pub prompts: Option<PromptsCapabilities>,
279    /// Resource capabilities.
280    #[serde(skip_serializing_if = "Option::is_none")]
281    pub resources: Option<ResourcesCapabilities>,
282    /// Tool capabilities.
283    #[serde(skip_serializing_if = "Option::is_none")]
284    pub tools: Option<ToolsCapabilities>,
285    /// Experimental capabilities.
286    #[serde(skip_serializing_if = "Option::is_none")]
287    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
288    pub experimental: Option<HashMap<String, Value>>,
289    /// MCP extensions the server supports.
290    /// Keys are reverse-DNS extension identifiers (e.g.
291    /// `"io.modelcontextprotocol/apps"`); values are per-extension settings.
292    /// Empty `{}` means support without settings.
293    #[serde(skip_serializing_if = "Option::is_none")]
294    #[serde(serialize_with = "crate::serde_maps::sorted_opt")]
295    pub extensions: Option<HashMap<String, Value>>,
296}
297
298// 2026-07-28 is stateless (SEP-2567, SEP-2575) — there is no initialize
299// handshake. Client info and capabilities travel in `RequestMetaObject` on
300// every request; server info and capabilities come from `DiscoverResult`
301// (see [`crate::discover`]).
302
303#[cfg(test)]
304mod tests {
305    use super::*;
306
307    #[test]
308    fn test_implementation_creation() {
309        let impl_info = Implementation::new("test-client", "1.0.0").with_title("Test Client");
310
311        assert_eq!(impl_info.name, "test-client");
312        assert_eq!(impl_info.version, "1.0.0");
313        assert_eq!(impl_info.title, Some("Test Client".to_string()));
314    }
315
316    #[test]
317    #[allow(deprecated)]
318    fn roots_capabilities_matches_schema_empty_object() {
319        // Schema: `ClientCapabilities.roots?: {}` — no `listChanged` sub-field.
320        let v = serde_json::to_value(RootsCapabilities::default()).unwrap();
321        assert_eq!(
322            v,
323            serde_json::json!({}),
324            "RootsCapabilities must serialize as an empty object, not carry `listChanged`"
325        );
326    }
327
328    #[test]
329    #[allow(deprecated)]
330    fn roots_capabilities_ignores_unknown_fields_on_deserialize() {
331        // Forward-compat: extra keys (including `listChanged`) must not
332        // reject deserialization.
333        let wire = serde_json::json!({"listChanged": true});
334        let parsed: Result<RootsCapabilities, _> = serde_json::from_value(wire);
335        assert!(parsed.is_ok());
336    }
337}