Fix regression matching templated McpServerResources (#897)
This commit is contained in:
parent
8e607e6093
commit
1343dd3ebd
1 changed files with 107 additions and 46 deletions
|
|
@ -0,0 +1,42 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace ModelContextProtocol.Tests.Configuration;
|
||||
|
||||
public sealed class McpServerResourceRoutingTests(ITestOutputHelper testOutputHelper) : ClientServerTestBase(testOutputHelper)
|
||||
{
|
||||
protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder)
|
||||
{
|
||||
mcpServerBuilder.WithResources([
|
||||
McpServerResource.Create(options: new() { UriTemplate = "test://resource/non-templated" } , method: () => "static"),
|
||||
McpServerResource.Create(options: new() { UriTemplate = "test://resource/{id}" }, method: (string id) => $"template: {id}"),
|
||||
McpServerResource.Create(options: new() { UriTemplate = "test://params{?a1,a2,a3}" }, method: (string a1, string a2, string a3) => $"params: {a1}, {a2}, {a3}"),
|
||||
]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MultipleTemplatedResources_MatchesCorrectResource()
|
||||
{
|
||||
// Verify that when multiple templated resources exist, the correct one is matched based on the URI pattern, not just the first one.
|
||||
// Regression test for https://github.com/modelcontextprotocol/csharp-sdk/issues/821.
|
||||
await using McpClient client = await CreateMcpClientForServer();
|
||||
|
||||
var nonTemplatedResult = await client.ReadResourceAsync("test://resource/non-templated", TestContext.Current.CancellationToken);
|
||||
Assert.Equal("static", ((TextResourceContents)nonTemplatedResult.Contents[0]).Text);
|
||||
|
||||
var templatedResult = await client.ReadResourceAsync("test://resource/12345", TestContext.Current.CancellationToken);
|
||||
Assert.Equal("template: 12345", ((TextResourceContents)templatedResult.Contents[0]).Text);
|
||||
|
||||
var exactTemplatedResult = await client.ReadResourceAsync("test://resource/{id}", TestContext.Current.CancellationToken);
|
||||
Assert.Equal("template: {id}", ((TextResourceContents)exactTemplatedResult.Contents[0]).Text);
|
||||
|
||||
var paramsResult = await client.ReadResourceAsync("test://params?a1=a&a2=b&a3=c", TestContext.Current.CancellationToken);
|
||||
Assert.Equal("params: a, b, c", ((TextResourceContents)paramsResult.Contents[0]).Text);
|
||||
|
||||
var mcpEx = await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync("test://params{?a1,a2,a3}", TestContext.Current.CancellationToken));
|
||||
Assert.Equal(McpErrorCode.InvalidParams, mcpEx.ErrorCode);
|
||||
Assert.Equal("Request failed (remote): Unknown resource URI: 'test://params{?a1,a2,a3}'", mcpEx.Message);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue