diff --git a/Directory.Packages.props b/Directory.Packages.props index e26d68cd..00234fa3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,8 +1,8 @@ true - 10.0.0-preview.2.25163.2 - 9.4.0-preview.1.25207.5 + 10.0.0-preview.3.25171.5 + 9.4.3-preview.1.25230.7 diff --git a/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs b/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs index 40403803..bf63e433 100644 --- a/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs +++ b/src/ModelContextProtocol/Server/AIFunctionMcpServerPrompt.cs @@ -68,6 +68,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( Description = options?.Description, MarshalResult = static (result, _, cancellationToken) => new ValueTask(result), SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions, + Services = options?.Services, ConfigureParameterBinding = pi => { if (pi.ParameterType == typeof(RequestContext)) @@ -88,35 +89,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( }; } - // We assume that if the services used to create the prompt support a particular type, - // so too do the services associated with the server. This is the same basic assumption - // made in ASP.NET. - if (options?.Services is { } services && - services.GetService() is { } ispis && - ispis.IsService(pi.ParameterType)) - { - return new() - { - ExcludeFromSchema = true, - BindParameter = (pi, args) => - GetRequestContext(args)?.Services?.GetService(pi.ParameterType) ?? - (pi.HasDefaultValue ? null : - throw new InvalidOperationException("No service of the requested type was found.")), - }; - } - - if (pi.GetCustomAttribute() is { } keyedAttr) - { - return new() - { - ExcludeFromSchema = true, - BindParameter = (pi, args) => - (GetRequestContext(args)?.Services as IKeyedServiceProvider)?.GetKeyedService(pi.ParameterType, keyedAttr.Key) ?? - (pi.HasDefaultValue ? null : - throw new InvalidOperationException("No service of the requested type was found.")), - }; - } - return default; static RequestContext? GetRequestContext(AIFunctionArguments args) diff --git a/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs b/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs index 658075c4..06e0eed0 100644 --- a/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs +++ b/src/ModelContextProtocol/Server/AIFunctionMcpServerTool.cs @@ -68,6 +68,7 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( Description = options?.Description, MarshalResult = static (result, _, cancellationToken) => new ValueTask(result), SerializerOptions = options?.SerializerOptions ?? McpJsonUtilities.DefaultOptions, + Services = options?.Services, ConfigureParameterBinding = pi => { if (pi.ParameterType == typeof(RequestContext)) @@ -109,35 +110,6 @@ private static AIFunctionFactoryOptions CreateAIFunctionFactoryOptions( }; } - // We assume that if the services used to create the tool support a particular type, - // so too do the services associated with the server. This is the same basic assumption - // made in ASP.NET. - if (options?.Services is { } services && - services.GetService() is { } ispis && - ispis.IsService(pi.ParameterType)) - { - return new() - { - ExcludeFromSchema = true, - BindParameter = (pi, args) => - GetRequestContext(args)?.Services?.GetService(pi.ParameterType) ?? - (pi.HasDefaultValue ? null : - throw new InvalidOperationException("No service of the requested type was found.")), - }; - } - - if (pi.GetCustomAttribute() is { } keyedAttr) - { - return new() - { - ExcludeFromSchema = true, - BindParameter = (pi, args) => - (GetRequestContext(args)?.Services as IKeyedServiceProvider)?.GetKeyedService(pi.ParameterType, keyedAttr.Key) ?? - (pi.HasDefaultValue ? null : - throw new InvalidOperationException("No service of the requested type was found.")), - }; - } - return default; static RequestContext? GetRequestContext(AIFunctionArguments args) diff --git a/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs b/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs index 3804cfbf..3f56c4a5 100644 --- a/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs +++ b/tests/ModelContextProtocol.Tests/Server/McpServerPromptTests.cs @@ -61,7 +61,7 @@ public async Task SupportsServiceFromDI() Assert.Contains("something", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []); Assert.DoesNotContain("actualMyService", prompt.ProtocolPrompt.Arguments?.Select(a => a.Name) ?? []); - await Assert.ThrowsAsync(async () => await prompt.GetAsync( + await Assert.ThrowsAsync(async () => await prompt.GetAsync( new RequestContext(new Mock().Object), TestContext.Current.CancellationToken));