@@ -53,6 +53,39 @@ public static partial class McpServerBuilderExtensions
5353 return builder ;
5454 }
5555
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+ /// This method discovers all instance methods (public and non-public) on the specified <typeparamref name="TToolType"/>
65+ /// type, where the methods are attributed as <see cref="McpServerToolAttribute"/>, and adds an <see cref="McpServerTool"/>
66+ /// instance for each, using <paramref name="target"/> as the associated instance.
67+ /// </remarks>
68+ public static IMcpServerBuilder WithTools < [ DynamicallyAccessedMembers (
69+ DynamicallyAccessedMemberTypes . PublicMethods |
70+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TToolType > (
71+ this IMcpServerBuilder builder ,
72+ TToolType target ,
73+ JsonSerializerOptions ? serializerOptions = null )
74+ {
75+ Throw . IfNull ( builder ) ;
76+ Throw . IfNull ( target ) ;
77+
78+ foreach ( var toolMethod in typeof ( TToolType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
79+ {
80+ if ( toolMethod . GetCustomAttribute < McpServerToolAttribute > ( ) is not null )
81+ {
82+ builder . Services . AddSingleton ( services => McpServerTool . Create ( toolMethod , target , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ;
83+ }
84+ }
85+
86+ return builder ;
87+ }
88+
5689 /// <summary>Adds <see cref="McpServerTool"/> instances to the service collection backing <paramref name="builder"/>.</summary>
5790 /// <param name="builder">The builder instance.</param>
5891 /// <param name="tools">The <see cref="McpServerTool"/> instances to add to the server.</param>
@@ -137,7 +170,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
137170 /// </para>
138171 /// <para>
139172 /// 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.
173+ /// Native AOT compatibility, consider using the generic <see cref="M: WithTools"/> method instead.
141174 /// </para>
142175 /// </remarks>
143176 [ RequiresUnreferencedCode ( WithToolsRequiresUnreferencedCodeMessage ) ]
@@ -193,6 +226,39 @@ where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
193226 return builder ;
194227 }
195228
229+ /// <summary>Adds <see cref="McpServerPrompt"/> instances to the service collection backing <paramref name="builder"/>.</summary>
230+ /// <typeparam name="TPromptType">The prompt type.</typeparam>
231+ /// <param name="builder">The builder instance.</param>
232+ /// <param name="target">The target instance from which the prompts should be sourced.</param>
233+ /// <param name="serializerOptions">The serializer options governing prompt parameter marshalling.</param>
234+ /// <returns>The builder provided in <paramref name="builder"/>.</returns>
235+ /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
236+ /// <remarks>
237+ /// This method discovers all instance methods (public and non-public) on the specified <typeparamref name="TPromptType"/>
238+ /// type, where the methods are attributed as <see cref="McpServerPromptAttribute"/>, and adds an <see cref="McpServerPrompt"/>
239+ /// instance for each, using <paramref name="target"/> as the associated instance.
240+ /// </remarks>
241+ public static IMcpServerBuilder WithPrompts < [ DynamicallyAccessedMembers (
242+ DynamicallyAccessedMemberTypes . PublicMethods |
243+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TPromptType > (
244+ this IMcpServerBuilder builder ,
245+ TPromptType target ,
246+ JsonSerializerOptions ? serializerOptions = null )
247+ {
248+ Throw . IfNull ( builder ) ;
249+ Throw . IfNull ( target ) ;
250+
251+ foreach ( var promptMethod in typeof ( TPromptType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
252+ {
253+ if ( promptMethod . GetCustomAttribute < McpServerPromptAttribute > ( ) is not null )
254+ {
255+ builder . Services . AddSingleton ( services => McpServerPrompt . Create ( promptMethod , target , new ( ) { Services = services , SerializerOptions = serializerOptions } ) ) ;
256+ }
257+ }
258+
259+ return builder ;
260+ }
261+
196262 /// <summary>Adds <see cref="McpServerPrompt"/> instances to the service collection backing <paramref name="builder"/>.</summary>
197263 /// <param name="builder">The builder instance.</param>
198264 /// <param name="prompts">The <see cref="McpServerPrompt"/> instances to add to the server.</param>
@@ -277,7 +343,7 @@ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnu
277343 /// </para>
278344 /// <para>
279345 /// 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.
346+ /// Native AOT compatibility, consider using the generic <see cref="M: WithPrompts"/> method instead.
281347 /// </para>
282348 /// </remarks>
283349 [ RequiresUnreferencedCode ( WithPromptsRequiresUnreferencedCodeMessage ) ]
@@ -311,7 +377,8 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
311377 /// instance for each. For instance members, an instance will be constructed for each invocation of the resource.
312378 /// </remarks>
313379 public static IMcpServerBuilder WithResources < [ DynamicallyAccessedMembers (
314- DynamicallyAccessedMemberTypes . PublicMethods | DynamicallyAccessedMemberTypes . NonPublicMethods |
380+ DynamicallyAccessedMemberTypes . PublicMethods |
381+ DynamicallyAccessedMemberTypes . NonPublicMethods |
315382 DynamicallyAccessedMemberTypes . PublicConstructors ) ] TResourceType > (
316383 this IMcpServerBuilder builder )
317384 {
@@ -330,6 +397,37 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
330397 return builder ;
331398 }
332399
400+ /// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
401+ /// <typeparam name="TResourceType">The resource type.</typeparam>
402+ /// <param name="builder">The builder instance.</param>
403+ /// <param name="target">The target instance from which the prompts should be sourced.</param>
404+ /// <returns>The builder provided in <paramref name="builder"/>.</returns>
405+ /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
406+ /// <remarks>
407+ /// This method discovers all instance methods (public and non-public) on the specified <typeparamref name="TResourceType"/>
408+ /// type, where the methods are attributed as <see cref="McpServerResource"/>, and adds an <see cref="McpServerResource"/>
409+ /// instance for each, using <paramref name="target"/> as the associated instance.
410+ /// </remarks>
411+ public static IMcpServerBuilder WithResources < [ DynamicallyAccessedMembers (
412+ DynamicallyAccessedMemberTypes . PublicMethods |
413+ DynamicallyAccessedMemberTypes . NonPublicMethods ) ] TResourceType > (
414+ this IMcpServerBuilder builder ,
415+ TResourceType target )
416+ {
417+ Throw . IfNull ( builder ) ;
418+ Throw . IfNull ( target ) ;
419+
420+ foreach ( var resourceTemplateMethod in typeof ( TResourceType ) . GetMethods ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static | BindingFlags . Instance ) )
421+ {
422+ if ( resourceTemplateMethod . GetCustomAttribute < McpServerResourceAttribute > ( ) is not null )
423+ {
424+ builder . Services . AddSingleton ( services => McpServerResource . Create ( resourceTemplateMethod , target , new ( ) { Services = services } ) ) ;
425+ }
426+ }
427+
428+ return builder ;
429+ }
430+
333431 /// <summary>Adds <see cref="McpServerResource"/> instances to the service collection backing <paramref name="builder"/>.</summary>
334432 /// <param name="builder">The builder instance.</param>
335433 /// <param name="resourceTemplates">The <see cref="McpServerResource"/> instances to add to the server.</param>
@@ -412,7 +510,7 @@ public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IE
412510 /// </para>
413511 /// <para>
414512 /// 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.
513+ /// Native AOT compatibility, consider using the generic <see cref="M: WithResources"/> method instead.
416514 /// </para>
417515 /// </remarks>
418516 [ RequiresUnreferencedCode ( WithResourcesRequiresUnreferencedCodeMessage ) ]
0 commit comments