Add articles on logging and progress (#717)
* Add articles on logging and progress * Address comments in PR review
This commit is contained in:
parent
52b1ed5fac
commit
7cb3a64c06
1 changed files with 629 additions and 5 deletions
52
docs/concepts/progress/samples/client/Program.cs
Normal file
52
docs/concepts/progress/samples/client/Program.cs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
using System.Text.Json;
|
||||
using ModelContextProtocol;
|
||||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol;
|
||||
|
||||
var endpoint = Environment.GetEnvironmentVariable("ENDPOINT") ?? "http://localhost:3001";
|
||||
|
||||
var clientTransport = new SseClientTransport(new()
|
||||
{
|
||||
Endpoint = new Uri(endpoint),
|
||||
TransportMode = HttpTransportMode.StreamableHttp,
|
||||
});
|
||||
|
||||
McpClientOptions options = new()
|
||||
{
|
||||
ClientInfo = new()
|
||||
{
|
||||
Name = "ProgressClient",
|
||||
Version = "1.0.0"
|
||||
}
|
||||
};
|
||||
|
||||
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport, options);
|
||||
|
||||
var tools = await mcpClient.ListToolsAsync();
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
Console.WriteLine($"Connected to server with tools: {tool.Name}");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Calling tool: {tools.First().Name}");
|
||||
|
||||
// <snippet_ProgressHandler>
|
||||
var progressHandler = new Progress<ProgressNotificationValue>(value =>
|
||||
{
|
||||
Console.WriteLine($"Tool progress: {value.Progress} of {value.Total} - {value.Message}");
|
||||
});
|
||||
|
||||
var result = await mcpClient.CallToolAsync(toolName: tools.First().Name, progress: progressHandler);
|
||||
// </snippet_ProgressHandler>
|
||||
|
||||
foreach (var block in result.Content)
|
||||
{
|
||||
if (block is TextContentBlock textBlock)
|
||||
{
|
||||
Console.WriteLine(textBlock.Text);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Received unexpected result content of type {block.GetType()}");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue