Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions mcp-macros/src/mcp_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct ResourceInfo {
path_pattern: String,
param_names: Vec<String>,
method_param_names: Vec<syn::Ident>,
method_param_types: Vec<syn::Type>,
is_async: bool,
has_params: bool,
}
Expand Down Expand Up @@ -54,6 +55,7 @@ fn parse_uri_template(uri_template: &str) -> (String, Vec<String>) {
}

/// Extract parameter names from URI template path
/// Handles both regular params {name} and catch-all params {*name}
fn extract_uri_parameters(path: &str) -> Vec<String> {
let mut params = Vec::new();
let mut chars = path.chars().peekable();
Expand All @@ -68,7 +70,10 @@ fn extract_uri_parameters(path: &str) -> Vec<String> {
param_name.push(ch);
}
if !param_name.is_empty() {
params.push(param_name);
// Strip the leading '*' for catch-all parameters
// matchit uses {*name} syntax but stores params without the '*'
let clean_name = param_name.strip_prefix('*').unwrap_or(&param_name);
params.push(clean_name.to_string());
}
}
}
Expand Down Expand Up @@ -251,14 +256,41 @@ fn generate_matchit_resource_impl(
.iter()
.enumerate()
.map(|(param_idx, param_name)| {
if let Some(method_param) = info.method_param_names.get(param_idx) {
let method_param = match info.method_param_names.get(param_idx) {
Some(p) => p,
None => return quote! {},
};
let param_type = match info.method_param_types.get(param_idx) {
Some(t) => t,
None => return quote! {},
};

// Check if the type is String - if so, no parsing needed
let is_string_type = if let syn::Type::Path(type_path) = param_type {
type_path.path.segments.last()
.map(|seg| seg.ident == "String")
.unwrap_or(false)
} else {
false
};

if is_string_type {
// String type - just convert directly
quote! {
let #method_param = matched.params.get(#param_name)
.unwrap_or("default")
let #method_param: #param_type = matched.params.get(#param_name)
.unwrap_or("")
.to_string();
}
} else {
quote! {}
// Non-String type - parse from string
quote! {
let #method_param: #param_type = matched.params.get(#param_name)
.unwrap_or("")
.parse()
.map_err(|e| pulseengine_mcp_protocol::Error::invalid_params(
format!("Failed to parse parameter '{}': {}", #param_name, e)
))?;
}
}
})
.collect();
Expand Down Expand Up @@ -471,14 +503,16 @@ pub fn mcp_tools_impl(_attr: TokenStream, item: TokenStream) -> syn::Result<Toke
// Parse URI template to get matchit path pattern
let (path_pattern, template_param_names) = parse_uri_template(&uri_template);

// Extract method parameter names
// Extract method parameter names and types
let mut method_param_names = Vec::new();
let mut method_param_types = Vec::new();
for input in &method.sig.inputs {
match input {
syn::FnArg::Receiver(_) => continue,
syn::FnArg::Typed(pat_type) => {
if let syn::Pat::Ident(pat_ident) = &*pat_type.pat {
method_param_names.push(pat_ident.ident.clone());
method_param_types.push((*pat_type.ty).clone());
}
}
}
Expand All @@ -492,6 +526,7 @@ pub fn mcp_tools_impl(_attr: TokenStream, item: TokenStream) -> syn::Result<Toke
path_pattern,
param_names: template_param_names,
method_param_names,
method_param_types,
is_async: method.sig.asyncness.is_some(),
has_params: method.sig.inputs.len() > 1,
};
Expand Down
Loading