Skip to content

Commit cceb695

Browse files
committed
Client quickstart for docs
1 parent bf323d2 commit cceb695

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
2626

2727
<!-- Testing dependencies -->
28-
<PackageVersion Include="Anthropic.SDK" Version="4.7.1" />
28+
<PackageVersion Include="Anthropic.SDK" Version="5.0.0" />
2929
<PackageVersion Include="GitHubActionsTestLogger" Version="2.4.1" />
3030
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="$(MicrosoftExtensionsAIVersion)" />
3131
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="$(MicrosoftExtensionsVersion)" />

samples/QuickstartClient/Program.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,68 @@
1111
.AddEnvironmentVariables()
1212
.AddUserSecrets<Program>();
1313

14+
var (command, arguments) = args switch
15+
{
16+
[var script] when script.EndsWith(".py") => ("python", script),
17+
[var script] when script.EndsWith(".js") => ("node", script),
18+
[var script] when Directory.Exists(script) || (File.Exists(script) && script.EndsWith(".csproj")) => ("dotnet", $"run --project {script} --no-build"),
19+
_ => ("dotnet", "run --project ../../../../QuickstartWeatherServer --no-build")
20+
};
21+
1422
var mcpClient = await McpClientFactory.CreateAsync(new()
1523
{
16-
Id = "weather",
17-
Name = "Weather",
24+
Id = "demo-client",
25+
Name = "Demo Client",
1826
TransportType = TransportTypes.StdIo,
1927
TransportOptions = new()
2028
{
21-
["command"] = "dotnet",
22-
["arguments"] = "run --project ../QuickstartWeatherServer",
29+
["command"] = command,
30+
["arguments"] = arguments,
2331
}
2432
});
2533

34+
var tools = await mcpClient.ListToolsAsync();
35+
foreach (var tool in tools)
36+
{
37+
Console.WriteLine($"Connected to server with tools: {tool.Name}");
38+
}
39+
2640
var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
2741
.Messages
2842
.AsBuilder()
2943
.UseFunctionInvocation()
3044
.Build();
3145

32-
var tools = await mcpClient.ListToolsAsync();
33-
foreach (var tool in tools)
46+
var options = new ChatOptions
3447
{
35-
Console.WriteLine($"Tool: {tool.Name}");
36-
}
48+
MaxOutputTokens = 1000,
49+
ModelId = "claude-3-5-sonnet-20241022",
50+
Tools = [.. tools.Cast<AITool>()]
51+
};
3752

3853
while (true)
3954
{
4055
Console.WriteLine("MCP Client Started!");
41-
Console.WriteLine("Enter a command (or 'exit' to quit):");
56+
Console.WriteLine("Type your queries or 'quit' to exit.");
4257

43-
string? command = Console.ReadLine();
58+
string? query = Console.ReadLine();
4459

45-
if (string.IsNullOrWhiteSpace(command))
60+
if (string.IsNullOrWhiteSpace(query))
4661
{
4762
continue;
4863
}
49-
if (string.Equals(command, "exit", StringComparison.OrdinalIgnoreCase))
64+
if (string.Equals(query, "quit", StringComparison.OrdinalIgnoreCase))
5065
{
5166
break;
5267
}
5368

54-
var response = await ProcessQueryAsync(command);
69+
var response = await anthropicClient.GetResponseAsync(query, options);
5570

56-
if (string.IsNullOrWhiteSpace(response))
71+
foreach (var message in response.Messages)
5772
{
58-
Console.WriteLine("No response received.");
59-
}
60-
else
61-
{
62-
Console.WriteLine($"Response: {response}");
73+
Console.WriteLine(message.Text);
6374
}
6475
}
6576

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-
}
77+
anthropicClient.Dispose();
78+
await mcpClient.DisposeAsync();

0 commit comments

Comments
 (0)