forked from modelcontextprotocol/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.rs
562 lines (533 loc) · 19.3 KB
/
tool.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use std::collections::HashSet;
use proc_macro2::TokenStream;
use quote::{ToTokens, quote};
use syn::{
Expr, FnArg, Ident, ItemFn, ItemImpl, MetaList, PatType, Token, Type, Visibility, parse::Parse,
parse_quote,
};
#[derive(Default)]
struct ToolImplItemAttrs {
tool_box: Option<Option<Ident>>,
}
impl Parse for ToolImplItemAttrs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut tool_box = None;
while !input.is_empty() {
let key: Ident = input.parse()?;
match key.to_string().as_str() {
"tool_box" => {
tool_box = Some(None);
if input.lookahead1().peek(Token![=]) {
let value: Ident = input.parse()?;
tool_box = Some(Some(value));
}
}
_ => {
return Err(syn::Error::new(key.span(), "unknown attribute"));
}
}
if input.is_empty() {
break;
}
input.parse::<Token![,]>()?;
}
Ok(ToolImplItemAttrs { tool_box })
}
}
#[derive(Default)]
struct ToolFnItemAttrs {
name: Option<Expr>,
description: Option<Expr>,
vis: Option<Visibility>,
}
impl Parse for ToolFnItemAttrs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut name = None;
let mut description = None;
let mut vis = None;
while !input.is_empty() {
let key: Ident = input.parse()?;
input.parse::<Token![=]>()?;
match key.to_string().as_str() {
"name" => {
let value: Expr = input.parse()?;
name = Some(value);
}
"description" => {
let value: Expr = input.parse()?;
description = Some(value);
}
"vis" => {
let value: Visibility = input.parse()?;
vis = Some(value);
}
_ => {
return Err(syn::Error::new(key.span(), "unknown attribute"));
}
}
if input.is_empty() {
break;
}
input.parse::<Token![,]>()?;
}
Ok(ToolFnItemAttrs {
name,
description,
vis,
})
}
}
struct ToolFnParamAttrs {
serde_meta: Vec<MetaList>,
schemars_meta: Vec<MetaList>,
ident: Ident,
rust_type: Box<Type>,
}
impl ToTokens for ToolFnParamAttrs {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ident = &self.ident;
let rust_type = &self.rust_type;
let serde_meta = &self.serde_meta;
let schemars_meta = &self.schemars_meta;
tokens.extend(quote! {
#(#[#serde_meta])*
#(#[#schemars_meta])*
pub #ident: #rust_type,
});
}
}
#[derive(Default)]
enum ToolParams {
Aggregated {
rust_type: PatType,
},
Params {
attrs: Vec<ToolFnParamAttrs>,
},
#[default]
NoParam,
}
#[derive(Default)]
struct ToolAttrs {
fn_item: ToolFnItemAttrs,
params: ToolParams,
}
const TOOL_IDENT: &str = "tool";
const SERDE_IDENT: &str = "serde";
const SCHEMARS_IDENT: &str = "schemars";
const PARAM_IDENT: &str = "param";
const AGGREGATED_IDENT: &str = "aggr";
const REQ_IDENT: &str = "req";
pub enum ParamMarker {
Param,
Aggregated,
}
impl Parse for ParamMarker {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let ident: Ident = input.parse()?;
match ident.to_string().as_str() {
PARAM_IDENT => Ok(ParamMarker::Param),
AGGREGATED_IDENT | REQ_IDENT => Ok(ParamMarker::Aggregated),
_ => Err(syn::Error::new(ident.span(), "unknown attribute")),
}
}
}
pub enum ToolItem {
Fn(ItemFn),
Impl(ItemImpl),
}
impl Parse for ToolItem {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let lookahead = input.lookahead1();
if lookahead.peek(Token![impl]) {
let item = input.parse::<ItemImpl>()?;
Ok(ToolItem::Impl(item))
} else {
let item = input.parse::<ItemFn>()?;
Ok(ToolItem::Fn(item))
}
}
}
// dispatch impl function item and impl block item
pub(crate) fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
let tool_item = syn::parse2::<ToolItem>(input)?;
match tool_item {
ToolItem::Fn(item) => tool_fn_item(attr, item),
ToolItem::Impl(item) => tool_impl_item(attr, item),
}
}
pub(crate) fn tool_impl_item(attr: TokenStream, mut input: ItemImpl) -> syn::Result<TokenStream> {
let tool_impl_attr: ToolImplItemAttrs = syn::parse2(attr)?;
let tool_box_ident = tool_impl_attr.tool_box;
if input.trait_.is_some() {
if let Some(ident) = tool_box_ident {
input.items.push(parse_quote!(
rmcp::tool_box!(@derive #ident);
));
}
} else if let Some(ident) = tool_box_ident {
let mut tool_fn_idents = Vec::new();
for item in &input.items {
if let syn::ImplItem::Fn(method) = item {
for attr in &method.attrs {
if attr.path().is_ident(TOOL_IDENT) {
tool_fn_idents.push(method.sig.ident.clone());
}
}
}
}
let this_type_ident = &input.self_ty;
input.items.push(parse_quote!(
rmcp::tool_box!(#this_type_ident {
#(#tool_fn_idents),*
} #ident);
));
}
Ok(quote! {
#input
})
}
pub(crate) fn tool_fn_item(attr: TokenStream, mut input_fn: ItemFn) -> syn::Result<TokenStream> {
let mut tool_macro_attrs = ToolAttrs::default();
let args: ToolFnItemAttrs = syn::parse2(attr)?;
tool_macro_attrs.fn_item = args;
// let mut fommated_fn_args: Punctuated<FnArg, Comma> = Punctuated::new();
let mut unextractable_args_indexes = HashSet::new();
for (index, mut fn_arg) in input_fn.sig.inputs.iter_mut().enumerate() {
enum Caught {
Param(ToolFnParamAttrs),
Aggregated(PatType),
}
let mut caught = None;
match &mut fn_arg {
FnArg::Receiver(_) => {
continue;
}
FnArg::Typed(pat_type) => {
let mut serde_metas = Vec::new();
let mut schemars_metas = Vec::new();
let mut arg_ident = match pat_type.pat.as_ref() {
syn::Pat::Ident(pat_ident) => Some(pat_ident.ident.clone()),
_ => None,
};
let raw_attrs: Vec<_> = pat_type.attrs.drain(..).collect();
for attr in raw_attrs {
match &attr.meta {
syn::Meta::List(meta_list) => {
if meta_list.path.is_ident(TOOL_IDENT) {
let pat_type = pat_type.clone();
let marker = meta_list.parse_args::<ParamMarker>()?;
match marker {
ParamMarker::Param => {
let Some(arg_ident) = arg_ident.take() else {
return Err(syn::Error::new(
proc_macro2::Span::call_site(),
"input param must have an ident as name",
));
};
caught.replace(Caught::Param(ToolFnParamAttrs {
serde_meta: Vec::new(),
schemars_meta: Vec::new(),
ident: arg_ident,
rust_type: pat_type.ty.clone(),
}));
}
ParamMarker::Aggregated => {
caught.replace(Caught::Aggregated(pat_type.clone()));
}
}
} else if meta_list.path.is_ident(SERDE_IDENT) {
serde_metas.push(meta_list.clone());
} else if meta_list.path.is_ident(SCHEMARS_IDENT) {
schemars_metas.push(meta_list.clone());
} else {
pat_type.attrs.push(attr);
}
}
_ => {
pat_type.attrs.push(attr);
}
}
}
match caught {
Some(Caught::Param(mut param)) => {
param.serde_meta = serde_metas;
param.schemars_meta = schemars_metas;
match &mut tool_macro_attrs.params {
ToolParams::Params { attrs } => {
attrs.push(param);
}
_ => {
tool_macro_attrs.params = ToolParams::Params { attrs: vec![param] };
}
}
unextractable_args_indexes.insert(index);
}
Some(Caught::Aggregated(rust_type)) => {
tool_macro_attrs.params = ToolParams::Aggregated { rust_type };
unextractable_args_indexes.insert(index);
}
None => {}
}
}
}
}
// input_fn.sig.inputs = fommated_fn_args;
let name = if let Some(expr) = tool_macro_attrs.fn_item.name {
expr
} else {
let fn_name = &input_fn.sig.ident;
parse_quote! {
stringify!(#fn_name)
}
};
let tool_attr_fn_ident = Ident::new(
&format!("{}_tool_attr", input_fn.sig.ident),
proc_macro2::Span::call_site(),
);
// generate get tool attr function
let tool_attr_fn = {
let description = if let Some(expr) = tool_macro_attrs.fn_item.description {
expr
} else {
parse_quote! {
""
}
};
let schema = match &tool_macro_attrs.params {
ToolParams::Aggregated { rust_type } => {
let ty = &rust_type.ty;
let schema = quote! {
rmcp::handler::server::tool::cached_schema_for_type::<#ty>()
};
schema
}
ToolParams::Params { attrs, .. } => {
let (param_type, temp_param_type_name) =
create_request_type(attrs, input_fn.sig.ident.to_string());
let schema = quote! {
{
#param_type
rmcp::handler::server::tool::cached_schema_for_type::<#temp_param_type_name>()
}
};
schema
}
ToolParams::NoParam => {
quote! {
rmcp::handler::server::tool::cached_schema_for_type::<rmcp::model::EmptyObject>()
}
}
};
let input_fn_attrs = &input_fn.attrs;
let input_fn_vis = &input_fn.vis;
quote! {
#(#input_fn_attrs)*
#input_fn_vis fn #tool_attr_fn_ident() -> rmcp::model::Tool {
rmcp::model::Tool {
name: #name.into(),
description: Some(#description.into()),
input_schema: #schema.into(),
annotations: None
}
}
}
};
// generate wrapped tool function
let tool_call_fn = {
// wrapper function have the same sig:
// async fn #tool_tool_call(context: rmcp::handler::server::tool::ToolCallContext<'_, Self>)
// -> std::result::Result<rmcp::model::CallToolResult, rmcp::Error>
//
// and the block part should be like:
// {
// use rmcp::handler::server::tool::*;
// let (t0, context) = <T0>::from_tool_call_context_part(context)?;
// let (t1, context) = <T1>::from_tool_call_context_part(context)?;
// ...
// let (tn, context) = <Tn>::from_tool_call_context_part(context)?;
// // for params
// ... expand helper types here
// let (__rmcp_tool_req, context) = rmcp::model::JsonObject::from_tool_call_context_part(context)?;
// let __#TOOL_ToolCallParam { param_0, param_1, param_2, .. } = parse_json_object(__rmcp_tool_req)?;
// // for aggr
// let (Parameters(aggr), context) = <Parameters<AggrType>>::from_tool_call_context_part(context)?;
// Self::#tool_ident(to, param_0, t1, param_1, ..., param_2, tn, aggr).await.into_call_tool_result()
//
// }
//
//
//
// for receiver type, name it as __rmcp_tool_receiver
let is_async = input_fn.sig.asyncness.is_some();
let receiver_ident = || Ident::new("__rmcp_tool_receiver", proc_macro2::Span::call_site());
// generate the extraction part for trivial args
let trivial_args = input_fn
.sig
.inputs
.iter()
.enumerate()
.filter_map(|(index, arg)| {
if unextractable_args_indexes.contains(&index) {
None
} else {
// get ident/type pair
let line = match arg {
FnArg::Typed(pat_type) => {
let pat = &pat_type.pat;
let ty = &pat_type.ty;
quote! {
let (#pat, context) = <#ty>::from_tool_call_context_part(context)?;
}
}
FnArg::Receiver(r) => {
let ty = r.ty.clone();
let pat = receiver_ident();
quote! {
let (#pat, context) = <#ty>::from_tool_call_context_part(context)?;
}
}
};
Some(line)
}
});
let trivial_arg_extraction_part = quote! {
#(#trivial_args)*
};
let processed_arg_extraction_part = match &mut tool_macro_attrs.params {
ToolParams::Aggregated { rust_type } => {
let PatType { pat, ty, .. } = rust_type;
quote! {
let (Parameters(#pat), context) = <Parameters<#ty>>::from_tool_call_context_part(context)?;
}
}
ToolParams::Params { attrs } => {
let (param_type, temp_param_type_name) =
create_request_type(attrs, input_fn.sig.ident.to_string());
let params_ident = attrs.iter().map(|attr| &attr.ident).collect::<Vec<_>>();
quote! {
#param_type
let (__rmcp_tool_req, context) = rmcp::model::JsonObject::from_tool_call_context_part(context)?;
let #temp_param_type_name {
#(#params_ident,)*
} = parse_json_object(__rmcp_tool_req)?;
}
}
ToolParams::NoParam => {
quote! {}
}
};
// generate the execution part
// has receiver?
let params = &input_fn
.sig
.inputs
.iter()
.map(|fn_arg| match fn_arg {
FnArg::Receiver(_) => {
let pat = receiver_ident();
quote! { #pat }
}
FnArg::Typed(pat_type) => {
let pat = &pat_type.pat.clone();
quote! { #pat }
}
})
.collect::<Vec<_>>();
let raw_fn_ident = &input_fn.sig.ident;
let call = if is_async {
quote! {
Self::#raw_fn_ident(#(#params),*).await.into_call_tool_result()
}
} else {
quote! {
Self::#raw_fn_ident(#(#params),*).into_call_tool_result()
}
};
// assemble the whole function
let tool_call_fn_ident = Ident::new(
&format!("{}_tool_call", input_fn.sig.ident),
proc_macro2::Span::call_site(),
);
let raw_fn_vis = tool_macro_attrs
.fn_item
.vis
.as_ref()
.unwrap_or(&input_fn.vis);
let raw_fn_attr = &input_fn
.attrs
.iter()
.filter(|attr| !attr.path().is_ident(TOOL_IDENT))
.collect::<Vec<_>>();
quote! {
#(#raw_fn_attr)*
#raw_fn_vis async fn #tool_call_fn_ident(context: rmcp::handler::server::tool::ToolCallContext<'_, Self>)
-> std::result::Result<rmcp::model::CallToolResult, rmcp::Error> {
use rmcp::handler::server::tool::*;
#trivial_arg_extraction_part
#processed_arg_extraction_part
#call
}
}
};
Ok(quote! {
#tool_attr_fn
#tool_call_fn
#input_fn
})
}
fn create_request_type(attrs: &[ToolFnParamAttrs], tool_name: String) -> (TokenStream, Ident) {
let pascal_case_tool_name = tool_name.to_ascii_uppercase();
let temp_param_type_name = Ident::new(
&format!("__{pascal_case_tool_name}ToolCallParam",),
proc_macro2::Span::call_site(),
);
(
quote! {
use rmcp::{serde, schemars};
#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
pub struct #temp_param_type_name {
#(#attrs)*
}
},
temp_param_type_name,
)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_tool_sync_macro() -> syn::Result<()> {
let attr = quote! {
name = "test_tool",
description = "test tool",
vis =
};
let input = quote! {
fn sum(&self, #[tool(aggr)] req: StructRequest) -> Result<CallToolResult, McpError> {
Ok(CallToolResult::success(vec![Content::text((req.a + req.b).to_string())]))
}
};
let input = tool(attr, input)?;
println!("input: {:#}", input);
Ok(())
}
#[test]
fn test_trait_tool_macro() -> syn::Result<()> {
let attr = quote! {};
let input = quote! {
impl ServerHandler for Calculator {
tool_box!(@derive);
fn get_info(&self) -> ServerInfo {
ServerInfo {
instructions: Some("A simple calculator".into()),
..Default::default()
}
}
}
};
let input = tool(attr, input)?;
println!("input: {:#}", input);
Ok(())
}
}