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")
1424
+
};
1425
+
1426
+
var mcpClient = await McpClientFactory.CreateAsync(new()
1427
+
{
1428
+
Id = "demo-client",
1429
+
Name = "Demo Client",
1430
+
TransportType = TransportTypes.StdIo,
1431
+
TransportOptions = new()
1432
+
{
1433
+
["command"] = command,
1434
+
["arguments"] = arguments,
1435
+
}
1436
+
});
1437
+
1438
+
var tools = await mcpClient.ListToolsAsync();
1439
+
foreach (var tool in tools)
1440
+
{
1441
+
Console.WriteLine($"Connected to server with tools: {tool.Name}");
1442
+
}
1443
+
```
1444
+
<Note>
1445
+
Be sure to add the `using` statements for the namespaces:
1446
+
```csharp
1447
+
using ModelContextProtocol.Client;
1448
+
using ModelContextProtocol.Protocol.Transport;
1449
+
```
1450
+
</Note>
1451
+
1452
+
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.
1453
+
1454
+
### Query processing logic
1455
+
Now let's add the core functionality for processing queries and handling tool calls:
1456
+
1457
+
```csharp
1458
+
IChatClient anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
1459
+
.Messages
1460
+
.AsBuilder()
1461
+
.UseFunctionInvocation()
1462
+
.Build();
1463
+
1464
+
var options = new ChatOptions
1465
+
{
1466
+
MaxOutputTokens = 1000,
1467
+
ModelId = "claude-3-5-sonnet-20241022",
1468
+
Tools = [.. tools.Cast<AITool>()]
1469
+
};
1470
+
1471
+
while (true)
1472
+
{
1473
+
Console.WriteLine("MCP Client Started!");
1474
+
Console.WriteLine("Type your queries or 'quit' to exit.");
1475
+
1476
+
string? query = Console.ReadLine();
1477
+
1478
+
if (string.IsNullOrWhiteSpace(query))
1479
+
{
1480
+
continue;
1481
+
}
1482
+
if (string.Equals(query, "quit", StringComparison.OrdinalIgnoreCase))
1483
+
{
1484
+
break;
1485
+
}
1486
+
1487
+
var response = await anthropicClient.GetResponseAsync(query, options);
1488
+
1489
+
foreach (var message in response.Messages)
1490
+
{
1491
+
Console.WriteLine(message.Text);
1492
+
}
1493
+
}
1494
+
1495
+
anthropicClient.Dispose();
1496
+
await mcpClient.DisposeAsync();
1497
+
```
1498
+
1499
+
## Key Components Explained
1500
+
1501
+
### 1. Client Initialization
1502
+
* The client is initialized using `McpClientFactory.CreateAsync()`, which sets up the transport type and command to run the server.
1503
+
1504
+
### 2. Server Connection
1505
+
* Supports Python, Node.js, and .NET servers.
1506
+
* The server is started using the command specified in the arguments.
1507
+
* Configures to use stdio for communication with the server.
1508
+
* Initializes the session and available tools.
1509
+
1510
+
### 3. Query Processing
1511
+
* Leverages [Microsoft.Extensions.AI](https://learn.microsoft.com/dotnet/ai/ai-extensions) for the chat client.
1512
+
* Configures the `IChatClient` to use automatic tool (function) invocation.
1513
+
* The client reads user input and sends it to the server.
1514
+
* The server processes the query and returns a response.
1515
+
* The response is displayed to the user.
1516
+
1517
+
### Running the Client
1518
+
To run your client with any MCP server:
1519
+
```bash
1520
+
dotnet run -- path/to/server.csproj # dotnet server
1521
+
dotnet run -- path/to/server.py # python server
1522
+
dotnet run -- path/to/server.js # node server
1523
+
```
1524
+
<Note>
1525
+
If you're continuing the weather tutorial from the server quickstart, your command might look something like this: `dotnet run -- path/to/QuickstartWeatherServer`.
1526
+
</Note>
1527
+
1528
+
The client will:
1529
+
1530
+
1. Connect to the specified server
1531
+
2. List available tools
1532
+
3. Start an interactive chat session where you can:
1533
+
- Enter queries
1534
+
- See tool executions
1535
+
- Get responses from Claude
1536
+
4. Exit the session when done
1537
+
1538
+
Here's an example of what it should look like it connected to a weather server quickstart:
0 commit comments