using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Client;
///
/// Represents a named resource template that can be retrieved from an MCP server.
///
///
///
/// This class provides a client-side wrapper around a resource template defined on an MCP server. It allows
/// retrieving the resource template's content by sending a request to the server with the resource's URI.
/// Instances of this class are typically obtained by calling .
///
///
public sealed class McpClientResourceTemplate
{
private readonly McpClient _client;
///
/// Initializes a new instance of the class.
///
/// The instance to use for reading the resource template.
/// The protocol definition describing the resource template's metadata.
///
///
/// This constructor enables reusing cached resource template definitions across different instances
/// without needing to call on every reconnect. This is particularly useful
/// in scenarios where resource template definitions are stable and network round-trips should be minimized.
///
///
/// The provided must represent a resource template that is actually available on the server
/// associated with the . Attempting to read a resource template that doesn't exist on the
/// server will result in an .
///
///
/// or is .
public McpClientResourceTemplate(McpClient client, ResourceTemplate resourceTemplate)
{
Throw.IfNull(client);
Throw.IfNull(resourceTemplate);
_client = client;
ProtocolResourceTemplate = resourceTemplate;
}
/// Gets the underlying protocol type for this instance.
///
///
/// This property provides direct access to the underlying protocol representation of the resource template,
/// which can be useful for advanced scenarios or when implementing custom MCP client extensions.
///
///
/// For most common use cases, you can use the more convenient and
/// properties instead of accessing the directly.
///
///
public ResourceTemplate ProtocolResourceTemplate { get; }
/// Gets the URI template of the resource template.
public string UriTemplate => ProtocolResourceTemplate.UriTemplate;
/// Gets the name of the resource template.
public string Name => ProtocolResourceTemplate.Name;
/// Gets the title of the resource template.
public string? Title => ProtocolResourceTemplate.Title;
/// Gets the description of the resource template.
public string? Description => ProtocolResourceTemplate.Description;
/// Gets the media (MIME) type of the resource template.
public string? MimeType => ProtocolResourceTemplate.MimeType;
///
/// Gets this resource template's content by formatting a URI from the template and supplied arguments
/// and sending a request to the server.
///
/// A dictionary of arguments to pass to the tool. Each key represents a parameter name,
/// and its associated value represents the argument value.
///
/// The to monitor for cancellation requests. The default is .
/// A containing the resource template's result with content and messages.
/// The request failed or the server returned an error response.
public ValueTask ReadAsync(
IReadOnlyDictionary arguments,
CancellationToken cancellationToken = default) =>
_client.ReadResourceAsync(UriTemplate, arguments, cancellationToken: cancellationToken);
}