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")
1459
+
};
1460
+
}
1453
1461
```
1454
-
</Note>
1455
1462
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.
1463
+
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
1464
1458
1465
### Query processing logic
1459
1466
Now let's add the core functionality for processing queries and handling tool calls:
1460
1467
1461
1468
```csharp
1462
-
using IChatClient anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
1469
+
using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
1463
1470
.Messages
1464
1471
.AsBuilder()
1465
1472
.UseFunctionInvocation()
@@ -1472,29 +1479,34 @@ var options = new ChatOptions
1472
1479
Tools = [.. tools]
1473
1480
};
1474
1481
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();
1482
+
Console.ForegroundColor = ConsoleColor.Green;
1483
+
Console.WriteLine("MCP Client Started!");
1484
+
Console.ResetColor();
1481
1485
1486
+
PromptForInput();
1487
+
while(Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase))
1488
+
{
1482
1489
if (string.IsNullOrWhiteSpace(query))
1483
1490
{
1491
+
PromptForInput();
1484
1492
continue;
1485
1493
}
1486
-
if (string.Equals(query, "quit", StringComparison.OrdinalIgnoreCase))
1487
-
{
1488
-
break;
1489
-
}
1490
-
1491
-
var response = anthropicClient.GetStreamingResponseAsync(query, options);
1492
1494
1493
-
await foreach (var message in response)
1495
+
await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options))
1494
1496
{
1495
-
Console.Write(message.Text);
1497
+
Console.Write(message);
1496
1498
}
1497
1499
Console.WriteLine();
1500
+
1501
+
PromptForInput();
1502
+
}
1503
+
1504
+
static void PromptForInput()
1505
+
{
1506
+
Console.WriteLine("Enter a command (or 'exit' to quit):");
1507
+
Console.ForegroundColor = ConsoleColor.Cyan;
1508
+
Console.Write("> ");
1509
+
Console.ResetColor();
1498
1510
}
1499
1511
```
1500
1512
@@ -1516,7 +1528,8 @@ while (true)
1516
1528
* The server processes the query and returns a response.
1517
1529
* The response is displayed to the user.
1518
1530
1519
-
### Running the Client
1531
+
## Running the Client
1532
+
1520
1533
To run your client with any MCP server:
1521
1534
```bash
1522
1535
dotnet run -- path/to/server.csproj # dotnet server
0 commit comments