using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Messages;
using ModelContextProtocol.Protocol.Types;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;
namespace ModelContextProtocol.Server;
///
/// Represents an invocable prompt used by Model Context Protocol clients and servers.
///
///
///
/// is an abstract base class that represents an MCP prompt for use in the server (as opposed
/// to , which provides the protocol representation of a prompt, and , which
/// provides a client-side representation of a prompt). Instances of can be added into a
/// to be picked up automatically when is used to create
/// an , or added into a .
///
///
/// Most commonly, instances are created using the static methods.
/// These methods enable creating an for a method, specified via a or
/// , and are what are used implicitly by and
/// . The methods
/// create instances capable of working with a large variety of .NET method signatures, automatically handling
/// how parameters are marshaled into the method from the JSON received from the MCP client, and how the return value is marshaled back
/// into the that's then serialized and sent back to the client.
///
///
/// By default, parameters are sourced from the dictionary, which is a collection
/// of key/value pairs. Those parameters are deserialized from the
/// values in that collection. There are a few exceptions to this:
///
/// -
///
/// parameters are automatically bound to a provided by the
/// and that respects any s sent by the client for this operation's
/// .
///
///
/// -
///
/// parameters are bound from the for this request.
///
///
/// -
///
/// parameters are bound directly to the instance associated
/// with this request's . Such parameters may be used to understand
/// what server is being used to process the request, and to interact with the client issuing the request to that server.
///
///
/// -
///
/// parameters accepting values
/// are bound to an instance manufactured to forward progress notifications
/// from the prompt to the client. If the client included a in their request, progress reports issued
/// to this instance will propagate to the client as notifications with
/// that token. If the client did not include a , the instance will ignore any progress reports issued to it.
///
///
/// -
///
/// When the is constructed, it may be passed an via
/// . Any parameter that can be satisfied by that
/// according to will be resolved from the provided to
/// rather than from the argument collection.
///
///
/// -
///
/// Any parameter attributed with will similarly be resolved from the
/// provided to rather than from the argument collection.
///
///
///
///
///
/// All other parameters are deserialized from the s in the dictionary.
///
///
/// In general, the data supplied via the 's dictionary is passed along from the caller and
/// should thus be considered unvalidated and untrusted. To provide validated and trusted data to the invocation of the prompt, consider having
/// the prompt be an instance method, referring to data stored in the instance, or using an instance or parameters resolved from the
/// to provide data to the method.
///
///
/// Return values from a method are used to create the that is sent back to the client:
///
///
/// -
///
/// Converted to a list containing a single with its set to contain the .
///
/// -
///
/// Converted to a list containing the single .
///
/// -
/// of
/// Converted to a list containing all of the returned instances.
///
/// -
///
/// Converted to a list of instances derived from the with .
///
/// -
/// of
/// Converted to a list of instances derived from all of the instances with .
///
///
///
/// Other returned types will result in an being thrown.
///
///
public abstract class McpServerPrompt : IMcpServerPrimitive
{
/// Initializes a new instance of the class.
protected McpServerPrompt()
{
}
/// Gets the protocol type for this instance.
///
/// The ProtocolPrompt property represents the underlying prompt definition as defined in the
/// Model Context Protocol specification. It contains metadata like the prompt's name,
/// description, and acceptable arguments.
///
public abstract Prompt ProtocolPrompt { get; }
///
/// Gets the prompt, rendering it with the provided request parameters and returning the prompt result.
///
///
/// The request context containing information about the prompt invocation, including any arguments
/// passed to the prompt. This object provides access to both the request parameters and the server context.
///
///
/// The to monitor for cancellation requests. The default is .
///
///
/// A representing the asynchronous operation, containing a with
/// the prompt content and messages.
///
/// is .
/// The prompt implementation returns or an unsupported result type.
public abstract ValueTask GetAsync(
RequestContext request,
CancellationToken cancellationToken = default);
///
/// Creates an instance for a method, specified via a instance.
///
/// The method to be represented via the created .
/// Optional options used in the creation of the to control its behavior.
/// The created for invoking .
/// is .
public static McpServerPrompt Create(
Delegate method,
McpServerPromptCreateOptions? options = null) =>
AIFunctionMcpServerPrompt.Create(method, options);
///
/// Creates an instance for a method, specified via a instance.
///
/// The method to be represented via the created .
/// The instance if is an instance method; otherwise, .
/// Optional options used in the creation of the to control its behavior.
/// The created for invoking .
/// is .
/// is an instance method but is .
public static McpServerPrompt Create(
MethodInfo method,
object? target = null,
McpServerPromptCreateOptions? options = null) =>
AIFunctionMcpServerPrompt.Create(method, target, options);
///
/// Creates an instance for a method, specified via an for
/// and instance method, along with a representing the type of the target object to
/// instantiate each time the method is invoked.
///
/// The instance method to be represented via the created .
///
/// The to construct an instance of on which to invoke when
/// the resulting is invoked. If services are provided,
/// ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise,
/// is used, utilizing the type's public parameterless constructor.
/// If an instance can't be constructed, an exception is thrown during the function's invocation.
///
/// Optional options used in the creation of the to control its behavior.
/// The created for invoking .
/// is .
public static McpServerPrompt Create(
MethodInfo method,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type targetType,
McpServerPromptCreateOptions? options = null) =>
AIFunctionMcpServerPrompt.Create(method, targetType, options);
/// Creates an that wraps the specified .
/// The function to wrap.
/// Optional options used in the creation of the to control its behavior.
/// is .
///
/// Unlike the other overloads of Create, the created by
/// does not provide all of the special parameter handling for MCP-specific concepts, like .
///
public static McpServerPrompt Create(
AIFunction function,
McpServerPromptCreateOptions? options = null) =>
AIFunctionMcpServerPrompt.Create(function, options);
///
public override string ToString() => ProtocolPrompt.Name;
///
string IMcpServerPrimitive.Id => ProtocolPrompt.Name;
}