Skip to content

Commit 0294c57

Browse files
authored
Fix binding to wrong overload in WithTools (#152)
1 parent 3c0679e commit 0294c57

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/ModelContextProtocol/Configuration/McpServerBuilderExtensions.Tools.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static partial class McpServerBuilderExtensions
3838
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
3939
{
4040
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
41-
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
41+
services => McpServerTool.Create(toolMethod, options: new() { Services = services }) :
4242
services => McpServerTool.Create(toolMethod, typeof(TTool), new() { Services = services })));
4343
}
4444
}
@@ -71,7 +71,7 @@ public static IMcpServerBuilder WithTools(this IMcpServerBuilder builder, params
7171
if (toolMethod.GetCustomAttribute<McpServerToolAttribute>() is not null)
7272
{
7373
builder.Services.AddSingleton((Func<IServiceProvider, McpServerTool>)(toolMethod.IsStatic ?
74-
services => McpServerTool.Create(toolMethod, new McpServerToolCreateOptions() { Services = services }) :
74+
services => McpServerTool.Create(toolMethod, options: new() { Services = services }) :
7575
services => McpServerTool.Create(toolMethod, toolType, new() { Services = services })));
7676
}
7777
}

tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs

+48
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,54 @@ public void Register_Tools_From_Current_Assembly()
395395
Assert.Contains(services.GetServices<McpServerTool>(), t => t.ProtocolTool.Name == "Echo");
396396
}
397397

398+
[Theory]
399+
[InlineData(false)]
400+
[InlineData(true)]
401+
public void WithTools_Parameters_Satisfiable_From_DI(bool parameterInServices)
402+
{
403+
ServiceCollection sc = new();
404+
if (parameterInServices)
405+
{
406+
sc.AddSingleton(new ComplexObject());
407+
}
408+
sc.AddMcpServer().WithTools(typeof(EchoTool));
409+
IServiceProvider services = sc.BuildServiceProvider();
410+
411+
McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "EchoComplex");
412+
if (parameterInServices)
413+
{
414+
Assert.DoesNotContain("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
415+
}
416+
else
417+
{
418+
Assert.Contains("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
419+
}
420+
}
421+
422+
[Theory]
423+
[InlineData(false)]
424+
[InlineData(true)]
425+
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(bool parameterInServices)
426+
{
427+
ServiceCollection sc = new();
428+
if (parameterInServices)
429+
{
430+
sc.AddSingleton(new ComplexObject());
431+
}
432+
sc.AddMcpServer().WithToolsFromAssembly();
433+
IServiceProvider services = sc.BuildServiceProvider();
434+
435+
McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "EchoComplex");
436+
if (parameterInServices)
437+
{
438+
Assert.DoesNotContain("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
439+
}
440+
else
441+
{
442+
Assert.Contains("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
443+
}
444+
}
445+
398446
[Fact]
399447
public async Task Recognizes_Parameter_Types()
400448
{

0 commit comments

Comments
 (0)