Enhance Tool Macro Functionality#161
Conversation
|
Apart from your functional changes, I want to point out follows:
|
Got it. As for the problem of comment names, it is related to my git config configuration. I have always been working on personal projects and trying to submit PR for the first time, so I don't have enough experience in these. Some insignificant changes are related to my use of cargo clippy --fix after deleting rustfmt. Some things may have been changed by clippy (guessing) |
|
@yIllusionSky Yeah, It's your freedom to leave your real name in commit message, just a reminder since I can tell that you don't often commit to public project. And we may need a clippy config @jokemanfire. It would be another pr. |
## 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
## Changes
This modification simplifies the usage of rmcp-macros, with the following key changes:
### 1. Merged Toolbox Declaration and Description
**Reference Example:**
Original code:
```rust
#[tool(tool_box)]
impl Calculator {
// Tool method implementations
}
#[tool(tool_box)]
impl ServerHandler for Calculator {
fn get_info(&self) -> ServerInfo {
ServerInfo {
instructions: Some("A simple calculator".into()),
capabilities: ServerCapabilities::builder().enable_tools().build(),
..Default::default()
}
}
}
```
Simplified:
```rust
#[tool(tool_box,description = "A simple calculator")]
impl Calculator {
// Tool method implementations
}
```
### 2. Simplified Implementation Process
- No longer requires separate implementation of the `ServerHandler` trait
- Tool descriptions are provided directly through macro parameters
- Reduces redundant code, improving maintainability
## Advantages
1. More concise code
2. Reduced boilerplate
3. More intuitive API usage
4. Lower barrier to entry
I reconstructed a new one according to the idea I gave you, except for a few differences in the naming. |
right, I am interested in this submit and I will also take a look later. |
I tried to fix the cli's warn error. These rustfmt configurations require unstable configuration to start, but rust-toolchain does not configure unstable |
|
you may forget update the stable channel in rustc. :) |
I have been developing with the latest nightly version of rust, and I update rustup almost every week.I think that might not be the problem? |
|
@yIllusionSky Great work! Can you apply the review and resolve conflicts? |
|
I am going to refactor the tool macro, which may conflict with this PR. |
I have completed the merge. According to the merge content, I found that the merged content did not conflict with what I wrote. I directly merged them by keeping them. In addition to the slight modification of use and Cargo.toml |
|
Thanks for your work , I think you must merger the #261 ,and rework, Feel free to open new pr. |
Changes
rust-toolchain.tomlfile as project doesn't need to restrict to a specific Rust version#[tool(param)]and#[tool(aggr)]parameters without manual markingtool_impl_itemfunctionality for default implementation ofServerHandlertraitdefault_buildparameter (defaults to true, can be disabled withdefault_build=false)Benefits
These improvements simplify API usage and reduce manual configuration needs. In most cases, users only need to mark
#[tool(tool_box)]to get default implementation of theServerHandlertrait.Testing
Basic tests have been performed to verify functionality.