MCP協議,.Net 使用示例

服務器端示例

基礎服務器

以下是一個基礎的 MCP 服務器示例,它使用標準輸入輸出(stdio)作為傳輸方式,并實現了一個簡單的回顯工具:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;namespace BasicMcpServer
{public class Program{public static async Task Main(string[] args){var builder = Host.CreateApplicationBuilder(args);// 配置日志輸出到標準錯誤builder.Logging.AddConsole(consoleLogOptions =>{consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;});// 配置 MCP 服務器builder.Services.AddMcpServer().WithStdioServerTransport().WithToolsFromAssembly();await builder.Build().RunAsync();}}[McpServerToolType]public static class BasicTools{[McpServerTool, Description("Echoes the message back to the client.")]public static string Echo(string message) => $"You said: {message}";[McpServerTool, Description("Adds two numbers together.")]public static double Add([Description("First number to add")] double a, [Description("Second number to add")] double b) => a + b;[McpServerTool, Description("Gets the current date and time.")]public static string GetDateTime() => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");}
}

這個示例展示了如何創建一個基本的 MCP 服務器,它包含三個簡單的工具:回顯消息、加法計算和獲取當前日期時間。

文件操作工具

以下是一個實現文件操作功能的 MCP 工具示例:

[McpServerToolType]
public static class FileTools
{[McpServerTool, Description("Reads the content of a text file.")]public static async Task ReadTextFile([Description("Path to the file to read")] string filePath,CancellationToken cancellationToken){if (!File.Exists(filePath)){throw new FileNotFoundException($"File not found: {filePath}");}return await File.ReadAllTextAsync(filePath, cancellationToken);}[McpServerTool, Description("Writes text content to a file.")]public static async Task WriteTextFile([Description("Path to the file to write")] string filePath,[Description("Content to write to the file")] string content,[Description("Whether to append to the file instead of overwriting")] bool append = false,CancellationToken cancellationToken = default){try{if (append){await File.AppendAllTextAsync(filePath, content, cancellationToken);}else{await File.WriteAllTextAsync(filePath, content, cancellationToken);}return $"Successfully wrote {content.Length} characters to {filePath}";}catch (Exception ex){return $"Error writing to file: {ex.Message}";}}[McpServerTool, Description("Lists files in a directory.")]public static string[] ListFiles([Description("Directory path to list files from")] string directoryPath,[Description("File pattern to match (e.g., *.txt)")] string pattern = "*.*"){if (!Directory.Exists(directoryPath)){throw new DirectoryNotFoundException($"Directory not found: {directoryPath}");}return Directory.GetFiles(directoryPath, pattern).Select(Path.GetFileName).ToArray();}
}

這個示例實現了三個文件操作工具:讀取文本文件、寫入文本文件和列出目錄中的文件。這些工具可以幫助 AI 模型訪問和操作本地文件系統。

Web 請求工具

以下是一個實現 Web 請求功能的 MCP 工具示例:

[McpServerToolType]
public static class WebTools
{[McpServerTool, Description("Fetches content from a URL.")]public static async Task FetchUrl(HttpClient httpClient,[Description("URL to fetch content from")] string url,CancellationToken cancellationToken){try{var response = await httpClient.GetAsync(url, cancellationToken);response.EnsureSuccessStatusCode();return await response.Content.ReadAsStringAsync(cancellationToken);}catch (Exception ex){return $"Error fetching URL: {ex.Message}";}}[McpServerTool, Description("Performs a web search and returns results.")]public static async Task WebSearch(HttpClient httpClient,[Description("Search query")] string query,[Description("Maximum number of results to return")] int maxResults = 5,CancellationToken cancellationToken = default){// 注意:這是一個示例實現,實際應用中應該使用真實的搜索 APIvar encodedQuery = Uri.EscapeDataString(query);var url = $"https://api.example.com/search?q={encodedQuery}&limit={maxResults}";try{var response = await httpClient.GetAsync(url, cancellationToken);response.EnsureSuccessStatusCode();var content = await response.Content.ReadAsStringAsync(cancellationToken);// 解析搜索結果(示例)return $"Search results for '{query}':\n{content}";}catch (Exception ex){return $"Error performing web search: {ex.Message}";}}[McpServerTool, Description("Posts data to a URL and returns the response.")]public static async Task PostToUrl(HttpClient httpClient,[Description("URL to post data to")] string url,[Description("JSON data to post")] string jsonData,CancellationToken cancellationToken){try{var content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");var response = await httpClient.PostAsync(url, content, cancellationToken);response.EnsureSuccessStatusCode();return await response.Content.ReadAsStringAsync(cancellationToken);}catch (Exception ex){return $"Error posting to URL: {ex.Message}";}}
}

這個示例實現了三個 Web 請求工具:獲取 URL 內容、執行 Web 搜索和向 URL 發送 POST 請求。這些工具可以幫助 AI 模型訪問互聯網上的信息。

注冊 HttpClient:

要使用上述 Web 工具,需要在服務配置中注冊 HttpClient:

builder.Services.AddHttpClient();

數據庫工具

以下是一個實現數據庫操作功能的 MCP 工具示例(使用 Entity Framework Core):

// 數據庫上下文
public class AppDbContext : DbContext
{public AppDbContext(DbContextOptions options) : base(options) { }public DbSet Products { get; set; }
}// 產品實體
public class Product
{public int Id { get; set; }public string Name { get; set; }public decimal Price { get; set; }public int Stock { get; set; }
}// 數據庫工具
[McpServerToolType]
public class DatabaseTools
{private readonly AppDbContext _dbContext;public DatabaseTools(AppDbContext dbContext){_dbContext = dbContext;}[McpServerTool, Description("Gets a list of products.")]public async Task GetProducts([Description("Maximum number of products to return")] int limit = 10,CancellationToken cancellationToken = default){var products = await _dbContext.Products.Take(limit).ToListAsync(cancellationToken);return JsonSerializer.Serialize(products, new JsonSerializerOptions{WriteIndented = true});}[McpServerTool, Description("Searches for products by name.")]public async Task SearchProducts([Description("Product name to search for")] string name,CancellationToken cancellationToken = default){var products = await _dbContext.Products.Where(p => p.Name.Contains(name)).ToListAsync(cancellationToken);return JsonSerializer.Serialize(products, new JsonSerializerOptions{WriteIndented = true});}[McpServerTool, Description("Adds a new product.")]public async Task AddProduct([Description("Product name")] string name,[Description("Product price")] decimal price,[Description("Product stock")] int stock,CancellationToken cancellationToken = default){var product = new Product{Name = name,Price = price,Stock = stock};_dbContext.Products.Add(product);await _dbContext.SaveChangesAsync(cancellationToken);return $"Product added successfully with ID: {product.Id}";}
}

這個示例實現了三個數據庫操作工具:獲取產品列表、搜索產品和添加新產品。這些工具可以幫助 AI 模型訪問和操作數據庫。

注冊數據庫上下文:

要使用上述數據庫工具,需要在服務配置中注冊數據庫上下文:

builder.Services.AddDbContext(options =>options.UseSqlite("Data Source=app.db"));// 注冊數據庫工具
builder.Services.AddTransient();

客戶端示例

基礎客戶端

以下是一個基礎的 MCP 客戶端示例,它連接到 MCP 服務器并列出可用的工具:

using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;namespace BasicMcpClient
{public class Program{public static async Task Main(string[] args){// 解析命令行參數var (command, arguments) = GetCommandAndArguments(args);// 創建 MCP 客戶端await using var mcpClient = await McpClientFactory.CreateAsync(new(){Id = "demo-server",Name = "Demo Server",TransportType = TransportTypes.StdIo,TransportOptions = new(){["command"] = command,["arguments"] = arguments,}});// 列出可用工具var tools = await mcpClient.ListToolsAsync();Console.WriteLine("Available tools:");foreach (var tool in tools){Console.WriteLine($"- {tool.Name}: {tool.Description}");if (tool.Parameters?.Any() == true){Console.WriteLine("  Parameters:");foreach (var param in tool.Parameters){Console.WriteLine($"  - {param.Name}: {param.Description} ({param.Type})");}}Console.WriteLine();}// 保持程序運行,等待用戶輸入Console.WriteLine("Press Enter to exit...");Console.ReadLine();}private static (string, string) GetCommandAndArguments(string[] args){// 解析命令行參數的邏輯// ...// 示例返回值return ("dotnet", "run --project ../BasicMcpServer/BasicMcpServer.csproj");}}
}

這個示例展示了如何創建一個基本的 MCP 客戶端,連接到 MCP 服務器,并列出可用的工具及其參數。

工具發現與調用

以下是一個展示如何發現和調用 MCP 工具的示例:

// 連接到 MCP 服務器
await using var mcpClient = await McpClientFactory.CreateAsync(new()
{Id = "demo-server",Name = "Demo Server",TransportType = TransportTypes.StdIo,TransportOptions = new(){["command"] = command,["arguments"] = arguments,}
});// 列出可用工具
var tools = await mcpClient.ListToolsAsync();
Console.WriteLine($"Found {tools.Count} tools:");
foreach (var tool in tools)
{Console.WriteLine($"- {tool.Name}");
}// 調用 Echo 工具
Console.WriteLine("\nCalling Echo tool...");
var echoResult = await mcpClient.CallToolAsync("echo",new Dictionary() { ["message"] = "Hello MCP!" },CancellationToken.None);
Console.WriteLine($"Echo result: {echoResult.Content.First(c => c.Type == "text").Text}");// 調用 Add 工具
Console.WriteLine("\nCalling Add tool...");
var addResult = await mcpClient.CallToolAsync("add",new Dictionary() { ["a"] = 5, ["b"] = 7 },CancellationToken.None);
Console.WriteLine($"Add result: {addResult.Content.First(c => c.Type == "text").Text}");// 調用 GetDateTime 工具
Console.WriteLine("\nCalling GetDateTime tool...");
var dateTimeResult = await mcpClient.CallToolAsync("getDateTime",new Dictionary(),CancellationToken.None);
Console.WriteLine($"DateTime result: {dateTimeResult.Content.First(c => c.Type == "text").Text}");

這個示例展示了如何列出可用的 MCP 工具,并調用不同類型的工具,包括帶參數和不帶參數的工具。

錯誤處理

以下是一個展示如何處理 MCP 工具調用錯誤的示例:

try
{// 嘗試調用不存在的工具var result = await mcpClient.CallToolAsync("nonExistentTool",new Dictionary(),CancellationToken.None);
}
catch (McpException ex)
{Console.WriteLine($"MCP Error: {ex.Message}");Console.WriteLine($"Error Code: {ex.Code}");Console.WriteLine($"Error Data: {ex.Data}");
}try
{// 嘗試調用工具但缺少必要參數var result = await mcpClient.CallToolAsync("echo",new Dictionary(),  // 缺少 message 參數CancellationToken.None);
}
catch (McpException ex)
{Console.WriteLine($"MCP Error: {ex.Message}");
}try
{// 嘗試調用工具但參數類型錯誤var result = await mcpClient.CallToolAsync("add",new Dictionary() { ["a"] = "not a number", ["b"] = 7 },CancellationToken.None);
}
catch (McpException ex)
{Console.WriteLine($"MCP Error: {ex.Message}");
}

這個示例展示了如何處理 MCP 工具調用中可能出現的各種錯誤,包括調用不存在的工具、缺少必要參數和參數類型錯誤。

LLM 集成示例

Claude 集成

以下是一個將 MCP 工具與 Claude 模型集成的示例:

using Anthropic.SDK;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;var builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddEnvironmentVariables().AddUserSecrets();// 創建 MCP 客戶端
var (command, arguments) = GetCommandAndArguments(args);
await using var mcpClient = await McpClientFactory.CreateAsync(new()
{Id = "demo-server",Name = "Demo Server",TransportType = TransportTypes.StdIo,TransportOptions = new(){["command"] = command,["arguments"] = arguments,}
});// 獲取可用工具
var tools = await mcpClient.ListToolsAsync();
Console.WriteLine($"Connected to server with {tools.Count} tools");// 創建 Claude 客戶端
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-20240229",Tools = [.. tools]
};Console.WriteLine("Chat with Claude (type 'exit' to quit):");
while (true)
{Console.Write("> ");var query = Console.ReadLine();if (string.IsNullOrWhiteSpace(query) || query.Equals("exit", StringComparison.OrdinalIgnoreCase)){break;}// 使用 Claude 處理查詢await foreach (var message in anthropicClient.GetStreamingResponseAsync(query, options)){Console.Write(message);}Console.WriteLine("\n");
}

這個示例展示了如何創建一個聊天應用,將 MCP 工具與 Claude 模型集成,使用戶能夠通過自然語言與 Claude 交互,而 Claude 能夠使用 MCP 工具來完成任務。

OpenAI 集成

以下是一個將 MCP 工具與 OpenAI 模型集成的示例:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using OpenAI_API;
using OpenAI_API.Chat;
using System.Text.Json;var builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddEnvironmentVariables().AddUserSecrets();// 創建 MCP 客戶端
var (command, arguments) = GetCommandAndArguments(args);
await using var mcpClient = await McpClientFactory.CreateAsync(new()
{Id = "demo-server",Name = "Demo Server",TransportType = TransportTypes.StdIo,TransportOptions = new(){["command"] = command,["arguments"] = arguments,}
});// 獲取可用工具
var tools = await mcpClient.ListToolsAsync();
Console.WriteLine($"Connected to server with {tools.Count} tools");// 創建 OpenAI 客戶端
var openAiApi = new OpenAIAPI(builder.Configuration["OPENAI_API_KEY"]);// 轉換 MCP 工具為 OpenAI 工具格式
var openAiTools = tools.Select(tool => new OpenAI_API.Tool
{Type = "function",Function = new OpenAI_API.Function{Name = tool.Name,Description = tool.Description,Parameters = new{Type = "object",Properties = tool.Parameters?.ToDictionary(p => p.Name,p => new{Type = ConvertToJsonSchemaType(p.Type),Description = p.Description}) ?? new Dictionary(),Required = tool.Parameters?.Where(p => p.Required).Select(p => p.Name).ToArray() ?? Array.Empty()}}
}).ToList();// 創建聊天會話
var chat = openAiApi.Chat.CreateConversation();
chat.Model = "gpt-4o";
chat.RequestParameters.Tools = openAiTools;Console.WriteLine("Chat with GPT (type 'exit' to quit):");
while (true)
{Console.Write("> ");var query = Console.ReadLine();if (string.IsNullOrWhiteSpace(query) || query.Equals("exit", StringComparison.OrdinalIgnoreCase)){break;}// 添加用戶消息chat.AppendUserInput(query);// 獲取 GPT 響應var response = await chat.GetResponseFromChatbotAsync();Console.WriteLine(response);// 處理工具調用if (chat.ResponseParameters.ToolCalls?.Any() == true){foreach (var toolCall in chat.ResponseParameters.ToolCalls){if (toolCall.Type == "function"){Console.WriteLine($"\nCalling tool: {toolCall.Function.Name}");// 解析參數var parameters = JsonSerializer.Deserialize>(toolCall.Function.Arguments);// 調用 MCP 工具var result = await mcpClient.CallToolAsync(toolCall.Function.Name,parameters,CancellationToken.None);var resultText = result.Content.First(c => c.Type == "text").Text;Console.WriteLine($"Tool result: {resultText}");// 將工具結果添加到對話chat.AppendToolResult(resultText, toolCall.Id);}}// 獲取 GPT 對工具結果的響應var finalResponse = await chat.GetResponseFromChatbotAsync();Console.WriteLine($"\nFinal response: {finalResponse}");}Console.WriteLine("\n");
}// 輔助方法:將 MCP 類型轉換為 JSON Schema 類型
string ConvertToJsonSchemaType(string mcpType)
{return mcpType.ToLower() switch{"string" => "string","integer" => "integer","number" => "number","boolean" => "boolean","array" => "array","object" => "object",_ => "string"};
}

這個示例展示了如何創建一個聊天應用,將 MCP 工具與 OpenAI 的 GPT 模型集成,使用戶能夠通過自然語言與 GPT 交互,而 GPT 能夠使用 MCP 工具來完成任務。

Semantic Kernel 集成

以下是一個將 MCP 工具與 Microsoft Semantic Kernel 集成的示例:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.SemanticKernel;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;var builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddEnvironmentVariables().AddUserSecrets();// 創建 MCP 客戶端
var (command, arguments) = GetCommandAndArguments(args);
await using var mcpClient = await McpClientFactory.CreateAsync(new()
{Id = "demo-server",Name = "Demo Server",TransportType = TransportTypes.StdIo,TransportOptions = new(){["command"] = command,["arguments"] = arguments,}
});// 獲取可用工具
var tools = await mcpClient.ListToolsAsync();
Console.WriteLine($"Connected to server with {tools.Count} tools");// 創建 Semantic Kernel
var kernel = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(deploymentName: builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"],endpoint: builder.Configuration["AZURE_OPENAI_ENDPOINT"],apiKey: builder.Configuration["AZURE_OPENAI_API_KEY"]).Build();// 注冊 MCP 工具為 Semantic Kernel 函數
foreach (var tool in tools)
{kernel.ImportPluginFromObject(new McpToolWrapper(mcpClient, tool), tool.Name);
}// 創建聊天歷史
var chatHistory = new ChatHistory();Console.WriteLine("Chat with Semantic Kernel (type 'exit' to quit):");
while (true)
{Console.Write("> ");var query = Console.ReadLine();if (string.IsNullOrWhiteSpace(query) || query.Equals("exit", StringComparison.OrdinalIgnoreCase)){break;}// 添加用戶消息到聊天歷史chatHistory.AddUserMessage(query);// 獲取 AI 響應var response = await kernel.InvokePromptAsync(chatHistory.ToString());Console.WriteLine(response);// 添加 AI 響應到聊天歷史chatHistory.AddAssistantMessage(response);Console.WriteLine("\n");
}// MCP 工具包裝類
public class McpToolWrapper
{private readonly IMcpClient _mcpClient;private readonly McpTool _tool;public McpToolWrapper(IMcpClient mcpClient, McpTool tool){_mcpClient = mcpClient;_tool = tool;}[KernelFunction]public async Task ExecuteAsync(Dictionary parameters){var result = await _mcpClient.CallToolAsync(_tool.Name,parameters,CancellationToken.None);return result.Content.First(c => c.Type == "text").Text;}
}

這個示例展示了如何創建一個聊天應用,將 MCP 工具與 Microsoft Semantic Kernel 集成,使用戶能夠通過自然語言與 AI 交互,而 AI 能夠使用 MCP 工具來完成任務。

應用場景

文檔分析

以下是一個使用 MCP 工具進行文檔分析的示例:

[McpServerToolType]
public static class DocumentTools
{[McpServerTool, Description("Analyzes a document and extracts key information.")]public static async Task AnalyzeDocument(HttpClient httpClient,[Description("URL of the document to analyze")] string documentUrl,[Description("Type of analysis to perform (summary, entities, sentiment)")] string analysisType = "summary",CancellationToken cancellationToken = default){try{// 下載文檔var response = await httpClient.GetAsync(documentUrl, cancellationToken);response.EnsureSuccessStatusCode();var content = await response.Content.ReadAsStringAsync(cancellationToken);// 根據分析類型執行不同的分析return analysisType.ToLower() switch{"summary" => await GenerateSummary(content, cancellationToken),"entities" => await ExtractEntities(content, cancellationToken),"sentiment" => await AnalyzeSentiment(content, cancellationToken),_ => throw new ArgumentException($"Unsupported analysis type: {analysisType}")};}catch (Exception ex){return $"Error analyzing document: {ex.Message}";}}private static async Task GenerateSummary(string content, CancellationToken cancellationToken){// 實現文檔摘要生成邏輯// 這里可以使用 NLP 庫或調用外部 API// 示例實現var summary = $"Document summary (length: {content.Length} characters):\n";summary += "This is a placeholder for the actual document summary.";return summary;}private static async Task ExtractEntities(string content, CancellationToken cancellationToken){// 實現實體提取邏輯// 這里可以使用 NLP 庫或調用外部 API// 示例實現var entities = "Extracted entities:\n";entities += "- Entity 1 (Person)\n";entities += "- Entity 2 (Organization)\n";entities += "- Entity 3 (Location)";return entities;}private static async Task AnalyzeSentiment(string content, CancellationToken cancellationToken){// 實現情感分析邏輯// 這里可以使用 NLP 庫或調用外部 API// 示例實現var sentiment = "Sentiment analysis:\n";sentiment += "- Overall sentiment: Positive\n";sentiment += "- Confidence score: 0.85\n";sentiment += "- Key positive phrases: [...]\n";sentiment += "- Key negative phrases: [...]";return sentiment;}
}

這個示例展示了如何實現文檔分析工具,包括生成摘要、提取實體和分析情感。這些工具可以幫助 AI 模型分析和理解文檔內容。

數據處理

以下是一個使用 MCP 工具進行數據處理的示例:

[McpServerToolType]
public static class DataProcessingTools
{[McpServerTool, Description("Processes CSV data and performs analysis.")]public static async Task ProcessCsvData([Description("URL of the CSV file to process")] string csvUrl,[Description("Type of analysis to perform (stats, filter, transform)")] string analysisType = "stats",[Description("Additional parameters for the analysis (e.g., filter criteria)")] string parameters = "",CancellationToken cancellationToken = default){try{// 下載 CSV 文件using var httpClient = new HttpClient();var response = await httpClient.GetAsync(csvUrl, cancellationToken);response.EnsureSuccessStatusCode();var csvContent = await response.Content.ReadAsStringAsync(cancellationToken);// 解析 CSV 數據var data = ParseCsv(csvContent);// 根據分析類型執行不同的處理return analysisType.ToLower() switch{"stats" => CalculateStatistics(data),"filter" => FilterData(data, parameters),"transform" => TransformData(data, parameters),_ => throw new ArgumentException($"Unsupported analysis type: {analysisType}")};}catch (Exception ex){return $"Error processing CSV data: {ex.Message}";}}private static List> ParseCsv(string csvContent){var result = new List>();var lines = csvContent.Split('\n');if (lines.Length < 2){return result;}var headers = lines[0].Split(',').Select(h => h.Trim()).ToArray();for (int i = 1; i < lines.Length; i++){var line = lines[i].Trim();if (string.IsNullOrEmpty(line)){continue;}var values = line.Split(',').Select(v => v.Trim()).ToArray();var row = new Dictionary();for (int j = 0; j < Math.Min(headers.Length, values.Length); j++){row[headers[j]] = values[j];}result.Add(row);}return result;}private static string CalculateStatistics(List> data){if (data.Count == 0){return "No data to analyze.";}var result = "Data Statistics:\n";result += $"- Row count: {data.Count}\n";result += $"- Column count: {data[0].Count}\n";result += "- Columns: " + string.Join(", ", data[0].Keys);return result;}private static string FilterData(List> data, string parameters){// 解析過濾參數// 格式:column=valuevar filterParams = parameters.Split('=');if (filterParams.Length != 2){return "Invalid filter parameters. Format should be 'column=value'.";}var column = filterParams[0].Trim();var value = filterParams[1].Trim();// 應用過濾var filteredData = data.Where(row => row.ContainsKey(column) && row[column] == value).ToList();var result = $"Filtered data (where {column} = {value}):\n";result += $"- Matching rows: {filteredData.Count}\n\n";// 顯示前 5 行for (int i = 0; i < Math.Min(5, filteredData.Count); i++){result += $"Row {i + 1}:\n";foreach (var kvp in filteredData[i]){result += $"  {kvp.Key}: {kvp.Value}\n";}result += "\n";}return result;}private static string TransformData(List> data, string parameters){// 解析轉換參數// 格式:operation:columnvar transformParams = parameters.Split(':');if (transformParams.Length != 2){return "Invalid transform parameters. Format should be 'operation:column'.";}var operation = transformParams[0].Trim().ToLower();var column = transformParams[1].Trim();// 檢查列是否存在if (data.Count > 0 && !data[0].ContainsKey(column)){return $"Column '{column}' not found in data.";}// 應用轉換var result = $"Transformed data ({operation} on {column}):\n\n";switch (operation){case "uppercase":foreach (var row in data){if (row.ContainsKey(column)){row[column] = row[column].ToUpper();}}break;case "lowercase":foreach (var row in data){if (row.ContainsKey(column)){row[column] = row[column].ToLower();}}break;default:return $"Unsupported operation: {operation}";}// 顯示前 5 行for (int i = 0; i < Math.Min(5, data.Count); i++){result += $"Row {i + 1}:\n";foreach (var kvp in data[i]){result += $"  {kvp.Key}: {kvp.Value}\n";}result += "\n";}return result;}
}

這個示例展示了如何實現數據處理工具,包括計算統計信息、過濾數據和轉換數據。這些工具可以幫助 AI 模型處理和分析結構化數據。

代碼生成

以下是一個使用 MCP 工具進行代碼生成的示例:

[McpServerToolType]
public static class CodeGenerationTools
{[McpServerTool, Description("Generates code based on a description.")]public static string GenerateCode([Description("Description of the code to generate")] string description,[Description("Programming language (csharp, python, javascript)")] string language = "csharp",[Description("Additional options (e.g., framework, style)")] string options = ""){// 根據語言選擇代碼生成模板var codeTemplate = language.ToLower() switch{"csharp" => GenerateCSharpCode(description, options),"python" => GeneratePythonCode(description, options),"javascript" => GenerateJavaScriptCode(description, options),_ => throw new ArgumentException($"Unsupported language: {language}")};return codeTemplate;}private static string GenerateCSharpCode(string description, string options){// 這里應該實現實際的代碼生成邏輯// 可以使用模板、規則或調用外部 API// 示例實現:生成一個簡單的 C# 類var className = GetClassName(description);var code = $@"// Generated C# code based on: {description}
using System;
using System.Threading.Tasks;namespace GeneratedCode
{{public class {className}{{public {className}(){{// Constructor}}public void Execute(){{Console.WriteLine(""Executing {className}..."");// TODO: Implement based on description// {description}}}public async Task ExecuteAsync(){{Console.WriteLine(""Executing {className} asynchronously..."");// TODO: Implement based on description// {description}await Task.CompletedTask;}}}}
}}";return code;}private static string GeneratePythonCode(string description, string options){// 示例實現:生成一個簡單的 Python 類var className = GetClassName(description);var code = $@"# Generated Python code based on: {description}
import asyncioclass {className}:def __init__(self):# Constructorpassdef execute(self):print(f'Executing {className}...')# TODO: Implement based on description# {description}async def execute_async(self):print(f'Executing {className} asynchronously...')# TODO: Implement based on description# {description}await asyncio.sleep(0)# Usage example
if __name__ == '__main__':obj = {className}()obj.execute()# Async usage# asyncio.run(obj.execute_async())";return code;}private static string GenerateJavaScriptCode(string description, string options){// 示例實現:生成一個簡單的 JavaScript 類var className = GetClassName(description);var code = $@"// Generated JavaScript code based on: {description}
class {className} {{constructor() {{// Constructor}}execute() {{console.log('Executing {className}...');// TODO: Implement based on description// {description}}}async executeAsync() {{console.log('Executing {className} asynchronously...');// TODO: Implement based on description// {description}await Promise.resolve();}}
}}// Usage example
const obj = new {className}();
obj.execute();// Async usage
// obj.executeAsync().then(() => console.log('Done'));";return code;}private static string GetClassName(string description){// 從描述中提取類名// 這里使用一個簡單的啟發式方法var words = description.Split(' ').Where(w => !string.IsNullOrEmpty(w)).Select(w => char.ToUpper(w[0]) + w.Substring(1).ToLower()).ToArray();return string.Join("", words).Replace(".", "").Replace(",", "");}
}

這個示例展示了如何實現代碼生成工具,可以根據描述生成不同編程語言的代碼。這些工具可以幫助 AI 模型生成可執行的代碼示例。

自動化任務

以下是一個使用 MCP 工具進行自動化任務的示例:

[McpServerToolType]
public class AutomationTools
{private readonly ILogger _logger;public AutomationTools(ILogger logger){_logger = logger;}[McpServerTool, Description("Schedules a task to run at a specified time.")]public string ScheduleTask([Description("Description of the task to schedule")] string taskDescription,[Description("When to run the task (format: yyyy-MM-dd HH:mm:ss)")] string scheduledTime,[Description("Whether to repeat the task daily")] bool repeatDaily = false){try{// 解析時間if (!DateTime.TryParse(scheduledTime, out var scheduleDateTime)){return $"Invalid date/time format: {scheduledTime}. Please use yyyy-MM-dd HH:mm:ss format.";}// 檢查時間是否在未來if (scheduleDateTime <= DateTime.Now){return "Scheduled time must be in the future.";}// 記錄任務信息_logger.LogInformation($"Task scheduled: {taskDescription}");_logger.LogInformation($"Scheduled time: {scheduleDateTime}");_logger.LogInformation($"Repeat daily: {repeatDaily}");// 在實際應用中,這里應該將任務添加到調度系統// 例如使用 Quartz.NET 或其他任務調度庫return $"Task scheduled successfully:\n" +$"- Description: {taskDescription}\n" +$"- Scheduled time: {scheduleDateTime}\n" +$"- Repeat daily: {repeatDaily}";}catch (Exception ex){_logger.LogError(ex, "Error scheduling task");return $"Error scheduling task: {ex.Message}";}}[McpServerTool, Description("Runs a command on the server.")]public async Task RunCommand([Description("Command to run")] string command,[Description("Working directory for the command")] string workingDirectory = "",[Description("Timeout in seconds (0 for no timeout)")] int timeoutSeconds = 60,CancellationToken cancellationToken = default){try{_logger.LogInformation($"Running command: {command}");// 設置工作目錄var workDir = string.IsNullOrEmpty(workingDirectory)? Directory.GetCurrentDirectory(): workingDirectory;// 檢查工作目錄是否存在if (!Directory.Exists(workDir)){return $"Working directory does not exist: {workDir}";}// 創建進程啟動信息var processStartInfo = new ProcessStartInfo{FileName = "cmd.exe",Arguments = $"/c {command}",WorkingDirectory = workDir,RedirectStandardOutput = true,RedirectStandardError = true,UseShellExecute = false,CreateNoWindow = true};// 啟動進程using var process = new Process { StartInfo = processStartInfo };process.Start();// 設置超時var timeoutTask = timeoutSeconds > 0? Task.Delay(timeoutSeconds * 1000, cancellationToken): Task.Delay(-1, cancellationToken);// 讀取輸出var outputTask = process.StandardOutput.ReadToEndAsync();var errorTask = process.StandardError.ReadToEndAsync();// 等待進程完成或超時var completedTask = await Task.WhenAny(Task.Run(() => process.WaitForExit(), cancellationToken),timeoutTask);// 檢查是否超時if (completedTask == timeoutTask && !process.HasExited){process.Kill();return $"Command timed out after {timeoutSeconds} seconds.";}// 獲取輸出var output = await outputTask;var error = await errorTask;// 返回結果return string.IsNullOrEmpty(error)? $"Command executed successfully:\n{output}": $"Command executed with errors:\n{error}\n\nOutput:\n{output}";}catch (Exception ex){_logger.LogError(ex, "Error running command");return $"Error running command: {ex.Message}";}}[McpServerTool, Description("Monitors a file or directory for changes.")]public string MonitorFileChanges([Description("Path to file or directory to monitor")] string path,[Description("Types of changes to monitor (Created, Deleted, Changed, Renamed)")] string changeTypes = "Created,Deleted,Changed,Renamed",[Description("Whether to include subdirectories")] bool includeSubdirectories = true){try{// 檢查路徑是否存在if (!File.Exists(path) && !Directory.Exists(path)){return $"Path does not exist: {path}";}// 解析變更類型var watcherChangeTypes = ParseChangeTypes(changeTypes);// 創建文件系統監視器var watcher = new FileSystemWatcher{Path = Directory.Exists(path) ? path : Path.GetDirectoryName(path),Filter = Directory.Exists(path) ? "*.*" : Path.GetFileName(path),NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,IncludeSubdirectories = includeSubdirectories};// 設置事件處理程序watcher.Changed += (sender, e) => _logger.LogInformation($"File changed: {e.FullPath}");watcher.Created += (sender, e) => _logger.LogInformation($"File created: {e.FullPath}");watcher.Deleted += (sender, e) => _logger.LogInformation($"File deleted: {e.FullPath}");watcher.Renamed += (sender, e) => _logger.LogInformation($"File renamed: {e.OldFullPath} to {e.FullPath}");// 啟動監視器watcher.EnableRaisingEvents = true;// 在實際應用中,應該將監視器存儲在某個地方,以便以后可以停止它return $"File monitoring started:\n" +$"- Path: {path}\n" +$"- Change types: {changeTypes}\n" +$"- Include subdirectories: {includeSubdirectories}";}catch (Exception ex){_logger.LogError(ex, "Error monitoring file changes");return $"Error monitoring file changes: {ex.Message}";}}private static NotifyFilters ParseChangeTypes(string changeTypes){var result = NotifyFilters.LastWrite;foreach (var type in changeTypes.Split(',').Select(t => t.Trim())){result |= type.ToLower() switch{"created" => NotifyFilters.FileName | NotifyFilters.DirectoryName,"deleted" => NotifyFilters.FileName | NotifyFilters.DirectoryName,"changed" => NotifyFilters.LastWrite,"renamed" => NotifyFilters.FileName | NotifyFilters.DirectoryName,_ => NotifyFilters.LastWrite};}return result;}
}

這個示例展示了如何實現自動化任務工具,包括調度任務、運行命令和監控文件變更。這些工具可以幫助 AI 模型自動化各種系統任務。

結論

本文檔提供了豐富的 .NET MCP 示例,涵蓋了服務器端實現、客戶端實現、LLM 集成和各種應用場景。這些示例可以幫助開發者快速上手 MCP,并將其應用到實際項目中。

MCP 的強大之處在于它提供了一種標準化的方式,使 AI 模型能夠安全地訪問和操作各種數據源和工具。通過實現 MCP 服務器和客戶端,開發者可以創建功能豐富的 AI 應用,使 AI 能夠執行各種任務,從簡單的文本處理到復雜的自動化操作。

隨著 MCP 生態系統的不斷發展,我們可以期待更多的功能和改進。官方的 C# SDK 提供了一個穩定的基礎,使 .NET 開發者能夠輕松地實現 MCP 服務器和客戶端。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/901683.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/901683.shtml
英文地址,請注明出處:http://en.pswp.cn/news/901683.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

智能語音處理+1.5使用PocketSphinxshinx實現語音轉文本(100%教會)

歡迎來到智能語音處理系列的最后一篇文章&#xff0c;到這里,基本上語音處理是沒問題了. 第一篇:智能語音處理1.1下載需要的庫(100%實現)-CSDN博客 第二篇:智能語音識別1.2用SAPI實現文本轉語音(100%教會)-CSDN博客 第三篇:智能語音處理1.3用SpeechLib實現文本轉語音(100%教會)…

Kubernetes 節點摘除指南

目錄 一、安全摘除節點的標準流程 1. 確認節點名稱及狀態 2. 標記節點為不可調度 3. 排空&#xff08;Drain&#xff09;節點 4. 刪除節點 二、驗證節點是否成功摘除 1. 檢查節點列表 2. 檢查節點詳細信息 3. 驗證 Pod 狀態 三、徹底清理節點&#xff08;可選&#xf…

信息安全管理與評估2021年國賽正式卷答案截圖以及十套國賽卷

2021年全國職業院校技能大賽高職組 “信息安全管理與評估”賽項 任務書1 賽項時間 共計X小時。 賽項信息 賽項內容 競賽階段 任務階段 競賽任務 競賽時間 分值 第一階段 平臺搭建與安全設備配置防護 任務1 網絡平臺搭建 任務2 網絡安全設備配置與防護 第二…

3D語義地圖中的全局路徑規劃!iPPD:基于3D語義地圖的指令引導路徑規劃視覺語言導航

作者&#xff1a; Zehao Wang, Mingxiao Li, Minye Wu, Marie-Francine Moens, Tinne Tuytelaars 單位&#xff1a;魯汶大學電氣工程系&#xff0c;魯汶大學計算機科學系 論文標題&#xff1a; Instruction-guided path planning with 3D semantic maps for vision-language …

《AI大模型應知應會100篇》第20篇:大模型倫理準則與監管趨勢

第20篇&#xff1a;大模型倫理準則與監管趨勢 摘要 隨著人工智能&#xff08;AI&#xff09;技術的飛速發展&#xff0c;尤其是大模型&#xff08;如GPT、PaLM等&#xff09;在自然語言處理、圖像生成等領域的廣泛應用&#xff0c;AI倫理問題和監管挑戰日益凸顯。本文將梳理當…

【Ai】dify:Linux環境安裝 dify 詳細步驟

一、什么是dify Dify 是一個 開源的大語言模型(LLM)應用開發平臺,旨在幫助開發者快速構建基于 AI 的應用程序,例如智能對話助手、知識庫問答、內容生成工具等。它提供了可視化的流程編排、模型集成、數據管理等功能,降低了開發門檻,支持快速迭代和部署。 核心功能與特點…

CentOS 操作系統下搭建 tsung性能測試環境

寫在前面 為何這么安裝,實際就是這么做的,這是經過好幾次實踐得出的經驗總結。 這為了讓大家更清楚的知道怎么安裝 tsung性能測試環境,按步照搬的安裝即可。 步驟 1、 下載軟件安裝包 CentOS-6.0-x86_64-bin-DVD1.iso jdk-6u4-linux-x64-rpm.bin erlang: otp_src_1…

Vulkanised

Vulkanised 1. About VulkanisedReferences The Premier Vulkan Developer Conference premier /?premi?(r)/ n. 總理&#xff1b;(尤用于報章等) 首相&#xff1b;(加拿大的) 省總理&#xff1b;地區總理 adj. 第一的&#xff1b;首要的&#xff1b;最著名的&#xff1b;最…

C++之 動態數組

一、新建一個動態數組 數組名和下標操作符[]的組合可以被替換成一個指向該數組的基地址的指針和對應的指針運算&#xff1a; int a[20]; int *x a; 指針變量 x 指向數組 a 的地址&#xff0c; a[0] 和 *x 都代表數組的第一個元素。 于是&#xff0c;根據指針運算原則&…

ubuntu1804服務器開啟ftp,局域網共享特定文件給匿名用戶

要在 Ubuntu 18.04 上設置一個 FTP 服務器&#xff0c;滿足以下要求&#xff1a; 允許匿名登錄&#xff08;無需賬號密碼&#xff09;。指定分享特定目錄下的文件。只允許只讀下載。 可以使用 vsftpd&#xff08;Very Secure FTP Daemon&#xff09;來實現。以下是詳細步驟&a…

mcp和API區別

MCP&#xff08;Model Context Protocol&#xff0c;模型上下文協議&#xff09;與傳統API&#xff08;Application Programming Interface&#xff0c;應用程序編程接口&#xff09;在技術架構、集成方式和應用場景等方面存在顯著差異&#xff0c;以下是主要區別的總結&#x…

高版本Android (AIDL HAL) 使用HIDL方法

目錄 修改步驟和編譯方法 注意事項 Android 11 引入了使用 AIDL 實現 HAL 的功能。 后續Android新版本,HAL默認切到了使用AIDL. 因此當導入舊HIDL實現方式時,需要做一些修改。 1.將HAL HIDL模塊拷貝到相應目錄,進行編譯 source build/envsetup.sh lunch xxx mmm 模塊路徑 1.…

基于redis 實現我的收藏功能優化詳細設計方案

基于redis 實現我的收藏功能優化詳細設計方案 一、架構設計 +---------------------+ +---------------------+ | 客戶端請求 | | 數據存儲層 | | (收藏列表查詢) | | (Redis Cluster) | +-------------------…

學習筆記 - Swfit 6.1 - 語法概覽

獲取版本號 swift -versionHello world print("Hello, world!")末尾不需要分號 值 常量(let),變量(var) var myVariable 42 myVariable 50 let myConstant 42可以顯式聲明變量類型,若沒有則隱式推斷,類似下面的Double let implicitInteger 70 let implicit…

確保連接器后殼高性能互連的完整性

本文探討了現代后殼技術如何促進高性能互連的電氣和機械完整性&#xff0c;以及在規范階段需要考慮的一些關鍵因素。 當今的航空航天、國防和醫療應用要求連接器能夠提供高速和緊湊的互連&#xff0c;能夠承受振動和沖擊&#xff0c;并保持對電磁和射頻干擾 &#xff08;EMI/R…

第IV部分有效應用程序的設計模式

第IV部分有效應用程序的設計模式 第IV部分有效應用程序的設計模式第23章:應用程序用戶界面的架構設計23.1設計考量23.2示例1:用于非分布式有界上下文的一個基于HTMLAF的、服務器端的UI23.3示例2:用于分布式有界上下文的一個基于數據API的客戶端UI23.4要點第24章:CQRS:一種…

學習筆記十四——一文看懂 Rust 迭代器

&#x1f300; 一文看懂 Rust 迭代器 &#x1f4da; 目錄導航 什么是迭代器&#xff1f;為什么 Rust 到處都在用它&#xff1f;Rust 迭代器的底層邏輯是什么&#xff1f;適配器 vs 消費者&#xff1a;誰是主角&#xff1f;常見適配器&#xff1a;加工數據的全能工廠常見消費者…

QR輕量二維碼生成系統PHP源碼

源碼介紹 基于PHP編寫的二維碼在線生成系統。只需點擊幾下就可以生成您的個人二維碼&#xff01;上傳您的徽標&#xff0c;選擇自定義顏色&#xff0c;生成多種類型。選擇一個圖案并下載最終的qrcode。可用格式&#xff1a;.png&#xff0c;.svg 效果預覽 源碼獲取 QR輕量二…

基于Spring MVC的客戶端真實IP獲取方案解析

文章目錄 基于Spring MVC的客戶端真實IP獲取方案解析概述核心方法解析代碼實現工作流程 IP獲取優先級策略IP有效性驗證異常處理與日志使用場景注意事項擴展建議 基于Spring MVC的客戶端真實IP獲取方案解析 概述 在Web應用開發中&#xff0c;準確獲取客戶端真實IP地址是常見的…

Idea集成AI:CodeGeeX開發

當入職新公司&#xff0c;或者調到新項目組進行開發時&#xff0c;需要快速熟悉項目代碼 而新的項目代碼&#xff0c;可能有很多模塊&#xff0c;很多的接口&#xff0c;很復雜的業務邏輯&#xff0c;更加有與之前自己的代碼風格不一致的現有復雜代碼 更別提很多人寫代碼不喜…