Skip to main content

rust_mcp_schema/generated_schema/2026_07_28/
validators.rs

1/// Validates that a deserialized string field matches a given constant value.
2///
3/// This function is intended for use with `#[serde(deserialize_with)]` to enforce
4/// that a field in a struct always has a fixed, expected string value during deserialization.
5///
6/// # Parameters
7/// - `struct_name`: The name of the struct where this validation is applied.
8/// - `field_name`: The name of the field being validated.
9/// - `expected`: The expected constant string value for the field.
10/// - `deserializer`: The Serde deserializer for the field.
11///
12/// # Returns
13/// - `Ok(String)` if the deserialized value matches the expected value.
14/// - `Err(D::Error)` if the value differs, with an error message indicating
15///   which struct and field failed validation.
16///
17pub fn const_str_validator<'de, D>(
18    struct_name: &'static str,
19    field_name: &'static str,
20    expected: &'static str,
21    deserializer: D,
22) -> Result<String, D::Error>
23where
24    D: serde::de::Deserializer<'de>,
25{
26    let value: String = serde::Deserialize::deserialize(deserializer)?;
27    if value == expected {
28        Ok(value)
29    } else {
30        Err(serde::de::Error::custom(format!(
31            "Expected field `{field_name}` in struct `{struct_name}` as const value '{expected}', but got '{value}'",
32        )))
33    }
34}
35
36/// Validator for `Option<String>` fields:
37/// - None      → accepted
38/// - Some(s)   → s must exactly match `expected`
39pub fn const_str_option_validator<'de, D>(
40    struct_name: &'static str,
41    field_name: &'static str,
42    expected: &'static str,
43    deserializer: D,
44) -> Result<Option<String>, D::Error>
45where
46    D: serde::de::Deserializer<'de>,
47{
48    let opt: Option<String> = serde::Deserialize::deserialize(deserializer)?;
49    match opt {
50        Some(ref value) if value != expected => {
51            Err(serde::de::Error::custom(format!(
52                "Expected field `{field_name}` in struct `{struct_name}` to be None or exactly \"{expected}\", but got Some(\"{value}\")",
53            )))
54        }
55        Some(value) => Ok(Some(value)), // value == expected
56        None => Ok(None),
57    }
58}
59
60// Used only by the `i64` arm of `validate!`; some schema versions (e.g. 2026-07-28) have no
61// integer const properties, so it would otherwise be flagged as dead code there.
62#[allow(dead_code)]
63fn i64_validator<'de, D>(
64    struct_name: &'static str,
65    field_name: &'static str,
66    expected: i64,
67    deserializer: D,
68) -> Result<i64, D::Error>
69where
70    D: serde::de::Deserializer<'de>,
71{
72    let value = serde::Deserialize::deserialize(deserializer)?;
73    if value == expected {
74        Ok(value)
75    } else {
76        Err(serde::de::Error::custom(format!(
77            "Invalid {struct_name}::{field_name}: expected {expected}, got {value}"
78        )))
79    }
80}
81
82macro_rules! validate {
83    // === String validation (required) ===
84    ($func_name:ident,  $struct:expr, $field:expr, $expected:expr $(,)?) => {
85        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<String, D::Error>
86        where
87            D: serde::de::Deserializer<'de>,
88        {
89            const_str_validator($struct, $field, $expected, deserializer)
90        }
91    };
92
93    // Optional String case (with trailing `, option`)
94    ($func_name:ident, $struct:expr, $field:expr, $expected:expr, option $(,)?) => {
95        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
96        where
97            D: serde::de::Deserializer<'de>,
98        {
99            const_str_option_validator($struct, $field, $expected, deserializer)
100        }
101    };
102
103    // === i64 validation (required) ===
104    ($func_name:ident, $struct:expr, $field:expr, $expected:expr, i64 $(,)?) => {
105        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<i64, D::Error>
106        where
107            D: serde::de::Deserializer<'de>,
108        {
109            i64_validator($struct, $field, $expected, deserializer)
110        }
111    };
112}
113
114//* Validator Functions *//
115validate!(audio_content_type_, "AudioContent", "type_", "audio");
116validate!(boolean_schema_type_, "BooleanSchema", "type_", "boolean");
117validate!(call_tool_request_jsonrpc, "CallToolRequest", "jsonrpc", "2.0");
118validate!(call_tool_request_method, "CallToolRequest", "method", "tools/call");
119validate!(call_tool_result_response_jsonrpc, "CallToolResultResponse", "jsonrpc", "2.0");
120validate!(cancelled_notification_jsonrpc, "CancelledNotification", "jsonrpc", "2.0");
121validate!(
122    cancelled_notification_method,
123    "CancelledNotification",
124    "method",
125    "notifications/cancelled"
126);
127validate!(client_notification_jsonrpc, "ClientNotification", "jsonrpc", "2.0");
128validate!(
129    client_notification_method,
130    "ClientNotification",
131    "method",
132    "notifications/cancelled"
133);
134validate!(complete_request_jsonrpc, "CompleteRequest", "jsonrpc", "2.0");
135validate!(complete_request_method, "CompleteRequest", "method", "completion/complete");
136validate!(complete_result_response_jsonrpc, "CompleteResultResponse", "jsonrpc", "2.0");
137validate!(
138    create_message_request_method,
139    "CreateMessageRequest",
140    "method",
141    "sampling/createMessage"
142);
143validate!(discover_request_jsonrpc, "DiscoverRequest", "jsonrpc", "2.0");
144validate!(discover_request_method, "DiscoverRequest", "method", "server/discover");
145validate!(discover_result_response_jsonrpc, "DiscoverResultResponse", "jsonrpc", "2.0");
146validate!(elicit_request_method, "ElicitRequest", "method", "elicitation/create");
147validate!(
148    elicit_request_form_params_mode,
149    "ElicitRequestFormParams",
150    "mode",
151    "form",
152    option
153);
154validate!(
155    elicit_request_form_params_requested_schema_type_,
156    "ElicitRequestFormParamsRequestedSchema",
157    "type_",
158    "object"
159);
160validate!(elicit_request_url_params_mode, "ElicitRequestUrlParams", "mode", "url");
161validate!(embedded_resource_type_, "EmbeddedResource", "type_", "resource");
162validate!(get_prompt_request_jsonrpc, "GetPromptRequest", "jsonrpc", "2.0");
163validate!(get_prompt_request_method, "GetPromptRequest", "method", "prompts/get");
164validate!(
165    get_prompt_result_response_jsonrpc,
166    "GetPromptResultResponse",
167    "jsonrpc",
168    "2.0"
169);
170validate!(header_mismatch_error_jsonrpc, "HeaderMismatchError", "jsonrpc", "2.0");
171validate!(image_content_type_, "ImageContent", "type_", "image");
172validate!(jsonrpc_error_response_jsonrpc, "JsonrpcErrorResponse", "jsonrpc", "2.0");
173validate!(jsonrpc_notification_jsonrpc, "JsonrpcNotification", "jsonrpc", "2.0");
174validate!(jsonrpc_request_jsonrpc, "JsonrpcRequest", "jsonrpc", "2.0");
175validate!(jsonrpc_result_response_jsonrpc, "JsonrpcResultResponse", "jsonrpc", "2.0");
176validate!(legacy_titled_enum_schema_type_, "LegacyTitledEnumSchema", "type_", "string");
177validate!(list_prompts_request_jsonrpc, "ListPromptsRequest", "jsonrpc", "2.0");
178validate!(list_prompts_request_method, "ListPromptsRequest", "method", "prompts/list");
179validate!(
180    list_prompts_result_response_jsonrpc,
181    "ListPromptsResultResponse",
182    "jsonrpc",
183    "2.0"
184);
185validate!(
186    list_resource_templates_request_jsonrpc,
187    "ListResourceTemplatesRequest",
188    "jsonrpc",
189    "2.0"
190);
191validate!(
192    list_resource_templates_request_method,
193    "ListResourceTemplatesRequest",
194    "method",
195    "resources/templates/list"
196);
197validate!(
198    list_resource_templates_result_response_jsonrpc,
199    "ListResourceTemplatesResultResponse",
200    "jsonrpc",
201    "2.0"
202);
203validate!(list_resources_request_jsonrpc, "ListResourcesRequest", "jsonrpc", "2.0");
204validate!(
205    list_resources_request_method,
206    "ListResourcesRequest",
207    "method",
208    "resources/list"
209);
210validate!(
211    list_resources_result_response_jsonrpc,
212    "ListResourcesResultResponse",
213    "jsonrpc",
214    "2.0"
215);
216validate!(list_roots_request_method, "ListRootsRequest", "method", "roots/list");
217validate!(list_tools_request_jsonrpc, "ListToolsRequest", "jsonrpc", "2.0");
218validate!(list_tools_request_method, "ListToolsRequest", "method", "tools/list");
219validate!(
220    list_tools_result_response_jsonrpc,
221    "ListToolsResultResponse",
222    "jsonrpc",
223    "2.0"
224);
225validate!(
226    logging_message_notification_jsonrpc,
227    "LoggingMessageNotification",
228    "jsonrpc",
229    "2.0"
230);
231validate!(
232    logging_message_notification_method,
233    "LoggingMessageNotification",
234    "method",
235    "notifications/message"
236);
237validate!(
238    missing_required_client_capability_error_jsonrpc,
239    "MissingRequiredClientCapabilityError",
240    "jsonrpc",
241    "2.0"
242);
243validate!(paginated_request_jsonrpc, "PaginatedRequest", "jsonrpc", "2.0");
244validate!(progress_notification_jsonrpc, "ProgressNotification", "jsonrpc", "2.0");
245validate!(
246    progress_notification_method,
247    "ProgressNotification",
248    "method",
249    "notifications/progress"
250);
251validate!(
252    prompt_list_changed_notification_jsonrpc,
253    "PromptListChangedNotification",
254    "jsonrpc",
255    "2.0"
256);
257validate!(
258    prompt_list_changed_notification_method,
259    "PromptListChangedNotification",
260    "method",
261    "notifications/prompts/list_changed"
262);
263validate!(prompt_reference_type_, "PromptReference", "type_", "ref/prompt");
264validate!(read_resource_request_jsonrpc, "ReadResourceRequest", "jsonrpc", "2.0");
265validate!(
266    read_resource_request_method,
267    "ReadResourceRequest",
268    "method",
269    "resources/read"
270);
271validate!(
272    read_resource_result_response_jsonrpc,
273    "ReadResourceResultResponse",
274    "jsonrpc",
275    "2.0"
276);
277validate!(resource_link_type_, "ResourceLink", "type_", "resource_link");
278validate!(
279    resource_list_changed_notification_jsonrpc,
280    "ResourceListChangedNotification",
281    "jsonrpc",
282    "2.0"
283);
284validate!(
285    resource_list_changed_notification_method,
286    "ResourceListChangedNotification",
287    "method",
288    "notifications/resources/list_changed"
289);
290validate!(
291    resource_template_reference_type_,
292    "ResourceTemplateReference",
293    "type_",
294    "ref/resource"
295);
296validate!(
297    resource_updated_notification_jsonrpc,
298    "ResourceUpdatedNotification",
299    "jsonrpc",
300    "2.0"
301);
302validate!(
303    resource_updated_notification_method,
304    "ResourceUpdatedNotification",
305    "method",
306    "notifications/resources/updated"
307);
308validate!(string_schema_type_, "StringSchema", "type_", "string");
309validate!(
310    subscriptions_acknowledged_notification_jsonrpc,
311    "SubscriptionsAcknowledgedNotification",
312    "jsonrpc",
313    "2.0"
314);
315validate!(
316    subscriptions_acknowledged_notification_method,
317    "SubscriptionsAcknowledgedNotification",
318    "method",
319    "notifications/subscriptions/acknowledged"
320);
321validate!(
322    subscriptions_listen_request_jsonrpc,
323    "SubscriptionsListenRequest",
324    "jsonrpc",
325    "2.0"
326);
327validate!(
328    subscriptions_listen_request_method,
329    "SubscriptionsListenRequest",
330    "method",
331    "subscriptions/listen"
332);
333validate!(
334    subscriptions_listen_result_response_jsonrpc,
335    "SubscriptionsListenResultResponse",
336    "jsonrpc",
337    "2.0"
338);
339validate!(text_content_type_, "TextContent", "type_", "text");
340validate!(
341    titled_multi_select_enum_schema_type_,
342    "TitledMultiSelectEnumSchema",
343    "type_",
344    "array"
345);
346validate!(
347    titled_single_select_enum_schema_type_,
348    "TitledSingleSelectEnumSchema",
349    "type_",
350    "string"
351);
352validate!(tool_input_schema_type_, "ToolInputSchema", "type_", "object");
353validate!(
354    tool_list_changed_notification_jsonrpc,
355    "ToolListChangedNotification",
356    "jsonrpc",
357    "2.0"
358);
359validate!(
360    tool_list_changed_notification_method,
361    "ToolListChangedNotification",
362    "method",
363    "notifications/tools/list_changed"
364);
365validate!(tool_result_content_type_, "ToolResultContent", "type_", "tool_result");
366validate!(tool_use_content_type_, "ToolUseContent", "type_", "tool_use");
367validate!(
368    unsupported_protocol_version_error_jsonrpc,
369    "UnsupportedProtocolVersionError",
370    "jsonrpc",
371    "2.0"
372);
373validate!(
374    untitled_multi_select_enum_schema_type_,
375    "UntitledMultiSelectEnumSchema",
376    "type_",
377    "array"
378);
379validate!(
380    untitled_multi_select_enum_schema_items_type_,
381    "UntitledMultiSelectEnumSchemaItems",
382    "type_",
383    "string"
384);
385validate!(
386    untitled_single_select_enum_schema_type_,
387    "UntitledSingleSelectEnumSchema",
388    "type_",
389    "string"
390);