- 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
chatClientIChatClientThe Microsoft.Extensions.AI.IChatClient with which to satisfy sampling requests.
serializerOptionsJsonSerializerOptionsThe 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
chatClientis 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
contentContentBlockThe ContentBlock to convert.
optionsJsonSerializerOptionsThe 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
contentis null.
ToAIContent(ResourceContents)
Creates a new Microsoft.Extensions.AI.AIContent from the content of a ResourceContents.
public static AIContent ToAIContent(this ResourceContents content)
Parameters
contentResourceContentsThe ResourceContents to convert.
Returns
- AIContent
The created Microsoft.Extensions.AI.AIContent.
Remarks
Exceptions
- ArgumentNullException
contentis 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
contentsIEnumerable<ContentBlock>The ContentBlock instances to convert.
optionsJsonSerializerOptionsThe JsonSerializerOptions to use for deserialization. If null, DefaultOptions is used.
Returns
- IList<AIContent>
The created Microsoft.Extensions.AI.AIContent instances.
Remarks
Exceptions
- ArgumentNullException
contentsis 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
contentsIEnumerable<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
contentsis 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
resultCallToolResultThe tool result to convert.
callIdstringThe identifier for the function call request that triggered the tool invocation.
optionsJsonSerializerOptionsThe 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
resultorcallIdis 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
promptMessagePromptMessageThe prompt message to convert.
optionsJsonSerializerOptionsThe 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
promptMessageis null.
ToChatMessages(GetPromptResult)
Converts a GetPromptResult to a list of Microsoft.Extensions.AI.ChatMessage objects.
public static IList<ChatMessage> ToChatMessages(this GetPromptResult promptResult)
Parameters
promptResultGetPromptResultThe 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
promptResultis 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
contentAIContentThe Microsoft.Extensions.AI.AIContent to convert.
optionsJsonSerializerOptionsThe JsonSerializerOptions to use for serialization. If null, DefaultOptions is used.
Returns
- ContentBlock
The created ContentBlock.
Exceptions
- ArgumentNullException
contentis null.
ToPromptMessages(ChatMessage)
Converts a Microsoft.Extensions.AI.ChatMessage to a list of PromptMessage objects.
public static IList<PromptMessage> ToPromptMessages(this ChatMessage chatMessage)
Parameters
chatMessageChatMessageThe chat message to convert.
Returns
- IList<PromptMessage>
A list of PromptMessage objects created from the chat message's contents.
Remarks
Exceptions
- ArgumentNullException
chatMessageis null.