Fix Metadata delegation in DelegatingMcpServerTool/Prompt/Resource (#1338)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> Co-authored-by: jeffhandley <1031940+jeffhandley@users.noreply.github.com>
This commit is contained in:
parent
34d4cd7552
commit
79b2dbeb20
1 changed files with 188 additions and 0 deletions
|
|
@ -0,0 +1,58 @@
|
|||
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 | BindingFlags.DeclaredOnly)
|
||||
.Where(m => m.IsVirtual || m.IsAbstract)
|
||||
.ToArray();
|
||||
|
||||
Assert.NotEmpty(baseMethods);
|
||||
|
||||
foreach (MethodInfo baseMethod in baseMethods)
|
||||
{
|
||||
Assert.True(
|
||||
typeof(DelegatingMcpServerTool).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Any(m => m.Name == baseMethod.Name),
|
||||
$"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";
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue