rust-mcp-macros 0.9.1

A procedural macro that derives the MCPToolSchema implementation for structs or enums, generating a tool_input_schema function used with rust_mcp_schema::Tool.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
extern crate proc_macro;

mod common;
mod elicit;
mod resource;
mod tool;
mod utils;

use crate::elicit::generator::{generate_form_schema, generate_from_impl};
use crate::elicit::parser::{ElicitArgs, ElicitMode};
use crate::resource::generator::{
    generate_resource_template_tokens, generate_resource_tokens, ResourceTemplateTokens,
    ResourceTokens,
};
use crate::resource::parser::{McpResourceMacroAttributes, McpResourceTemplateMacroAttributes};
use crate::tool::generator::{generate_tool_tokens, ToolTokens};
use crate::tool::parser::McpToolMacroAttributes;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};
use utils::{base_crate, is_option, is_vec_string, renamed_field, type_to_json_schema};

/// A procedural macro attribute to generate rust_mcp_schema::Tool related utility methods for a struct.
///
/// The `mcp_tool` macro generates an implementation for the annotated struct that includes:
/// - A `tool_name()` method returning the tool's name as a string.
/// - A `tool()` method returning a `rust_mcp_schema::Tool` instance with the tool's name,
///   description, input schema, meta, and title derived from the struct's fields and attributes.
///
/// # Attributes
/// * `name` - The name of the tool (required, non-empty string).
/// * `description` - A description of the tool (required, non-empty string).
/// * `meta` - Optional JSON object as a string literal for metadata.
/// * `title` - Optional string for the tool's title.
///
/// # Panics
/// Panics if the macro is applied to anything other than a struct.
///
/// # Example
/// ```rust,ignore
/// # #[cfg(not(feature = "sdk"))]
/// # {
/// #[rust_mcp_macros::mcp_tool(
///     name = "example_tool",
///     description = "An example tool",
///     meta = "{\"version\": \"1.0\"}",
///     title = "Example Tool"
/// )]
/// #[derive(rust_mcp_macros::JsonSchema)]
/// struct ExampleTool {
///     field1: String,
///     field2: i32,
/// }
///
/// assert_eq!(ExampleTool::tool_name(), "example_tool");
/// let tool: rust_mcp_schema::Tool = ExampleTool::tool();
/// assert_eq!(tool.name, "example_tool");
/// assert_eq!(tool.description.unwrap(), "An example tool");
/// assert_eq!(tool.meta.as_ref().unwrap().get("version").unwrap(), "1.0");
/// assert_eq!(tool.title.unwrap(), "Example Tool");
///
/// let schema_properties = tool.input_schema.properties.unwrap();
/// assert_eq!(schema_properties.len(), 2);
/// assert!(schema_properties.contains_key("field1"));
/// assert!(schema_properties.contains_key("field2"));
/// }
/// ```
#[proc_macro_attribute]
pub fn mcp_tool(attributes: TokenStream, input: TokenStream) -> TokenStream {
    let input_item: syn::Item = parse_macro_input!(input as syn::Item);

    let (ident, original_item) = match input_item {
        syn::Item::Struct(struct_item) => {
            let ident = struct_item.ident.clone();
            (ident, syn::Item::Struct(struct_item))
        }
        syn::Item::Type(type_item) => {
            // Only support simple non-generic type aliases like `type Foo = Bar;`
            // (no paths with ::, no generics)
            let aliased_ty = type_item.ty.clone();
            if let syn::Type::Path(type_path) = *aliased_ty {
                if type_path.path.leading_colon.is_none() && type_path.path.segments.len() == 1 {
                    let segment = type_path.path.segments.first().unwrap();
                    if matches!(segment.arguments, syn::PathArguments::None) {
                        let ident = type_item.ident.clone();
                        (ident, syn::Item::Type(type_item))
                    } else {
                        return quote! {
                                compile_error!("mcp_tool does not support type aliases with generic arguments");
                            }
                            .into();
                    }
                } else {
                    return quote! {
                            compile_error!("mcp_tool only supports simple type aliases to a single identifier (e.g. `type Foo = Bar;`)");
                        }
                        .into();
                }
            } else {
                return quote! {
                    compile_error!("mcp_tool only supports type aliases to path types");
                }
                .into();
            }
        }
        _ => {
            return quote! {
                compile_error!("#[mcp_tool] can only be applied to structs or type aliases");
            }
            .into();
        }
    };

    let input_ident = &ident;
    let macro_attributes = parse_macro_input!(attributes as McpToolMacroAttributes);

    let ToolTokens {
        base_crate,
        tool_name,
        tool_description,
        meta,
        title,
        output_schema,
        annotations,
        execution,
        icons,
    } = generate_tool_tokens(macro_attributes);

    // TODO: add support for schema version to ToolInputSchema :
    // it defaults to JSON Schema 2020-12 when no explicit $schema is provided.
    let tool_token = quote! {
        #base_crate::Tool {
            name: #tool_name.to_string(),
            description: Some(#tool_description.to_string()),
            #output_schema
            #title
            #meta
            #annotations
            #execution
            #icons
            input_schema: #base_crate::ToolInputSchema::new(required, properties, None)
        }
    };

    let output = quote! {
        impl #input_ident {
            /// Returns the name of the tool as a String.
            pub fn tool_name() -> String {
                #tool_name.to_string()
            }

            /// Returns a `CallToolRequestParams` initialized with the current tool's name.
            ///
            /// You can further customize the request by adding arguments or other attributes
            /// using the builder pattern. For example:
            ///
            /// ```ignore
            /// # use my_crate::{MyTool};
            /// let args = serde_json::Map::new();
            /// let task_meta = TaskMetadata{ttl: Some(200)}
            ///
            /// let params: CallToolRequestParams = MyTool::request_params()
            ///     .with_arguments(args)
            ///     .with_task(task_meta);
            /// ```
            ///
            /// # Returns
            /// A `CallToolRequestParams` with the tool name set.
            pub fn request_params() -> #base_crate::CallToolRequestParams {
               #base_crate::CallToolRequestParams::new(#tool_name.to_string())
            }

            /// Constructs and returns a `rust_mcp_schema::Tool` instance.
            ///
            /// The tool includes the name, description, input schema, meta, and title derived from
            /// the struct's attributes.
            pub fn tool() -> #base_crate::Tool {
                let json_schema = &#input_ident::json_schema();

                let required: Vec<_> = match json_schema.get("required").and_then(|r| r.as_array()) {
                    Some(arr) => arr
                        .iter()
                        .filter_map(|item| item.as_str().map(String::from))
                        .collect(),
                    None => Vec::new(),
                };

                let properties: Option<
                    std::collections::BTreeMap<String, serde_json::Map<String, serde_json::Value>>,
                > = json_schema
                    .get("properties")
                    .and_then(|v| v.as_object()) // Safely extract "properties" as an object.
                    .map(|properties| {
                        properties
                            .iter()
                            .filter_map(|(key, value)| {
                                serde_json::to_value(value)
                                    .ok() // If serialization fails, return None.
                                    .and_then(|v| {
                                        if let serde_json::Value::Object(obj) = v {
                                            Some(obj)
                                        } else {
                                            None
                                        }
                                    })
                                    .map(|obj| (key.to_string(), obj)) // Return the (key, value) tuple
                            })
                            .collect()
                    });

                #tool_token
            }
        }
        // Retain the original item (struct definition)
        #original_item
    };

    TokenStream::from(output)
}

#[proc_macro_attribute]
pub fn mcp_elicit(args: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let fields = match &input.data {
        Data::Struct(s) => match &s.fields {
            Fields::Named(n) => &n.named,
            _ => panic!("mcp_elicit only supports structs with named fields"),
        },
        _ => panic!("mcp_elicit only supports structs"),
    };

    let struct_name = &input.ident;
    let elicit_args = parse_macro_input!(args as ElicitArgs);

    let base_crate = base_crate();

    let message = &elicit_args.message;

    let impl_block = match elicit_args.mode {
        ElicitMode::Form => {
            let (from_content, init) = generate_from_impl(fields, &base_crate);
            let schema = generate_form_schema(struct_name, &base_crate);

            quote! {
                impl #struct_name {
                    pub fn message() -> &'static str{
                        #message
                    }

                    pub fn requested_schema() -> #base_crate::ElicitFormSchema {
                        #schema
                    }

                    pub fn elicit_mode()->&'static str{
                        "form"
                    }

                    pub fn elicit_form_params() -> #base_crate::ElicitRequestFormParams {
                            #base_crate::ElicitRequestFormParams::new(
                                Self::message().to_string(),
                                Self::requested_schema(),
                                None,
                                None,
                            )
                    }

                    pub fn elicit_request_params() -> #base_crate::ElicitRequestParams {
                        Self::elicit_form_params().into()
                    }

                    pub fn from_elicit_result_content(
                        mut content: Option<std::collections::BTreeMap<String, #base_crate::ElicitResultContent>>,
                    ) -> Result<Self, #base_crate::RpcError> {
                        use #base_crate::{ElicitResultContent as V, RpcError};
                        let mut map = content.take().unwrap_or_default();
                            #from_content
                            Ok(#init)
                    }

                }
            }
        }
        ElicitMode::Url { url } => {
            let (from_content, init) = generate_from_impl(fields, &base_crate);

            quote! {
                impl #struct_name {
                    pub fn message() -> &'static str {
                        #message
                    }

                    pub fn url() -> &'static str {
                        #url
                    }

                    pub fn elicit_mode()->&'static str {
                        "url"
                    }

                    pub fn elicit_url_params(elicitation_id:String) -> #base_crate::ElicitRequestUrlParams {
                            #base_crate::ElicitRequestUrlParams::new(
                                elicitation_id,
                                Self::message().to_string(),
                                Self::url().to_string(),
                                None,
                                None,
                            )
                    }

                    pub fn elicit_request_params(elicitation_id:String) -> #base_crate::ElicitRequestParams {
                        Self::elicit_url_params(elicitation_id).into()
                    }

                    pub fn from_elicit_result_content(
                        mut content: Option<std::collections::BTreeMap<String, #base_crate::ElicitResultContent>>,
                    ) -> Result<Self, RpcError> {
                        use #base_crate::{ElicitResultContent as V, RpcError};
                        let mut map = content.take().unwrap_or_default();
                            #from_content
                            Ok(#init)
                    }
                }
            }
        }
    };

    let expanded = quote! {
        #input
        #impl_block
    };

    TokenStream::from(expanded)
}

/// Derives a JSON Schema representation for a struct.
///
/// This procedural macro generates a `json_schema()` method for the annotated struct, returning a
/// `serde_json::Map<String, serde_json::Value>` that represents the struct as a JSON Schema object.
/// The schema includes the struct's fields as properties, with support for basic types, `Option<T>`,
/// `Vec<T>`, and nested structs that also derive `JsonSchema`.
///
/// # Features
/// - **Basic Types:** Maps `String` to `"string"`, `i32` to `"integer"`, `bool` to `"boolean"`, etc.
/// - **`Option<T>`:** Adds `"nullable": true` to the schema of the inner type, indicating the field is optional.
/// - **`Vec<T>`:** Generates an `"array"` schema with an `"items"` field describing the inner type.
/// - **Nested Structs:** Recursively includes the schema of nested structs (assumed to derive `JsonSchema`),
///   embedding their `"properties"` and `"required"` fields.
/// - **Required Fields:** Adds a top-level `"required"` array listing field names not wrapped in `Option`.
///
/// # Notes
/// It’s designed as a straightforward solution to meet the basic needs of this package, supporting
/// common types and simple nested structures. For more advanced features or robust JSON Schema generation,
/// consider exploring established crates like
/// [`schemars`](https://crates.io/crates/schemars) on crates.io
///
/// # Limitations
/// - Supports only structs with named fields (e.g., `struct S { field: Type }`).
/// - Nested structs must also derive `JsonSchema`, or compilation will fail.
/// - Unknown types are mapped to `{"type": "unknown"}`.
/// - Type paths must be in scope (e.g., fully qualified paths like `my_mod::InnerStruct` work if imported).
///
/// # Panics
/// - If the input is not a struct with named fields (e.g., tuple structs or enums).
///
/// # Dependencies
/// Relies on `serde_json` for `Map` and `Value` types.
///
#[proc_macro_derive(JsonSchema, attributes(json_schema))]
pub fn derive_json_schema(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let schema_body = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => {
                let field_entries = fields.named.iter().map(|field| {
                    let field_attrs = &field.attrs;
                    let renamed_field = renamed_field(field_attrs);
                    let field_name =
                        renamed_field.unwrap_or(field.ident.as_ref().unwrap().to_string());
                    let field_type = &field.ty;

                    let schema = type_to_json_schema(field_type, field_attrs);
                    quote! {
                        properties.insert(
                            #field_name.to_string(),
                            serde_json::Value::Object(#schema)
                        );
                    }
                });

                let required_fields = fields.named.iter().filter_map(|field| {
                    let renamed_field = renamed_field(&field.attrs);
                    let field_name =
                        renamed_field.unwrap_or(field.ident.as_ref().unwrap().to_string());

                    let field_type = &field.ty;
                    if !is_option(field_type) {
                        Some(quote! {
                            required.push(#field_name.to_string());
                        })
                    } else {
                        None
                    }
                });

                quote! {
                    let mut schema = serde_json::Map::new();
                    let mut properties = serde_json::Map::new();
                    let mut required = Vec::new();

                    #(#field_entries)*

                    #(#required_fields)*

                    schema.insert("type".to_string(), serde_json::Value::String("object".to_string()));
                    schema.insert("properties".to_string(), serde_json::Value::Object(properties));
                    if !required.is_empty() {
                        schema.insert("required".to_string(), serde_json::Value::Array(
                            required.into_iter().map(serde_json::Value::String).collect()
                        ));
                    }

                    schema
                }
            }
            _ => panic!("JsonSchema derive macro only supports named fields for structs"),
        },
        Data::Enum(data) => {
            let variant_schemas = data.variants.iter().map(|variant| {
                let variant_attrs = &variant.attrs;
                let variant_name = variant.ident.to_string();
                let renamed_variant = renamed_field(variant_attrs).unwrap_or(variant_name.clone());

                // Parse variant-level json_schema attributes
                let mut title: Option<String> = None;
                let mut description: Option<String> = None;
                for attr in variant_attrs {
                    if attr.path().is_ident("json_schema") {
                        let _ = attr.parse_nested_meta(|meta| {
                            if meta.path.is_ident("title") {
                                title = Some(meta.value()?.parse::<syn::LitStr>()?.value());
                            } else if meta.path.is_ident("description") {
                                description = Some(meta.value()?.parse::<syn::LitStr>()?.value());
                            }
                            Ok(())
                        });
                    }
                }

                let title_quote = title.as_ref().map(|t| {
                    quote! { map.insert("title".to_string(), serde_json::Value::String(#t.to_string())); }
                });
                let description_quote = description.as_ref().map(|desc| {
                    quote! { map.insert("description".to_string(), serde_json::Value::String(#desc.to_string())); }
                });

                match &variant.fields {
                    Fields::Unit => {
                        // Unit variant: use "enum" with the variant name
                        quote! {
                            {
                                let mut map = serde_json::Map::new();
                                map.insert("enum".to_string(), serde_json::Value::Array(vec![
                                    serde_json::Value::String(#renamed_variant.to_string())
                                ]));
                                #title_quote
                                #description_quote
                                serde_json::Value::Object(map)
                            }
                        }
                    }
                    Fields::Unnamed(fields) => {
                        // Newtype or tuple variant
                        if fields.unnamed.len() == 1 {
                            // Newtype variant: use the inner type's schema
                            let field = &fields.unnamed[0];
                            let field_type = &field.ty;
                            let field_attrs = &field.attrs;
                            let schema = type_to_json_schema(field_type, field_attrs);
                            quote! {
                                {
                                    let mut map = #schema;
                                    #title_quote
                                    #description_quote
                                    serde_json::Value::Object(map)
                                }
                            }
                        } else {
                            // Tuple variant: array with items
                            let field_schemas = fields.unnamed.iter().map(|field| {
                                let field_type = &field.ty;
                                let field_attrs = &field.attrs;
                                let schema = type_to_json_schema(field_type, field_attrs);
                                quote! { serde_json::Value::Object(#schema) }
                            });
                            quote! {
                                {
                                    let mut map = serde_json::Map::new();
                                    map.insert("type".to_string(), serde_json::Value::String("array".to_string()));
                                    map.insert("items".to_string(), serde_json::Value::Array(vec![#(#field_schemas),*]));
                                    map.insert("additionalItems".to_string(), serde_json::Value::Bool(false));
                                    #title_quote
                                    #description_quote
                                    serde_json::Value::Object(map)
                                }
                            }
                        }
                    }
                    Fields::Named(fields) => {
                        // Struct variant: object with properties and required fields
                        let field_entries = fields.named.iter().map(|field| {
                            let field_attrs = &field.attrs;
                            let renamed_field = renamed_field(field_attrs);
                            let field_name = renamed_field.unwrap_or(field.ident.as_ref().unwrap().to_string());
                            let field_type = &field.ty;

                            let schema = type_to_json_schema(field_type, field_attrs);
                            quote! {
                                properties.insert(
                                    #field_name.to_string(),
                                    serde_json::Value::Object(#schema)
                                );
                            }
                        });

                        let required_fields = fields.named.iter().filter_map(|field| {
                            let renamed_field = renamed_field(&field.attrs);
                            let field_name = renamed_field.unwrap_or(field.ident.as_ref().unwrap().to_string());

                            let field_type = &field.ty;
                            if !is_option(field_type) {
                                Some(quote! {
                                    required.push(#field_name.to_string());
                                })
                            } else {
                                None
                            }
                        });

                        quote! {
                            {
                                let mut map = serde_json::Map::new();
                                let mut properties = serde_json::Map::new();
                                let mut required = Vec::new();

                                #(#field_entries)*

                                #(#required_fields)*

                                map.insert("type".to_string(), serde_json::Value::String("object".to_string()));
                                map.insert("properties".to_string(), serde_json::Value::Object(properties));
                                if !required.is_empty() {
                                    map.insert("required".to_string(), serde_json::Value::Array(
                                        required.into_iter().map(serde_json::Value::String).collect()
                                    ));
                                }
                                #title_quote
                                #description_quote
                                serde_json::Value::Object(map)
                            }
                        }
                    }
                }
            });

            quote! {
                let mut schema = serde_json::Map::new();
                schema.insert("oneOf".to_string(), serde_json::Value::Array(vec![
                    #(#variant_schemas),*
                ]));
                schema
            }
        }
        _ => panic!("JsonSchema derive macro only supports structs and enums"),
    };

    let expanded = quote! {
        impl #name {
            pub fn json_schema() -> serde_json::Map<String, serde_json::Value> {
                #schema_body
            }
        }
    };
    TokenStream::from(expanded)
}

#[proc_macro_attribute]
/// A procedural macro attribute to generate `rust_mcp_schema::Resource` related utility methods for a struct.
///
/// The `mcp_resource` macro adds static methods to the annotated struct that provide access to
/// resource metadata and construct a fully populated `rust_mcp_schema::Resource` instance.
///
/// Generated methods:
/// - `resource_name()` → returns the resource name as `&'static str`
/// - `resource_uri()` → returns the resource URI as `&'static str`
/// - `resource()` → constructs and returns a complete `rust_mcp_schema::Resource` value
///
/// # Attributes
///
/// All attributes are optional except `name` and `uri`, which are **required** and must be non-empty.
///
/// | Attribute     | Type                                 | Required | Description |
/// |---------------|--------------------------------------|----------|-------------|
/// | `name`        | string literal or `concat!(...)`     | Yes      | Unique name of the resource. |
/// | `description` | string literal or `concat!(...)`     | Yes      | Human-readable description of the resource. |
/// | `title`       | string literal or `concat!(...)`     | No       | Display title for the resource. |
/// | `meta`        | JSON object as string literal        | No       | Arbitrary metadata as a valid JSON object. Must parse as a JSON object (not array, null, etc.). |
/// | `mime_type`   | string literal                       | No       | MIME type of the resource (e.g., `"image/png"`, `"application/pdf"`). |
/// | `size`        | integer literal (`i64`)              | No       | Size of the resource in bytes. |
/// | `uri`         | string literal                       | No       | URI where the resource can be accessed. |
/// | `audience`    | array of string literals             | No       | List of intended audiences (e.g., `["user", "system"]`). |
/// | `icons`       | array of icon objects                | No       | List of icons in the same format as web app manifests (supports `src`, `sizes`, `type`). |
///
/// String fields (`name`, `description`, `title`) support `concat!(...)` with string literals.
///
/// # Panics
///
/// The macro will cause a compile-time error (not a runtime panic) if:
/// - Applied to anything other than a struct.
/// - Required attributes (`name` or `uri`) are missing or empty.
/// - `meta` is provided but is not a valid JSON object.
/// - Invalid types are used for any attribute (e.g., non-integer for `size`).
///
/// # Example
///
/// ```rust
/// use rust_mcp_macros::mcp_resource;
/// #[mcp_resource(
///     name = "company-logo",
///     description = "The official company logo in high resolution",
///     title = "Company Logo",
///     mime_type = "image/png",
///     size = 102400,
///     uri = "https://example.com/assets/logo.png",
///     audience = ["user", "assistant"],
///     meta = "{\"license\": \"proprietary\", \"author\": \"Ali Hashemi\"}",
///     icons = [
///     ( src = "logo-192.png", sizes = ["192x192"], mime_type = "image/png" ),
///     ( src = "logo-512.png", sizes = ["512x512"], mime_type = "image/png" )
///     ]
/// )]
/// struct CompanyLogo{};
///
/// // Usage
/// assert_eq!(CompanyLogo::resource_name(), "company-logo");
/// assert_eq!(CompanyLogo::resource_uri(), "https://example.com/assets/logo.png");
///
/// let resource = CompanyLogo::resource();
/// assert_eq!(resource.name, "company-logo");
/// assert_eq!(resource.mime_type.unwrap(), "image/png");
/// assert_eq!(resource.size.unwrap(), 102400);
/// assert!(resource.icons.len() == 2);
/// ```
pub fn mcp_resource(attributes: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let input_ident = &input.ident;
    let macro_attributes = parse_macro_input!(attributes as McpResourceMacroAttributes);

    let ResourceTokens {
        base_crate,
        name,
        description,
        meta,
        title,
        icons,
        annotations,
        mime_type,
        size,
        uri,
    } = generate_resource_tokens(macro_attributes);

    quote! {
         impl #input_ident {

            /// returns the Resource uri
            pub fn resource_uri()->&'static str{
                #uri
            }

            /// returns the Resource name
            pub fn resource_name()->&'static str{
                #name
            }

            /// Constructs and returns a `rust_mcp_schema::Resource` instance.
            pub fn resource()->#base_crate::Resource{
                #base_crate::Resource{
                    annotations: #annotations,
                    description: #description,
                    icons: #icons,
                    meta: #meta,
                    mime_type: #mime_type,
                    name: #name,
                    size: #size,
                    title: #title,
                    uri: #uri
                }
            }
         }
         #input
    }
    .into()
}

#[proc_macro_attribute]
/// A procedural macro attribute to generate `rust_mcp_schema::Resource` related utility methods for a struct.
///
/// The `mcp_resource` macro adds static methods to the annotated struct that provide access to
/// resource metadata and construct a fully populated `rust_mcp_schema::Resource` instance.
///
/// Generated methods:
/// - `resource_name()` → returns the resource name as `&'static str`
/// - `resource_uri_template()` → returns the resource template URI as `&'static str`
/// - `resource()` → constructs and returns a complete `rust_mcp_schema::Resource` value
///
/// # Attributes
///
/// All attributes are optional except `name` and `uri`, which are **required** and must be non-empty.
///
/// | Attribute     | Type                                 | Required | Description |
/// |---------------|--------------------------------------|----------|-------------|
/// | `name`        | string literal or `concat!(...)`     | Yes      | Unique name of the resource. |
/// | `description` | string literal or `concat!(...)`     | Yes      | Human-readable description of the resource. |
/// | `title`       | string literal or `concat!(...)`     | No       | Display title for the resource. |
/// | `meta`        | JSON object as string literal        | No       | Arbitrary metadata as a valid JSON object. Must parse as a JSON object (not array, null, etc.). |
/// | `mime_type`   | string literal                       | No       | MIME type of the resource (e.g., `"image/png"`, `"application/pdf"`). |
/// | `uri_template`         | string literal                       | No       | URI template where the resource can be accessed. |
/// | `audience`    | array of string literals             | No       | List of intended audiences (e.g., `["user", "system"]`). |
/// | `icons`       | array of icon objects                | No       | List of icons in the same format as web app manifests (supports `src`, `sizes`, `type`). |
///
/// String fields (`name`, `description`, `title`) support `concat!(...)` with string literals.
///
/// # Panics
///
/// The macro will cause a compile-time error (not a runtime panic) if:
/// - Applied to anything other than a struct.
/// - Required attributes (`name` or `uri_template`) are missing or empty.
/// - `meta` is provided but is not a valid JSON object.
/// - Invalid types are used for any attribute (e.g., non-integer for `size`).
///
/// # Example
///
/// ```rust
/// use rust_mcp_macros::mcp_resource_template;
/// #[mcp_resource_template(
///     name = "company-logos",
///     description = "The official company logos in different resolutions",
///     title = "Company Logos",
///     mime_type = "image/png",
///     uri_template = "https://example.com/assets/{file_path}",
///     audience = ["user", "assistant"],
///     meta = "{\"license\": \"proprietary\", \"author\": \"Ali Hashemi\"}",
///     icons = [
///     ( src = "logo-192.png", sizes = ["192x192"], mime_type = "image/png" ),
///     ( src = "logo-512.png", sizes = ["512x512"], mime_type = "image/png" )
///     ]
/// )]
/// struct CompanyLogo {};
///
/// // Usage
/// assert_eq!(CompanyLogo::resource_template_name(), "company-logos");
/// assert_eq!(
///     CompanyLogo::resource_template_uri(),
///     "https://example.com/assets/{file_path}"
/// );
///
/// let resource_template = CompanyLogo::resource_template();
/// assert_eq!(resource_template.name, "company-logos");
/// assert_eq!(resource_template.mime_type.unwrap(), "image/png");
/// assert!(resource_template.icons.len() == 2);
/// ```
pub fn mcp_resource_template(attributes: TokenStream, input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let input_ident = &input.ident;
    let macro_attributes = parse_macro_input!(attributes as McpResourceTemplateMacroAttributes);

    let ResourceTemplateTokens {
        base_crate,
        name,
        description,
        meta,
        title,
        icons,
        annotations,
        mime_type,
        uri_template,
    } = generate_resource_template_tokens(macro_attributes);

    quote! {
         impl #input_ident {

            /// returns the Resource Template uri
            pub fn resource_template_uri()->&'static str{
                #uri_template
            }

            /// returns the Resource Template name
            pub fn resource_template_name()->&'static str{
                #name
            }

            /// Constructs and returns a `rust_mcp_schema::Resource` instance.
            pub fn resource_template()->#base_crate::ResourceTemplate{
                #base_crate::ResourceTemplate{
                    annotations: #annotations,
                    description: #description,
                    icons: #icons,
                    meta: #meta,
                    mime_type: #mime_type,
                    name: #name,
                    title: #title,
                    uri_template: #uri_template
                }
            }
         }
         #input
    }
    .into()
}