pmcp-macros 0.6.1

Procedural macros for PMCP SDK - Model Context Protocol
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
//! Shared codegen utilities for `#[mcp_tool]` and `#[mcp_server]` macros.
//!
//! Provides parameter classification, type detection, and schema generation
//! helpers used by both standalone and impl-block macro expansions.

use proc_macro2::TokenStream;
use quote::quote;
use syn::parse::Parser;
use syn::{FnArg, GenericParam, Generics, Type, TypePath};

/// Parameter slot for call-site argument ordering.
///
/// Used by `#[mcp_tool]`, `#[mcp_prompt]`, and `#[mcp_server]` to track
/// the user's declared parameter order and generate correct call-site code.
#[derive(Debug, Clone, Copy)]
pub enum ParamSlot {
    Args,
    State,
    Extra,
}

/// Role of a function parameter in an `#[mcp_tool]` function.
#[derive(Debug)]
pub enum ParamRole {
    /// First struct implementing `JsonSchema` + `Deserialize` = tool input args.
    Args(Type),
    /// `State<T>` = shared state injection.
    State {
        /// The inner type `T` from `State<T>`.
        inner_ty: Type,
    },
    /// `RequestHandlerExtra` = cancellation/progress/auth context.
    Extra,
    /// `&self` in impl block = server instance.
    SelfRef,
}

/// Classify a function parameter by its type.
///
/// Determines whether a parameter is:
/// - `State<T>` (shared state injection)
/// - `RequestHandlerExtra` (cancellation/progress/auth)
/// - `&self` (impl block receiver)
/// - Any other type (tool input args)
pub fn classify_param(param: &FnArg) -> syn::Result<ParamRole> {
    match param {
        FnArg::Receiver(_) => Ok(ParamRole::SelfRef),
        FnArg::Typed(pat_type) => {
            let ty = &*pat_type.ty;
            if type_name_matches(ty, "State") {
                let inner = extract_state_inner(ty)?;
                Ok(ParamRole::State { inner_ty: inner })
            } else if type_name_matches(ty, "RequestHandlerExtra") {
                Ok(ParamRole::Extra)
            } else {
                Ok(ParamRole::Args(ty.clone()))
            }
        },
    }
}

/// Check if the last segment of a type path matches the given name.
/// Handles qualified paths like `pmcp::State<T>` by checking only the final segment.
pub fn type_name_matches(ty: &Type, name: &str) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        path.segments.last().is_some_and(|s| s.ident == name)
    } else {
        false
    }
}

/// Extract inner `T` from `State<T>`.
pub fn extract_state_inner(ty: &Type) -> syn::Result<Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                if let Some(syn::GenericArgument::Type(inner)) = args.args.first() {
                    return Ok(inner.clone());
                }
            }
        }
    }
    Err(syn::Error::new_spanned(
        ty,
        "Expected State<T> with a type parameter",
    ))
}

/// Check if a type is `serde_json::Value` or a common alias.
///
/// Per D-15: skip `outputSchema` generation for `Result<Value>` returns.
/// Matches: `Value`, `JsonValue`, `serde_json::Value`.
///
/// This is a best-effort heuristic — proc macros cannot resolve type aliases,
/// so a user-defined `struct Value` would be a false positive.
pub fn is_value_type(ty: &Type) -> bool {
    if let Type::Path(TypePath { path, .. }) = ty {
        let segments = &path.segments;
        match segments.len() {
            1 => {
                let ident = &segments[0].ident;
                ident == "Value" || ident == "JsonValue"
            },
            2 => segments[0].ident == "serde_json" && segments[1].ident == "Value",
            _ => false,
        }
    } else {
        false
    }
}

/// Extract the Ok type from `Result<T>` or `Result<T, E>`.
pub fn extract_result_ok_type(ty: &Type) -> Option<Type> {
    if let Type::Path(TypePath { path, .. }) = ty {
        if let Some(segment) = path.segments.last() {
            if segment.ident == "Result" {
                if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
                    if let Some(syn::GenericArgument::Type(ok_type)) = args.args.first() {
                        return Some(ok_type.clone());
                    }
                }
            }
        }
    }
    None
}

/// Generate input schema code for a given args type.
///
/// Uses `schemars::schema_for!` and `normalize_schema`.
pub fn generate_input_schema_code(args_type: &Type) -> TokenStream {
    quote! {
        {
            let schema = schemars::schema_for!(#args_type);
            let json_schema = serde_json::to_value(&schema).unwrap_or_else(|_| {
                serde_json::json!({"type": "object", "properties": {}})
            });
            pmcp::server::schema_utils::normalize_schema(json_schema)
        }
    }
}

/// Generate output schema code for a typed output (not `Value`).
pub fn generate_output_schema_code(output_type: &Type) -> TokenStream {
    quote! {
        {
            let schema = schemars::schema_for!(#output_type);
            Some(serde_json::to_value(&schema).unwrap_or_else(|_| {
                serde_json::json!({"type": "object"})
            }))
        }
    }
}

/// Generate output schema tokens from a return type.
///
/// If return type is `Result<T>` where T is not `Value`, generates schema code.
/// Otherwise returns `quote! { None }`.
pub fn output_schema_tokens(return_type: Option<&Type>) -> TokenStream {
    let Some(rt) = return_type else {
        return quote! { None };
    };
    if let Some(ok_type) = extract_result_ok_type(rt) {
        if is_value_type(&ok_type) {
            quote! { None }
        } else {
            generate_output_schema_code(&ok_type)
        }
    } else {
        quote! { None }
    }
}

/// Generate empty schema for no-args tools.
pub fn generate_empty_schema_code() -> TokenStream {
    quote! {
        serde_json::json!({
            "type": "object",
            "properties": {},
            "required": []
        })
    }
}

/// Add Send + Sync + 'static bounds to all type parameters.
/// Used by `#[mcp_server]` to ensure handler structs are thread-safe.
pub fn add_async_trait_bounds(mut generics: Generics) -> Generics {
    for param in &mut generics.params {
        if let GenericParam::Type(type_param) = param {
            type_param.bounds.push(syn::parse_quote!(Send));
            type_param.bounds.push(syn::parse_quote!(Sync));
            type_param.bounds.push(syn::parse_quote!('static));
        }
    }
    generics
}

#[cfg(test)]
mod tests {
    use super::*;
    use syn::parse_quote;

    #[test]
    fn test_classify_param_state() {
        let param: FnArg = parse_quote!(db: State<Database>);
        let role = classify_param(&param).unwrap();
        match role {
            ParamRole::State { inner_ty, .. } => {
                let inner_str = quote!(#inner_ty).to_string();
                assert_eq!(inner_str, "Database");
            },
            _ => panic!("Expected ParamRole::State, got {:?}", role),
        }
    }

    #[test]
    fn test_classify_param_extra() {
        let param: FnArg = parse_quote!(extra: RequestHandlerExtra);
        let role = classify_param(&param).unwrap();
        assert!(matches!(role, ParamRole::Extra));
    }

    #[test]
    fn test_classify_param_args() {
        let param: FnArg = parse_quote!(args: CalculatorArgs);
        let role = classify_param(&param).unwrap();
        match role {
            ParamRole::Args(ty) => {
                let ty_str = quote!(#ty).to_string();
                assert_eq!(ty_str, "CalculatorArgs");
            },
            _ => panic!("Expected ParamRole::Args, got {:?}", role),
        }
    }

    #[test]
    fn test_classify_param_self_ref() {
        let param: FnArg = parse_quote!(&self);
        let role = classify_param(&param).unwrap();
        assert!(matches!(role, ParamRole::SelfRef));
    }

    #[test]
    fn test_is_value_type_true() {
        let ty: Type = parse_quote!(Value);
        assert!(is_value_type(&ty));
    }

    #[test]
    fn test_is_value_type_json_value_alias() {
        let ty: Type = parse_quote!(JsonValue);
        assert!(
            is_value_type(&ty),
            "should recognize JsonValue alias for serde_json::Value"
        );
    }

    #[test]
    fn test_is_value_type_fully_qualified() {
        let ty: Type = parse_quote!(serde_json::Value);
        assert!(
            is_value_type(&ty),
            "should recognize fully qualified serde_json::Value"
        );
    }

    #[test]
    fn test_is_value_type_false() {
        let ty: Type = parse_quote!(CalculatorResult);
        assert!(!is_value_type(&ty));
    }

    #[test]
    fn test_is_value_type_false_other_value() {
        // A user-defined type named MyValue should NOT match
        let ty: Type = parse_quote!(MyValue);
        assert!(!is_value_type(&ty));
    }

    #[test]
    fn test_extract_result_ok_type() {
        let ty: Type = parse_quote!(Result<String, Error>);
        let ok_type = extract_result_ok_type(&ty).unwrap();
        let ok_str = quote!(#ok_type).to_string();
        assert_eq!(ok_str, "String");
    }

    #[test]
    fn test_extract_result_ok_type_single_param() {
        let ty: Type = parse_quote!(Result<String>);
        let ok_type = extract_result_ok_type(&ty).unwrap();
        let ok_str = quote!(#ok_type).to_string();
        assert_eq!(ok_str, "String");
    }

    #[test]
    fn test_extract_result_ok_type_not_result() {
        let ty: Type = parse_quote!(String);
        assert!(extract_result_ok_type(&ty).is_none());
    }

    #[test]
    fn test_extract_state_inner() {
        let ty: Type = parse_quote!(State<Database>);
        let inner = extract_state_inner(&ty).unwrap();
        let inner_str = quote!(#inner).to_string();
        assert_eq!(inner_str, "Database");
    }

    #[test]
    fn test_extract_state_inner_error_no_params() {
        let ty: Type = parse_quote!(State);
        assert!(extract_state_inner(&ty).is_err());
    }

    #[test]
    fn test_type_name_matches_state_qualified() {
        let ty: Type = parse_quote!(pmcp::State<Database>);
        assert!(type_name_matches(&ty, "State"));
    }

    #[test]
    fn test_type_name_matches_extra_qualified() {
        let ty: Type = parse_quote!(pmcp::RequestHandlerExtra);
        assert!(type_name_matches(&ty, "RequestHandlerExtra"));
    }
}

/// Error message used when `#[mcp_tool]` has neither a `description = "..."`
/// attribute nor a rustdoc comment. Kept as a `pub const` so both
/// parse sites emit byte-identical diagnostics.
pub const MCP_TOOL_MISSING_DESCRIPTION_ERROR: &str =
    "mcp_tool requires either a `description = \"...\"` attribute or a rustdoc comment on the function";

/// Build a synthetic `description = "..."` nested-meta from a plain string.
///
/// Uses `syn::LitStr::new` + `parse_quote!` rather than string formatting
/// to guarantee correct handling of embedded quotes, backslashes, and
/// arbitrary UTF-8.
fn build_description_meta(desc: &str) -> darling::ast::NestedMeta {
    let lit = syn::LitStr::new(desc, proc_macro2::Span::call_site());
    let meta: syn::Meta = syn::parse_quote! { description = #lit };
    darling::ast::NestedMeta::Meta(meta)
}

/// True iff `metas` already contains a `description = ...` `NameValue` entry.
/// Note: empty string `description = ""` counts as present — explicit empty
/// wins silently over rustdoc.
fn has_description_meta(metas: &[darling::ast::NestedMeta]) -> bool {
    metas.iter().any(|m| {
        matches!(
            m,
            darling::ast::NestedMeta::Meta(syn::Meta::NameValue(nv))
                if nv.path.is_ident("description")
        )
    })
}

/// Shared resolver — the one place where rustdoc fallback logic lives.
///
/// # Arguments
/// - `args_tokens`: the `(...)` inside `#[mcp_tool(...)]`. Pass an empty
///   `TokenStream` for `#[mcp_tool]` with no parens.
/// - `item_attrs`: the annotated item's attributes (function-level for the
///   standalone site, method-level for the impl-block site) — the rustdoc
///   harvest source.
/// - `error_span_ident`: the identifier used for the `Span` of the
///   missing-description compile error (typically `&sig.ident`).
///
/// # Returns
/// A `Vec<NestedMeta>` guaranteed to contain a `description = ...` entry,
/// ready to feed into `McpToolArgs::from_list`. Or `Err` with a parse error
/// (if `args_tokens` is malformed) or the canonical missing-description
/// error (if neither source supplied a description).
pub fn resolve_tool_args(
    args_tokens: proc_macro2::TokenStream,
    item_attrs: &[syn::Attribute],
    error_span_ident: &syn::Ident,
) -> syn::Result<Vec<darling::ast::NestedMeta>> {
    let parser =
        syn::punctuated::Punctuated::<darling::ast::NestedMeta, syn::Token![,]>::parse_terminated;
    let mut nested_metas: Vec<darling::ast::NestedMeta> =
        parser.parse2(args_tokens)?.into_iter().collect();

    let mut has_desc = has_description_meta(&nested_metas);
    if !has_desc {
        if let Some(doc_desc) = pmcp_macros_support::rustdoc::extract_doc_description(item_attrs) {
            nested_metas.push(build_description_meta(&doc_desc));
            has_desc = true;
        }
    }

    if !has_desc {
        return Err(syn::Error::new_spanned(
            error_span_ident,
            MCP_TOOL_MISSING_DESCRIPTION_ERROR,
        ));
    }

    Ok(nested_metas)
}

#[cfg(test)]
mod rustdoc_fallback_tests {
    use super::{
        build_description_meta, has_description_meta, resolve_tool_args,
        MCP_TOOL_MISSING_DESCRIPTION_ERROR,
    };
    use quote::ToTokens;

    fn sample_ident() -> syn::Ident {
        syn::Ident::new("sample", proc_macro2::Span::call_site())
    }

    fn doc_attrs(lines: &[&str]) -> Vec<syn::Attribute> {
        lines
            .iter()
            .map(|line| {
                let lit = syn::LitStr::new(line, proc_macro2::Span::call_site());
                syn::parse_quote! { #[doc = #lit] }
            })
            .collect()
    }

    fn description_value(metas: &[darling::ast::NestedMeta]) -> Option<String> {
        metas.iter().find_map(|m| {
            if let darling::ast::NestedMeta::Meta(syn::Meta::NameValue(nv)) = m {
                if nv.path.is_ident("description") {
                    if let syn::Expr::Lit(syn::ExprLit {
                        lit: syn::Lit::Str(s),
                        ..
                    }) = &nv.value
                    {
                        return Some(s.value());
                    }
                }
            }
            None
        })
    }

    #[test]
    fn resolve_empty_args_with_rustdoc_synthesizes() {
        let attrs = doc_attrs(&[" Hello from rustdoc."]);
        let ident = sample_ident();
        let metas = resolve_tool_args(proc_macro2::TokenStream::new(), &attrs, &ident)
            .expect("should synthesize from rustdoc");
        assert_eq!(
            description_value(&metas).as_deref(),
            Some("Hello from rustdoc.")
        );
    }

    #[test]
    fn resolve_with_description_ignores_rustdoc() {
        let attrs = doc_attrs(&[" IGNORED"]);
        let ident = sample_ident();
        let args: proc_macro2::TokenStream = syn::parse_str(r#"description = "WINS""#).unwrap();
        let metas = resolve_tool_args(args.to_token_stream(), &attrs, &ident)
            .expect("attribute should win");
        assert_eq!(description_value(&metas).as_deref(), Some("WINS"));
    }

    #[test]
    fn resolve_neither_present_errors_with_canonical_wording() {
        let ident = sample_ident();
        let err = resolve_tool_args(proc_macro2::TokenStream::new(), &[], &ident)
            .expect_err("should error");
        let msg = err.to_string();
        assert!(
            msg.contains("mcp_tool requires either"),
            "error should contain canonical prefix, got: {msg}"
        );
        assert!(
            msg.contains("or a rustdoc comment on the function"),
            "error should mention rustdoc fallback, got: {msg}"
        );
    }

    #[test]
    fn resolve_empty_string_description_with_rustdoc_keeps_empty() {
        // `description = ""` is PRESENT, so rustdoc is not consulted and
        // the empty string passes through.
        let attrs = doc_attrs(&[" IGNORED rustdoc."]);
        let ident = sample_ident();
        let args: proc_macro2::TokenStream = syn::parse_str(r#"description = """#).unwrap();
        let metas = resolve_tool_args(args.to_token_stream(), &attrs, &ident)
            .expect("empty string is valid input");
        assert_eq!(description_value(&metas).as_deref(), Some(""));
    }

    #[test]
    fn resolve_empty_string_description_no_rustdoc_keeps_empty() {
        // Same as above but NO rustdoc. Empty string still counts as
        // present — no error, no synthesis.
        let ident = sample_ident();
        let args: proc_macro2::TokenStream = syn::parse_str(r#"description = """#).unwrap();
        let metas = resolve_tool_args(args.to_token_stream(), &[], &ident)
            .expect("empty string is valid input");
        assert_eq!(description_value(&metas).as_deref(), Some(""));
    }

    #[test]
    fn mcp_tool_missing_error_wording_exact() {
        assert_eq!(
            MCP_TOOL_MISSING_DESCRIPTION_ERROR,
            "mcp_tool requires either a `description = \"...\"` attribute or a rustdoc comment on the function"
        );
    }

    #[test]
    fn has_description_meta_true_when_present() {
        let meta = build_description_meta("hello");
        assert!(has_description_meta(&[meta]));
    }

    #[test]
    fn has_description_meta_false_when_absent() {
        let metas: Vec<darling::ast::NestedMeta> = Vec::new();
        assert!(!has_description_meta(&metas));
    }

    #[test]
    fn build_description_meta_roundtrips_embedded_quotes() {
        let meta = build_description_meta("he said \"hi\"");
        assert!(has_description_meta(&[meta]));
    }

    #[test]
    fn resolve_skips_include_str_doc_and_errors_if_no_other_source() {
        // `#[doc = include_str!("...")]` is Expr::Macro, not Expr::Lit(Str).
        // The support-crate helper skips it silently. With no other
        // description source, the resolver errors with the canonical wording.
        let attr: syn::Attribute = syn::parse_quote! { #[doc = include_str!("nonexistent.md")] };
        let ident = sample_ident();
        let err = resolve_tool_args(proc_macro2::TokenStream::new(), &[attr], &ident)
            .expect_err("include_str!() doc must not satisfy description");
        assert!(
            err.to_string().contains("mcp_tool requires either"),
            "include_str!-only should trigger canonical error"
        );
    }

    #[test]
    fn resolve_skips_cfg_attr_doc_and_errors_if_no_other_source() {
        // `#[cfg_attr(doc, doc = "...")]` is a `#[cfg_attr(...)]` attribute
        // whose path is `cfg_attr`, NOT `doc`. The harvest helper only
        // inspects attrs whose path is `doc`, so this is silently skipped.
        let attr: syn::Attribute = syn::parse_quote! { #[cfg_attr(doc, doc = "conditional doc")] };
        let ident = sample_ident();
        let err = resolve_tool_args(proc_macro2::TokenStream::new(), &[attr], &ident)
            .expect_err("cfg_attr-gated doc must not satisfy description");
        assert!(
            err.to_string().contains("mcp_tool requires either"),
            "cfg_attr-only should trigger canonical error"
        );
    }
}