forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
494 lines (409 loc) · 22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using ModelContextProtocol;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Protocol.Types;
namespace MCPClient;
internal sealed partial class Program
{
public static async Task Main(string[] args)
{
await UseMCPToolsAsync();
await UseMCPPromptAsync();
await UseMCPResourcesAsync();
await UseMCPResourceTemplatesAsync();
await UseMCPSamplingAsync();
await UseChatCompletionAgentWithMCPToolsAsync();
await UseAzureAIAgentWithMCPToolsAsync();
}
/// <summary>
/// Demonstrates how to use the MCP resources with the Semantic Kernel.
/// The code in this method:
/// 1. Creates an MCP client.
/// 2. Retrieves the list of resources provided by the MCP server.
/// 3. Retrieves the `image://cat.jpg` resource content from the MCP server.
/// 4. Adds the image to the chat history and prompts the AI model to describe the content of the image.
/// </summary>
private static async Task UseMCPResourcesAsync()
{
Console.WriteLine($"Running the {nameof(UseMCPResourcesAsync)} sample.");
// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
// Retrieve list of resources provided by the MCP server and display them
IList<Resource> resources = await mcpClient.ListResourcesAsync();
DisplayResources(resources);
// Create a kernel
Kernel kernel = CreateKernelWithChatCompletionService();
// Enable automatic function calling
OpenAIPromptExecutionSettings executionSettings = new()
{
Temperature = 0,
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
};
// Retrieve the `image://cat.jpg` resource from the MCP server
ReadResourceResult resource = await mcpClient.ReadResourceAsync("image://cat.jpg");
// Add the resource to the chat history and prompt the AI model to describe the content of the image
ChatHistory chatHistory = [];
chatHistory.AddUserMessage(resource.ToChatMessageContentItemCollection());
chatHistory.AddUserMessage("Describe the content of the image?");
// Execute a prompt using the MCP resource and prompt added to the chat history
IChatCompletionService chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
ChatMessageContent result = await chatCompletion.GetChatMessageContentAsync(chatHistory, executionSettings, kernel);
Console.WriteLine(result);
Console.WriteLine();
// The expected output is: The image features a fluffy cat sitting in a lush, colorful garden.
// The garden is filled with various flowers and plants, creating a vibrant and serene atmosphere...
}
/// <summary>
/// Demonstrates how to use the MCP resource templates with the Semantic Kernel.
/// The code in this method:
/// 1. Creates an MCP client.
/// 2. Retrieves the list of resource templates provided by the MCP server.
/// 3. Reads relevant to the prompt records from the `vectorStore://records/{prompt}` MCP resource template.
/// 4. Adds the records to the chat history and prompts the AI model to explain what SK is.
/// </summary>
private static async Task UseMCPResourceTemplatesAsync()
{
Console.WriteLine($"Running the {nameof(UseMCPResourceTemplatesAsync)} sample.");
// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
// Retrieve list of resource templates provided by the MCP server and display them
IList<ResourceTemplate> resourceTemplates = await mcpClient.ListResourceTemplatesAsync();
DisplayResourceTemplates(resourceTemplates);
// Create a kernel
Kernel kernel = CreateKernelWithChatCompletionService();
// Enable automatic function calling
OpenAIPromptExecutionSettings executionSettings = new()
{
Temperature = 0,
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
};
string prompt = "What is the Semantic Kernel?";
// Retrieve relevant to the prompt records via MCP resource template
ReadResourceResult resource = await mcpClient.ReadResourceAsync($"vectorStore://records/{prompt}");
// Add the resource content/records to the chat history and prompt the AI model to explain what SK is
ChatHistory chatHistory = [];
chatHistory.AddUserMessage(resource.ToChatMessageContentItemCollection());
chatHistory.AddUserMessage(prompt);
// Execute a prompt using the MCP resource and prompt added to the chat history
IChatCompletionService chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
ChatMessageContent result = await chatCompletion.GetChatMessageContentAsync(chatHistory, executionSettings, kernel);
Console.WriteLine(result);
Console.WriteLine();
// The expected output is: The Semantic Kernel (SK) is a lightweight software development kit (SDK) designed for use in .NET applications.
// It acts as an orchestrator that facilitates interaction between AI models and available plugins, enabling them to work together to produce desired outputs.
}
/// <summary>
/// Demonstrates how to use the MCP tools with the Semantic Kernel.
/// The code in this method:
/// 1. Creates an MCP client.
/// 2. Retrieves the list of tools provided by the MCP server.
/// 3. Creates a kernel and registers the MCP tools as Kernel functions.
/// 4. Sends the prompt to AI model together with the MCP tools represented as Kernel functions.
/// 5. The AI model calls DateTimeUtils-GetCurrentDateTimeInUtc function to get the current date time in UTC required as an argument for the next function.
/// 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.
/// 7. Having received the weather information from the function call, the AI model returns the answer to the prompt.
/// </summary>
private static async Task UseMCPToolsAsync()
{
Console.WriteLine($"Running the {nameof(UseMCPToolsAsync)} sample.");
// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
// Retrieve and display the list provided by the MCP server
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
DisplayTools(tools);
// Create a kernel and register the MCP tools
Kernel kernel = CreateKernelWithChatCompletionService();
kernel.Plugins.AddFromFunctions("Tools", tools.Select(aiFunction => aiFunction.AsKernelFunction()));
// Enable automatic function calling
OpenAIPromptExecutionSettings executionSettings = new()
{
Temperature = 0,
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
};
string prompt = "What is the likely color of the sky in Boston today?";
Console.WriteLine(prompt);
// Execute a prompt using the MCP tools. The AI model will automatically call the appropriate MCP tools to answer the prompt.
FunctionResult result = await kernel.InvokePromptAsync(prompt, new(executionSettings));
Console.WriteLine(result);
Console.WriteLine();
// The expected output is: The likely color of the sky in Boston today is gray, as it is currently rainy.
}
/// <summary>
/// Demonstrates how to use the MCP prompt with the Semantic Kernel.
/// The code in this method:
/// 1. Creates an MCP client.
/// 2. Retrieves the list of prompts provided by the MCP server.
/// 3. Gets the current weather for Boston and Sydney using the `GetCurrentWeatherForCity` prompt.
/// 4. Adds the MCP server prompts to the chat history and prompts the AI model to compare the weather in the two cities and suggest the best place to go for a walk.
/// 5. After receiving and processing the weather data for both cities and the prompt, the AI model returns an answer.
/// </summary>
private static async Task UseMCPPromptAsync()
{
Console.WriteLine($"Running the {nameof(UseMCPPromptAsync)} sample.");
// Create an MCP client
await using IMcpClient mcpClient = await CreateMcpClientAsync();
// Retrieve and display the list of prompts provided by the MCP server
IList<McpClientPrompt> prompts = await mcpClient.ListPromptsAsync();
DisplayPrompts(prompts);
// Create a kernel
Kernel kernel = CreateKernelWithChatCompletionService();
// Get weather for Boston using the `GetCurrentWeatherForCity` prompt from the MCP server
GetPromptResult bostonWeatherPrompt = await mcpClient.GetPromptAsync("GetCurrentWeatherForCity", new Dictionary<string, object?>() { ["city"] = "Boston", ["time"] = DateTime.UtcNow.ToString() });
// Get weather for Sydney using the `GetCurrentWeatherForCity` prompt from the MCP server
GetPromptResult sydneyWeatherPrompt = await mcpClient.GetPromptAsync("GetCurrentWeatherForCity", new Dictionary<string, object?>() { ["city"] = "Sydney", ["time"] = DateTime.UtcNow.ToString() });
// Add the prompts to the chat history
ChatHistory chatHistory = [];
chatHistory.AddRange(bostonWeatherPrompt.ToChatMessageContents());
chatHistory.AddRange(sydneyWeatherPrompt.ToChatMessageContents());
chatHistory.AddUserMessage("Compare the weather in the two cities and suggest the best place to go for a walk.");
// Execute a prompt using the MCP tools and prompt
IChatCompletionService chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
ChatMessageContent result = await chatCompletion.GetChatMessageContentAsync(chatHistory, kernel: kernel);
Console.WriteLine(result);
Console.WriteLine();
// The expected output is: Given these conditions, Sydney would be the better choice for a pleasant walk, as the sunny and warm weather is ideal for outdoor activities.
// The rain in Boston could make walking less enjoyable and potentially inconvenient.
}
/// <summary>
/// Demonstrates how to use the MCP sampling with the Semantic Kernel.
/// The code in this method:
/// 1. Creates an MCP client and register the sampling request handler.
/// 2. Retrieves the list of tools provided by the MCP server and registers them as Kernel functions.
/// 3. Prompts the AI model to create a schedule based on the latest unread emails in the mailbox.
/// 4. The AI model calls the `MailboxUtils-SummarizeUnreadEmails` function to summarize the unread emails.
/// 5. The `MailboxUtils-SummarizeUnreadEmails` function creates a few sample emails with attachments and
/// sends a sampling request to the client to summarize them:
/// 5.1. The client receive sampling request from server and invokes the sampling request handler.
/// 5.2. SK intercepts the sampling request invocation via `HumanInTheLoopFilter` filter to enable human-in-the-loop processing.
/// 5.3. The `HumanInTheLoopFilter` allows invocation of the sampling request handler.
/// 5.5. The sampling request handler sends the sampling request to the AI model to summarize the emails.
/// 5.6. The AI model processes the request and returns the summary to the handler which sends it back to the server.
/// 5.7. The `MailboxUtils-SummarizeUnreadEmails` function receives the result and returns it to the AI model.
/// 7. Having received the summary, the AI model creates a schedule based on the unread emails.
/// </summary>
private static async Task UseMCPSamplingAsync()
{
Console.WriteLine($"Running the {nameof(UseMCPSamplingAsync)} sample.");
// Create a kernel
Kernel kernel = CreateKernelWithChatCompletionService();
// Register the human-in-the-loop filter that intercepts function calls allowing users to review and approve or reject them
kernel.FunctionInvocationFilters.Add(new HumanInTheLoopFilter());
// Create an MCP client with a custom sampling request handler
await using IMcpClient mcpClient = await CreateMcpClientAsync(kernel, SamplingRequestHandlerAsync);
// Import MCP tools as Kernel functions so AI model can call them
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();
kernel.Plugins.AddFromFunctions("Tools", tools.Select(aiFunction => aiFunction.AsKernelFunction()));
// Enable automatic function calling
OpenAIPromptExecutionSettings executionSettings = new()
{
Temperature = 0,
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
};
// Execute a prompt
string prompt = "Create a schedule for me based on the latest unread emails in my inbox.";
IChatCompletionService chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
ChatMessageContent result = await chatCompletion.GetChatMessageContentAsync(prompt, executionSettings, kernel);
Console.WriteLine(result);
Console.WriteLine();
// The expected output is:
// ### Today
// - **Review Sales Report:**
// - **Task:** Provide feedback on the Carretera Sales Report for January to June 2014.
// - **Deadline:** End of the day.
// - **Details:** Check the attached spreadsheet for sales data.
//
// ### Tomorrow
// - **Update Employee Information:**
// - **Task:** Update the list of employee birthdays and positions.
// - **Deadline:** By the end of the day.
// - **Details:** Refer to the attached table for employee details.
//
// ### Saturday
// - **Attend BBQ:**
// - **Event:** BBQ Invitation
// - **Details:** Join the BBQ as mentioned in the sales report email.
//
// ### Sunday
// - **Join Hike:**
// - **Event:** Hiking Invitation
// - **Details:** Participate in the hike as mentioned in the HR email.
}
/// <summary>
/// Creates an instance of <see cref="Kernel"/> with the OpenAI chat completion service registered.
/// </summary>
/// <returns>An instance of <see cref="Kernel"/>.</returns>
private static Kernel CreateKernelWithChatCompletionService()
{
// Load and validate configuration
IConfigurationRoot config = new ConfigurationBuilder()
.AddUserSecrets<Program>()
.AddEnvironmentVariables()
.Build();
if (config["OpenAI:ApiKey"] is not { } apiKey)
{
const string Message = "Please provide a valid OpenAI:ApiKey to run this sample. See the associated README.md for more details.";
Console.Error.WriteLine(Message);
throw new InvalidOperationException(Message);
}
string modelId = config["OpenAI:ChatModelId"] ?? "gpt-4o-mini";
// Create kernel
var kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.Services.AddOpenAIChatCompletion(serviceId: "openai", modelId: modelId, apiKey: apiKey);
return kernelBuilder.Build();
}
/// <summary>
/// Creates an MCP client and connects it to the MCPServer server.
/// </summary>
/// <param name="kernel">Optional kernel instance to use for the MCP client.</param>
/// <param name="samplingRequestHandler">Optional handler for MCP sampling requests.</param>
/// <returns>An instance of <see cref="IMcpClient"/>.</returns>
private static Task<IMcpClient> CreateMcpClientAsync(
Kernel? kernel = null,
Func<Kernel, CreateMessageRequestParams?, IProgress<ProgressNotificationValue>, CancellationToken, Task<CreateMessageResult>>? samplingRequestHandler = null)
{
KernelFunction? skSamplingHandler = null;
// Create and return the MCP client
return McpClientFactory.CreateAsync(
clientTransport: new StdioClientTransport(new StdioClientTransportOptions
{
Name = "MCPServer",
Command = GetMCPServerPath(), // Path to the MCPServer executable
}),
clientOptions: samplingRequestHandler != null ? new McpClientOptions()
{
Capabilities = new ClientCapabilities
{
Sampling = new SamplingCapability
{
SamplingHandler = InvokeHandlerAsync
},
},
} : null
);
async ValueTask<CreateMessageResult> InvokeHandlerAsync(CreateMessageRequestParams? request, IProgress<ProgressNotificationValue> progress, CancellationToken cancellationToken)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
skSamplingHandler ??= KernelFunctionFactory.CreateFromMethod(
(CreateMessageRequestParams? request, IProgress<ProgressNotificationValue> progress, CancellationToken ct) =>
{
return samplingRequestHandler(kernel!, request, progress, ct);
},
"MCPSamplingHandler"
);
// The argument names must match the parameter names of the delegate the SK Function is created from
KernelArguments kernelArguments = new()
{
["request"] = request,
["progress"] = progress
};
FunctionResult functionResult = await skSamplingHandler.InvokeAsync(kernel!, kernelArguments, cancellationToken);
return functionResult.GetValue<CreateMessageResult>()!;
}
}
/// <summary>
/// Handles sampling requests from the MCP client.
/// </summary>
/// <param name="kernel">The kernel instance.</param>
/// <param name="request">The sampling request.</param>
/// <param name="progress">The progress notification.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The result of the sampling request.</returns>
private static async Task<CreateMessageResult> SamplingRequestHandlerAsync(Kernel kernel, CreateMessageRequestParams? request, IProgress<ProgressNotificationValue> progress, CancellationToken cancellationToken)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
// Map the MCP sampling request to the Semantic Kernel prompt execution settings
OpenAIPromptExecutionSettings promptExecutionSettings = new()
{
Temperature = request.Temperature,
MaxTokens = request.MaxTokens,
StopSequences = request.StopSequences?.ToList(),
};
// Create a chat history from the MCP sampling request
ChatHistory chatHistory = [];
if (!string.IsNullOrEmpty(request.SystemPrompt))
{
chatHistory.AddSystemMessage(request.SystemPrompt);
}
chatHistory.AddRange(request.Messages.ToChatMessageContents());
// Prompt the AI model to generate a response
IChatCompletionService chatCompletion = kernel.GetRequiredService<IChatCompletionService>();
ChatMessageContent result = await chatCompletion.GetChatMessageContentAsync(chatHistory, promptExecutionSettings, cancellationToken: cancellationToken);
return result.ToCreateMessageResult();
}
/// <summary>
/// Returns the path to the MCPServer server executable.
/// </summary>
/// <returns>The path to the MCPServer server executable.</returns>
private static string GetMCPServerPath()
{
// Determine the configuration (Debug or Release)
string configuration;
#if DEBUG
configuration = "Debug";
#else
configuration = "Release";
#endif
return Path.Combine("..", "..", "..", "..", "MCPServer", "bin", configuration, "net8.0", "MCPServer.exe");
}
/// <summary>
/// Displays the list of available MCP tools.
/// </summary>
/// <param name="tools">The list of the tools to display.</param>
private static void DisplayTools(IList<McpClientTool> tools)
{
Console.WriteLine("Available MCP tools:");
foreach (var tool in tools)
{
Console.WriteLine($"- Name: {tool.Name}, Description: {tool.Description}");
}
Console.WriteLine();
}
/// <summary>
/// Displays the list of available MCP prompts.
/// </summary>
/// <param name="prompts">The list of the prompts to display.</param>
private static void DisplayPrompts(IList<McpClientPrompt> prompts)
{
Console.WriteLine("Available MCP prompts:");
foreach (var prompt in prompts)
{
Console.WriteLine($"- Name: {prompt.Name}, Description: {prompt.Description}");
}
Console.WriteLine();
}
private static void DisplayResources(IList<Resource> resources)
{
Console.WriteLine("Available MCP resources:");
foreach (var resource in resources)
{
Console.WriteLine($"- Name: {resource.Name}, Uri: {resource.Uri}, Description: {resource.Description}");
}
Console.WriteLine();
}
private static void DisplayResourceTemplates(IList<ResourceTemplate> resourceTemplates)
{
Console.WriteLine("Available MCP resource templates:");
foreach (var template in resourceTemplates)
{
Console.WriteLine($"- Name: {template.Name}, Description: {template.Description}");
}
Console.WriteLine();
}
}