@@ -48,7 +48,7 @@ public static partial class McpServerBuilderExtensions
48
48
{
49
49
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
50
50
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 } ) ) ) ;
52
52
}
53
53
}
54
54
@@ -105,7 +105,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
105
105
{
106
106
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerTool > ) ( toolMethod . IsStatic ?
107
107
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 } ) ) ) ;
109
109
}
110
110
}
111
111
}
@@ -188,7 +188,7 @@ where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
188
188
{
189
189
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerPrompt > ) ( promptMethod . IsStatic ?
190
190
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 } ) ) ) ;
192
192
}
193
193
}
194
194
@@ -245,7 +245,7 @@ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnu
245
245
{
246
246
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerPrompt > ) ( promptMethod . IsStatic ?
247
247
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 } ) ) ) ;
249
249
}
250
250
}
251
251
}
@@ -325,7 +325,7 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
325
325
{
326
326
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerResource > ) ( resourceTemplateMethod . IsStatic ?
327
327
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 } ) ) ) ;
329
329
}
330
330
}
331
331
@@ -381,7 +381,7 @@ public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IE
381
381
{
382
382
builder . Services . AddSingleton ( ( Func < IServiceProvider , McpServerResource > ) ( resourceTemplateMethod . IsStatic ?
383
383
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 } ) ) ) ;
385
385
}
386
386
}
387
387
}
@@ -775,4 +775,13 @@ private static void AddSingleSessionServerDependencies(IServiceCollection servic
775
775
} ) ;
776
776
}
777
777
#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
778
787
}
0 commit comments