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 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";
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue