Skip to content

Commit 7ae2728

Browse files
authored
fix(macros): add error deal (#109)
1.Check whether the parameters include aggregated parameters and individual parameters 2.Check if the toolbox attribute is default Signed-off-by: jokemanfire <[email protected]>
1 parent a1112bc commit 7ae2728

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

crates/rmcp-macros/src/tool.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use proc_macro2::TokenStream;
44
use quote::{ToTokens, quote};
55
use syn::{
66
Expr, FnArg, Ident, ItemFn, ItemImpl, MetaList, PatType, Token, Type, Visibility, parse::Parse,
7-
parse_quote,
7+
parse_quote, spanned::Spanned,
88
};
99

1010
#[derive(Default)]
@@ -21,6 +21,7 @@ impl Parse for ToolImplItemAttrs {
2121
"tool_box" => {
2222
tool_box = Some(None);
2323
if input.lookahead1().peek(Token![=]) {
24+
input.parse::<Token![=]>()?;
2425
let value: Ident = input.parse()?;
2526
tool_box = Some(Some(value));
2627
}
@@ -224,6 +225,11 @@ pub(crate) fn tool_impl_item(attr: TokenStream, mut input: ItemImpl) -> syn::Res
224225
rmcp::tool_box!(@derive #ident);
225226
));
226227
}
228+
} else {
229+
return Err(syn::Error::new(
230+
proc_macro2::Span::call_site(),
231+
"tool_box attribute is required for trait implementation",
232+
));
227233
}
228234
} else if let Some(ident) = tool_box_ident {
229235
// if it is a normal impl block
@@ -367,6 +373,12 @@ pub(crate) fn tool_fn_item(attr: TokenStream, mut input_fn: ItemFn) -> syn::Resu
367373
unextractable_args_indexes.insert(index);
368374
}
369375
Some(Caught::Aggregated(rust_type)) => {
376+
if let ToolParams::Params { .. } = tool_macro_attrs.params {
377+
return Err(syn::Error::new(
378+
rust_type.span(),
379+
"cannot mix aggregated and individual parameters",
380+
));
381+
}
370382
tool_macro_attrs.params = ToolParams::Aggregated { rust_type };
371383
unextractable_args_indexes.insert(index);
372384
}
@@ -626,10 +638,12 @@ mod test {
626638

627639
#[test]
628640
fn test_trait_tool_macro() -> syn::Result<()> {
629-
let attr = quote! {};
641+
let attr = quote! {
642+
tool_box = Calculator
643+
};
630644
let input = quote! {
631645
impl ServerHandler for Calculator {
632-
tool_box!(@derive);
646+
#[tool]
633647
fn get_info(&self) -> ServerInfo {
634648
ServerInfo {
635649
instructions: Some("A simple calculator".into()),

0 commit comments

Comments
 (0)