@@ -3,7 +3,7 @@ use proc_macro2::{Span, TokenStream};
33use quote:: { ToTokens , format_ident, quote} ;
44use syn:: { Expr , Ident , ImplItemFn , LitStr , ReturnType , parse_quote} ;
55
6- use crate :: common:: { extract_doc_line, none_expr } ;
6+ use crate :: common:: extract_doc_line;
77
88/// Check if a type is Json<T> and extract the inner type T
99fn extract_json_inner_type ( ty : & syn:: Type ) -> Option < & syn:: Type > {
@@ -110,8 +110,8 @@ pub struct ResolvedToolAttribute {
110110 pub description : Option < Expr > ,
111111 pub input_schema : Expr ,
112112 pub output_schema : Option < Expr > ,
113- pub annotations : Expr ,
114- pub execution : Expr ,
113+ pub annotations : Option < Expr > ,
114+ pub execution : Option < Expr > ,
115115 pub icons : Option < Expr > ,
116116 pub meta : Option < Expr > ,
117117}
@@ -134,26 +134,22 @@ impl ResolvedToolAttribute {
134134 } else {
135135 quote ! { None }
136136 } ;
137- let output_schema = if let Some ( output_schema) = output_schema {
138- quote ! { Some ( #output_schema) }
139- } else {
140- quote ! { None }
141- } ;
142- let title = if let Some ( title) = title {
143- quote ! { Some ( #title. into( ) ) }
144- } else {
145- quote ! { None }
146- } ;
147- let icons = if let Some ( icons) = icons {
148- quote ! { Some ( #icons) }
149- } else {
150- quote ! { None }
151- } ;
152- let meta = if let Some ( meta) = meta {
153- quote ! { Some ( #meta) }
154- } else {
155- quote ! { None }
156- } ;
137+ let title_call = title
138+ . map ( |t| quote ! { . with_title( #t) } )
139+ . unwrap_or_default ( ) ;
140+ let output_schema_call = output_schema
141+ . map ( |s| quote ! { . with_raw_output_schema( #s) } )
142+ . unwrap_or_default ( ) ;
143+ let annotations_call = annotations
144+ . map ( |a| quote ! { . with_annotations( #a) } )
145+ . unwrap_or_default ( ) ;
146+ let execution_call = execution
147+ . map ( |e| quote ! { . with_execution( #e) } )
148+ . unwrap_or_default ( ) ;
149+ let icons_call = icons
150+ . map ( |i| quote ! { . with_icons( #i) } )
151+ . unwrap_or_default ( ) ;
152+ let meta_call = meta. map ( |m| quote ! { . with_meta( #m) } ) . unwrap_or_default ( ) ;
157153 let doc_comment = format ! ( "Generated tool metadata function for {name}" ) ;
158154 let doc_attr: syn:: Attribute = parse_quote ! ( #[ doc = #doc_comment] ) ;
159155 let tokens = quote ! {
@@ -164,12 +160,12 @@ impl ResolvedToolAttribute {
164160 #description,
165161 #input_schema,
166162 )
167- . with_title ( #title )
168- . with_raw_output_schema ( #output_schema )
169- . with_annotations ( #annotations )
170- . with_execution ( #execution )
171- . with_icons ( #icons )
172- . with_meta ( #meta )
163+ #title_call
164+ #output_schema_call
165+ #annotations_call
166+ #execution_call
167+ #icons_call
168+ #meta_call
173169 }
174170 } ;
175171 syn:: parse2 :: < ImplItemFn > ( tokens)
@@ -260,17 +256,17 @@ pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
260256 let idempotent_hint = wrap_option ( idempotent_hint) ;
261257 let open_world_hint = wrap_option ( open_world_hint) ;
262258 let token_stream = quote ! {
263- Some ( rmcp:: model:: ToolAnnotations :: from_raw(
259+ rmcp:: model:: ToolAnnotations :: from_raw(
264260 #title,
265261 #read_only_hint,
266262 #destructive_hint,
267263 #idempotent_hint,
268264 #open_world_hint,
269- ) )
265+ )
270266 } ;
271- syn:: parse2 :: < Expr > ( token_stream) ?
267+ Some ( syn:: parse2 :: < Expr > ( token_stream) ?)
272268 } else {
273- none_expr ( ) ?
269+ None
274270 } ;
275271 let execution_expr = if let Some ( execution) = attribute. execution {
276272 let ToolExecutionAttribute { task_support } = execution;
@@ -296,13 +292,13 @@ pub fn tool(attr: TokenStream, input: TokenStream) -> syn::Result<TokenStream> {
296292 } ;
297293
298294 let token_stream = quote ! {
299- Some ( rmcp:: model:: ToolExecution :: from_raw(
295+ rmcp:: model:: ToolExecution :: from_raw(
300296 #task_support_expr,
301- ) )
297+ )
302298 } ;
303- syn:: parse2 :: < Expr > ( token_stream) ?
299+ Some ( syn:: parse2 :: < Expr > ( token_stream) ?)
304300 } else {
305- none_expr ( ) ?
301+ None
306302 } ;
307303 // Handle output_schema - either explicit or generated from return type
308304 let output_schema_expr = attribute. output_schema . or_else ( || {
0 commit comments