Skip to content

Commit a04cf6a

Browse files
authored
Expand a few DI-related tests (#201)
1 parent 4ecb6bc commit a04cf6a

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

tests/ModelContextProtocol.Tests/Configuration/McpServerBuilderExtensionsToolsTests.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,20 +508,33 @@ public void WithTools_Parameters_Satisfiable_From_DI(bool parameterInServices)
508508
}
509509

510510
[Theory]
511-
[InlineData(false)]
512-
[InlineData(true)]
513-
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(bool parameterInServices)
511+
[InlineData(ServiceLifetime.Singleton)]
512+
[InlineData(ServiceLifetime.Scoped)]
513+
[InlineData(ServiceLifetime.Transient)]
514+
[InlineData(null)]
515+
public void WithToolsFromAssembly_Parameters_Satisfiable_From_DI(ServiceLifetime? lifetime)
514516
{
515517
ServiceCollection sc = new();
516-
if (parameterInServices)
518+
switch (lifetime)
517519
{
518-
sc.AddSingleton(new ComplexObject());
520+
case ServiceLifetime.Singleton:
521+
sc.AddSingleton(new ComplexObject());
522+
break;
523+
524+
case ServiceLifetime.Scoped:
525+
sc.AddScoped(_ => new ComplexObject());
526+
break;
527+
528+
case ServiceLifetime.Transient:
529+
sc.AddTransient(_ => new ComplexObject());
530+
break;
519531
}
532+
520533
sc.AddMcpServer().WithToolsFromAssembly();
521534
IServiceProvider services = sc.BuildServiceProvider();
522535

523536
McpServerTool tool = services.GetServices<McpServerTool>().First(t => t.ProtocolTool.Name == "EchoComplex");
524-
if (parameterInServices)
537+
if (lifetime is not null)
525538
{
526539
Assert.DoesNotContain("\"complex\"", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
527540
}

tests/ModelContextProtocol.Tests/Server/McpServerToolTests.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,47 @@ public async Task SupportsIMcpServer()
4545
Assert.Equal("42", result.Content[0].Text);
4646
}
4747

48-
[Fact]
49-
public async Task SupportsServiceFromDI()
48+
[Theory]
49+
[InlineData(ServiceLifetime.Singleton)]
50+
[InlineData(ServiceLifetime.Scoped)]
51+
[InlineData(ServiceLifetime.Transient)]
52+
public async Task SupportsServiceFromDI(ServiceLifetime injectedArgumentLifetime)
5053
{
51-
MyService expectedMyService = new();
54+
MyService singletonService = new();
5255

5356
ServiceCollection sc = new();
54-
sc.AddSingleton(expectedMyService);
55-
IServiceProvider services = sc.BuildServiceProvider();
57+
switch (injectedArgumentLifetime)
58+
{
59+
case ServiceLifetime.Singleton:
60+
sc.AddSingleton(singletonService);
61+
break;
62+
63+
case ServiceLifetime.Scoped:
64+
sc.AddScoped(_ => new MyService());
65+
break;
66+
67+
case ServiceLifetime.Transient:
68+
sc.AddTransient(_ => new MyService());
69+
break;
70+
}
5671

57-
McpServerTool tool = McpServerTool.Create((MyService actualMyService) =>
72+
sc.AddSingleton(services =>
5873
{
59-
Assert.Same(expectedMyService, actualMyService);
60-
return "42";
61-
}, new() { Services = services });
74+
return McpServerTool.Create((MyService actualMyService) =>
75+
{
76+
Assert.NotNull(actualMyService);
77+
if (injectedArgumentLifetime == ServiceLifetime.Singleton)
78+
{
79+
Assert.Same(singletonService, actualMyService);
80+
}
81+
82+
return "42";
83+
}, new() { Services = services });
84+
});
85+
86+
IServiceProvider services = sc.BuildServiceProvider();
87+
88+
McpServerTool tool = services.GetRequiredService<McpServerTool>();
6289

6390
Assert.DoesNotContain("actualMyService", JsonSerializer.Serialize(tool.ProtocolTool.InputSchema));
6491

0 commit comments

Comments
 (0)