Fix McpClientPrompt/Resource types to use RequestOptions like McpClientTool does (#1370)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
This commit is contained in:
parent
1446cd738c
commit
8c46411f82
1 changed files with 115 additions and 12 deletions
|
|
@ -3,6 +3,7 @@ using ModelContextProtocol.Client;
|
|||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Client;
|
||||
|
||||
|
|
@ -23,6 +24,10 @@ public class McpClientResourceTests : ClientServerTestBase
|
|||
{
|
||||
[McpServerResource, Description("A sample resource")]
|
||||
public static string Sample() => "Sample content";
|
||||
|
||||
[McpServerResource, Description("Echoes back the metadata it receives")]
|
||||
public static string MetadataEcho(RequestContext<ReadResourceRequestParams> context) =>
|
||||
context.Params?.Meta?.ToJsonString() ?? "{}";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -31,7 +36,7 @@ public class McpClientResourceTests : ClientServerTestBase
|
|||
await using McpClient client = await CreateMcpClientForServer();
|
||||
|
||||
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
|
||||
var originalResource = resources.First();
|
||||
var originalResource = resources.First(r => r.Name == "sample");
|
||||
var resourceDefinition = originalResource.ProtocolResource;
|
||||
|
||||
var newResource = new McpClientResource(client, resourceDefinition);
|
||||
|
|
@ -94,7 +99,7 @@ public class McpClientResourceTests : ClientServerTestBase
|
|||
await using McpClient client = await CreateMcpClientForServer();
|
||||
|
||||
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
|
||||
var originalResource = resources.First();
|
||||
var originalResource = resources.First(r => r.Name == "sample");
|
||||
var resourceDefinition = originalResource.ProtocolResource;
|
||||
|
||||
var reusedResource = new McpClientResource(client, resourceDefinition);
|
||||
|
|
@ -125,4 +130,31 @@ public class McpClientResourceTests : ClientServerTestBase
|
|||
Assert.Equal("A sample resource", clientResource.Description);
|
||||
Assert.Equal("file:///sample.txt", clientResource.Uri);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadAsync_WithRequestOptions_PassesMetaToServer()
|
||||
{
|
||||
await using McpClient client = await CreateMcpClientForServer();
|
||||
|
||||
var resources = await client.ListResourcesAsync(cancellationToken: TestContext.Current.CancellationToken);
|
||||
var resource = resources.Single(r => r.Name == "metadata_echo");
|
||||
|
||||
RequestOptions requestOptions = new()
|
||||
{
|
||||
Meta = new JsonObject
|
||||
{
|
||||
["traceId"] = "test-trace-123",
|
||||
["customKey"] = "customValue"
|
||||
}
|
||||
};
|
||||
|
||||
var result = await resource.ReadAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.NotNull(result);
|
||||
var content = Assert.IsType<TextResourceContents>(result.Contents.First());
|
||||
var receivedMetadata = JsonNode.Parse(content.Text)?.AsObject();
|
||||
Assert.NotNull(receivedMetadata);
|
||||
Assert.Equal("test-trace-123", receivedMetadata["traceId"]?.GetValue<string>());
|
||||
Assert.Equal("customValue", receivedMetadata["customKey"]?.GetValue<string>());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue