Class McpServerPromptAttribute
- Namespace
- ModelContextProtocol.Server
- Assembly
- ModelContextProtocol.dll
Used to indicate that a method should be considered an McpServerPrompt.
[AttributeUsage(AttributeTargets.Method)]
public sealed class McpServerPromptAttribute : Attribute
- Inheritance
-
McpServerPromptAttribute
- Inherited Members
Remarks
This attribute is applied to methods that should be exposed as prompts in the Model Context Protocol. When a class containing methods marked with this attribute is registered with McpServerBuilderExtensions, these methods become available as prompts that can be called by MCP clients.
When methods are provided directly to McpServerPrompt.Create, the attribute is not required.
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 the prompt invocation rather than from the argument collection.
- Any parameter attributed with FromKeyedServicesAttribute will similarly be resolved from the IServiceProvider provided to the prompt invocation 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
McpServerPromptAttribute()
Initializes a new instance of the McpServerPromptAttribute class.
public McpServerPromptAttribute()
Properties
Name
Gets the name of the prompt.
public string? Name { get; set; }
Property Value
Remarks
If null, the method name will be used.