You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_ => throw new NotSupportedException("An unsupported server script was provided. Supported scripts are .py, .js, or .csproj")
1456
+
};
1457
+
}
1453
1458
```
1454
-
</Note>
1455
1459
1456
-
This configures a MCP client that will connect to a server that is provided as a command line argument. It then lists the available tools from the connected server.
1460
+
This creates a MCP client that will connect to a server that is provided as a command line argument. It then lists the available tools from the connected server.
1457
1461
1458
1462
### Query processing logic
1459
1463
Now let's add the core functionality for processing queries and handling tool calls:
1460
1464
1461
1465
```csharp
1462
-
using IChatClient anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
1466
+
using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
1463
1467
.Messages
1464
1468
.AsBuilder()
1465
1469
.UseFunctionInvocation()
@@ -1472,29 +1476,34 @@ var options = new ChatOptions
1472
1476
Tools = [.. tools]
1473
1477
};
1474
1478
1475
-
while (true)
1476
-
{
1477
-
Console.WriteLine("MCP Client Started!");
1478
-
Console.WriteLine("Type your queries or 'quit' to exit.");
1479
-
1480
-
string? query = Console.ReadLine();
1479
+
Console.ForegroundColor = ConsoleColor.Green;
1480
+
Console.WriteLine("MCP Client Started!");
1481
+
Console.ResetColor();
1481
1482
1483
+
PromptForInput();
1484
+
while(Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase))
1485
+
{
1482
1486
if (string.IsNullOrWhiteSpace(query))
1483
1487
{
1488
+
PromptForInput();
1484
1489
continue;
1485
1490
}
1486
-
if (string.Equals(query, "quit", StringComparison.OrdinalIgnoreCase))
1487
-
{
1488
-
break;
1489
-
}
1490
1491
1491
-
var response = anthropicClient.GetStreamingResponseAsync(query, options);
1492
-
1493
-
await foreach (var message in response)
1492
+
await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options))
1494
1493
{
1495
-
Console.Write(message.Text);
1494
+
Console.Write(message);
1496
1495
}
1497
1496
Console.WriteLine();
1497
+
1498
+
PromptForInput();
1499
+
}
1500
+
1501
+
static void PromptForInput()
1502
+
{
1503
+
Console.WriteLine("Enter a command (or 'exit' to quit):");
1504
+
Console.ForegroundColor = ConsoleColor.Cyan;
1505
+
Console.Write("> ");
1506
+
Console.ResetColor();
1498
1507
}
1499
1508
```
1500
1509
@@ -1516,7 +1525,8 @@ while (true)
1516
1525
* The server processes the query and returns a response.
1517
1526
* The response is displayed to the user.
1518
1527
1519
-
### Running the Client
1528
+
## Running the Client
1529
+
1520
1530
To run your client with any MCP server:
1521
1531
```bash
1522
1532
dotnet run -- path/to/server.csproj # dotnet server
0 commit comments