Table of Contents

Class McpServerPrompt

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

Represents an invocable prompt used by Model Context Protocol clients and servers.

public abstract class McpServerPrompt : IMcpServerPrimitive
Inheritance
McpServerPrompt
Implements
Derived
Inherited Members

Remarks

McpServerPrompt is an abstract base class that represents an MCP prompt for use in the server (as opposed to Prompt, which provides the protocol representation of a prompt, and McpClientPrompt, which provides a client-side representation of a prompt). Instances of McpServerPrompt can be added into a IServiceCollection to be picked up automatically when McpServerFactory is used to create an IMcpServer, or added into a McpServerPrimitiveCollection<T>.

Most commonly, McpServerPrompt instances are created using the static McpServerPrompt.Create methods. These methods enable creating an McpServerPrompt for a method, specified via a Delegate or MethodInfo, and are what are used implicitly by WithPromptsFromAssembly(IMcpServerBuilder, Assembly?, JsonSerializerOptions?) and McpServerBuilderExtensions.WithPrompts. The McpServerPrompt.Create methods create McpServerPrompt 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 GetPromptResult that's then serialized and sent back to the client.

By default, parameters are sourced from the Arguments dictionary, which is a collection of key/value pairs. Those parameters are deserialized from the JsonElement values in that collection. There are a few exceptions to this:

All other parameters are deserialized from the JsonElements in the Arguments dictionary.

In general, the data supplied via the Arguments'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 IServiceProvider to provide data to the method.

Return values from a method are used to create the GetPromptResult that is sent back to the client:

stringConverted to a list containing a single PromptMessage with its Content set to contain the string.
PromptMessageConverted to a list containing the single PromptMessage.
IEnumerable<T> of PromptMessageConverted to a list containing all of the returned PromptMessage instances.
Microsoft.Extensions.AI.ChatMessageConverted to a list of PromptMessage instances derived from the Microsoft.Extensions.AI.ChatMessage with ToPromptMessages(ChatMessage).
IEnumerable<T> of PromptMessageConverted to a list of PromptMessage instances derived from all of the Microsoft.Extensions.AI.ChatMessage instances with ToPromptMessages(ChatMessage).

Other returned types will result in an InvalidOperationException being thrown.

Constructors

McpServerPrompt()

Initializes a new instance of the McpServerPrompt class.

protected McpServerPrompt()

Properties

ProtocolPrompt

Gets the protocol Prompt type for this instance.

public abstract Prompt ProtocolPrompt { get; }

Property Value

Prompt

Remarks

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.

Methods

Create(AIFunction, McpServerPromptCreateOptions?)

Creates an McpServerPrompt that wraps the specified Microsoft.Extensions.AI.AIFunction.

public static McpServerPrompt Create(AIFunction function, McpServerPromptCreateOptions? options = null)

Parameters

function AIFunction

The function to wrap.

options McpServerPromptCreateOptions

Optional options used in the creation of the McpServerPrompt to control its behavior.

Returns

McpServerPrompt

Remarks

Unlike the other overloads of Create, the McpServerPrompt created by Create(AIFunction, McpServerPromptCreateOptions?) does not provide all of the special parameter handling for MCP-specific concepts, like IMcpServer.

Exceptions

ArgumentNullException

function is null.

Create(Delegate, McpServerPromptCreateOptions?)

Creates an McpServerPrompt instance for a method, specified via a Delegate instance.

public static McpServerPrompt Create(Delegate method, McpServerPromptCreateOptions? options = null)

Parameters

method Delegate

The method to be represented via the created McpServerPrompt.

options McpServerPromptCreateOptions

Optional options used in the creation of the McpServerPrompt to control its behavior.

Returns

McpServerPrompt

The created McpServerPrompt for invoking method.

Exceptions

ArgumentNullException

method is null.

Create(MethodInfo, object?, McpServerPromptCreateOptions?)

Creates an McpServerPrompt instance for a method, specified via a Delegate instance.

public static McpServerPrompt Create(MethodInfo method, object? target = null, McpServerPromptCreateOptions? options = null)

Parameters

method MethodInfo

The method to be represented via the created McpServerPrompt.

target object

The instance if method is an instance method; otherwise, null.

options McpServerPromptCreateOptions

Optional options used in the creation of the McpServerPrompt to control its behavior.

Returns

McpServerPrompt

The created McpServerPrompt for invoking method.

Exceptions

ArgumentNullException

method is null.

ArgumentException

method is an instance method but target is null.

Create(MethodInfo, Type, McpServerPromptCreateOptions?)

Creates an McpServerPrompt instance for a method, specified via an MethodInfo for and instance method, along with a Type representing the type of the target object to instantiate each time the method is invoked.

public static McpServerPrompt Create(MethodInfo method, Type targetType, McpServerPromptCreateOptions? options = null)

Parameters

method MethodInfo

The instance method to be represented via the created Microsoft.Extensions.AI.AIFunction.

targetType Type

The Type to construct an instance of on which to invoke method when the resulting Microsoft.Extensions.AI.AIFunction is invoked. If services are provided, ActivatorUtilities.CreateInstance will be used to construct the instance using those services; otherwise, CreateInstance(Type) 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.

options McpServerPromptCreateOptions

Optional options used in the creation of the McpServerPrompt to control its behavior.

Returns

McpServerPrompt

The created Microsoft.Extensions.AI.AIFunction for invoking method.

Exceptions

ArgumentNullException

method is null.

GetAsync(RequestContext<GetPromptRequestParams>, CancellationToken)

Gets the prompt, rendering it with the provided request parameters and returning the prompt result.

public abstract Task<GetPromptResult> GetAsync(RequestContext<GetPromptRequestParams> request, CancellationToken cancellationToken = default)

Parameters

request RequestContext<GetPromptRequestParams>

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.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task<GetPromptResult>

A Task representing the asynchronous operation, containing a GetPromptResult with the prompt content and messages.

Exceptions

ArgumentNullException

request is null.

InvalidOperationException

The prompt implementation returns null or an unsupported result type.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.