@@ -48,7 +48,7 @@ public static partial class McpServerBuilderExtensions
4848 {
4949 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
5050 services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
51- services => McpServerTool . Create ( toolMethod , typeof ( TToolType ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
51+ services => McpServerTool . Create ( toolMethod , static r => CreateTarget ( r . Services , typeof ( TToolType ) ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
5252 }
5353 }
5454
@@ -105,7 +105,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
105105 {
106106 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
107107 services => McpServerTool . Create ( toolMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
108- services => McpServerTool . Create ( toolMethod , toolType , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
108+ services => McpServerTool . Create ( toolMethod , r => CreateTarget ( r . Services , toolType ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
109109 }
110110 }
111111 }
@@ -188,7 +188,7 @@ where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
188188 {
189189 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerPrompt > ) ( promptMethod . IsStatic ?
190190 services => McpServerPrompt . Create ( promptMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
191- services => McpServerPrompt . Create ( promptMethod , typeof ( TPromptType ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
191+ services => McpServerPrompt . Create ( promptMethod , static r => CreateTarget ( r . Services , typeof ( TPromptType ) ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
192192 }
193193 }
194194
@@ -245,7 +245,7 @@ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnu
245245 {
246246 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerPrompt > ) ( promptMethod . IsStatic ?
247247 services => McpServerPrompt . Create ( promptMethod , options : new ( ) { Services = services , SerializerOptions = serializerOptions } ) :
248- services => McpServerPrompt . Create ( promptMethod , promptType , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
248+ services => McpServerPrompt . Create ( promptMethod , r => CreateTarget ( r . Services , promptType ) , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ) ;
249249 }
250250 }
251251 }
@@ -325,7 +325,7 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
325325 {
326326 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerResource > ) ( resourceTemplateMethod . IsStatic ?
327327 services => McpServerResource . Create ( resourceTemplateMethod , options : new ( ) { Services = services } ) :
328- services => McpServerResource . Create ( resourceTemplateMethod , typeof ( TResourceType ) , new ( ) { Services = services } ) ) ) ;
328+ services => McpServerResource . Create ( resourceTemplateMethod , static r => CreateTarget ( r . Services , typeof ( TResourceType ) ) , new ( ) { Services = services } ) ) ) ;
329329 }
330330 }
331331
@@ -381,7 +381,7 @@ public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IE
381381 {
382382 builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerResource > ) ( resourceTemplateMethod . IsStatic ?
383383 services => McpServerResource . Create ( resourceTemplateMethod , options : new ( ) { Services = services } ) :
384- services => McpServerResource . Create ( resourceTemplateMethod , resourceTemplateType , new ( ) { Services = services } ) ) ) ;
384+ services => McpServerResource . Create ( resourceTemplateMethod , r => CreateTarget ( r . Services , resourceTemplateType ) , new ( ) { Services = services } ) ) ) ;
385385 }
386386 }
387387 }
@@ -775,4 +775,13 @@ private static void AddSingleSessionServerDependencies(IServiceCollection servic
775775 } ) ;
776776 }
777777 #endregion
778+
779+ #region Helpers
780+ /// <summary>Creates an instance of the target object.</summary>
781+ private static object CreateTarget (
782+ IServiceProvider ? services ,
783+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors ) ] Type type ) =>
784+ services is not null ? ActivatorUtilities . CreateInstance ( services , type ) :
785+ Activator . CreateInstance ( type ) ! ;
786+ #endregion
778787}
0 commit comments