csharp-sdk/tests/ModelContextProtocol.Tests/Server/McpServerResourceTests.cs
Stephen Toub 7d77be68cf
Clean up (use of) McpException and McpTransportException (#321)
* Clean up (use of) McpException

* Replace McpTransportException with InvalidOperationException

* Add cancellation test
2025-04-18 09:25:29 -07:00

100 lines
3.2 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Server;
namespace ModelContextProtocol.Tests.Server;
public class McpServerResourceTests
{
[Fact]
public void CanCreateServerWithResourceTemplates()
{
var services = new ServiceCollection();
services.AddMcpServer()
.WithStdioServerTransport()
.WithListResourceTemplatesHandler(async (ctx, ct) =>
{
return new ListResourceTemplatesResult
{
ResourceTemplates =
[
new ResourceTemplate { Name = "Static Resource", Description = "A static resource with a numeric ID", UriTemplate = "test://static/resource/{id}" }
]
};
})
.WithReadResourceHandler(async (ctx, ct) =>
{
return new ReadResourceResult
{
Contents = [new TextResourceContents
{
Uri = ctx.Params!.Uri!,
Text = "Static Resource",
MimeType = "text/plain",
}]
};
});
var provider = services.BuildServiceProvider();
provider.GetRequiredService<IMcpServer>();
}
[Fact]
public void CanCreateServerWithResources()
{
var services = new ServiceCollection();
services.AddMcpServer()
.WithStdioServerTransport()
.WithListResourcesHandler(async (ctx, ct) =>
{
return new ListResourcesResult
{
Resources =
[
new Resource { Name = "Static Resource", Description = "A static resource with a numeric ID", Uri = "test://static/resource/foo.txt" }
]
};
})
.WithReadResourceHandler(async (ctx, ct) =>
{
return new ReadResourceResult
{
Contents = [new TextResourceContents
{
Uri = ctx.Params!.Uri!,
Text = "Static Resource",
MimeType = "text/plain",
}]
};
});
var provider = services.BuildServiceProvider();
provider.GetRequiredService<IMcpServer>();
}
[Fact]
public void CreatingReadHandlerWithNoListHandlerFails()
{
var services = new ServiceCollection();
services.AddMcpServer()
.WithStdioServerTransport()
.WithReadResourceHandler(async (ctx, ct) =>
{
return new ReadResourceResult
{
Contents = [new TextResourceContents
{
Uri = ctx.Params!.Uri!,
Text = "Static Resource",
MimeType = "text/plain",
}]
};
});
var sp = services.BuildServiceProvider();
Assert.Throws<InvalidOperationException>(sp.GetRequiredService<IMcpServer>);
}
}