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:
- CancellationToken parameters are automatically bound to a CancellationToken provided by the IMcpServer and that respects any CancelledNotifications sent by the client for this operation's RequestId.
- IServiceProvider parameters are bound from the RequestContext<TParams> for this request.
- IMcpServer parameters are bound directly to the IMcpServer instance associated with this request's RequestContext<TParams>. 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.
- IProgress<T> parameters accepting ProgressNotificationValue values are bound to an IProgress<T> instance manufactured to forward progress notifications from the prompt to the client. If the client included a ProgressToken in their request, progress reports issued to this instance will propagate to the client as ProgressNotification notifications with that token. If the client did not include a ProgressToken, the instance will ignore any progress reports issued to it.
- When the McpServerPrompt is constructed, it may be passed an IServiceProvider via Services. Any parameter that can be satisfied by that IServiceProvider according to IServiceProviderIsService will be resolved from the IServiceProvider provided to GetAsync(RequestContext<GetPromptRequestParams>, CancellationToken) rather than from the argument collection.
- Any parameter attributed with FromKeyedServicesAttribute will similarly be resolved from the IServiceProvider provided to GetAsync(RequestContext<GetPromptRequestParams>, CancellationToken) rather than from the argument collection.
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:
string | Converted to a list containing a single PromptMessage with its Content set to contain the string. |
PromptMessage | Converted to a list containing the single PromptMessage. |
IEnumerable<T> of PromptMessage | Converted to a list containing all of the returned PromptMessage instances. |
Microsoft.Extensions.AI.ChatMessage | Converted to a list of PromptMessage instances derived from the Microsoft.Extensions.AI.ChatMessage with ToPromptMessages(ChatMessage). |
IEnumerable<T> of PromptMessage | Converted 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
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
AIFunctionThe function to wrap.
options
McpServerPromptCreateOptionsOptional options used in the creation of the McpServerPrompt to control its behavior.
Returns
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
DelegateThe method to be represented via the created McpServerPrompt.
options
McpServerPromptCreateOptionsOptional 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
MethodInfoThe method to be represented via the created McpServerPrompt.
target
objectThe instance if
method
is an instance method; otherwise, null.options
McpServerPromptCreateOptionsOptional 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 buttarget
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
MethodInfoThe instance method to be represented via the created Microsoft.Extensions.AI.AIFunction.
targetType
TypeThe 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
McpServerPromptCreateOptionsOptional 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
CancellationTokenThe 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.