Skip to content

Commit bf323d2

Browse files
committed
WIP
1 parent 19fdd4b commit bf323d2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

ModelContextProtocol.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatWithTools", "samples\Ch
4848
EndProject
4949
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickstartWeatherServer", "samples\QuickstartWeatherServer\QuickstartWeatherServer.csproj", "{ADD88C0E-8225-4350-8483-E863B8E7CC51}"
5050
EndProject
51+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickstartClient", "samples\QuickstartClient\QuickstartClient.csproj", "{1BA3186A-11B8-44B2-85A9-512F536C1022}"
52+
EndProject
5153
Global
5254
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5355
Debug|Any CPU = Debug|Any CPU
@@ -86,6 +88,10 @@ Global
8688
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Debug|Any CPU.Build.0 = Debug|Any CPU
8789
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Release|Any CPU.ActiveCfg = Release|Any CPU
8890
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Release|Any CPU.Build.0 = Release|Any CPU
91+
{1BA3186A-11B8-44B2-85A9-512F536C1022}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
92+
{1BA3186A-11B8-44B2-85A9-512F536C1022}.Debug|Any CPU.Build.0 = Debug|Any CPU
93+
{1BA3186A-11B8-44B2-85A9-512F536C1022}.Release|Any CPU.ActiveCfg = Release|Any CPU
94+
{1BA3186A-11B8-44B2-85A9-512F536C1022}.Release|Any CPU.Build.0 = Release|Any CPU
8995
EndGlobalSection
9096
GlobalSection(SolutionProperties) = preSolution
9197
HideSolutionNode = FALSE
@@ -100,6 +106,7 @@ Global
100106
{20AACB9B-307D-419C-BCC6-1C639C402295} = {1288ADA5-1BF1-4A7F-A33E-9EA29097AA40}
101107
{0C6D0512-D26D-63D3-5019-C5F7A657B28C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
102108
{ADD88C0E-8225-4350-8483-E863B8E7CC51} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
109+
{1BA3186A-11B8-44B2-85A9-512F536C1022} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
103110
EndGlobalSection
104111
GlobalSection(ExtensibilityGlobals) = postSolution
105112
SolutionGuid = {384A3888-751F-4D75-9AE5-587330582D89}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Anthropic.SDK;
2+
using Microsoft.Extensions.AI;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.Hosting;
5+
using ModelContextProtocol.Client;
6+
using ModelContextProtocol.Protocol.Transport;
7+
8+
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
9+
10+
builder.Configuration
11+
.AddEnvironmentVariables()
12+
.AddUserSecrets<Program>();
13+
14+
var mcpClient = await McpClientFactory.CreateAsync(new()
15+
{
16+
Id = "weather",
17+
Name = "Weather",
18+
TransportType = TransportTypes.StdIo,
19+
TransportOptions = new()
20+
{
21+
["command"] = "dotnet",
22+
["arguments"] = "run --project ../QuickstartWeatherServer",
23+
}
24+
});
25+
26+
var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
27+
.Messages
28+
.AsBuilder()
29+
.UseFunctionInvocation()
30+
.Build();
31+
32+
var tools = await mcpClient.ListToolsAsync();
33+
foreach (var tool in tools)
34+
{
35+
Console.WriteLine($"Tool: {tool.Name}");
36+
}
37+
38+
while (true)
39+
{
40+
Console.WriteLine("MCP Client Started!");
41+
Console.WriteLine("Enter a command (or 'exit' to quit):");
42+
43+
string? command = Console.ReadLine();
44+
45+
if (string.IsNullOrWhiteSpace(command))
46+
{
47+
continue;
48+
}
49+
if (string.Equals(command, "exit", StringComparison.OrdinalIgnoreCase))
50+
{
51+
break;
52+
}
53+
54+
var response = await ProcessQueryAsync(command);
55+
56+
if (string.IsNullOrWhiteSpace(response))
57+
{
58+
Console.WriteLine("No response received.");
59+
}
60+
else
61+
{
62+
Console.WriteLine($"Response: {response}");
63+
}
64+
}
65+
66+
async Task<string> ProcessQueryAsync(string query)
67+
{
68+
var options = new ChatOptions
69+
{
70+
MaxOutputTokens = 1000,
71+
ModelId = "claude-3-5-sonnet-20241022",
72+
Tools = [.. tools.Cast<AITool>()]
73+
};
74+
75+
var response = await anthropicClient.GetResponseAsync(query, options);
76+
77+
return "";
78+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<UserSecretsId>a4e20a70-5009-4b81-b5b6-780b6d43e78e</UserSecretsId>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Anthropic.SDK" />
17+
<PackageReference Include="Microsoft.Extensions.Hosting" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)