1
1
using Microsoft . Extensions . AI ;
2
2
using ModelContextProtocol . Protocol . Types ;
3
3
using System . ComponentModel ;
4
+ using System . Diagnostics . CodeAnalysis ;
4
5
using System . Reflection ;
5
6
6
7
namespace ModelContextProtocol . Server ;
@@ -40,7 +41,7 @@ public abstract Task<CallToolResponse> InvokeAsync(
40
41
/// </param>
41
42
/// <param name="services">
42
43
/// 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
44
45
/// are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
45
46
/// </param>
46
47
/// <returns>The created <see cref="McpServerTool"/> for invoking <paramref name="method"/>.</returns>
@@ -68,7 +69,7 @@ public static McpServerTool Create(
68
69
/// </param>
69
70
/// <param name="services">
70
71
/// 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
72
73
/// are satisfied via this provider should match what's satisfied via the provider passed in at invocation time.
73
74
/// </param>
74
75
/// <returns>The created <see cref="McpServerTool"/> for invoking <paramref name="method"/>.</returns>
@@ -82,6 +83,43 @@ public static McpServerTool Create(
82
83
IServiceProvider ? services = null ) =>
83
84
AIFunctionMcpServerTool . Create ( method , target , name , description , services ) ;
84
85
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
+
85
123
/// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
86
124
/// <param name="function">The function to wrap.</param>
87
125
/// <exception cref="ArgumentNullException"><paramref name="function"/> is <see langword="null"/>.</exception>
0 commit comments