@@ -20,11 +20,113 @@ internal sealed class Program
20
20
{
21
21
public static async Task Main ( string [ ] args )
22
22
{
23
- // Use the MCP tools with the Semantic Kernel
24
- await UseMCPToolsWithSKAsync ( ) ;
23
+ await UseMCPToolsAsync ( ) ;
25
24
26
- // Use the MCP tools and MCP prompt with the Semantic Kernel
27
- await UseMCPToolsAndPromptWithSKAsync ( ) ;
25
+ await UseMCPToolsAndPromptAsync ( ) ;
26
+
27
+ await UseMCPResourcesAsync ( ) ;
28
+
29
+ await UseMCPResourceTemplatesAsync ( ) ;
30
+ }
31
+
32
+ /// <summary>
33
+ /// Demonstrates how to use the MCP resources with the Semantic Kernel.
34
+ /// The code in this method:
35
+ /// 1. Creates an MCP client.
36
+ /// 2. Retrieves the list of resources provided by the MCP server.
37
+ /// 3. Retrieves the `image://cat.jpg` resource content from the MCP server.
38
+ /// 4. Adds the image to the chat history and prompts the AI model to describe the content of the image.
39
+ /// </summary>
40
+ private static async Task UseMCPResourcesAsync ( )
41
+ {
42
+ Console . WriteLine ( $ "Running the { nameof ( UseMCPResourcesAsync ) } sample.") ;
43
+
44
+ // Create an MCP client
45
+ await using IMcpClient mcpClient = await CreateMcpClientAsync ( ) ;
46
+
47
+ // Retrieve list of resources provided by the MCP server and display them
48
+ IList < Resource > resources = await mcpClient . ListResourcesAsync ( ) ;
49
+ DisplayResources ( resources ) ;
50
+
51
+ // Create a kernel
52
+ Kernel kernel = CreateKernelWithChatCompletionService ( ) ;
53
+
54
+ // Enable automatic function calling
55
+ OpenAIPromptExecutionSettings executionSettings = new ( )
56
+ {
57
+ Temperature = 0 ,
58
+ FunctionChoiceBehavior = FunctionChoiceBehavior . Auto ( options : new ( ) { RetainArgumentTypes = true } )
59
+ } ;
60
+
61
+ // Retrieve the `image://cat.jpg` resource from the MCP server
62
+ ReadResourceResult resource = await mcpClient . ReadResourceAsync ( "image://cat.jpg" ) ;
63
+
64
+ // Add the resource to the chat history and prompt the AI model to describe the content of the image
65
+ ChatHistory chatHistory = [ ] ;
66
+ chatHistory . AddUserMessage ( resource . ToChatMessageContentItemCollection ( ) ) ;
67
+ chatHistory . AddUserMessage ( "Describe the content of the image?" ) ;
68
+
69
+ // Execute a prompt using the MCP resource and prompt added to the chat history
70
+ IChatCompletionService chatCompletion = kernel . GetRequiredService < IChatCompletionService > ( ) ;
71
+
72
+ ChatMessageContent result = await chatCompletion . GetChatMessageContentAsync ( chatHistory , executionSettings , kernel ) ;
73
+
74
+ Console . WriteLine ( result ) ;
75
+ Console . WriteLine ( ) ;
76
+
77
+ // The expected output is: The image features a fluffy cat sitting in a lush, colorful garden.
78
+ // The garden is filled with various flowers and plants, creating a vibrant and serene atmosphere...
79
+ }
80
+
81
+ /// <summary>
82
+ /// Demonstrates how to use the MCP resource templates with the Semantic Kernel.
83
+ /// The code in this method:
84
+ /// 1. Creates an MCP client.
85
+ /// 2. Retrieves the list of resource templates provided by the MCP server.
86
+ /// 3. Reads relevant to the prompt records from the `vectorStore://records/{prompt}` MCP resource template.
87
+ /// 4. Adds the records to the chat history and prompts the AI model to explain what SK is.
88
+ /// </summary>
89
+ private static async Task UseMCPResourceTemplatesAsync ( )
90
+ {
91
+ Console . WriteLine ( $ "Running the { nameof ( UseMCPResourceTemplatesAsync ) } sample.") ;
92
+
93
+ // Create an MCP client
94
+ await using IMcpClient mcpClient = await CreateMcpClientAsync ( ) ;
95
+
96
+ // Retrieve list of resource templates provided by the MCP server and display them
97
+ IList < ResourceTemplate > resourceTemplates = await mcpClient . ListResourceTemplatesAsync ( ) ;
98
+ DisplayResourceTemplates ( resourceTemplates ) ;
99
+
100
+ // Create a kernel
101
+ Kernel kernel = CreateKernelWithChatCompletionService ( ) ;
102
+
103
+ // Enable automatic function calling
104
+ OpenAIPromptExecutionSettings executionSettings = new ( )
105
+ {
106
+ Temperature = 0 ,
107
+ FunctionChoiceBehavior = FunctionChoiceBehavior . Auto ( options : new ( ) { RetainArgumentTypes = true } )
108
+ } ;
109
+
110
+ string prompt = "What is the Semantic Kernel?" ;
111
+
112
+ // Retrieve relevant to the prompt records via MCP resource template
113
+ ReadResourceResult resource = await mcpClient . ReadResourceAsync ( $ "vectorStore://records/{ prompt } ") ;
114
+
115
+ // Add the resource content/records to the chat history and prompt the AI model to explain what SK is
116
+ ChatHistory chatHistory = [ ] ;
117
+ chatHistory . AddUserMessage ( resource . ToChatMessageContentItemCollection ( ) ) ;
118
+ chatHistory . AddUserMessage ( prompt ) ;
119
+
120
+ // Execute a prompt using the MCP resource and prompt added to the chat history
121
+ IChatCompletionService chatCompletion = kernel . GetRequiredService < IChatCompletionService > ( ) ;
122
+
123
+ ChatMessageContent result = await chatCompletion . GetChatMessageContentAsync ( chatHistory , executionSettings , kernel ) ;
124
+
125
+ Console . WriteLine ( result ) ;
126
+ Console . WriteLine ( ) ;
127
+
128
+ // The expected output is: The Semantic Kernel (SK) is a lightweight software development kit (SDK) designed for use in .NET applications.
129
+ // It acts as an orchestrator that facilitates interaction between AI models and available plugins, enabling them to work together to produce desired outputs.
28
130
}
29
131
30
132
/// <summary>
@@ -38,9 +140,9 @@ public static async Task Main(string[] args)
38
140
/// 6. The AI model calls WeatherUtils-GetWeatherForCity function with the current date time and the `Boston` arguments extracted from the prompt to get the weather information.
39
141
/// 7. Having received the weather information from the function call, the AI model returns the answer to the prompt.
40
142
/// </summary>
41
- private static async Task UseMCPToolsWithSKAsync ( )
143
+ private static async Task UseMCPToolsAsync ( )
42
144
{
43
- Console . WriteLine ( $ "Running the { nameof ( UseMCPToolsWithSKAsync ) } sample.") ;
145
+ Console . WriteLine ( $ "Running the { nameof ( UseMCPToolsAsync ) } sample.") ;
44
146
45
147
// Create an MCP client
46
148
await using IMcpClient mcpClient = await CreateMcpClientAsync ( ) ;
@@ -86,9 +188,9 @@ private static async Task UseMCPToolsWithSKAsync()
86
188
/// 9. The AI model calls WeatherUtils-GetWeatherForCity function with the current date time and the `Boston` arguments extracted from the prompt to get the weather information.
87
189
/// 10. Having received the weather information from the function call, the AI model returns the answer to the prompt.
88
190
/// </summary>
89
- private static async Task UseMCPToolsAndPromptWithSKAsync ( )
191
+ private static async Task UseMCPToolsAndPromptAsync ( )
90
192
{
91
- Console . WriteLine ( $ "Running the { nameof ( UseMCPToolsAndPromptWithSKAsync ) } sample.") ;
193
+ Console . WriteLine ( $ "Running the { nameof ( UseMCPToolsAndPromptAsync ) } sample.") ;
92
194
93
195
// Create an MCP client
94
196
await using IMcpClient mcpClient = await CreateMcpClientAsync ( ) ;
@@ -208,7 +310,7 @@ private static void DisplayTools(IList<McpClientTool> tools)
208
310
Console . WriteLine ( "Available MCP tools:" ) ;
209
311
foreach ( var tool in tools )
210
312
{
211
- Console . WriteLine ( $ "- { tool . Name } : { tool . Description } ") ;
313
+ Console . WriteLine ( $ "- Name: { tool . Name } , Description : { tool . Description } ") ;
212
314
}
213
315
Console . WriteLine ( ) ;
214
316
}
@@ -222,7 +324,27 @@ private static void DisplayPrompts(IList<McpClientPrompt> prompts)
222
324
Console . WriteLine ( "Available MCP prompts:" ) ;
223
325
foreach ( var prompt in prompts )
224
326
{
225
- Console . WriteLine ( $ "- { prompt . Name } : { prompt . Description } ") ;
327
+ Console . WriteLine ( $ "- Name: { prompt . Name } , Description: { prompt . Description } ") ;
328
+ }
329
+ Console . WriteLine ( ) ;
330
+ }
331
+
332
+ private static void DisplayResources ( IList < Resource > resources )
333
+ {
334
+ Console . WriteLine ( "Available MCP resources:" ) ;
335
+ foreach ( var resource in resources )
336
+ {
337
+ Console . WriteLine ( $ "- Name: { resource . Name } , Uri: { resource . Uri } , Description: { resource . Description } ") ;
338
+ }
339
+ Console . WriteLine ( ) ;
340
+ }
341
+
342
+ private static void DisplayResourceTemplates ( IList < ResourceTemplate > resourceTemplates )
343
+ {
344
+ Console . WriteLine ( "Available MCP resource templates:" ) ;
345
+ foreach ( var template in resourceTemplates )
346
+ {
347
+ Console . WriteLine ( $ "- Name: { template . Name } , Description: { template . Description } ") ;
226
348
}
227
349
Console . WriteLine ( ) ;
228
350
}
0 commit comments