From d4e767d11e02a524851724c1c3c4479096d7d6c3 Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Thu, 3 Apr 2025 14:16:14 +0100 Subject: [PATCH 1/4] Some changes to add clarity and align with the Blog Post --- .../Demos/ModelContextProtocolPlugin/Program.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs index c6c53704aa17..989df9856be6 100644 --- a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs +++ b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs @@ -5,7 +5,9 @@ using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Connectors.OpenAI; +using ModelContextProtocol; using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol.Types; var config = new ConfigurationBuilder() .AddUserSecrets() @@ -20,7 +22,7 @@ // Create an MCPClient for the GitHub server await using var mcpClient = await McpClientFactory.CreateAsync( - new() + new McpServerConfig() { Id = "github", Name = "GitHub", @@ -31,7 +33,14 @@ ["arguments"] = "-y @modelcontextprotocol/server-github", } }, - new() { ClientInfo = new() { Name = "GitHub", Version = "1.0.0" } }).ConfigureAwait(false); + new McpClientOptions() + { + ClientInfo = new Implementation() + { + Name = "GitHub", + Version = "1.0.0" + } + }).ConfigureAwait(false); // Retrieve the list of tools available on the GitHub server var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false); @@ -45,7 +54,6 @@ builder.Services .AddLogging(c => c.AddDebug().SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace)) .AddOpenAIChatCompletion( - serviceId: "openai", modelId: config["OpenAI:ChatModelId"] ?? "gpt-4o-mini", apiKey: apiKey); Kernel kernel = builder.Build(); From 5a2d9dd0c5e5d5dec12b8668129d9a54ff5f38da Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Thu, 3 Apr 2025 15:14:01 +0100 Subject: [PATCH 2/4] Extend sample to show how to use MCP Tools from an Agent --- .../ModelContextProtocolPlugin.csproj | 2 ++ .../ModelContextProtocolPlugin/Program.cs | 21 +++++++++++++++++++ .../GettingStartedWithAgents.csproj | 1 + 3 files changed, 24 insertions(+) diff --git a/dotnet/samples/Demos/ModelContextProtocolPlugin/ModelContextProtocolPlugin.csproj b/dotnet/samples/Demos/ModelContextProtocolPlugin/ModelContextProtocolPlugin.csproj index 20d6d6b81dbc..8b872e1db766 100644 --- a/dotnet/samples/Demos/ModelContextProtocolPlugin/ModelContextProtocolPlugin.csproj +++ b/dotnet/samples/Demos/ModelContextProtocolPlugin/ModelContextProtocolPlugin.csproj @@ -19,6 +19,8 @@ + + diff --git a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs index 989df9856be6..07a21b4bb013 100644 --- a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs +++ b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel; +using Microsoft.SemanticKernel.Agents; using Microsoft.SemanticKernel.Connectors.OpenAI; using ModelContextProtocol; using ModelContextProtocol.Client; @@ -70,3 +71,23 @@ var prompt = "Summarize the last four commits to the microsoft/semantic-kernel repository?"; var result = await kernel.InvokePromptAsync(prompt, new(executionSettings)).ConfigureAwait(false); Console.WriteLine($"\n\n{prompt}\n{result}"); + +// Define the agent +ChatCompletionAgent agent = +new() +{ + Instructions = "Answer questions about GitGub repositories.", + Name = "GitHubAgent", + Kernel = kernel, + Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }), +}; + +/// Create the chat history thread to capture the agent interaction. +AgentThread thread = new ChatHistoryAgentThread(); + +// Respond to user input, invoking functions where appropriate. +Console.WriteLine($"\n\nResponse from GitHubAgent:"); +await foreach (ChatMessageContent response in agent.InvokeAsync("Summarize the last four commits to the microsoft/semantic-kernel repository?", thread)) +{ + Console.WriteLine(response.Content); +} diff --git a/dotnet/samples/GettingStartedWithAgents/GettingStartedWithAgents.csproj b/dotnet/samples/GettingStartedWithAgents/GettingStartedWithAgents.csproj index ffc4734e10d6..d3dafa2f7c52 100644 --- a/dotnet/samples/GettingStartedWithAgents/GettingStartedWithAgents.csproj +++ b/dotnet/samples/GettingStartedWithAgents/GettingStartedWithAgents.csproj @@ -17,6 +17,7 @@ + From 74184e50d8f6f3848e17346b182431d02d4edd08 Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Fri, 4 Apr 2025 11:37:49 +0100 Subject: [PATCH 3/4] Update to latest Agent pattern --- .../Demos/ModelContextProtocolPlugin/Program.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs index b9523fd692a1..1df3148888db 100644 --- a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs +++ b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs @@ -73,8 +73,7 @@ Console.WriteLine($"\n\n{prompt}\n{result}"); // Define the agent -ChatCompletionAgent agent = -new() +ChatCompletionAgent agent = new() { Instructions = "Answer questions about GitGub repositories.", Name = "GitHubAgent", @@ -82,12 +81,6 @@ Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }), }; -/// Create the chat history thread to capture the agent interaction. -AgentThread thread = new ChatHistoryAgentThread(); - // Respond to user input, invoking functions where appropriate. -Console.WriteLine("\n\nResponse from GitHubAgent:"); -await foreach (ChatMessageContent response in agent.InvokeAsync("Summarize the last four commits to the microsoft/semantic-kernel repository?", thread)) -{ - Console.WriteLine(response.Content); -} +ChatMessageContent response = await agent.InvokeAsync("Summarize the last four commits to the microsoft/semantic-kernel repository?").FirstAsync(); +Console.WriteLine($"\n\nResponse from GitHubAgent:\n{response.Content}"); From c495bdc8dacb82f05178586d5c24b3b9a44f2107 Mon Sep 17 00:00:00 2001 From: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Date: Fri, 4 Apr 2025 18:43:08 +0100 Subject: [PATCH 4/4] Update dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> --- dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs index 1df3148888db..6bca66c66b3f 100644 --- a/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs +++ b/dotnet/samples/Demos/ModelContextProtocolPlugin/Program.cs @@ -75,7 +75,7 @@ // Define the agent ChatCompletionAgent agent = new() { - Instructions = "Answer questions about GitGub repositories.", + Instructions = "Answer questions about GitHub repositories.", Name = "GitHubAgent", Kernel = kernel, Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),