Skip to main content

turul_mcp_builders/
protocol_impls.rs

1//! Framework trait implementations for protocol types.
2//!
3//! Implements the framework traits for the concrete protocol types (`Resource`, `Prompt`,
4//! `Tool`, `Root`, and others) from `turul_mcp_protocol`, so they can be used wherever a
5//! `ResourceDefinition`, `PromptDefinition`, `ToolDefinition`, etc. is expected.
6//!
7//! A protocol type with corresponding framework traits needs its implementations here;
8//! without them it does not satisfy the definition traits.
9
10use crate::traits::*;
11use turul_mcp_protocol::completion::{
12    CompleteArgument, CompleteRequest, CompletionContext, CompletionReference,
13};
14use turul_mcp_protocol::notifications::{
15    CancelledNotification, Notification, ProgressNotification, PromptListChangedNotification,
16    ResourceListChangedNotification, ResourceUpdatedNotification, ToolListChangedNotification,
17};
18// `Root` is deprecated-but-present in 2026-07-28 (SEP-2577); roots remain a valid feature.
19#[allow(deprecated)]
20use turul_mcp_protocol::roots::Root;
21use turul_mcp_protocol::{Prompt, Resource, Tool, ToolSchema};
22
23#[cfg(feature = "protocol-2025-11-25")]
24use turul_mcp_protocol::elicitation::{ElicitCreateRequest, ElicitationSchema};
25#[cfg(feature = "protocol-2025-11-25")]
26use turul_mcp_protocol::logging::{LoggingLevel, LoggingMessageNotification};
27#[cfg(feature = "protocol-2025-11-25")]
28use turul_mcp_protocol::notifications::{InitializedNotification, RootsListChangedNotification};
29#[cfg(feature = "protocol-2025-11-25")]
30use turul_mcp_protocol::sampling::{CreateMessageParams, ModelPreferences, SamplingMessage};
31#[cfg(feature = "protocol-2025-11-25")]
32use turul_mcp_protocol::tools::ToolExecution;
33
34// ============================================================================
35// Resource trait implementations
36// ============================================================================
37
38impl HasResourceMetadata for Resource {
39    fn name(&self) -> &str {
40        &self.name
41    }
42
43    fn title(&self) -> Option<&str> {
44        self.title.as_deref()
45    }
46}
47
48impl HasResourceDescription for Resource {
49    fn description(&self) -> Option<&str> {
50        self.description.as_deref()
51    }
52}
53
54impl HasResourceUri for Resource {
55    fn uri(&self) -> &str {
56        &self.uri
57    }
58}
59
60impl HasResourceMimeType for Resource {
61    fn mime_type(&self) -> Option<&str> {
62        self.mime_type.as_deref()
63    }
64}
65
66impl HasResourceSize for Resource {
67    fn size(&self) -> Option<u64> {
68        self.size
69    }
70}
71
72impl HasResourceAnnotations for Resource {
73    fn annotations(&self) -> Option<&turul_mcp_protocol::meta::Annotations> {
74        self.annotations.as_ref()
75    }
76}
77
78impl HasResourceMeta for Resource {
79    fn resource_meta(&self) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
80        self.meta.as_ref()
81    }
82}
83
84impl HasIcons for Resource {
85    fn icons(&self) -> Option<&Vec<turul_mcp_protocol::icons::Icon>> {
86        self.icons.as_ref()
87    }
88}
89
90// ResourceDefinition is automatically implemented via blanket impl
91
92// ============================================================================
93// Prompt trait implementations
94// ============================================================================
95
96impl HasPromptMetadata for Prompt {
97    fn name(&self) -> &str {
98        &self.name
99    }
100
101    fn title(&self) -> Option<&str> {
102        self.title.as_deref()
103    }
104}
105
106impl HasPromptDescription for Prompt {
107    fn description(&self) -> Option<&str> {
108        self.description.as_deref()
109    }
110}
111
112impl HasPromptArguments for Prompt {
113    fn arguments(&self) -> Option<&Vec<turul_mcp_protocol::prompts::PromptArgument>> {
114        self.arguments.as_ref()
115    }
116}
117
118impl HasPromptAnnotations for Prompt {
119    fn annotations(&self) -> Option<&crate::traits::prompt_traits::PromptAnnotations> {
120        // `Prompt` has no annotations field.
121        None
122    }
123}
124
125impl HasPromptMeta for Prompt {
126    fn prompt_meta(&self) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
127        self.meta.as_ref()
128    }
129}
130
131impl HasIcons for Prompt {
132    fn icons(&self) -> Option<&Vec<turul_mcp_protocol::icons::Icon>> {
133        self.icons.as_ref()
134    }
135}
136
137// PromptDefinition is automatically implemented via blanket impl
138
139// ============================================================================
140// Tool trait implementations
141// ============================================================================
142
143impl HasBaseMetadata for Tool {
144    fn name(&self) -> &str {
145        &self.name
146    }
147
148    fn title(&self) -> Option<&str> {
149        self.title.as_deref()
150    }
151}
152
153impl HasDescription for Tool {
154    fn description(&self) -> Option<&str> {
155        self.description.as_deref()
156    }
157}
158
159impl HasInputSchema for Tool {
160    fn input_schema(&self) -> &ToolSchema {
161        &self.input_schema
162    }
163}
164
165impl HasOutputSchema for Tool {
166    #[cfg(feature = "protocol-2025-11-25")]
167    fn output_schema(&self) -> Option<&ToolSchema> {
168        self.output_schema.as_ref()
169    }
170
171    #[cfg(feature = "protocol-2026-07-28")]
172    fn output_schema(&self) -> Option<&ToolSchema> {
173        None
174    }
175}
176
177impl HasAnnotations for Tool {
178    fn annotations(&self) -> Option<&turul_mcp_protocol::tools::ToolAnnotations> {
179        self.annotations.as_ref()
180    }
181}
182
183impl HasToolMeta for Tool {
184    fn tool_meta(&self) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
185        self.meta.as_ref()
186    }
187}
188
189impl HasIcons for Tool {
190    fn icons(&self) -> Option<&Vec<turul_mcp_protocol::icons::Icon>> {
191        self.icons.as_ref()
192    }
193}
194
195#[cfg(feature = "protocol-2025-11-25")]
196impl HasExecution for Tool {
197    fn execution(&self) -> Option<ToolExecution> {
198        self.execution.clone()
199    }
200}
201
202// ToolDefinition is automatically implemented via blanket impl
203
204// ============================================================================
205// Root trait implementations
206// ============================================================================
207
208#[allow(deprecated)]
209impl HasRootMetadata for Root {
210    fn name(&self) -> Option<&str> {
211        self.name.as_deref()
212    }
213
214    fn uri(&self) -> &str {
215        &self.uri
216    }
217}
218
219#[allow(deprecated)]
220impl HasRootPermissions for Root {
221    // `Root` has no permissions field; the trait defaults apply.
222}
223
224#[allow(deprecated)]
225impl HasRootFiltering for Root {
226    // `Root` has no filtering field; the trait defaults apply.
227}
228
229#[allow(deprecated)]
230impl HasRootAnnotations for Root {
231    fn annotations(&self) -> Option<&std::collections::HashMap<String, serde_json::Value>> {
232        self.meta.as_ref()
233    }
234}
235
236// RootDefinition is automatically implemented via blanket impl
237
238// ============================================================================
239// Sampling trait implementations
240// ============================================================================
241
242#[cfg(feature = "protocol-2025-11-25")]
243impl HasSamplingConfig for CreateMessageParams {
244    fn max_tokens(&self) -> u32 {
245        self.max_tokens
246    }
247
248    fn temperature(&self) -> Option<f64> {
249        self.temperature
250    }
251
252    fn stop_sequences(&self) -> Option<&Vec<String>> {
253        self.stop_sequences.as_ref()
254    }
255}
256
257#[cfg(feature = "protocol-2025-11-25")]
258impl HasSamplingContext for CreateMessageParams {
259    fn messages(&self) -> &[SamplingMessage] {
260        &self.messages
261    }
262
263    fn system_prompt(&self) -> Option<&str> {
264        self.system_prompt.as_deref()
265    }
266
267    fn include_context(&self) -> Option<&str> {
268        self.include_context.as_deref()
269    }
270}
271
272#[cfg(feature = "protocol-2025-11-25")]
273impl HasModelPreferences for CreateMessageParams {
274    fn model_preferences(&self) -> Option<&ModelPreferences> {
275        self.model_preferences.as_ref()
276    }
277
278    fn metadata(&self) -> Option<&serde_json::Value> {
279        self.metadata.as_ref()
280    }
281}
282
283#[cfg(feature = "protocol-2025-11-25")]
284impl HasSamplingTools for CreateMessageParams {
285    fn tools(&self) -> Option<&Vec<turul_mcp_protocol::Tool>> {
286        self.tools.as_ref()
287    }
288}
289
290// SamplingDefinition is automatically implemented via blanket impl
291
292#[cfg(feature = "protocol-2025-11-25")]
293impl HasSamplingMessageMetadata for SamplingMessage {
294    fn role(&self) -> &turul_mcp_protocol::sampling::Role {
295        &self.role
296    }
297
298    fn content(&self) -> &turul_mcp_protocol::prompts::ContentBlock {
299        &self.content
300    }
301}
302
303// ============================================================================
304// Logging trait implementations
305// ============================================================================
306
307#[cfg(feature = "protocol-2025-11-25")]
308impl HasLoggingMetadata for LoggingMessageNotification {
309    fn method(&self) -> &str {
310        &self.method
311    }
312
313    fn logger_name(&self) -> Option<&str> {
314        self.params.logger.as_deref()
315    }
316}
317
318#[cfg(feature = "protocol-2025-11-25")]
319impl HasLogLevel for LoggingMessageNotification {
320    fn level(&self) -> LoggingLevel {
321        self.params.level
322    }
323}
324
325#[cfg(feature = "protocol-2025-11-25")]
326impl HasLogFormat for LoggingMessageNotification {
327    fn data(&self) -> &serde_json::Value {
328        &self.params.data
329    }
330}
331
332#[cfg(feature = "protocol-2025-11-25")]
333impl HasLogTransport for LoggingMessageNotification {
334    // Use default implementations
335}
336
337// LoggerDefinition is automatically implemented via blanket impl
338
339// ============================================================================
340// Completion trait implementations
341// ============================================================================
342
343impl HasCompletionMetadata for CompleteRequest {
344    fn method(&self) -> &str {
345        &self.method
346    }
347
348    fn reference(&self) -> &CompletionReference {
349        &self.params.reference
350    }
351}
352
353impl HasCompletionContext for CompleteRequest {
354    fn argument(&self) -> &CompleteArgument {
355        &self.params.argument
356    }
357
358    fn context(&self) -> Option<&CompletionContext> {
359        self.params.context.as_ref()
360    }
361}
362
363impl HasCompletionHandling for CompleteRequest {
364    // Use default implementations
365}
366
367// CompletionDefinition is automatically implemented via blanket impl
368
369// ============================================================================
370// Elicitation trait implementations
371// ============================================================================
372
373#[cfg(feature = "protocol-2025-11-25")]
374impl HasElicitationMetadata for ElicitCreateRequest {
375    fn message(&self) -> &str {
376        &self.params.message
377    }
378
379    // title() uses default implementation which returns None
380}
381
382#[cfg(feature = "protocol-2025-11-25")]
383impl HasElicitationSchema for ElicitCreateRequest {
384    fn requested_schema(&self) -> &ElicitationSchema {
385        &self.params.requested_schema
386    }
387}
388
389#[cfg(feature = "protocol-2025-11-25")]
390impl HasElicitationHandling for ElicitCreateRequest {
391    // Use default implementations
392}
393
394// ElicitationDefinition is automatically implemented via blanket impl
395
396// ============================================================================
397// Notification trait implementations
398// ============================================================================
399
400// Base Notification type
401impl HasNotificationMetadata for Notification {
402    fn method(&self) -> &str {
403        &self.method
404    }
405}
406
407impl HasNotificationPayload for Notification {
408    fn payload(&self) -> Option<serde_json::Value> {
409        self.params.as_ref().map(|params| {
410            let mut map = serde_json::Map::new();
411
412            // Add all params.other fields
413            for (key, value) in &params.other {
414                map.insert(key.clone(), value.clone());
415            }
416
417            // Add _meta if present
418            if let Some(meta) = &params.meta
419                && let Ok(meta_value) = serde_json::to_value(meta)
420            {
421                map.insert("_meta".to_string(), meta_value);
422            }
423
424            serde_json::Value::Object(map)
425        })
426    }
427}
428
429impl HasNotificationRules for Notification {}
430
431// ResourceListChangedNotification
432impl HasNotificationMetadata for ResourceListChangedNotification {
433    fn method(&self) -> &str {
434        &self.method
435    }
436}
437
438impl HasNotificationPayload for ResourceListChangedNotification {
439    fn payload(&self) -> Option<serde_json::Value> {
440        // Serialize params if present (includes _meta)
441        self.params
442            .as_ref()
443            .and_then(|p| serde_json::to_value(p).ok())
444    }
445}
446
447impl HasNotificationRules for ResourceListChangedNotification {}
448
449// ToolListChangedNotification
450impl HasNotificationMetadata for ToolListChangedNotification {
451    fn method(&self) -> &str {
452        &self.method
453    }
454}
455
456impl HasNotificationPayload for ToolListChangedNotification {
457    fn payload(&self) -> Option<serde_json::Value> {
458        // Serialize params if present (includes _meta)
459        self.params
460            .as_ref()
461            .and_then(|p| serde_json::to_value(p).ok())
462    }
463}
464
465impl HasNotificationRules for ToolListChangedNotification {}
466
467// PromptListChangedNotification
468impl HasNotificationMetadata for PromptListChangedNotification {
469    fn method(&self) -> &str {
470        &self.method
471    }
472}
473
474impl HasNotificationPayload for PromptListChangedNotification {
475    fn payload(&self) -> Option<serde_json::Value> {
476        // Serialize params if present (includes _meta)
477        self.params
478            .as_ref()
479            .and_then(|p| serde_json::to_value(p).ok())
480    }
481}
482
483impl HasNotificationRules for PromptListChangedNotification {}
484
485// RootsListChangedNotification
486#[cfg(feature = "protocol-2025-11-25")]
487impl HasNotificationMetadata for RootsListChangedNotification {
488    fn method(&self) -> &str {
489        &self.method
490    }
491}
492
493#[cfg(feature = "protocol-2025-11-25")]
494impl HasNotificationPayload for RootsListChangedNotification {
495    fn payload(&self) -> Option<serde_json::Value> {
496        // Serialize params if present (includes _meta)
497        self.params
498            .as_ref()
499            .and_then(|p| serde_json::to_value(p).ok())
500    }
501}
502
503#[cfg(feature = "protocol-2025-11-25")]
504impl HasNotificationRules for RootsListChangedNotification {}
505
506// ProgressNotification
507impl HasNotificationMetadata for ProgressNotification {
508    fn method(&self) -> &str {
509        &self.method
510    }
511}
512
513impl HasNotificationPayload for ProgressNotification {
514    fn payload(&self) -> Option<serde_json::Value> {
515        // Serialize the entire params struct (includes progressToken, progress, total, message, _meta)
516        serde_json::to_value(&self.params).ok()
517    }
518}
519
520impl HasNotificationRules for ProgressNotification {
521    fn priority(&self) -> u32 {
522        2 // Higher than the default priority
523    }
524}
525
526// ResourceUpdatedNotification
527impl HasNotificationMetadata for ResourceUpdatedNotification {
528    fn method(&self) -> &str {
529        &self.method
530    }
531}
532
533impl HasNotificationPayload for ResourceUpdatedNotification {
534    fn payload(&self) -> Option<serde_json::Value> {
535        // Serialize params (includes uri, _meta)
536        serde_json::to_value(&self.params).ok()
537    }
538}
539
540impl HasNotificationRules for ResourceUpdatedNotification {}
541
542// CancelledNotification
543impl HasNotificationMetadata for CancelledNotification {
544    fn method(&self) -> &str {
545        &self.method
546    }
547}
548
549impl HasNotificationPayload for CancelledNotification {
550    fn payload(&self) -> Option<serde_json::Value> {
551        // Serialize params (includes requestId, reason, _meta)
552        serde_json::to_value(&self.params).ok()
553    }
554}
555
556impl HasNotificationRules for CancelledNotification {
557    fn priority(&self) -> u32 {
558        3 // Highest priority
559    }
560}
561
562// InitializedNotification
563#[cfg(feature = "protocol-2025-11-25")]
564impl HasNotificationMetadata for InitializedNotification {
565    fn method(&self) -> &str {
566        &self.method
567    }
568}
569
570#[cfg(feature = "protocol-2025-11-25")]
571impl HasNotificationPayload for InitializedNotification {
572    fn payload(&self) -> Option<serde_json::Value> {
573        // Serialize params if present (includes _meta)
574        self.params
575            .as_ref()
576            .and_then(|p| serde_json::to_value(p).ok())
577    }
578}
579
580#[cfg(feature = "protocol-2025-11-25")]
581impl HasNotificationRules for InitializedNotification {
582    fn priority(&self) -> u32 {
583        3 // Highest priority
584    }
585}
586
587// NotificationDefinition is automatically implemented via blanket impl for all notification types