Table of Contents

Class AIContentExtensions

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

This class serves as an adapter layer between Model Context Protocol (MCP) types and the Microsoft.Extensions.AI.AIContent model types from the Microsoft.Extensions.AI namespace.

Methods

CreateSamplingHandler(IChatClient)

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)

Parameters

chatClient IChatClient

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

Returns

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

The created handler delegate that can be assigned to SamplingHandler.

Remarks

This method creates a function that converts MCP message requests into chat client calls, enabling an MCP client to generate text or other content using an actual AI model via the provided chat client.

The handler can process text messages, image messages, resource messages, and tool use/results as defined in the Model Context Protocol.

Exceptions

ArgumentNullException

chatClient is null.

ToAIContent(ContentBlock)

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

public static AIContent? ToAIContent(this ContentBlock content)

Parameters

content ContentBlock

The ContentBlock to convert.

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

This method converts Model Context Protocol content types to the equivalent Microsoft.Extensions.AI content types, enabling seamless integration between the protocol and AI client libraries.

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

This method converts Model Context Protocol resource types to the equivalent Microsoft.Extensions.AI content types, enabling seamless integration between the protocol and AI client libraries.

Exceptions

ArgumentNullException

content is null.

NotSupportedException

The resource type is not supported.

ToAIContents(IEnumerable<ContentBlock>)

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

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

Parameters

contents IEnumerable<ContentBlock>

The ContentBlock instances to convert.

Returns

IList<AIContent>

The created Microsoft.Extensions.AI.AIContent instances.

Remarks

This method converts a collection of Model Context Protocol content objects into a collection of Microsoft.Extensions.AI content objects. It's useful when working with multiple content items, such as when processing the contents of a message or response.

Each ContentBlock object is converted using ToAIContent(ContentBlock), preserving the type-specific conversion logic for text, images, audio, and resources.

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

This method converts a collection of Model Context Protocol resource objects into a collection of Microsoft.Extensions.AI content objects. It's useful when working with multiple resources, such as when processing the contents of a ReadResourceResult.

Each ResourceContents object is converted using ToAIContent(ResourceContents), preserving the type-specific conversion logic: text resources become TextContentBlock objects and binary resources become Microsoft.Extensions.AI.DataContent objects.

Exceptions

ArgumentNullException

contents is null.

ToChatMessage(CallToolResult, string)

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

public static ChatMessage ToChatMessage(this CallToolResult result, string callId)

Parameters

result CallToolResult

The tool result to convert.

callId string

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

Returns

ChatMessage

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

Remarks

This method transforms a protocol-specific CallToolResult from the Model Context Protocol into a standard Microsoft.Extensions.AI.ChatMessage object that can be used with AI client libraries. It produces a Microsoft.Extensions.AI.ChatRole.Tool message containing a Microsoft.Extensions.AI.FunctionResultContent with result as a serialized JsonElement.

Exceptions

ArgumentNullException

result or callId is null.

ToChatMessage(PromptMessage)

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

public static ChatMessage ToChatMessage(this PromptMessage promptMessage)

Parameters

promptMessage PromptMessage

The prompt message to convert.

Returns

ChatMessage

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

Remarks

This method transforms a protocol-specific PromptMessage from the Model Context Protocol into a standard Microsoft.Extensions.AI.ChatMessage object that can be used with AI client libraries.

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

This method transforms protocol-specific PromptMessage objects from a Model Context Protocol prompt result into standard Microsoft.Extensions.AI.ChatMessage objects that can be used with AI client libraries.

Exceptions

ArgumentNullException

promptResult is null.

ToContentBlock(AIContent)

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

public static ContentBlock ToContentBlock(this AIContent content)

Parameters

content AIContent

The Microsoft.Extensions.AI.AIContent to convert.

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

This method transforms standard Microsoft.Extensions.AI.ChatMessage objects used with AI client libraries into protocol-specific PromptMessage objects for the Model Context Protocol system. Only representable content items are processed.

Exceptions

ArgumentNullException

chatMessage is null.