Class McpClientPrompt
- Namespace
- ModelContextProtocol.Client
- Assembly
- ModelContextProtocol.Core.dll
Represents a named prompt that can be retrieved from an MCP server and invoked with arguments.
public sealed class McpClientPrompt
- Inheritance
-
McpClientPrompt
- Inherited Members
Remarks
This class provides a client-side wrapper around a prompt defined on an MCP server. It allows retrieving the prompt's content by sending a request to the server with optional arguments. Instances of this class are typically obtained by calling ListPromptsAsync(RequestOptions?, CancellationToken).
Each prompt has a name and optionally a description, and it can be invoked with arguments to produce customized prompt content from the server.
Constructors
McpClientPrompt(McpClient, Prompt)
Initializes a new instance of the McpClientPrompt class.
public McpClientPrompt(McpClient client, Prompt prompt)
Parameters
clientMcpClientThe McpClient instance to use for invoking the prompt.
promptPromptThe protocol Prompt definition describing the prompt's metadata.
Remarks
This constructor enables reusing cached prompt definitions across different McpClient instances without needing to call ListPromptsAsync(RequestOptions?, CancellationToken) on every reconnect. This is particularly useful in scenarios where prompt definitions are stable and network round-trips should be minimized.
The provided prompt must represent a prompt that is actually available on the server
associated with the client. Attempting to invoke a prompt that doesn't exist on the
server will result in an McpException.
Exceptions
- ArgumentNullException
clientorpromptis null.
Properties
Description
Gets the description of the prompt.
public string? Description { get; }
Property Value
Name
Gets the name of the prompt.
public string Name { get; }
Property Value
ProtocolPrompt
Gets the underlying protocol Prompt type for this instance.
public Prompt ProtocolPrompt { get; }
Property Value
Remarks
This property provides direct access to the underlying protocol representation of the prompt, which can be useful for advanced scenarios or when implementing custom MCP client extensions.
For most common use cases, you can use the more convenient Name and Description properties instead of accessing the ProtocolPrompt directly.
Title
Gets the title of the prompt.
public string? Title { get; }
Property Value
Methods
GetAsync(IEnumerable<KeyValuePair<string, object?>>?, JsonSerializerOptions?, CancellationToken)
Gets this prompt's content by sending a request to the server with optional arguments.
public ValueTask<GetPromptResult> GetAsync(IEnumerable<KeyValuePair<string, object?>>? arguments = null, JsonSerializerOptions? serializerOptions = null, CancellationToken cancellationToken = default)
Parameters
argumentsIEnumerable<KeyValuePair<string, object>>Optional arguments to pass to the prompt. Keys are parameter names, and values are the argument values.
serializerOptionsJsonSerializerOptionsThe serialization options governing argument serialization.
cancellationTokenCancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- ValueTask<GetPromptResult>
A ValueTask containing the prompt's result with content and messages.
Remarks
This method sends a request to the MCP server to execute this prompt with the provided arguments. The server will process the request and return a result containing messages or other content.
This is a convenience method that internally calls GetPromptAsync(string, IReadOnlyDictionary<string, object?>?, RequestOptions?, CancellationToken) with this prompt's name and arguments.