chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UserSecretsId>5ee045b0-aea3-4f08-8d31-32d1a6f8fed0</UserSecretsId>
|
||||
<NoWarn>$(NoWarn);CA2249;CS0612;SKEXP0001;VSTHRD111;CA2007</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ModelContextProtocol" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Agents\Abstractions\Agents.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Agents\Core\Agents.Core.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.Agents;
|
||||
using Microsoft.SemanticKernel.Connectors.OpenAI;
|
||||
using ModelContextProtocol.Client;
|
||||
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddUserSecrets<Program>()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
if (config["OpenAI:ApiKey"] is not { } apiKey)
|
||||
{
|
||||
Console.Error.WriteLine("Please provide a valid OpenAI:ApiKey to run this sample. See the associated README.md for more details.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create an MCPClient for the GitHub server
|
||||
var mcpClient = await McpClient.CreateAsync(new StdioClientTransport(new()
|
||||
{
|
||||
Name = "MCPServer",
|
||||
Command = "npx",
|
||||
Arguments = ["-y", "@modelcontextprotocol/server-github"],
|
||||
}));
|
||||
|
||||
// Retrieve the list of tools available on the GitHub server
|
||||
var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
Console.WriteLine($"{tool.Name}: {tool.Description}");
|
||||
}
|
||||
|
||||
// Prepare and build kernel with the MCP tools as Kernel functions
|
||||
var builder = Kernel.CreateBuilder();
|
||||
builder.Services
|
||||
.AddLogging(c => c.AddDebug().SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace))
|
||||
.AddOpenAIChatCompletion(
|
||||
modelId: config["OpenAI:ChatModelId"] ?? "gpt-4o-mini",
|
||||
apiKey: apiKey);
|
||||
Kernel kernel = builder.Build();
|
||||
kernel.Plugins.AddFromFunctions("GitHub", tools.Select(aiFunction => aiFunction.AsKernelFunction()));
|
||||
|
||||
// Enable automatic function calling
|
||||
OpenAIPromptExecutionSettings executionSettings = new()
|
||||
{
|
||||
Temperature = 0,
|
||||
FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
|
||||
};
|
||||
|
||||
// Test using GitHub tools
|
||||
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 GitHub repositories.",
|
||||
Name = "GitHubAgent",
|
||||
Kernel = kernel,
|
||||
Arguments = new KernelArguments(executionSettings),
|
||||
};
|
||||
|
||||
// Respond to user input, invoking functions where appropriate.
|
||||
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}");
|
||||
@@ -0,0 +1,46 @@
|
||||
# Model Context Protocol Sample
|
||||
|
||||
This example demonstrates how to use Model Context Protocol tools with Semantic Kernel.
|
||||
|
||||
MCP is an open protocol that standardizes how applications provide context to LLMs.
|
||||
|
||||
For information on Model Context Protocol (MCP) please refer to the [documentation](https://modelcontextprotocol.io/introduction).
|
||||
|
||||
The sample shows:
|
||||
|
||||
1. How to connect to an MCP Server using [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol)
|
||||
2. Retrieve the list of tools the MCP Server makes available
|
||||
3. Convert the MCP tools to Semantic Kernel functions so they can be added to a Kernel instance
|
||||
4. Invoke the tools from Semantic Kernel using function calling
|
||||
|
||||
## Installing Prerequisites
|
||||
|
||||
The sample requires node.js and npm to be installed. So, please install them from [here](https://nodejs.org/en/download/).
|
||||
|
||||
## Configuring Secrets or Environment Variables
|
||||
|
||||
The example require credentials to access OpenAI.
|
||||
|
||||
If you have set up those credentials as secrets within Secret Manager or through environment variables for other samples from the solution in which this project is found, they will be re-used.
|
||||
|
||||
### To set your secrets with Secret Manager
|
||||
|
||||
```text
|
||||
cd dotnet/samples/Demos/ModelContextProtocolPlugin
|
||||
|
||||
dotnet user-secrets init
|
||||
|
||||
dotnet user-secrets set "OpenAI:ChatModelId" "..."
|
||||
dotnet user-secrets set "OpenAI:ApiKey" "..."
|
||||
"..."
|
||||
```
|
||||
|
||||
### To set your secrets with environment variables
|
||||
|
||||
Use these names:
|
||||
|
||||
```text
|
||||
# OpenAI
|
||||
OpenAI__ChatModelId
|
||||
OpenAI__ApiKey
|
||||
```
|
||||
Reference in New Issue
Block a user