GitHubスター
0
ユーザー評価
未評価
お気に入り
0
閲覧数
4
フォーク
0
イシュー
0
QuickstartClient
MCP Client Quickstart with MCP C# SDK
System Requirements
Before starting, ensure your system meets these requirements:
- .NET 8.0 or higher
- Anthropic API key (Claude)
- Windows, Linux, or MacOS
Setting up your environment
First, create a new .NET project:
dotnet new console -n QuickstartClient
cd QuickstartClient
Then, add the required dependencies to your project:
dotnet add package ModelContextProtocol --prerelease
dotnet add package Anthropic.SDK
dotnet add package Microsoft.Extensions.Hosting
Setting up your API key
You'll need an Anthropic API key from the Anthropic Console.
dotnet user-secrets init
dotnet user-secrets set "ANTHROPIC_API_KEY" "<your key here>"
Creating the Client
Basic Client Structure
First, let's setup the basic client class:
using Anthropic.SDK;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
var builder = Host.CreateApplicationBuilder(args);
builder.Configuration
.AddEnvironmentVariables()
.AddUserSecrets<Program>();
This creates the beginnings of a .NET console application that can read the API key from user secrets.
Next, we'll setup the MCP Client:
var (command, arguments) = GetCommandAndArguments(args);
var clientTransport = new StdioClientTransport(new()
{
Name = "Demo Server",
Command = command,
Arguments = arguments,
});
await using var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
Console.WriteLine($"Connected to server with tools: {tool.Name}");
}
Add this function at the end of the Program.cs file:
static (string command, string[] arguments) GetCommandAndArguments(string[] args)
{
return args switch
{
[var script] when script.EndsWith(".py") => ("python", args),
[var script] when script.EndsWith(".js") => ("node", args),
[var script] when Directory.Exists(script) || (File.Exists(script) && script.EndsWith(".csproj")) => ("dotnet", ["run", "--project", script, "--no-build"]),
_ => throw new NotSupportedException("An unsupported server script was provided. Supported scripts are .py, .js, or .csproj")
};
}
This creates and 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.
Query processing logic
Now let's add the core functionality for processing queries and handling tool calls:
using var anthropicClient = new AnthropicClient(new APIAuthentication(builder.Configuration["ANTHROPIC_API_KEY"]))
.Messages
.AsBuilder()
.UseFunctionInvocation()
.Build();
var options = new ChatOptions
{
MaxOutputTokens = 1000,
ModelId = "claude-3-5-sonnet-20241022",
Tools = [.. tools]
};
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("MCP Client Started!");
Console.ResetColor();
PromptForInput();
while(Console.ReadLine() is string query && !"exit".Equals(query, StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrWhiteSpace(query))
{
PromptForInput();
continue;
}
await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options))
{
Console.Write(message);
}
Console.WriteLine();
PromptForInput();
}
static void PromptForInput()
{
Console.WriteLine("Enter a command (or 'exit' to quit):");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("> ");
Console.ResetColor();
}
Key Components Explained
1. Client Initialization
- The client is initialized using
McpClientFactory.CreateAsync()
, which sets up the transport type and command to run the server.
2. Server Connection
- Supports Python, Node.js, and .NET servers.
- The server is started using the command specified in the arguments.
- Configures to use stdio for communication with the server.
- Initializes the session and available tools.
3. Query Processing
- Leverages Microsoft.Extensions.AI for the chat client.
- Configures the
IChatClient
to use automatic tool (function) invocation. - The client reads user input and sends it to the server.
- The server processes the query and returns a response.
- The response is displayed to the user.
Running the Client
To run your client with any MCP server:
dotnet run -- path/to/server.csproj # dotnet server
dotnet run -- path/to/server.py # python server
dotnet run -- path/to/server.js # node server
The client will:
- Connect to the specified server
- List available tools
- Start an interactive chat session where you can:
- Enter queries
- See tool executions
- Get responses from Claude
- Exit the session when done
Here's an example of what it should look like it connected to a weather server quickstart:
0
フォロワー
0
リポジトリ
0
Gist
0
貢献数
A comprehensive set of samples of creating and using MCP servers and clients with .NET