@@ -53,6 +53,53 @@ public static partial class McpServerBuilderExtensions
53
53
return builder ;
54
54
}
55
55
56
+ /// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
57
+ /// <typeparam name="TToolType">The tool type.</typeparam>
58
+ /// <param name="builder">The builder instance.</param>
59
+ /// <param name="target">The target instance from which the tools should be sourced.</param>
60
+ /// <param name="serializerOptions">The serializer options governing tool parameter marshalling.</param>
61
+ /// <returns>The builder provided in <paramref name="builder"/>.</returns>
62
+ /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
63
+ /// <remarks>
64
+ /// <para>
65
+ /// This method discovers all methods (public and non-public) on the specified <typeparamref name="TToolType"/>
66
+ /// type, where the methods are attributed as <see cref="McpServerToolAttribute"/>, and adds an <see cref="McpServerTool"/>
67
+ /// instance for each, using <paramref name="target"/> as the associated instance for instance methods.
68
+ /// </para>
69
+ /// <para>
70
+ /// However, if <typeparamref name="TToolType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerTool"/>,
71
+ /// this method will register those tools directly without scanning for methods on <typeparamref name="TToolType"/>.
72
+ /// </para>
73
+ /// </remarks>
74
+ public static IMcpServerBuilder WithTools < [ DynamicallyAccessedMembers (
75
+ DynamicallyAccessedMemberTypes . PublicMethods |
76
+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TToolType > (
77
+ this IMcpServerBuilder builder ,
78
+ TToolType target ,
79
+ JsonSerializerOptions ? serializerOptions = null )
80
+ {
81
+ Throw . IfNull ( builder ) ;
82
+ Throw . IfNull ( target ) ;
83
+
84
+ if ( target is IEnumerable < McpServerTool > tools )
85
+ {
86
+ return builder . WithTools ( tools ) ;
87
+ }
88
+
89
+ foreach ( var toolMethod in typeof ( TToolType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
90
+ {
91
+ if ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( ) is not null )
92
+ {
93
+ builder . Services . AddSingleton ( services => McpServerTool . Create (
94
+ toolMethod ,
95
+ toolMethod . IsStatic ? null : target ,
96
+ new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ;
97
+ }
98
+ }
99
+
100
+ return builder ;
101
+ }
102
+
56
103
/// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
57
104
/// <param name="builder">The builder instance.</param>
58
105
/// <param name="tools">The <see cref="McpServerTool"/> instances to add to the server.</param>
@@ -137,7 +184,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
137
184
/// </para>
138
185
/// <para>
139
186
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
140
- /// Native AOT compatibility, consider using the generic <see cref="WithTools{TToolType} "/> method instead.
187
+ /// Native AOT compatibility, consider using the generic <see cref="M: WithTools"/> method instead.
141
188
/// </para>
142
189
/// </remarks>
143
190
[ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ]
@@ -193,6 +240,50 @@ where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
193
240
return builder ;
194
241
}
195
242
243
+ /// <summary>Adds <see cref="McpServerPrompt"/> instances to the service collection backing <paramref name="builder"/>.</summary>
244
+ /// <typeparam name="TPromptType">The prompt type.</typeparam>
245
+ /// <param name="builder">The builder instance.</param>
246
+ /// <param name="target">The target instance from which the prompts should be sourced.</param>
247
+ /// <param name="serializerOptions">The serializer options governing prompt parameter marshalling.</param>
248
+ /// <returns>The builder provided in <paramref name="builder"/>.</returns>
249
+ /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
250
+ /// <remarks>
251
+ /// <para>
252
+ /// This method discovers all methods (public and non-public) on the specified <typeparamref name="TPromptType"/>
253
+ /// type, where the methods are attributed as <see cref="McpServerPromptAttribute"/>, and adds an <see cref="McpServerPrompt"/>
254
+ /// instance for each, using <paramref name="target"/> as the associated instance for instance methods.
255
+ /// </para>
256
+ /// <para>
257
+ /// However, if <typeparamref name="TPromptType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerPrompt"/>,
258
+ /// this method will register those prompts directly without scanning for methods on <typeparamref name="TPromptType"/>.
259
+ /// </para>
260
+ /// </remarks>
261
+ public static IMcpServerBuilder WithPrompts < [ DynamicallyAccessedMembers (
262
+ DynamicallyAccessedMemberTypes . PublicMethods |
263
+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TPromptType > (
264
+ this IMcpServerBuilder builder ,
265
+ TPromptType target ,
266
+ JsonSerializerOptions ? serializerOptions = null )
267
+ {
268
+ Throw . IfNull ( builder ) ;
269
+ Throw . IfNull ( target ) ;
270
+
271
+ if ( target is IEnumerable < McpServerPrompt > prompts )
272
+ {
273
+ return builder . WithPrompts ( prompts ) ;
274
+ }
275
+
276
+ foreach ( var promptMethod in typeof ( TPromptType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
277
+ {
278
+ if ( promptMethod . GetCustomAttribute < McpServerPromptAttribute > ( ) is not null )
279
+ {
280
+ builder . Services . AddSingleton ( services => McpServerPrompt . Create ( promptMethod , target , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ;
281
+ }
282
+ }
283
+
284
+ return builder ;
285
+ }
286
+
196
287
/// <summary>Adds <see cref="McpServerPrompt"/> instances to the service collection backing <paramref name="builder"/>.</summary>
197
288
/// <param name="builder">The builder instance.</param>
198
289
/// <param name="prompts">The <see cref="McpServerPrompt"/> instances to add to the server.</param>
@@ -277,7 +368,7 @@ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnu
277
368
/// </para>
278
369
/// <para>
279
370
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
280
- /// Native AOT compatibility, consider using the generic <see cref="WithPrompts{TPromptType} "/> method instead.
371
+ /// Native AOT compatibility, consider using the generic <see cref="M: WithPrompts"/> method instead.
281
372
/// </para>
282
373
/// </remarks>
283
374
[ RequiresUnreferencedCode ( WithPromptsRequiresUnreferencedCodeMessage ) ]
@@ -311,7 +402,8 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
311
402
/// instance for each. For instance members, an instance will be constructed for each invocation of the resource.
312
403
/// </remarks>
313
404
public static IMcpServerBuilder WithResources < [ DynamicallyAccessedMembers (
314
- DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods |
405
+ DynamicallyAccessedMemberTypes . PublicMethods |
406
+ DynamicallyAccessedMemberTypes . NonPublicMethods |
315
407
DynamicallyAccessedMemberTypes . PublicConstructors ) ] TResourceType > (
316
408
this IMcpServerBuilder builder )
317
409
{
@@ -330,6 +422,48 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
330
422
return builder ;
331
423
}
332
424
425
+ /// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
426
+ /// <typeparam name="TResourceType">The resource type.</typeparam>
427
+ /// <param name="builder">The builder instance.</param>
428
+ /// <param name="target">The target instance from which the prompts should be sourced.</param>
429
+ /// <returns>The builder provided in <paramref name="builder"/>.</returns>
430
+ /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
431
+ /// <remarks>
432
+ /// <para>
433
+ /// This method discovers all methods (public and non-public) on the specified <typeparamref name="TResourceType"/>
434
+ /// type, where the methods are attributed as <see cref="McpServerResourceAttribute"/>, and adds an <see cref="McpServerResource"/>
435
+ /// instance for each, using <paramref name="target"/> as the associated instance for instance methods.
436
+ /// </para>
437
+ /// <para>
438
+ /// However, if <typeparamref name="TResourceType"/> is itself an <see cref="IEnumerable{T}"/> of <see cref="McpServerResource"/>,
439
+ /// this method will register those resources directly without scanning for methods on <typeparamref name="TResourceType"/>.
440
+ /// </para>
441
+ /// </remarks>
442
+ public static IMcpServerBuilder WithResources < [ DynamicallyAccessedMembers (
443
+ DynamicallyAccessedMemberTypes . PublicMethods |
444
+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TResourceType > (
445
+ this IMcpServerBuilder builder ,
446
+ TResourceType target )
447
+ {
448
+ Throw . IfNull ( builder ) ;
449
+ Throw . IfNull ( target ) ;
450
+
451
+ if ( target is IEnumerable < McpServerResource > resources )
452
+ {
453
+ return builder . WithResources ( resources ) ;
454
+ }
455
+
456
+ foreach ( var resourceTemplateMethod in typeof ( TResourceType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
457
+ {
458
+ if ( resourceTemplateMethod . GetCustomAttribute < McpServerResourceAttribute > ( ) is not null )
459
+ {
460
+ builder . Services . AddSingleton ( services => McpServerResource . Create ( resourceTemplateMethod , target , new ( ) { Services = services } ) ) ;
461
+ }
462
+ }
463
+
464
+ return builder ;
465
+ }
466
+
333
467
/// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
334
468
/// <param name="builder">The builder instance.</param>
335
469
/// <param name="resourceTemplates">The <see cref="McpServerResource"/> instances to add to the server.</param>
@@ -412,7 +546,7 @@ public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IE
412
546
/// </para>
413
547
/// <para>
414
548
/// Note that this method performs reflection at runtime and may not work in Native AOT scenarios. For
415
- /// Native AOT compatibility, consider using the generic <see cref="WithResources{TResourceType} "/> method instead.
549
+ /// Native AOT compatibility, consider using the generic <see cref="M: WithResources"/> method instead.
416
550
/// </para>
417
551
/// </remarks>
418
552
[ RequiresUnreferencedCode ( WithResourcesRequiresUnreferencedCodeMessage ) ]
0 commit comments