11using Microsoft . Extensions . AI ;
22using ModelContextProtocol . Protocol . Types ;
33using System . ComponentModel ;
4+ using System . Diagnostics . CodeAnalysis ;
45using System . Reflection ;
56
67namespace ModelContextProtocol . Server ;
@@ -40,7 +41,7 @@ public abstract Task<CallToolResponse> InvokeAsync(
4041 /// </param>
4142 /// <param name="services">
4243 /// Optional services used in the construction of the <see cref="McpServerTool"/>. These services will be
43- /// used to determine which parameters should be satisifed from dependency injection, and so what services
44+ /// used to determine which parameters should be satisifed from dependency injection; what services
4445 /// are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
4546 /// </param>
4647 /// <returns>The created <see cref="McpServerTool"/> for invoking <paramref name="method"/>.</returns>
@@ -68,7 +69,7 @@ public static McpServerTool Create(
6869 /// </param>
6970 /// <param name="services">
7071 /// Optional services used in the construction of the <see cref="McpServerTool"/>. These services will be
71- /// used to determine which parameters should be satisifed from dependency injection, and so what services
72+ /// used to determine which parameters should be satisifed from dependency injection; what services
7273 /// are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
7374 /// </param>
7475 /// <returns>The created <see cref="McpServerTool"/> for invoking <paramref name="method"/>.</returns>
@@ -82,6 +83,43 @@ public static McpServerTool Create(
8283 IServiceProvider ? services = null ) =>
8384 AIFunctionMcpServerTool . Create ( method , target , name , description , services ) ;
8485
86+ /// <summary>
87+ /// Creates an <see cref="McpServerTool"/> instance for a method, specified via an <see cref="MethodInfo"/> for
88+ /// and instance method, along with a <see cref="Type"/> representing the type of the target object to
89+ /// instantiate each time the method is invoked.
90+ /// </summary>
91+ /// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param>
92+ /// <param name="targetType">
93+ /// The <see cref="Type"/> to construct an instance of on which to invoke <paramref name="method"/> when
94+ /// the resulting <see cref="AIFunction"/> is invoked. If services are provided,
95+ /// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise,
96+ /// <see cref="Activator.CreateInstance(Type)"/> is used, utilizing the type's public parameterless constructor.
97+ /// If an instance can't be constructed, an exception is thrown during the function's invocation.
98+ /// </param>
99+ /// <param name="name">
100+ /// The name to use for the <see cref="McpServerTool"/>. If <see langword="null"/>, but an <see cref="McpServerToolAttribute"/>
101+ /// is applied to <paramref name="method"/>, the name from the attribute will be used. If that's not present, the name based
102+ /// on <paramref name="method"/>'s name will be used.
103+ /// </param>
104+ /// <param name="description">
105+ /// The description to use for the <see cref="McpServerTool"/>. If <see langword="null"/>, but a <see cref="DescriptionAttribute"/>
106+ /// is applied to <paramref name="method"/>, the description from that attribute will be used.
107+ /// </param>
108+ /// <param name="services">
109+ /// Optional services used in the construction of the <see cref="McpServerTool"/>. These services will be
110+ /// used to determine which parameters should be satisifed from dependency injection; what services
111+ /// are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
112+ /// </param>
113+ /// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
114+ /// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
115+ public static McpServerTool Create (
116+ MethodInfo method ,
117+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicConstructors ) ] Type targetType ,
118+ string ? name = null ,
119+ string ? description = null ,
120+ IServiceProvider ? services = null ) =>
121+ AIFunctionMcpServerTool . Create ( method , targetType , name , description , services ) ;
122+
85123 /// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
86124 /// <param name="function">The function to wrap.</param>
87125 /// <exception cref="ArgumentNullException"><paramref name="function"/> is <see langword="null"/>.</exception>
0 commit comments