You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Motivation, Context and Description
This PR adds sample showing how to power MCP prompt by SK prompt server
side and consume this prompt client side.
Contributes to:
#11199
---------
Co-authored-by: westey <[email protected]>
Copy file name to clipboardExpand all lines: dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient/Program.cs
+96
Original file line number
Diff line number
Diff line change
@@ -7,17 +7,41 @@
7
7
usingSystem.Threading.Tasks;
8
8
usingMicrosoft.Extensions.Configuration;
9
9
usingMicrosoft.SemanticKernel;
10
+
usingMicrosoft.SemanticKernel.ChatCompletion;
10
11
usingMicrosoft.SemanticKernel.Connectors.OpenAI;
11
12
usingModelContextProtocol;
12
13
usingModelContextProtocol.Client;
13
14
usingModelContextProtocol.Protocol.Transport;
15
+
usingModelContextProtocol.Protocol.Types;
14
16
15
17
namespaceMCPClient;
16
18
17
19
internalsealedclassProgram
18
20
{
19
21
publicstaticasyncTaskMain(string[]args)
20
22
{
23
+
// Use the MCP tools with the Semantic Kernel
24
+
awaitUseMCPToolsWithSKAsync();
25
+
26
+
// Use the MCP tools and MCP prompt with the Semantic Kernel
27
+
awaitUseMCPToolsAndPromptWithSKAsync();
28
+
}
29
+
30
+
/// <summary>
31
+
/// Demonstrates how to use the MCP tools with the Semantic Kernel.
32
+
/// The code in this method:
33
+
/// 1. Creates an MCP client.
34
+
/// 2. Retrieves the list of tools provided by the MCP server.
35
+
/// 3. Creates a kernel and registers the MCP tools as Kernel functions.
36
+
/// 4. Sends the prompt to AI model together with the MCP tools represented as Kernel functions.
37
+
/// 5. The AI model calls DateTimeUtils-GetCurrentDateTimeInUtc function to get the current date time in UTC required as an argument for the next function.
38
+
/// 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
+
/// 7. Having received the weather information from the function call, the AI model returns the answer to the prompt.
40
+
/// </summary>
41
+
privatestaticasyncTaskUseMCPToolsWithSKAsync()
42
+
{
43
+
Console.WriteLine($"Running the {nameof(UseMCPToolsWithSKAsync)} sample.");
// The expected output is: The likely color of the sky in Boston today is gray, as it is currently rainy.
48
73
}
49
74
75
+
/// <summary>
76
+
/// Demonstrates how to use the MCP tools and MCP prompt with the Semantic Kernel.
77
+
/// The code in this method:
78
+
/// 1. Creates an MCP client.
79
+
/// 2. Retrieves the list of tools provided by the MCP server.
80
+
/// 3. Retrieves the list of prompts provided by the MCP server.
81
+
/// 4. Creates a kernel and registers the MCP tools as Kernel functions.
82
+
/// 5. Requests the `GetCurrentWeatherForCity` prompt from the MCP server.
83
+
/// 6. The MCP server renders the prompt using the `Boston` as value for the `city` parameter and the result of the `DateTimeUtils-GetCurrentDateTimeInUtc` server-side invocation added to the prompt as part of prompt rendering.
84
+
/// 7. Converts the MCP server prompt: list of messages where each message is represented by content and role to a chat history.
85
+
/// 8. Sends the chat history to the AI model together with the MCP tools represented as Kernel functions.
86
+
/// 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
+
/// 10. Having received the weather information from the function call, the AI model returns the answer to the prompt.
0 commit comments