Skip to content

Commit ca3a8ab

Browse files
authored
Update M.E.AI.Abstractions to 9.5.0-preview.1.25262.9 (#412)
1 parent 4043538 commit ca3a8ab

11 files changed

+68
-58
lines changed

Directory.Packages.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<System10Version>10.0.0-preview.3.25171.5</System10Version>
5-
<MicrosoftExtensionsAIVersion>9.4.4-preview.1.25259.16</MicrosoftExtensionsAIVersion>
5+
<MicrosoftExtensionsAIVersion>9.5.0-preview.1.25262.9</MicrosoftExtensionsAIVersion>
66
</PropertyGroup>
77

88
<!-- Product dependencies netstandard -->
@@ -31,7 +31,7 @@
3131

3232
<!-- Product dependencies shared -->
3333
<ItemGroup>
34-
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="9.4.4-preview.1.25259.16" />
34+
<PackageVersion Include="Microsoft.Extensions.AI.Abstractions" Version="$(MicrosoftExtensionsAIVersion)" />
3535
<PackageVersion Include="Microsoft.Extensions.AI" Version="$(MicrosoftExtensionsAIVersion)" />
3636
<PackageVersion Include="System.Net.ServerSentEvents" Version="$(System10Version)" />
3737
</ItemGroup>

src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static partial class McpServerBuilderExtensions
4848
{
4949
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
5050
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 })));
5252
}
5353
}
5454

@@ -105,7 +105,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, IEnume
105105
{
106106
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
107107
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 })));
109109
}
110110
}
111111
}
@@ -188,7 +188,7 @@ where t.GetCustomAttribute<McpServerToolTypeAttribute>() is not null
188188
{
189189
builder.Services.AddSingleton((Func<IServiceProvider, McpServerPrompt>)(promptMethod.IsStatic ?
190190
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 })));
192192
}
193193
}
194194

@@ -245,7 +245,7 @@ public static IMcpServerBuilder WithPrompts(this IMcpServerBuilder builder, IEnu
245245
{
246246
builder.Services.AddSingleton((Func<IServiceProvider, McpServerPrompt>)(promptMethod.IsStatic ?
247247
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 })));
249249
}
250250
}
251251
}
@@ -325,7 +325,7 @@ where t.GetCustomAttribute<McpServerPromptTypeAttribute>() is not null
325325
{
326326
builder.Services.AddSingleton((Func<IServiceProvider, McpServerResource>)(resourceTemplateMethod.IsStatic ?
327327
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 })));
329329
}
330330
}
331331

@@ -381,7 +381,7 @@ public static IMcpServerBuilder WithResources(this IMcpServerBuilder builder, IE
381381
{
382382
builder.Services.AddSingleton((Func<IServiceProvider, McpServerResource>)(resourceTemplateMethod.IsStatic ?
383383
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 })));
385385
}
386386
}
387387
}
@@ -775,4 +775,13 @@ private static void AddSingleSessionServerDependencies(IServiceCollection servic
775775
});
776776
}
777777
#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
778787
}

src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using ModelContextProtocol.Utils;
55
using ModelContextProtocol.Utils.Json;
66
using System.ComponentModel;
7-
using System.Diagnostics.CodeAnalysis;
87
using System.Reflection;
98
using System.Text.Json;
109

@@ -49,15 +48,20 @@ internal sealed class AIFunctionMcpServerPrompt : McpServerPrompt
4948
/// </summary>
5049
public static new AIFunctionMcpServerPrompt Create(
5150
MethodInfo method,
52-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
51+
Func<RequestContext<GetPromptRequestParams>, object> createTargetFunc,
5352
McpServerPromptCreateOptions? options)
5453
{
5554
Throw.IfNull(method);
55+
Throw.IfNull(createTargetFunc);
5656

5757
options = DeriveOptions(method, options);
5858

5959
return Create(
60-
AIFunctionFactory.Create(method, targetType, CreateAIFunctionFactoryOptions(method, options)),
60+
AIFunctionFactory.Create(method, args =>
61+
{
62+
var request = (RequestContext<GetPromptRequestParams>)args.Context![typeof(RequestContext<GetPromptRequestParams>)]!;
63+
return createTargetFunc(request);
64+
}, CreateAIFunctionFactoryOptions(method, options)),
6165
options);
6266
}
6367

@@ -69,7 +73,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
6973
Description = options?.Description,
7074
MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
7175
SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions,
72-
CreateInstance = AIFunctionMcpServerTool.GetCreateInstanceFunc(),
7376
ConfigureParameterBinding = pi =>
7477
{
7578
if (pi.ParameterType == typeof(RequestContext<GetPromptRequestParams>))

src/ModelContextProtocol/Server/AIFunctionMcpServerResource.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using ModelContextProtocol.Utils.Json;
66
using System.Collections.Concurrent;
77
using System.ComponentModel;
8-
using System.Diagnostics.CodeAnalysis;
98
using System.Globalization;
109
using System.Reflection;
1110
using System.Text;
@@ -56,15 +55,20 @@ internal sealed class AIFunctionMcpServerResource : McpServerResource
5655
/// </summary>
5756
public static new AIFunctionMcpServerResource Create(
5857
MethodInfo method,
59-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
58+
Func<RequestContext<ReadResourceRequestParams>, object> createTargetFunc,
6059
McpServerResourceCreateOptions? options)
6160
{
6261
Throw.IfNull(method);
62+
Throw.IfNull(createTargetFunc);
6363

6464
options = DeriveOptions(method, options);
6565

6666
return Create(
67-
AIFunctionFactory.Create(method, targetType, CreateAIFunctionFactoryOptions(method, options)),
67+
AIFunctionFactory.Create(method, args =>
68+
{
69+
var request = (RequestContext<ReadResourceRequestParams>)args.Context![typeof(RequestContext<ReadResourceRequestParams>)]!;
70+
return createTargetFunc(request);
71+
}, CreateAIFunctionFactoryOptions(method, options)),
6872
options);
6973
}
7074

@@ -76,7 +80,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
7680
Description = options?.Description,
7781
MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
7882
SerializerOptions = McpJsonUtilities.DefaultOptions,
79-
CreateInstance = AIFunctionMcpServerTool.GetCreateInstanceFunc(),
8083
ConfigureParameterBinding = pi =>
8184
{
8285
if (pi.ParameterType == typeof(RequestContext<ReadResourceRequestParams>))

src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ internal sealed class AIFunctionMcpServerTool : McpServerTool
4949
/// </summary>
5050
public static new AIFunctionMcpServerTool Create(
5151
MethodInfo method,
52-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
52+
Func<RequestContext<CallToolRequestParams>, object> createTargetFunc,
5353
McpServerToolCreateOptions? options)
5454
{
5555
Throw.IfNull(method);
56+
Throw.IfNull(createTargetFunc);
5657

5758
options = DeriveOptions(method, options);
5859

5960
return Create(
60-
AIFunctionFactory.Create(method, targetType, CreateAIFunctionFactoryOptions(method, options)),
61+
AIFunctionFactory.Create(method, args =>
62+
{
63+
var request = (RequestContext<CallToolRequestParams>)args.Context![typeof(RequestContext<CallToolRequestParams>)]!;
64+
return createTargetFunc(request);
65+
}, CreateAIFunctionFactoryOptions(method, options)),
6166
options);
6267
}
6368

@@ -77,7 +82,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions(
7782
Description = options?.Description,
7883
MarshalResult = static (result, _, cancellationToken) => new ValueTask<object?>(result),
7984
SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions,
80-
CreateInstance = GetCreateInstanceFunc(),
8185
ConfigureParameterBinding = pi =>
8286
{
8387
if (pi.ParameterType == typeof(RequestContext<CallToolRequestParams>))

src/ModelContextProtocol/Server/McpServerPrompt.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using ModelContextProtocol.Client;
44
using ModelContextProtocol.Protocol.Messages;
55
using ModelContextProtocol.Protocol.Types;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.Reflection;
87
using System.Text.Json;
98

@@ -184,21 +183,19 @@ public static McpServerPrompt Create(
184183
/// instantiate each time the method is invoked.
185184
/// </summary>
186185
/// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param>
187-
/// <param name="targetType">
188-
/// The <see cref="Type"/> to construct an instance of on which to invoke <paramref name="method"/> when
189-
/// the resulting <see cref="AIFunction"/> is invoked. If services are provided,
190-
/// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise,
191-
/// <see cref="Activator.CreateInstance(Type)"/> is used, utilizing the type's public parameterless constructor.
192-
/// If an instance can't be constructed, an exception is thrown during the function's invocation.
186+
/// <param name="createTargetFunc">
187+
/// Callback used on each function invocation to create an instance of the type on which the instance method <paramref name="method"/>
188+
/// will be invoked. If the returned instance is <see cref="IAsyncDisposable"/> or <see cref="IDisposable"/>, it will
189+
/// be disposed of after method completes its invocation.
193190
/// </param>
194191
/// <param name="options">Optional options used in the creation of the <see cref="McpServerPrompt"/> to control its behavior.</param>
195192
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
196193
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
197194
public static McpServerPrompt Create(
198195
MethodInfo method,
199-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
196+
Func<RequestContext<GetPromptRequestParams>, object> createTargetFunc,
200197
McpServerPromptCreateOptions? options = null) =>
201-
AIFunctionMcpServerPrompt.Create(method, targetType, options);
198+
AIFunctionMcpServerPrompt.Create(method, createTargetFunc, options);
202199

203200
/// <summary>Creates an <see cref="McpServerPrompt"/> that wraps the specified <see cref="AIFunction"/>.</summary>
204201
/// <param name="function">The function to wrap.</param>

src/ModelContextProtocol/Server/McpServerResource.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using ModelContextProtocol.Protocol.Messages;
44
using ModelContextProtocol.Protocol.Types;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.Reflection;
76

87
namespace ModelContextProtocol.Server;
@@ -205,21 +204,19 @@ public static McpServerResource Create(
205204
/// instantiate each time the method is invoked.
206205
/// </summary>
207206
/// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param>
208-
/// <param name="targetType">
209-
/// The <see cref="Type"/> to construct an instance of on which to invoke <paramref name="method"/> when
210-
/// the resulting <see cref="AIFunction"/> is invoked. If services are provided,
211-
/// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise,
212-
/// <see cref="Activator.CreateInstance(Type)"/> is used, utilizing the type's public parameterless constructor.
213-
/// If an instance can't be constructed, an exception is thrown during the function's invocation.
207+
/// <param name="createTargetFunc">
208+
/// Callback used on each function invocation to create an instance of the type on which the instance method <paramref name="method"/>
209+
/// will be invoked. If the returned instance is <see cref="IAsyncDisposable"/> or <see cref="IDisposable"/>, it will
210+
/// be disposed of after method completes its invocation.
214211
/// </param>
215212
/// <param name="options">Optional options used in the creation of the <see cref="McpServerResource"/> to control its behavior.</param>
216213
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
217214
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
218215
public static McpServerResource Create(
219216
MethodInfo method,
220-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
217+
Func<RequestContext<ReadResourceRequestParams>, object> createTargetFunc,
221218
McpServerResourceCreateOptions? options = null) =>
222-
AIFunctionMcpServerResource.Create(method, targetType, options);
219+
AIFunctionMcpServerResource.Create(method, createTargetFunc, options);
223220

224221
/// <summary>Creates an <see cref="McpServerResource"/> that wraps the specified <see cref="AIFunction"/>.</summary>
225222
/// <param name="function">The function to wrap.</param>

src/ModelContextProtocol/Server/McpServerTool.cs

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using ModelContextProtocol.Protocol.Messages;
55
using ModelContextProtocol.Protocol.Types;
66
using ModelContextProtocol.Utils.Json;
7-
using System.Diagnostics.CodeAnalysis;
87
using System.Reflection;
98
using System.Text.Json;
109

@@ -187,21 +186,19 @@ public static McpServerTool Create(
187186
/// instantiate each time the method is invoked.
188187
/// </summary>
189188
/// <param name="method">The instance method to be represented via the created <see cref="AIFunction"/>.</param>
190-
/// <param name="targetType">
191-
/// The <see cref="Type"/> to construct an instance of on which to invoke <paramref name="method"/> when
192-
/// the resulting <see cref="AIFunction"/> is invoked. If services are provided,
193-
/// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise,
194-
/// <see cref="Activator.CreateInstance(Type)"/> is used, utilizing the type's public parameterless constructor.
195-
/// If an instance can't be constructed, an exception is thrown during the function's invocation.
189+
/// <param name="createTargetFunc">
190+
/// Callback used on each function invocation to create an instance of the type on which the instance method <paramref name="method"/>
191+
/// will be invoked. If the returned instance is <see cref="IAsyncDisposable"/> or <see cref="IDisposable"/>, it will
192+
/// be disposed of after method completes its invocation.
196193
/// </param>
197194
/// <param name="options">Optional options used in the creation of the <see cref="McpServerTool"/> to control its behavior.</param>
198195
/// <returns>The created <see cref="AIFunction"/> for invoking <paramref name="method"/>.</returns>
199196
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
200197
public static McpServerTool Create(
201198
MethodInfo method,
202-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
199+
Func<RequestContext<CallToolRequestParams>, object> createTargetFunc,
203200
McpServerToolCreateOptions? options = null) =>
204-
AIFunctionMcpServerTool.Create(method, targetType, options);
201+
AIFunctionMcpServerTool.Create(method, createTargetFunc, options);
205202

206203
/// <summary>Creates an <see cref="McpServerTool"/> that wraps the specified <see cref="AIFunction"/>.</summary>
207204
/// <param name="function">The function to wrap.</param>

0 commit comments

Comments
 (0)