Skip to content

Commit d9232cf

Browse files
author
illusion_autumn
committed
# Simplify rcmp-macros Usage
## Changes Simplified the usage of rcmp-macros with the following changes: ### Before ```rust #[tool(tool_box)] impl Calculator { #[tool(description = "Calculate the sum of two numbers")] fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String { (a + b).to_string() } #[tool(description = "Calculate the difference of two numbers")] fn sub( &self, #[tool(param)] #[schemars(description = "the left hand side number")] a: i32, #[tool(param)] #[schemars(description = "the right hand side number")] b: i32, ) -> Json<i32> { Json(a - b) } } ``` ### After ```rust #[tool(tool_box)] impl Calculator { #[tool(description = "Calculate the sum of two numbers",aggr)] fn sum(&self, SumRequest { a, b }: SumRequest) -> String { (a + b).to_string() } #[tool(description = "Calculate the difference of two numbers")] fn sub( &self, #[schemars(description = "the left hand side number")] a: i32, #[schemars(description = "the right hand side number")] b: i32, ) -> Json<i32> { Json(a - b) } } ``` ## Improvements 1. Moved parameter-level `#[tool(aggr)]` attribute to the function level 2. Removed redundant `#[tool(param)]` markers 3. Maintained the same functionality while making the code more concise and readable
1 parent 94a0da3 commit d9232cf

File tree

9 files changed

+41
-47
lines changed

9 files changed

+41
-47
lines changed

crates/rmcp-macros/src/tool.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,24 @@ struct ToolFnItemAttrs {
4545
name: Option<Expr>,
4646
description: Option<Expr>,
4747
vis: Option<Visibility>,
48+
aggr: bool,
4849
}
4950

5051
impl Parse for ToolFnItemAttrs {
5152
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
5253
let mut name = None;
5354
let mut description = None;
5455
let mut vis = None;
56+
let mut aggr = false;
5557
while !input.is_empty() {
5658
let key: Ident = input.parse()?;
59+
let key_str = key.to_string();
60+
if key_str == AGGREGATED_IDENT {
61+
aggr = true;
62+
continue;
63+
}
5764
input.parse::<Token![=]>()?;
58-
match key.to_string().as_str() {
65+
match key_str.as_str() {
5966
"name" => {
6067
let value: Expr = input.parse()?;
6168
name = Some(value);
@@ -82,6 +89,7 @@ impl Parse for ToolFnItemAttrs {
8289
name,
8390
description,
8491
vis,
92+
aggr,
8593
})
8694
}
8795
}
@@ -346,29 +354,7 @@ pub(crate) fn tool_fn_item(attr: TokenStream, mut input_fn: ItemFn) -> syn::Resu
346354
for attr in raw_attrs {
347355
match &attr.meta {
348356
syn::Meta::List(meta_list) => {
349-
if meta_list.path.is_ident(TOOL_IDENT) {
350-
let pat_type = pat_type.clone();
351-
let marker = meta_list.parse_args::<ParamMarker>()?;
352-
match marker {
353-
ParamMarker::Param => {
354-
let Some(arg_ident) = arg_ident.take() else {
355-
return Err(syn::Error::new(
356-
proc_macro2::Span::call_site(),
357-
"input param must have an ident as name",
358-
));
359-
};
360-
caught.replace(Caught::Param(ToolFnParamAttrs {
361-
serde_meta: Vec::new(),
362-
schemars_meta: Vec::new(),
363-
ident: arg_ident,
364-
rust_type: pat_type.ty.clone(),
365-
}));
366-
}
367-
ParamMarker::Aggregated => {
368-
caught.replace(Caught::Aggregated(pat_type.clone()));
369-
}
370-
}
371-
} else if meta_list.path.is_ident(SERDE_IDENT) {
357+
if meta_list.path.is_ident(SERDE_IDENT) {
372358
serde_metas.push(meta_list.clone());
373359
} else if meta_list.path.is_ident(SCHEMARS_IDENT) {
374360
schemars_metas.push(meta_list.clone());
@@ -381,6 +367,23 @@ pub(crate) fn tool_fn_item(attr: TokenStream, mut input_fn: ItemFn) -> syn::Resu
381367
}
382368
}
383369
}
370+
let pat_type = pat_type.clone();
371+
if tool_macro_attrs.fn_item.aggr{
372+
caught.replace(Caught::Aggregated(pat_type.clone()));
373+
}else{
374+
let Some(arg_ident) = arg_ident.take() else {
375+
return Err(syn::Error::new(
376+
proc_macro2::Span::call_site(),
377+
"input param must have an ident as name",
378+
));
379+
};
380+
caught.replace(Caught::Param(ToolFnParamAttrs {
381+
serde_meta: Vec::new(),
382+
schemars_meta: Vec::new(),
383+
ident: arg_ident,
384+
rust_type: pat_type.ty.clone(),
385+
}));
386+
}
384387
match caught {
385388
Some(Caught::Param(mut param)) => {
386389
param.serde_meta = serde_metas;

crates/rmcp/tests/common/calculator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ pub struct SumRequest {
1313
pub struct Calculator;
1414
#[tool(tool_box)]
1515
impl Calculator {
16-
#[tool(description = "Calculate the sum of two numbers")]
17-
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
16+
#[tool(description = "Calculate the sum of two numbers",aggr)]
17+
fn sum(&self, SumRequest { a, b }: SumRequest) -> String {
1818
(a + b).to_string()
1919
}
2020

2121
#[tool(description = "Calculate the sub of two numbers")]
2222
fn sub(
2323
&self,
24-
#[tool(param)]
2524
#[schemars(description = "the left hand side number")]
2625
a: i32,
27-
#[tool(param)]
2826
#[schemars(description = "the right hand side number")]
2927
b: i32,
3028
) -> String {

crates/rmcp/tests/test_complex_schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ impl Demo {
3030
Self
3131
}
3232

33-
#[tool(description = "LLM")]
33+
#[tool(description = "LLM",aggr)]
3434
async fn chat(
3535
&self,
36-
#[tool(aggr)] chat_request: ChatRequest,
36+
chat_request: ChatRequest,
3737
) -> Result<CallToolResult, McpError> {
3838
let content = Content::json(chat_request)?;
3939
Ok(CallToolResult::success(vec![content]))

crates/rmcp/tests/test_tool_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct Server {}
3030
impl Server {
3131
/// This tool is used to get the weather of a city.
3232
#[tool(name = "get-weather", description = "Get the weather of a city.", vis = )]
33-
pub async fn get_weather(&self, #[tool(param)] city: String) -> String {
33+
pub async fn get_weather(&self, city: String) -> String {
3434
drop(city);
3535
"rain".to_string()
3636
}

examples/servers/src/common/calculator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ pub struct SumRequest {
1515
pub struct Calculator;
1616
#[tool(tool_box)]
1717
impl Calculator {
18-
#[tool(description = "Calculate the sum of two numbers")]
19-
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
18+
#[tool(description = "Calculate the sum of two numbers",aggr)]
19+
fn sum(&self, SumRequest { a, b }: SumRequest) -> String {
2020
(a + b).to_string()
2121
}
2222

2323
#[tool(description = "Calculate the difference of two numbers")]
2424
fn sub(
2525
&self,
26-
#[tool(param)]
2726
#[schemars(description = "the left hand side number")]
2827
a: i32,
29-
#[tool(param)]
3028
#[schemars(description = "the right hand side number")]
3129
b: i32,
3230
) -> Json<i32> {

examples/servers/src/common/counter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,16 @@ impl Counter {
6464
#[tool(description = "Repeat what you say")]
6565
fn echo(
6666
&self,
67-
#[tool(param)]
6867
#[schemars(description = "Repeat what you say")]
6968
saying: String,
7069
) -> Result<CallToolResult, McpError> {
7170
Ok(CallToolResult::success(vec![Content::text(saying)]))
7271
}
7372

74-
#[tool(description = "Calculate the sum of two numbers")]
73+
#[tool(description = "Calculate the sum of two numbers",aggr)]
7574
fn sum(
7675
&self,
77-
#[tool(aggr)] StructRequest { a, b }: StructRequest,
76+
StructRequest { a, b }: StructRequest,
7877
) -> Result<CallToolResult, McpError> {
7978
Ok(CallToolResult::success(vec![Content::text(
8079
(a + b).to_string(),

examples/servers/src/common/generic_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<DS: DataService> GenericService<DS> {
5656
}
5757

5858
#[tool(description = "set memory to service")]
59-
pub async fn set_data(&self, #[tool(param)] data: String) -> String {
59+
pub async fn set_data(&self, data: String) -> String {
6060
let new_data = data.clone();
6161
format!("Current memory: {}", new_data)
6262
}

examples/transport/src/common/calculator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@ pub struct SumRequest {
99
#[derive(Debug, Clone)]
1010
pub struct Calculator;
1111
impl Calculator {
12-
#[tool(description = "Calculate the sum of two numbers")]
13-
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
12+
#[tool(description = "Calculate the sum of two numbers",aggr)]
13+
fn sum(&self, SumRequest { a, b }: SumRequest) -> String {
1414
(a + b).to_string()
1515
}
1616

1717
#[tool(description = "Calculate the sub of two numbers")]
1818
fn sub(
1919
&self,
20-
#[tool(param)]
2120
#[schemars(description = "the left hand side number")]
2221
a: i32,
23-
#[tool(param)]
2422
#[schemars(description = "the right hand side number")]
2523
b: i32,
2624
) -> String {

examples/wasi/src/calculator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ pub struct SumRequest {
1313
#[derive(Debug, Clone)]
1414
pub struct Calculator;
1515
impl Calculator {
16-
#[tool(description = "Calculate the sum of two numbers")]
17-
fn sum(&self, #[tool(aggr)] SumRequest { a, b }: SumRequest) -> String {
16+
#[tool(description = "Calculate the sum of two numbers",aggr)]
17+
fn sum(&self, SumRequest { a, b }: SumRequest) -> String {
1818
(a + b).to_string()
1919
}
2020

2121
#[tool(description = "Calculate the sub of two numbers")]
2222
fn sub(
2323
&self,
24-
#[tool(param)]
2524
#[schemars(description = "the left hand side number")]
2625
a: i32,
27-
#[tool(param)]
2826
#[schemars(description = "the right hand side number")]
2927
b: i32,
3028
) -> String {

0 commit comments

Comments
 (0)