Table of Contents

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:

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

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

string

Remarks

If null, the method name will be used.