-
Notifications
You must be signed in to change notification settings - Fork 734
Expand file tree
/
Copy pathDelegatingMcpServerToolTests.cs
More file actions
61 lines (50 loc) · 2.46 KB
/
Copy pathDelegatingMcpServerToolTests.cs
File metadata and controls
61 lines (50 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.Reflection;
namespace ModelContextProtocol.Tests.Server;
public class DelegatingMcpServerToolTests
{
[Fact]
public void Ctor_NullInnerTool_Throws()
{
Assert.Throws<ArgumentNullException>("innerTool", () => new TestDelegatingTool(null!));
}
[Fact]
public async Task AllMembers_DelegateToInnerTool()
{
Tool expectedTool = new() { Name = "sentinel-tool" };
IReadOnlyList<object> expectedMetadata = new object[] { "m1" };
CallToolResult expectedResult = new() { Content = [] };
InnerTool inner = new(expectedTool, expectedMetadata, expectedResult);
TestDelegatingTool delegating = new(inner);
Assert.Same(expectedTool, delegating.ProtocolTool);
Assert.Same(expectedMetadata, delegating.Metadata);
Assert.Same(expectedResult, await delegating.InvokeAsync(null!, CancellationToken.None));
Assert.Equal(inner.ToString(), delegating.ToString());
}
[Fact]
public void OverridesAllVirtualAndAbstractMembers()
{
MethodInfo[] baseMethods = typeof(McpServerTool).GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => (m.IsVirtual || m.IsAbstract) && m.DeclaringType == typeof(McpServerTool))
.ToArray();
Assert.NotEmpty(baseMethods);
foreach (MethodInfo baseMethod in baseMethods)
{
MethodInfo? overriddenMethod = typeof(DelegatingMcpServerTool).GetMethod(
baseMethod.Name,
baseMethod.GetParameters().Select(p => p.ParameterType).ToArray());
Assert.True(
overriddenMethod is not null && overriddenMethod.DeclaringType == typeof(DelegatingMcpServerTool),
$"DelegatingMcpServerTool does not override {baseMethod.Name} from McpServerTool.");
}
}
private sealed class TestDelegatingTool(McpServerTool innerTool) : DelegatingMcpServerTool(innerTool);
private sealed class InnerTool(Tool protocolTool, IReadOnlyList<object> metadata, CallToolResult result) : McpServerTool
{
public override Tool ProtocolTool => protocolTool;
public override IReadOnlyList<object> Metadata => metadata;
public override ValueTask<CallToolResult> InvokeAsync(RequestContext<CallToolRequestParams> request, CancellationToken cancellationToken = default) => new(result);
public override string ToString() => "inner-tool";
}
}