rust_mcp_schema/generated_schema/2025_11_25/
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
60fn i64_validator<'de, D>(
61    struct_name: &'static str,
62    field_name: &'static str,
63    expected: i64,
64    deserializer: D,
65) -> Result<i64, D::Error>
66where
67    D: serde::de::Deserializer<'de>,
68{
69    let value = serde::Deserialize::deserialize(deserializer)?;
70    if value == expected {
71        Ok(value)
72    } else {
73        Err(serde::de::Error::custom(format!(
74            "Invalid {struct_name}::{field_name}: expected {expected}, got {value}"
75        )))
76    }
77}
78
79macro_rules! validate {
80    // === String validation (required) ===
81    ($func_name:ident,  $struct:expr, $field:expr, $expected:expr $(,)?) => {
82        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<String, D::Error>
83        where
84            D: serde::de::Deserializer<'de>,
85        {
86            const_str_validator($struct, $field, $expected, deserializer)
87        }
88    };
89
90    // Optional String case (with trailing `, option`)
91    ($func_name:ident, $struct:expr, $field:expr, $expected:expr, option $(,)?) => {
92        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
93        where
94            D: serde::de::Deserializer<'de>,
95        {
96            const_str_option_validator($struct, $field, $expected, deserializer)
97        }
98    };
99
100    // === i64 validation (required) ===
101    ($func_name:ident, $struct:expr, $field:expr, $expected:expr, i64 $(,)?) => {
102        pub(crate) fn $func_name<'de, D>(deserializer: D) -> Result<i64, D::Error>
103        where
104            D: serde::de::Deserializer<'de>,
105        {
106            i64_validator($struct, $field, $expected, deserializer)
107        }
108    };
109}
110
111//* Validator Functions *//
112validate!(audio_content_type_, "AudioContent", "type_", "audio");
113validate!(boolean_schema_type_, "BooleanSchema", "type_", "boolean");
114validate!(call_tool_request_jsonrpc, "CallToolRequest", "jsonrpc", "2.0");
115validate!(call_tool_request_method, "CallToolRequest", "method", "tools/call");
116validate!(cancel_task_request_jsonrpc, "CancelTaskRequest", "jsonrpc", "2.0");
117validate!(cancel_task_request_method, "CancelTaskRequest", "method", "tasks/cancel");
118validate!(cancelled_notification_jsonrpc, "CancelledNotification", "jsonrpc", "2.0");
119validate!(
120    cancelled_notification_method,
121    "CancelledNotification",
122    "method",
123    "notifications/cancelled"
124);
125validate!(complete_request_jsonrpc, "CompleteRequest", "jsonrpc", "2.0");
126validate!(complete_request_method, "CompleteRequest", "method", "completion/complete");
127validate!(create_message_request_jsonrpc, "CreateMessageRequest", "jsonrpc", "2.0");
128validate!(
129    create_message_request_method,
130    "CreateMessageRequest",
131    "method",
132    "sampling/createMessage"
133);
134validate!(elicit_form_schema_type_, "ElicitFormSchema", "type_", "object");
135validate!(elicit_request_jsonrpc, "ElicitRequest", "jsonrpc", "2.0");
136validate!(elicit_request_method, "ElicitRequest", "method", "elicitation/create");
137validate!(
138    elicit_request_form_params_mode,
139    "ElicitRequestFormParams",
140    "mode",
141    "form",
142    option
143);
144validate!(elicit_request_url_params_mode, "ElicitRequestUrlParams", "mode", "url");
145validate!(
146    elicitation_complete_notification_jsonrpc,
147    "ElicitationCompleteNotification",
148    "jsonrpc",
149    "2.0"
150);
151validate!(
152    elicitation_complete_notification_method,
153    "ElicitationCompleteNotification",
154    "method",
155    "notifications/elicitation/complete"
156);
157validate!(embedded_resource_type_, "EmbeddedResource", "type_", "resource");
158validate!(get_prompt_request_jsonrpc, "GetPromptRequest", "jsonrpc", "2.0");
159validate!(get_prompt_request_method, "GetPromptRequest", "method", "prompts/get");
160validate!(get_task_payload_request_jsonrpc, "GetTaskPayloadRequest", "jsonrpc", "2.0");
161validate!(
162    get_task_payload_request_method,
163    "GetTaskPayloadRequest",
164    "method",
165    "tasks/result"
166);
167validate!(get_task_request_jsonrpc, "GetTaskRequest", "jsonrpc", "2.0");
168validate!(get_task_request_method, "GetTaskRequest", "method", "tasks/get");
169validate!(image_content_type_, "ImageContent", "type_", "image");
170validate!(initialize_request_jsonrpc, "InitializeRequest", "jsonrpc", "2.0");
171validate!(initialize_request_method, "InitializeRequest", "method", "initialize");
172validate!(initialized_notification_jsonrpc, "InitializedNotification", "jsonrpc", "2.0");
173validate!(
174    initialized_notification_method,
175    "InitializedNotification",
176    "method",
177    "notifications/initialized"
178);
179validate!(jsonrpc_error_response_jsonrpc, "JsonrpcErrorResponse", "jsonrpc", "2.0");
180validate!(jsonrpc_notification_jsonrpc, "JsonrpcNotification", "jsonrpc", "2.0");
181validate!(jsonrpc_request_jsonrpc, "JsonrpcRequest", "jsonrpc", "2.0");
182validate!(jsonrpc_result_response_jsonrpc, "JsonrpcResultResponse", "jsonrpc", "2.0");
183validate!(legacy_titled_enum_schema_type_, "LegacyTitledEnumSchema", "type_", "string");
184validate!(list_prompts_request_jsonrpc, "ListPromptsRequest", "jsonrpc", "2.0");
185validate!(list_prompts_request_method, "ListPromptsRequest", "method", "prompts/list");
186validate!(
187    list_resource_templates_request_jsonrpc,
188    "ListResourceTemplatesRequest",
189    "jsonrpc",
190    "2.0"
191);
192validate!(
193    list_resource_templates_request_method,
194    "ListResourceTemplatesRequest",
195    "method",
196    "resources/templates/list"
197);
198validate!(list_resources_request_jsonrpc, "ListResourcesRequest", "jsonrpc", "2.0");
199validate!(
200    list_resources_request_method,
201    "ListResourcesRequest",
202    "method",
203    "resources/list"
204);
205validate!(list_roots_request_jsonrpc, "ListRootsRequest", "jsonrpc", "2.0");
206validate!(list_roots_request_method, "ListRootsRequest", "method", "roots/list");
207validate!(list_tasks_request_jsonrpc, "ListTasksRequest", "jsonrpc", "2.0");
208validate!(list_tasks_request_method, "ListTasksRequest", "method", "tasks/list");
209validate!(list_tools_request_jsonrpc, "ListToolsRequest", "jsonrpc", "2.0");
210validate!(list_tools_request_method, "ListToolsRequest", "method", "tools/list");
211validate!(
212    logging_message_notification_jsonrpc,
213    "LoggingMessageNotification",
214    "jsonrpc",
215    "2.0"
216);
217validate!(
218    logging_message_notification_method,
219    "LoggingMessageNotification",
220    "method",
221    "notifications/message"
222);
223validate!(paginated_request_jsonrpc, "PaginatedRequest", "jsonrpc", "2.0");
224validate!(ping_request_jsonrpc, "PingRequest", "jsonrpc", "2.0");
225validate!(ping_request_method, "PingRequest", "method", "ping");
226validate!(progress_notification_jsonrpc, "ProgressNotification", "jsonrpc", "2.0");
227validate!(
228    progress_notification_method,
229    "ProgressNotification",
230    "method",
231    "notifications/progress"
232);
233validate!(
234    prompt_list_changed_notification_jsonrpc,
235    "PromptListChangedNotification",
236    "jsonrpc",
237    "2.0"
238);
239validate!(
240    prompt_list_changed_notification_method,
241    "PromptListChangedNotification",
242    "method",
243    "notifications/prompts/list_changed"
244);
245validate!(prompt_reference_type_, "PromptReference", "type_", "ref/prompt");
246validate!(read_resource_request_jsonrpc, "ReadResourceRequest", "jsonrpc", "2.0");
247validate!(
248    read_resource_request_method,
249    "ReadResourceRequest",
250    "method",
251    "resources/read"
252);
253validate!(resource_link_type_, "ResourceLink", "type_", "resource_link");
254validate!(
255    resource_list_changed_notification_jsonrpc,
256    "ResourceListChangedNotification",
257    "jsonrpc",
258    "2.0"
259);
260validate!(
261    resource_list_changed_notification_method,
262    "ResourceListChangedNotification",
263    "method",
264    "notifications/resources/list_changed"
265);
266validate!(
267    resource_template_reference_type_,
268    "ResourceTemplateReference",
269    "type_",
270    "ref/resource"
271);
272validate!(
273    resource_updated_notification_jsonrpc,
274    "ResourceUpdatedNotification",
275    "jsonrpc",
276    "2.0"
277);
278validate!(
279    resource_updated_notification_method,
280    "ResourceUpdatedNotification",
281    "method",
282    "notifications/resources/updated"
283);
284validate!(
285    roots_list_changed_notification_jsonrpc,
286    "RootsListChangedNotification",
287    "jsonrpc",
288    "2.0"
289);
290validate!(
291    roots_list_changed_notification_method,
292    "RootsListChangedNotification",
293    "method",
294    "notifications/roots/list_changed"
295);
296validate!(set_level_request_jsonrpc, "SetLevelRequest", "jsonrpc", "2.0");
297validate!(set_level_request_method, "SetLevelRequest", "method", "logging/setLevel");
298validate!(string_schema_type_, "StringSchema", "type_", "string");
299validate!(subscribe_request_jsonrpc, "SubscribeRequest", "jsonrpc", "2.0");
300validate!(subscribe_request_method, "SubscribeRequest", "method", "resources/subscribe");
301validate!(task_status_notification_jsonrpc, "TaskStatusNotification", "jsonrpc", "2.0");
302validate!(
303    task_status_notification_method,
304    "TaskStatusNotification",
305    "method",
306    "notifications/tasks/status"
307);
308validate!(text_content_type_, "TextContent", "type_", "text");
309validate!(
310    titled_multi_select_enum_schema_type_,
311    "TitledMultiSelectEnumSchema",
312    "type_",
313    "array"
314);
315validate!(
316    titled_single_select_enum_schema_type_,
317    "TitledSingleSelectEnumSchema",
318    "type_",
319    "string"
320);
321validate!(tool_input_schema_type_, "ToolInputSchema", "type_", "object");
322validate!(
323    tool_list_changed_notification_jsonrpc,
324    "ToolListChangedNotification",
325    "jsonrpc",
326    "2.0"
327);
328validate!(
329    tool_list_changed_notification_method,
330    "ToolListChangedNotification",
331    "method",
332    "notifications/tools/list_changed"
333);
334validate!(tool_output_schema_type_, "ToolOutputSchema", "type_", "object");
335validate!(tool_result_content_type_, "ToolResultContent", "type_", "tool_result");
336validate!(tool_use_content_type_, "ToolUseContent", "type_", "tool_use");
337validate!(unsubscribe_request_jsonrpc, "UnsubscribeRequest", "jsonrpc", "2.0");
338validate!(
339    unsubscribe_request_method,
340    "UnsubscribeRequest",
341    "method",
342    "resources/unsubscribe"
343);
344validate!(
345    untitled_multi_select_enum_schema_type_,
346    "UntitledMultiSelectEnumSchema",
347    "type_",
348    "array"
349);
350validate!(
351    untitled_multi_select_enum_schema_items_type_,
352    "UntitledMultiSelectEnumSchemaItems",
353    "type_",
354    "string"
355);
356validate!(
357    untitled_single_select_enum_schema_type_,
358    "UntitledSingleSelectEnumSchema",
359    "type_",
360    "string"
361);
362validate!(url_elicit_error_code, "UrlElicitError", "code", -32042i64, i64);
363validate!(
364    url_elicitation_required_error_jsonrpc,
365    "UrlElicitationRequiredError",
366    "jsonrpc",
367    "2.0"
368);