Class AIContentExtensions | MCP C# SDK

Namespace
ModelContextProtocol
Assembly
ModelContextProtocol.Core.dll

Provides extension methods for converting between Model Context Protocol (MCP) types and Microsoft.Extensions.AI types.

public static class AIContentExtensions
Inheritance

AIContentExtensions

Inherited Members

Remarks

Methods

CreateSamplingHandler(IChatClient, JsonSerializerOptions?)

Creates a sampling handler for use with SamplingHandler that will satisfy sampling requests using the specified Microsoft.Extensions.AI.IChatClient.

public static Func<CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>> CreateSamplingHandler(this IChatClient chatClient, JsonSerializerOptions? serializerOptions = null)

Parameters

chatClient IChatClient

The Microsoft.Extensions.AI.IChatClient with which to satisfy sampling requests.

serializerOptions JsonSerializerOptions

The JsonSerializerOptions to use for serializing user-provided objects. If null, DefaultOptions is used.

Returns

Func<CreateMessageRequestParams, IProgress<ProgressNotificationValue>, CancellationToken, ValueTask<CreateMessageResult>>

The created handler delegate that can be assigned to SamplingHandler.

Remarks

Exceptions

ArgumentNullException

chatClient is null.

ToAIContent(ContentBlock, JsonSerializerOptions?)

Creates a new Microsoft.Extensions.AI.AIContent from the content of a ContentBlock.

public static AIContent? ToAIContent(this ContentBlock content, JsonSerializerOptions? options = null)

Parameters

content ContentBlock

The ContentBlock to convert.

options JsonSerializerOptions

The JsonSerializerOptions to use for deserialization. If null, DefaultOptions is used.

Returns

AIContent

The created Microsoft.Extensions.AI.AIContent. If the content can't be converted (such as when it's a resource link), null is returned.

Remarks

Exceptions

ArgumentNullException

content is null.

ToAIContent(ResourceContents)

Creates a new Microsoft.Extensions.AI.AIContent from the content of a ResourceContents.

public static AIContent ToAIContent(this ResourceContents content)

Parameters

content ResourceContents

The ResourceContents to convert.

Returns

AIContent

The created Microsoft.Extensions.AI.AIContent.

Remarks

Exceptions

ArgumentNullException

content is null.

NotSupportedException

The resource type is not supported.

ToAIContents(IEnumerable<ContentBlock>, JsonSerializerOptions?)

Creates a list of Microsoft.Extensions.AI.AIContent from a sequence of ContentBlock.

public static IList<AIContent> ToAIContents(this IEnumerable<ContentBlock> contents, JsonSerializerOptions? options = null)

Parameters

contents IEnumerable<ContentBlock>

The ContentBlock instances to convert.

options JsonSerializerOptions

The JsonSerializerOptions to use for deserialization. If null, DefaultOptions is used.

Returns

IList<AIContent>

The created Microsoft.Extensions.AI.AIContent instances.

Remarks

Exceptions

ArgumentNullException

contents is null.

ToAIContents(IEnumerable<ResourceContents>)

Creates a list of Microsoft.Extensions.AI.AIContent from a sequence of ResourceContents.

public static IList<AIContent> ToAIContents(this IEnumerable<ResourceContents> contents)

Parameters

contents IEnumerable<ResourceContents>

The ResourceContents instances to convert.

Returns

IList<AIContent>

A list of Microsoft.Extensions.AI.AIContent objects created from the resource contents.

Remarks

Exceptions

ArgumentNullException

contents is null.

ToChatMessage(CallToolResult, string, JsonSerializerOptions?)

Converts a CallToolResult to a Microsoft.Extensions.AI.ChatMessage object.

public static ChatMessage ToChatMessage(this CallToolResult result, string callId, JsonSerializerOptions? options = null)

Parameters

result CallToolResult

The tool result to convert.

callId string

The identifier for the function call request that triggered the tool invocation.

options JsonSerializerOptions

The JsonSerializerOptions to use for serialization. If null, DefaultOptions is used.

Returns

ChatMessage

A Microsoft.Extensions.AI.ChatMessage object created from the tool result.

Remarks

Exceptions

ArgumentNullException

result or callId is null.

ToChatMessage(PromptMessage, JsonSerializerOptions?)

Converts a PromptMessage to a Microsoft.Extensions.AI.ChatMessage object.

public static ChatMessage ToChatMessage(this PromptMessage promptMessage, JsonSerializerOptions? options = null)

Parameters

promptMessage PromptMessage

The prompt message to convert.

options JsonSerializerOptions

The JsonSerializerOptions to use for deserialization. If null, DefaultOptions is used.

Returns

ChatMessage

A Microsoft.Extensions.AI.ChatMessage object created from the prompt message.

Remarks

Exceptions

ArgumentNullException

promptMessage is null.

ToChatMessages(GetPromptResult)

Converts a GetPromptResult to a list of Microsoft.Extensions.AI.ChatMessage objects.

public static IList<ChatMessage> ToChatMessages(this GetPromptResult promptResult)

Parameters

promptResult GetPromptResult

The prompt result containing messages to convert.

Returns

IList<ChatMessage>

A list of Microsoft.Extensions.AI.ChatMessage objects created from the prompt messages.

Remarks

Exceptions

ArgumentNullException

promptResult is null.

ToContentBlock(AIContent, JsonSerializerOptions?)

Creates a new ContentBlock from the content of an Microsoft.Extensions.AI.AIContent.

public static ContentBlock ToContentBlock(this AIContent content, JsonSerializerOptions? options = null)

Parameters

content AIContent

The Microsoft.Extensions.AI.AIContent to convert.

options JsonSerializerOptions

The JsonSerializerOptions to use for serialization. If null, DefaultOptions is used.

Returns

ContentBlock

The created ContentBlock.

Exceptions

ArgumentNullException

content is null.

ToPromptMessages(ChatMessage)

Converts a Microsoft.Extensions.AI.ChatMessage to a list of PromptMessage objects.

public static IList<PromptMessage> ToPromptMessages(this ChatMessage chatMessage)

Parameters

chatMessage ChatMessage

The chat message to convert.

Returns

IList<PromptMessage>

A list of PromptMessage objects created from the chat message's contents.

Remarks

Exceptions

ArgumentNullException

chatMessage is null.