Table of Contents

Class PromptMessage

Namespace
ModelContextProtocol.Protocol
Assembly
ModelContextProtocol.dll

Represents a message within the Model Context Protocol (MCP) system, used for communication between clients and AI models.

public class PromptMessage
Inheritance
PromptMessage
Inherited Members
Extension Methods

Remarks

A PromptMessage encapsulates content sent to or received from AI models in the Model Context Protocol. Each message has a specific role (User or Assistant) and contains content which can be text, images, audio, or embedded resources.

This class is similar to SamplingMessage, but with enhanced support for embedding resources from the MCP server. It serves as a core data structure in the MCP message exchange flow, particularly in prompt formation and model responses.

PromptMessage objects are typically used in collections within GetPromptResult to represent complete conversations or prompt sequences. They can be converted to and from Microsoft.Extensions.AI.ChatMessage objects using the extension methods ToChatMessage(PromptMessage) and ToPromptMessages(ChatMessage).

See the schema for details.

Properties

Content

Gets or sets the content of the message, which can be text, image, audio, or an embedded resource.

[JsonPropertyName("content")]
public Content Content { get; set; }

Property Value

Content

Remarks

The Content object contains all the message payload, whether it's simple text, base64-encoded binary data (for images/audio), or a reference to an embedded resource. The Type property indicates the specific content type.

Role

Gets or sets the role of the message sender, specifying whether it's from a "user" or an "assistant".

[JsonPropertyName("role")]
public Role Role { get; set; }

Property Value

Role

Remarks

In the Model Context Protocol, each message must have a clear role assignment to maintain the conversation flow. User messages represent queries or inputs from users, while assistant messages represent responses generated by AI models.