using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Client;
///
/// Represents a named prompt that can be retrieved from an MCP server and invoked with arguments.
///
///
///
/// This class provides a client-side wrapper around a prompt defined on an MCP server. It allows
/// retrieving the prompt's content by sending a request to the server with optional arguments.
/// Instances of this class are typically obtained by calling .
///
///
/// Each prompt has a name and optionally a description, and it can be invoked with arguments
/// to produce customized prompt content from the server.
///
///
public sealed class McpClientPrompt
{
private readonly McpClient _client;
///
/// Initializes a new instance of the class.
///
/// The instance to use for invoking the prompt.
/// The protocol definition describing the prompt's metadata.
///
///
/// This constructor enables reusing cached prompt definitions across different instances
/// without needing to call on every reconnect. This is particularly useful
/// in scenarios where prompt definitions are stable and network round-trips should be minimized.
///
///
/// The provided must represent a prompt that is actually available on the server
/// associated with the . Attempting to invoke a prompt that doesn't exist on the
/// server will result in an .
///
///
/// or is .
public McpClientPrompt(McpClient client, Prompt prompt)
{
Throw.IfNull(client);
Throw.IfNull(prompt);
_client = client;
ProtocolPrompt = prompt;
}
/// Gets the underlying protocol type for this instance.
///
///
/// This property provides direct access to the underlying protocol representation of the prompt,
/// 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 Prompt ProtocolPrompt { get; }
/// Gets the name of the prompt.
public string Name => ProtocolPrompt.Name;
/// Gets the title of the prompt.
public string? Title => ProtocolPrompt.Title;
/// Gets the description of the prompt.
public string? Description => ProtocolPrompt.Description;
///
/// Gets this prompt's content by sending a request to the server with optional arguments.
///
/// Optional arguments to pass to the prompt. Keys are parameter names, and values are the argument values.
/// Optional request options including metadata, serialization settings, and progress tracking.
/// The to monitor for cancellation requests. The default is .
/// A containing the prompt's result with content and messages.
/// The request failed or the server returned an error response.
///
///
/// This method sends a request to the MCP server to execute this prompt with the provided arguments.
/// The server will process the request and return a result containing messages or other content.
///
///
/// This is a convenience method that internally calls
///
/// with this prompt's name and arguments.
///
///
public async ValueTask GetAsync(
IEnumerable>? arguments = null,
RequestOptions? options = null,
CancellationToken cancellationToken = default)
{
IReadOnlyDictionary? argDict =
arguments as IReadOnlyDictionary ??
arguments?.ToDictionary();
return await _client.GetPromptAsync(ProtocolPrompt.Name, argDict, options, cancellationToken).ConfigureAwait(false);
}
}