Simplify client samples (#14)
- Remove duplicate projects - Replace client sample with a simple chat app
This commit is contained in:
parent
33b9582199
commit
3e4dd011a1
2 changed files with 61 additions and 304 deletions
21
samples/ChatWithTools/ChatWithTools.csproj
Normal file
21
samples/ChatWithTools/ChatWithTools.csproj
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.AI" />
|
||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
|
||||
<PackageReference Include="Anthropic.SDK" />
|
||||
<PackageReference Include="System.Linq.AsyncEnumerable" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
52
samples/ChatWithTools/Program.cs
Normal file
52
samples/ChatWithTools/Program.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using Microsoft.Extensions.AI;
|
||||
using OpenAI;
|
||||
|
||||
// Connect to an MCP server
|
||||
Console.WriteLine("Connecting client to MCP 'everything' server");
|
||||
var mcpClient = await McpClientFactory.CreateAsync(
|
||||
new()
|
||||
{
|
||||
Id = "everything",
|
||||
Name = "Everything",
|
||||
TransportType = TransportTypes.StdIo,
|
||||
TransportOptions = new()
|
||||
{
|
||||
["command"] = "npx", ["arguments"] = "-y @modelcontextprotocol/server-everything",
|
||||
}
|
||||
},
|
||||
new() { ClientInfo = new() { Name = "ChatClient", Version = "1.0.0" } });
|
||||
|
||||
// Get all available tools
|
||||
Console.WriteLine("Tools available:");
|
||||
var tools = await mcpClient.GetAIFunctionsAsync();
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
Console.WriteLine($" {tool}");
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
// Create an IChatClient. (This shows using OpenAIClient, but it could be any other IChatClient implementation.)
|
||||
// Provide your own OPENAI_API_KEY via an environment variable.
|
||||
using IChatClient chatClient =
|
||||
new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY")).AsChatClient("gpt-4o-mini")
|
||||
.AsBuilder().UseFunctionInvocation().Build();
|
||||
|
||||
// Have a conversation, making all tools available to the LLM.
|
||||
List<ChatMessage> messages = [];
|
||||
while (true)
|
||||
{
|
||||
Console.Write("Q: ");
|
||||
messages.Add(new(ChatRole.User, Console.ReadLine()));
|
||||
|
||||
List<ChatResponseUpdate> updates = [];
|
||||
await foreach (var update in chatClient.GetStreamingResponseAsync(messages, new() { Tools = [.. tools] }))
|
||||
{
|
||||
Console.Write(update);
|
||||
updates.Add(update);
|
||||
}
|
||||
Console.WriteLine();
|
||||
|
||||
messages.AddMessages(updates);
|
||||
}
|
||||
Loading…
Reference in a new issue