Class McpServerPrompt | MCP C# SDK

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.Core.dll

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

public abstract class McpServerPrompt : IMcpServerPrimitive
Inheritance

McpServerPrompt

Implements
Derived
Inherited Members

Constructors

McpServerPrompt()

protected McpServerPrompt()

Properties

Metadata

Gets the metadata for this prompt instance.

public abstract IReadOnlyList<object> Metadata { get; }

Property Value

IReadOnlyList<object>

ProtocolPrompt

Gets the protocol Prompt type for this instance.

public abstract Prompt ProtocolPrompt { get; }

Property Value

Prompt

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

Exceptions

ArgumentNullException

function is null.

Create(Delegate, McpServerPromptCreateOptions?)

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, Func<RequestContext<GetPromptRequestParams>, object>, McpServerPromptCreateOptions?)

Creates an McpServerPrompt instance for a method, specified via a MethodInfo for an 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, Func<RequestContext<GetPromptRequestParams>, object> createTargetFunc, McpServerPromptCreateOptions? options = null)

Parameters

method MethodInfo

The instance method to be represented via the created McpServerPrompt.

createTargetFunc Func<RequestContext<GetPromptRequestParams>, object>

Callback used on each function invocation to create an instance of the type on which the instance method method will be invoked. If the returned instance is IAsyncDisposable or IDisposable, it will be disposed of after method completes its invocation.

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 or createTargetFunc is null.

Create(MethodInfo, object?, McpServerPromptCreateOptions?)

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.

GetAsync(RequestContext<GetPromptRequestParams>, CancellationToken)

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

public abstract ValueTask<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

ValueTask<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.