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
6 changed files with 188 additions and 0 deletions
|
|
@ -23,6 +23,9 @@ public abstract class DelegatingMcpServerPrompt : McpServerPrompt
|
|||
/// <inheritdoc />
|
||||
public override Prompt ProtocolPrompt => _innerPrompt.ProtocolPrompt;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyList<object> Metadata => _innerPrompt.Metadata;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ValueTask<GetPromptResult> GetAsync(
|
||||
RequestContext<GetPromptRequestParams> request,
|
||||
|
|
|
|||
|
|
@ -26,6 +26,9 @@ public abstract class DelegatingMcpServerResource : McpServerResource
|
|||
/// <inheritdoc />
|
||||
public override ResourceTemplate ProtocolResourceTemplate => _innerResource.ProtocolResourceTemplate;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyList<object> Metadata => _innerResource.Metadata;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool IsMatch(string uri) => _innerResource.IsMatch(uri);
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ public abstract class DelegatingMcpServerTool : McpServerTool
|
|||
/// <inheritdoc />
|
||||
public override Tool ProtocolTool => _innerTool.ProtocolTool;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override IReadOnlyList<object> Metadata => _innerTool.Metadata;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override ValueTask<CallToolResult> InvokeAsync(
|
||||
RequestContext<CallToolRequestParams> request,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Server;
|
||||
|
||||
public class DelegatingMcpServerPromptTests
|
||||
{
|
||||
[Fact]
|
||||
public void Ctor_NullInnerPrompt_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>("innerPrompt", () => new TestDelegatingPrompt(null!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AllMembers_DelegateToInnerPrompt()
|
||||
{
|
||||
Prompt expectedPrompt = new() { Name = "sentinel-prompt" };
|
||||
IReadOnlyList<object> expectedMetadata = new object[] { "m1" };
|
||||
GetPromptResult expectedResult = new() { Messages = [] };
|
||||
InnerPrompt inner = new(expectedPrompt, expectedMetadata, expectedResult);
|
||||
|
||||
TestDelegatingPrompt delegating = new(inner);
|
||||
|
||||
Assert.Same(expectedPrompt, delegating.ProtocolPrompt);
|
||||
Assert.Same(expectedMetadata, delegating.Metadata);
|
||||
Assert.Same(expectedResult, await delegating.GetAsync(null!, CancellationToken.None));
|
||||
Assert.Equal(inner.ToString(), delegating.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverridesAllVirtualAndAbstractMembers()
|
||||
{
|
||||
MethodInfo[] baseMethods = typeof(McpServerPrompt).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(DelegatingMcpServerPrompt).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Any(m => m.Name == baseMethod.Name),
|
||||
$"DelegatingMcpServerPrompt does not override {baseMethod.Name} from McpServerPrompt.");
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestDelegatingPrompt(McpServerPrompt innerPrompt) : DelegatingMcpServerPrompt(innerPrompt);
|
||||
|
||||
private sealed class InnerPrompt(Prompt protocolPrompt, IReadOnlyList<object> metadata, GetPromptResult result) : McpServerPrompt
|
||||
{
|
||||
public override Prompt ProtocolPrompt => protocolPrompt;
|
||||
public override IReadOnlyList<object> Metadata => metadata;
|
||||
public override ValueTask<GetPromptResult> GetAsync(RequestContext<GetPromptRequestParams> request, CancellationToken cancellationToken = default) => new(result);
|
||||
public override string ToString() => "inner-prompt";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Server;
|
||||
|
||||
public class DelegatingMcpServerResourceTests
|
||||
{
|
||||
[Fact]
|
||||
public void Ctor_NullInnerResource_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>("innerResource", () => new TestDelegatingResource(null!));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AllMembers_DelegateToInnerResource()
|
||||
{
|
||||
ResourceTemplate expectedTemplate = new() { Name = "sentinel-resource", UriTemplate = "test://resource" };
|
||||
Resource expectedResource = new() { Name = "sentinel-resource", Uri = "test://resource" };
|
||||
IReadOnlyList<object> expectedMetadata = new object[] { "m1" };
|
||||
ReadResourceResult expectedResult = new() { Contents = [] };
|
||||
InnerResource inner = new(expectedTemplate, expectedResource, expectedMetadata, expectedResult);
|
||||
|
||||
TestDelegatingResource delegating = new(inner);
|
||||
|
||||
Assert.Same(expectedTemplate, delegating.ProtocolResourceTemplate);
|
||||
Assert.Same(expectedResource, delegating.ProtocolResource);
|
||||
Assert.Same(expectedMetadata, delegating.Metadata);
|
||||
Assert.True(delegating.IsMatch("test://resource"));
|
||||
Assert.Same(expectedResult, await delegating.ReadAsync(null!, CancellationToken.None));
|
||||
Assert.Equal(inner.ToString(), delegating.ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverridesAllVirtualAndAbstractMembers()
|
||||
{
|
||||
MethodInfo[] baseMethods = typeof(McpServerResource).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(DelegatingMcpServerResource).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
||||
.Any(m => m.Name == baseMethod.Name),
|
||||
$"DelegatingMcpServerResource does not override {baseMethod.Name} from McpServerResource.");
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestDelegatingResource(McpServerResource innerResource) : DelegatingMcpServerResource(innerResource);
|
||||
|
||||
private sealed class InnerResource(ResourceTemplate protocolResourceTemplate, Resource protocolResource, IReadOnlyList<object> metadata, ReadResourceResult result) : McpServerResource
|
||||
{
|
||||
public override ResourceTemplate ProtocolResourceTemplate => protocolResourceTemplate;
|
||||
public override Resource? ProtocolResource => protocolResource;
|
||||
public override IReadOnlyList<object> Metadata => metadata;
|
||||
public override bool IsMatch(string uri) => true;
|
||||
public override ValueTask<ReadResourceResult> ReadAsync(RequestContext<ReadResourceRequestParams> request, CancellationToken cancellationToken = default) => new(result);
|
||||
public override string ToString() => "inner-resource";
|
||||
}
|
||||
}
|
||||
|
|
@ -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