1
- using Microsoft . Extensions . AI ;
1
+ using Microsoft . Extensions . AI ;
2
2
using ModelContextProtocol . Protocol . Types ;
3
3
using ModelContextProtocol . Utils ;
4
4
using ModelContextProtocol . Utils . Json ;
7
7
8
8
namespace ModelContextProtocol ;
9
9
10
- /// <summary>Provides helpers for conversions related to <see cref="AIContent"/>.</summary>
10
+ /// <summary>
11
+ /// Provides extension methods for converting between Model Context Protocol (MCP) types and Microsoft.Extensions.AI types.
12
+ /// </summary>
13
+ /// <remarks>
14
+ /// This class serves as an adapter layer between Model Context Protocol (MCP) types and the <see cref="AIContent"/> model types
15
+ /// from the Microsoft.Extensions.AI namespace.
16
+ /// </remarks>
11
17
public static class AIContentExtensions
12
18
{
13
- /// <summary>Creates a <see cref="ChatMessage"/> from a <see cref="PromptMessage"/>.</summary>
14
- /// <param name="promptMessage">The message to convert.</param>
15
- /// <returns>The created <see cref="ChatMessage"/>.</returns>
19
+ /// <summary>
20
+ /// Converts a <see cref="PromptMessage"/> to a <see cref="ChatMessage"/> object.
21
+ /// </summary>
22
+ /// <param name="promptMessage">The prompt message to convert.</param>
23
+ /// <returns>A <see cref="ChatMessage"/> object created from the prompt message.</returns>
24
+ /// <remarks>
25
+ /// This method transforms a protocol-specific <see cref="PromptMessage"/> from the Model Context Protocol
26
+ /// into a standard <see cref="ChatMessage"/> object that can be used with AI client libraries.
27
+ /// </remarks>
16
28
public static ChatMessage ToChatMessage ( this PromptMessage promptMessage )
17
29
{
18
30
Throw . IfNull ( promptMessage ) ;
@@ -25,19 +37,32 @@ public static ChatMessage ToChatMessage(this PromptMessage promptMessage)
25
37
} ;
26
38
}
27
39
28
- /// <summary>Creates <see cref="ChatMessage"/>s from a <see cref="GetPromptResult"/>.</summary>
29
- /// <param name="promptResult">The messages to convert.</param>
30
- /// <returns>The created <see cref="ChatMessage"/>.</returns>
40
+ /// <summary>
41
+ /// Converts a <see cref="GetPromptResult"/> to a list of <see cref="ChatMessage"/> objects.
42
+ /// </summary>
43
+ /// <param name="promptResult">The prompt result containing messages to convert.</param>
44
+ /// <returns>A list of <see cref="ChatMessage"/> objects created from the prompt messages.</returns>
45
+ /// <remarks>
46
+ /// This method transforms protocol-specific <see cref="PromptMessage"/> objects from a Model Context Protocol
47
+ /// prompt result into standard <see cref="ChatMessage"/> objects that can be used with AI client libraries.
48
+ /// </remarks>
31
49
public static IList < ChatMessage > ToChatMessages ( this GetPromptResult promptResult )
32
50
{
33
51
Throw . IfNull ( promptResult ) ;
34
52
35
53
return promptResult . Messages . Select ( m => m . ToChatMessage ( ) ) . ToList ( ) ;
36
54
}
37
55
38
- /// <summary>Gets <see cref="PromptMessage"/> instances for the specified <see cref="ChatMessage"/>.</summary>
39
- /// <param name="chatMessage">The message for which to extract its contents as <see cref="PromptMessage"/> instances.</param>
40
- /// <returns>The converted content.</returns>
56
+ /// <summary>
57
+ /// Converts a <see cref="ChatMessage"/> to a list of <see cref="PromptMessage"/> objects.
58
+ /// </summary>
59
+ /// <param name="chatMessage">The chat message to convert.</param>
60
+ /// <returns>A list of <see cref="PromptMessage"/> objects created from the chat message's contents.</returns>
61
+ /// <remarks>
62
+ /// This method transforms standard <see cref="ChatMessage"/> objects used with AI client libraries into
63
+ /// protocol-specific <see cref="PromptMessage"/> objects for the Model Context Protocol system.
64
+ /// Only representable content items are processed.
65
+ /// </remarks>
41
66
public static IList < PromptMessage > ToPromptMessages ( this ChatMessage chatMessage )
42
67
{
43
68
Throw . IfNull ( chatMessage ) ;
@@ -59,6 +84,10 @@ public static IList<PromptMessage> ToPromptMessages(this ChatMessage chatMessage
59
84
/// <summary>Creates a new <see cref="AIContent"/> from the content of a <see cref="Content"/>.</summary>
60
85
/// <param name="content">The <see cref="Content"/> to convert.</param>
61
86
/// <returns>The created <see cref="AIContent"/>.</returns>
87
+ /// <remarks>
88
+ /// This method converts Model Context Protocol content types to the equivalent Microsoft.Extensions.AI
89
+ /// content types, enabling seamless integration between the protocol and AI client libraries.
90
+ /// </remarks>
62
91
public static AIContent ToAIContent ( this Content content )
63
92
{
64
93
Throw . IfNull ( content ) ;
@@ -85,6 +114,10 @@ public static AIContent ToAIContent(this Content content)
85
114
/// <summary>Creates a new <see cref="AIContent"/> from the content of a <see cref="ResourceContents"/>.</summary>
86
115
/// <param name="content">The <see cref="ResourceContents"/> to convert.</param>
87
116
/// <returns>The created <see cref="AIContent"/>.</returns>
117
+ /// <remarks>
118
+ /// This method converts Model Context Protocol resource types to the equivalent Microsoft.Extensions.AI
119
+ /// content types, enabling seamless integration between the protocol and AI client libraries.
120
+ /// </remarks>
88
121
public static AIContent ToAIContent ( this ResourceContents content )
89
122
{
90
123
Throw . IfNull ( content ) ;
@@ -105,6 +138,17 @@ public static AIContent ToAIContent(this ResourceContents content)
105
138
/// <summary>Creates a list of <see cref="AIContent"/> from a sequence of <see cref="Content"/>.</summary>
106
139
/// <param name="contents">The <see cref="Content"/> instances to convert.</param>
107
140
/// <returns>The created <see cref="AIContent"/> instances.</returns>
141
+ /// <remarks>
142
+ /// <para>
143
+ /// This method converts a collection of Model Context Protocol content objects into a collection of
144
+ /// Microsoft.Extensions.AI content objects. It's useful when working with multiple content items, such as
145
+ /// when processing the contents of a message or response.
146
+ /// </para>
147
+ /// <para>
148
+ /// Each <see cref="Content"/> object is converted using <see cref="ToAIContent(Content)"/>,
149
+ /// preserving the type-specific conversion logic for text, images, audio, and resources.
150
+ /// </para>
151
+ /// </remarks>
108
152
public static IList < AIContent > ToAIContents ( this IEnumerable < Content > contents )
109
153
{
110
154
Throw . IfNull ( contents ) ;
@@ -114,7 +158,19 @@ public static IList<AIContent> ToAIContents(this IEnumerable<Content> contents)
114
158
115
159
/// <summary>Creates a list of <see cref="AIContent"/> from a sequence of <see cref="ResourceContents"/>.</summary>
116
160
/// <param name="contents">The <see cref="ResourceContents"/> instances to convert.</param>
117
- /// <returns>The created <see cref="AIContent"/> instances.</returns>
161
+ /// <returns>A list of <see cref="AIContent"/> objects created from the resource contents.</returns>
162
+ /// <remarks>
163
+ /// <para>
164
+ /// This method converts a collection of Model Context Protocol resource objects into a collection of
165
+ /// Microsoft.Extensions.AI content objects. It's useful when working with multiple resources, such as
166
+ /// when processing the contents of a <see cref="ReadResourceResult"/>.
167
+ /// </para>
168
+ /// <para>
169
+ /// Each <see cref="ResourceContents"/> object is converted using <see cref="ToAIContent(ResourceContents)"/>,
170
+ /// preserving the type-specific conversion logic: text resources become <see cref="TextContent"/> objects and
171
+ /// binary resources become <see cref="DataContent"/> objects.
172
+ /// </para>
173
+ /// </remarks>
118
174
public static IList < AIContent > ToAIContents ( this IEnumerable < ResourceContents > contents )
119
175
{
120
176
Throw . IfNull ( contents ) ;
0 commit comments