using ModelContextProtocol.Protocol;
using System.Text.Json;
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
/// or .
///
///
/// 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;
internal McpClientPrompt(McpClient client, Prompt 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 a 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.
/// The serialization options governing argument serialization.
/// The to monitor for cancellation requests. The default is .
/// A containing the prompt's result with content and messages.
///
///
/// 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,
JsonSerializerOptions? serializerOptions = null,
CancellationToken cancellationToken = default)
{
IReadOnlyDictionary? argDict =
arguments as IReadOnlyDictionary ??
arguments?.ToDictionary();
return await _client.GetPromptAsync(ProtocolPrompt.Name, argDict, serializerOptions, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}