@@ -12,7 +12,9 @@ namespace ModelContextProtocol;
1212/// </summary> 
1313public  static partial  class  McpServerBuilderExtensions 
1414{ 
15-     private  const  string  RequiresUnreferencedCodeMessage  =  "This method requires dynamic lookup of method metadata and might not work in Native AOT." ; 
15+     private  const  string  WithToolsRequiresUnreferencedCodeMessage  =  
16+         $ "The non-generic { nameof ( WithTools ) }  and { nameof ( WithToolsFromAssembly ) }  methods require dynamic lookup of method metadata"+ 
17+         $ "and may not work in Native AOT. Use the generic { nameof ( WithTools ) }  method instead."; 
1618
1719    /// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary> 
1820    /// <typeparam name="TTool">The tool type.</typeparam> 
@@ -35,14 +37,9 @@ public static partial class McpServerBuilderExtensions
3537        { 
3638            if  ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( )  is  not null ) 
3739            { 
38-                 if  ( toolMethod . IsStatic ) 
39-                 { 
40-                     builder . Services . AddSingleton ( services =>  McpServerTool . Create ( toolMethod ,  services :  services ) ) ; 
41-                 } 
42-                 else 
43-                 { 
44-                     builder . Services . AddSingleton ( services =>  McpServerTool . Create ( toolMethod ,  typeof ( TTool ) ,  services :  services ) ) ; 
45-                 } 
40+                 builder . Services . AddSingleton ( ( Func < IServiceProvider ,  McpServerTool > ) ( toolMethod . IsStatic  ? 
41+                     services =>  McpServerTool . Create ( toolMethod ,  new  McpServerToolCreateOptions ( )  {  Services  =  services  } )  : 
42+                     services =>  McpServerTool . Create ( toolMethod ,  typeof ( TTool ) ,  new ( )  {  Services  =  services  } ) ) ) ; 
4643            } 
4744        } 
4845
@@ -59,7 +56,7 @@ public static partial class McpServerBuilderExtensions
5956    /// types, where the methods are attributed as <see cref="McpServerToolAttribute"/>, and adds an <see cref="McpServerTool"/> 
6057    /// instance for each. For instance methods, an instance will be constructed for each invocation of the tool. 
6158    /// </remarks> 
62-     [ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ] 
59+     [ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ] 
6360    public  static IMcpServerBuilder  WithTools ( this  IMcpServerBuilder  builder ,  params  IEnumerable < Type > toolTypes ) 
6461    { 
6562        Throw . IfNull ( builder ) ; 
@@ -69,18 +66,13 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
6966        { 
7067            if  ( toolType  is  not null ) 
7168            { 
72-                 foreach  ( var  method  in  toolType . GetMethods ( BindingFlags . Public  |  BindingFlags . NonPublic  |  BindingFlags . Static  |  BindingFlags . Instance ) ) 
69+                 foreach  ( var  toolMethod  in  toolType . GetMethods ( BindingFlags . Public  |  BindingFlags . NonPublic  |  BindingFlags . Static  |  BindingFlags . Instance ) ) 
7370                { 
74-                     if  ( method . GetCustomAttribute < McpServerToolAttribute > ( )  is  not null ) 
71+                     if  ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( )  is  not null ) 
7572                    { 
76-                         if  ( method . IsStatic ) 
77-                         { 
78-                             builder . Services . AddSingleton ( services =>  McpServerTool . Create ( method ,  services :  services ) ) ; 
79-                         } 
80-                         else 
81-                         { 
82-                             builder . Services . AddSingleton ( services =>  McpServerTool . Create ( method ,  toolType ,  services :  services ) ) ; 
83-                         } 
73+                         builder . Services . AddSingleton ( ( Func < IServiceProvider ,  McpServerTool > ) ( toolMethod . IsStatic  ? 
74+                             services =>  McpServerTool . Create ( toolMethod ,  new  McpServerToolCreateOptions ( )  {  Services  =  services  } )  : 
75+                             services =>  McpServerTool . Create ( toolMethod ,  toolType ,  new ( )  {  Services  =  services  } ) ) ) ; 
8476                    } 
8577                } 
8678            } 
@@ -95,7 +87,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
9587    /// <param name="builder">The builder instance.</param> 
9688    /// <param name="toolAssembly">The assembly to load the types from. Null to get the current assembly</param> 
9789    /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception> 
98-     [ RequiresUnreferencedCode ( RequiresUnreferencedCodeMessage ) ] 
90+     [ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ] 
9991    public  static IMcpServerBuilder  WithToolsFromAssembly ( this  IMcpServerBuilder  builder ,  Assembly ?  toolAssembly  =  null ) 
10092    { 
10193        Throw . IfNull ( builder ) ; 
0 commit comments