You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/rmcp-macros/README.md
+56-9Lines changed: 56 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,10 @@
6
6
7
7
This library primarily provides the following macros:
8
8
9
-
-`#[tool]`: Used to mark functions as RMCP tools, automatically generating necessary metadata and invocation mechanisms
9
+
-`#[tool]`: Mark an async/sync function as an RMCP tool and generate metadata + schema glue
10
+
-`#[tool_router]`: Collect all `#[tool]` functions in an impl block into a router value
11
+
-`#[tool_handler]`: Implement the `call_tool` and `list_tools` entry points by delegating to a router expression
12
+
-`#[task_handler]`: Wire up the task lifecycle (list/enqueue/get/cancel) on top of an `OperationProcessor`
10
13
11
14
## Usage
12
15
@@ -16,7 +19,7 @@ This macro is used to mark a function as a tool handler.
16
19
17
20
This will generate a function that return the attribute of this tool, with type `rmcp::model::Tool`.
18
21
19
-
#### Usage
22
+
#### Tool attributes
20
23
21
24
| field | type | usage |
22
25
| :- | :- | :- |
@@ -25,7 +28,7 @@ This will generate a function that return the attribute of this tool, with type
25
28
|`input_schema`|`Expr`| A JSON Schema object defining the expected parameters for the tool. If not provide, if will use the json schema of its argument with type `Parameters<T>`|
26
29
|`annotations`|`ToolAnnotationsAttribute`| Additional tool information. Defaults to `None`. |
27
30
28
-
#### Example
31
+
#### Tool example
29
32
30
33
```rust
31
34
#[tool(name ="my_tool", description ="This is my tool", annotations(title ="我的工具", read_only_hint = true))]
@@ -42,14 +45,14 @@ It creates a function that returns a `ToolRouter` instance.
42
45
43
46
In most case, you need to add a field for handler to store the router information and initialize it when creating handler, or store it with a static variable.
44
47
45
-
#### Usage
48
+
#### Router attributes
46
49
47
50
| field | type | usage |
48
51
| :- | :- | :- |
49
52
|`router`|`Ident`| The name of the router function to be generated. Defaults to `tool_router`. |
50
53
|`vis`|`Visibility`| The visibility of the generated router function. Defaults to empty. |
51
54
52
-
#### Example
55
+
#### Router example
53
56
54
57
```rust
55
58
#[tool_router]
@@ -104,13 +107,14 @@ impl MyToolHandler {
104
107
105
108
This macro will generate the handler for `tool_call` and `list_tools` methods in the implementation block, by using an existing `ToolRouter` instance.
106
109
107
-
#### Usage
110
+
#### Handler attributes
108
111
109
112
| field | type | usage |
110
113
| :- | :- | :- |
111
114
|`router`|`Expr`| The expression to access the `ToolRouter` instance. Defaults to `self.tool_router`. |
112
115
113
-
#### Example
116
+
#### Handler example
117
+
114
118
```rust
115
119
#[tool_handler]
116
120
implServerHandlerforMyToolHandler {
@@ -119,15 +123,18 @@ impl ServerHandler for MyToolHandler {
119
123
```
120
124
121
125
or using a custom router expression:
126
+
122
127
```rust
123
128
#[tool_handler(router = self.get_router().await)]
124
129
implServerHandlerforMyToolHandler {
125
130
// ...implement other handler
126
131
}
127
132
```
128
133
129
-
#### Explained
134
+
#### Handler expansion
135
+
130
136
This macro will be expended to something like this:
137
+
131
138
```rust
132
139
implServerHandlerforMyToolHandler {
133
140
asyncfncall_tool(
@@ -150,6 +157,46 @@ impl ServerHandler for MyToolHandler {
150
157
}
151
158
```
152
159
160
+
### task_handler
161
+
162
+
This macro wires the task lifecycle endpoints (`list_tasks`, `enqueue_task`, `get_task`, `cancel_task`) to an implementation of `OperationProcessor`. It keeps the handler lean by delegating scheduling, status tracking, and cancellation semantics to the processor.
163
+
164
+
#### Task handler attributes
165
+
166
+
| field | type | usage |
167
+
| :- | :- | :- |
168
+
|`processor`|`Expr`| Expression that yields an `Arc<dyn OperationProcessor>` (or compatible trait object). Defaults to `self.processor.clone()`. |
0 commit comments