使用C#创建一个MCP客户端

前言

网上使用Python创建一个MCP客户端的教程已经有很多了,而使用C#创建一个MCP客户端的教程还很少。

为什么要创建一个MCP客户端呢?

创建了一个MCP客户端之后,你就可以使用别人写好的一些MCP服务了。

效果展示

为了方便大家复现,我没有使用WPF/Avalonia之类的做界面。只是一个简单的控制台程序,可以很容易看懂。

接入了fetch_mcp可以实现获取网页内容了,使用的模型只要具有tool use能力的应该都可以。

我使用的是Qwen/Qwen2.5-72B-Instruct。

开始实践

主要使用的包如下所示:

首先获取MCP服务器:

csharp 复制代码
 private static async Task<IMcpClient> GetMcpClientAsync()
 {
     DotEnv.Load();
     var envVars = DotEnv.Read();
     McpClientOptions options = new()
     {
         ClientInfo = new() { Name = "SimpleToolsConsole", Version = "1.0.0" }
     };

     var config = new McpServerConfig
     {
         Id = "test",
         Name = "Test",
         TransportType = TransportTypes.StdIo,
         TransportOptions = new Dictionary<string, string>
         {
             ["command"] = envVars["MCPCommand"],
             ["arguments"] = envVars["MCPArguments"],
         }
     };

     var factory = new McpClientFactory(
         new[] { config },
         options,
         NullLoggerFactory.Instance
     );

     return await factory.GetClientAsync("test");
 }

写死的话就是这样写:

csharp 复制代码
 private static async Task<IMcpClient> GetMcpClientAsync()
 {
     DotEnv.Load();
     var envVars = DotEnv.Read();
     McpClientOptions options = new()
     {
         ClientInfo = new() { Name = "SimpleToolsConsole", Version = "1.0.0" }
     };

     var config = new McpServerConfig
     {
         Id = "test",
         Name = "Test",
         TransportType = TransportTypes.StdIo,
         TransportOptions = new Dictionary<string, string>
         {
             ["command"] = node,
             ["arguments"] = D:/Learning/AI-related/fetch-mcp/dist/index.js,
         }
     };

     var factory = new McpClientFactory(
         new[] { config },
         options,
         NullLoggerFactory.Instance
     );

     return await factory.GetClientAsync("test");
 }

重点在:

csharp 复制代码
 TransportOptions = new Dictionary<string, string>
         {
             ["command"] = node,
             ["arguments"] = D:/Learning/AI-related/fetch-mcp/dist/index.js,
         }

用于连接你想连接的MCP服务器。

如果能正确显示你连接mcp服务器提供的工具,说明连接成功。

csharp 复制代码
  var listToolsResult = await client.ListToolsAsync();
  var mappedTools = listToolsResult.Tools.Select(t => t.ToAITool(client)).ToList();
  Console.WriteLine("Tools available:");
  foreach (var tool in mappedTools)
  {
      Console.WriteLine("  " + tool);
  }

开启一个聊天循环:

csharp 复制代码
    Console.WriteLine("\nMCP Client Started!");
    Console.WriteLine("Type your queries or 'quit' to exit.");

    ChatDemo chatDemo = new ChatDemo();

    while (true)
    {
        try
        {
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("\nQuery: ");
            string query = Console.ReadLine()?.Trim() ?? string.Empty;

            if (query.ToLower() == "quit")
                break;
            if (query.ToLower() == "clear")
            {
                Console.Clear();
                chatDemo.Messages.Clear();                    
            }
            else 
            {
                string response = await chatDemo.ProcessQueryAsync(query, mappedTools);
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine($"AI回答:{response}");
                Console.ForegroundColor = ConsoleColor.White;
            }                      
        }
        catch (Exception ex)
        {
            Console.WriteLine($"\nError: {ex.Message}");
        }
    }
}

处理每次询问:

csharp 复制代码
 public async Task<string> ProcessQueryAsync(string query, List<AITool> tools)
 {
     if(Messages.Count == 0)
     {
         Messages =
         [
          // Add a system message
         new(ChatRole.System, "You are a helpful assistant, helping us test MCP server functionality."),
         ];
     }
     
     // Add a user message
     Messages.Add(new(ChatRole.User, query));

     var response = await ChatClient.GetResponseAsync(
            Messages,
            new() { Tools = tools });
     Messages.AddMessages(response);
     var toolUseMessage = response.Messages.Where(m => m.Role == ChatRole.Tool);

     if (toolUseMessage.Count() > 0)
     {
         var functionMessage = response.Messages.Where(m => m.Text == "").First();             
         var functionCall = (FunctionCallContent)functionMessage.Contents[1];
         Console.ForegroundColor = ConsoleColor.Green;
         string arguments = "";
         foreach (var arg in functionCall.Arguments)
         {
             arguments += $"{arg.Key}:{arg.Value};";
         }
         Console.WriteLine($"调用函数名:{functionCall.Name};参数信息:{arguments}");
         foreach (var message in toolUseMessage)
         {
             var functionResultContent = (FunctionResultContent)message.Contents[0];
             Console.WriteLine($"调用工具结果:{functionResultContent.Result}");
         }

         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine("本次没有调用工具");
         Console.ForegroundColor = ConsoleColor.White;
     }

     return response.Text;
 }

代码已经放到GitHub,地址:https://github.com/Ming-jiayou/mcp_demo。

将.env-example修改为.env应该就可以运行,如果报错,设置成嵌入的资源即可。

.env配置示例:

csharp 复制代码
API_KEY=sk-xxx
BaseURL=https://api.siliconflow.cn/v1
ModelID=Qwen/Qwen2.5-72B-Instruct
MCPCommand=node
MCPArguments=D:/Learning/AI-related/fetch-mcp/dist/index.js

最后

对C#使用MCP感兴趣的朋友可以关注这个项目:https://github.com/PederHP/mcpdotnet。

有问题欢迎一起交流学习。

相关推荐
宝桥南山1 天前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong1231 天前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp
星辰徐哥1 天前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
leoufung1 天前
LeetCode 30:Substring with Concatenation of All Words 题解(含 C 语言 uthash 实现)
c语言·leetcode·c#
hacker7071 天前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
SKY -dada1 天前
Understand 使用教程
开发语言·c#·流程图·软件构建·敏捷流程·代码复审·源代码管理
William_cl1 天前
【C#/.NET 进阶】ASP.NET 架构与最佳实践:DI 依赖注入(IoC 核心)从入门到避坑
c#·asp.net·.net
武藤一雄1 天前
WPF:MessageBox系统消息框
前端·microsoft·c#·.net·wpf
武藤一雄1 天前
WPF进阶:万字详解WPF如何性能优化
windows·性能优化·c#·.net·wpf·.netcore·鲁棒性
xiaogutou11212 天前
2026年历史课件PPT模板选购指南:教师备课效率与精度的平衡方案
开发语言·c#