当前位置: 首页 > news >正文

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 服务器和客户端。

相关文章:

MCP协议,.Net 使用示例

服务器端示例 基础服务器 以下是一个基础的 MCP 服务器示例&#xff0c;它使用标准输入输出&#xff08;stdio&#xff09;作为传输方式&#xff0c;并实现了一个简单的回显工具&#xff1a; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.H…...

Node.js 数据库 事务 项目示例

1、参考&#xff1a;JavaScript语言的事务管理_js 函数 事务性-CSDN博客 或者百度搜索&#xff1a;Nodejs控制事务&#xff0c; 2、实践 2.1、对于MySQL或MariaDB&#xff0c;你可以使用mysql或mysql2库&#xff0c;并结合Promise或async/await语法来控制事务。 使用 mysql2…...

【AI插件开发】Notepad++ AI插件开发实践:支持多平台多模型

引言 上篇文章我们的Notepad插件介绍到Dock窗口集成&#xff0c;本篇将继续完善插件功能&#xff0c;主要包括两个部分&#xff1a; 支持多平台、多模型支持多种授权验证、接口类型 一、多平台 原先的配置项很简单&#xff1a; // PluginConf.h class PlatformConf { publ…...

微信小程序数字滚动效果

效果图 .wxml <view class"container"><view class"container-num" wx:for"{{number}}" wx:key"index"><view class"num-container" style"--h:{{h}}px;--y:{{-item * h }}px;"><view wx:f…...

wx219基于ssm+vue+uniapp的教师管理系统小程序

开发语言&#xff1a;Java框架&#xff1a;ssmuniappJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;M…...

Python 注释进阶之Google风格

文章目录 1. Google 风格 Docstring 的核心特点2. Google 风格的基本结构3. 编写规则和注意事项4. 最常用的 Google 风格 Docstring 示例示例 1&#xff1a;普通函数 示例 2&#xff1a;带默认参数和可变参数的函数示例 3&#xff1a;类示例 4&#xff1a;生成器函数示例 5&…...

写测试文档时,需要的环境配置怎么查看

操作系统 cat /etc/os-releaseCPU信息 lscpu 内存 sudo dmidecode --type memory | grep -E "Size:|Type:|Speed:"硬盘 列出当前系统中 所有块设备&#xff08;Block Devices&#xff09; 的信息&#xff0c;并显示指定列&#xff08;-o 参数&#xff09; lsblk…...

强化学习的数学原理(十)actor-critic 方法

由于全文太长&#xff0c;只好分开发了。(已完结&#xff01;在专栏查看本系列其他文章&#xff09; 个人博客可以直接看全文~ 本系列为在学习赵世钰老师的“强化学习的数学原理” 课程后所作笔记。 课堂视频链接https://www.bilibili.com/video/BV1sd4y167NS/ 第十章 acto…...

多个定时器同时工作时,会出现哪些常见的bug ,如何解决??(定时任务未实时更新但刷新后正常的问题分析)

1. 定时器冲突与覆盖 问题&#xff1a;后设置的定时器可能覆盖先前的定时器&#xff0c;导致前一个定时器失效 原因&#xff1a;未正确管理定时器ID或未清除前一个定时器 2. 性能问题 内存泄漏&#xff1a;未清除不再需要的定时器会导致内存占用不断增加 CPU过载&#xff1a…...

代码随想录算法训练营day5(哈希表)

华子目录 有效的字母异位词思路 有效的字母异位词 https://leetcode.cn/problems/valid-anagram/description/ 思路 使用哈希表&#xff0c;这里哈希表使用数组先申请一个26空间的大小的数组遍历第一个字符串&#xff0c;记录每个字符出现的次数1遍历第二个字符串&#xff0c…...

Python(17)Python字符编码完全指南:从存储原理到乱码终结实战

目录 背景介绍一、字符编码核心原理1. 计算机存储本质2. Python3的编码革命3. 主流编码格式对比 二、编码转换核心方法1. 编码&#xff08;Encode&#xff09;过程2. 解码&#xff08;Decode&#xff09;过程3. 错误处理策略 三、文件操作编码实战1. 文本文件读写2. 二进制模式…...

Node.js 文件读取与复制相关内容

Node.js 文件读取与复制相关内容的系统总结&#xff0c;包括 同步读取、异步读取、流式读取、复制操作、两者对比及内存测试。 &#x1f9e9; 一、Node.js 文件读取方式总结 Node.js 使用 fs&#xff08;文件系统&#xff09;模块进行文件操作&#xff1a; 1. 同步读取&#…...

大数据面试问答-HBase/ClickHouse

1. HBase 1.1 概念 HBase是构建在Hadoop HDFS之上的分布式NoSQL数据库&#xff0c;采用列式存储模型&#xff0c;支持海量数据的实时读写和随机访问。适用于高吞吐、低延迟的场景&#xff0c;如实时日志处理、在线交易等。 RowKey&#xff08;行键&#xff09; 定义&#xf…...

jupyter 文件浏览器,加强版,超好用,免费exe

第一步&#xff1a;github搜索 lukairui的 jupyter-viewer-plus 仓库 第二步&#xff1a; git clone 到本地。 解压zip包 第三步&#xff1a; 进入压缩包&#xff0c;第一次双击打开jupyter-viewer-plus.exe运行&#xff0c;第一次运行后&#xff0c;界面上有一个“设为…...

【AI工具】用大模型生成脑图初试

刚试用了一下通过大模型生成脑图&#xff0c;非常简单&#xff0c;记录一下 一、用大模型生成脑图文件 关键&#xff1a;存在markdown文件 举例&#xff1a;使用Deepseek&#xff0c;输入问题&#xff1a;“针对大模型的后训练&#xff0c;生成一个开发计划&#xff0c;用ma…...

数据结构-树与二叉树

一、树的定义与基本术语 1.1 树的定义 树&#xff08;Tree&#xff09;是一种非线性的数据结构&#xff0c;它是由 n&#xff08;n ≥ 0&#xff09;个有限节点组成的集合。如果 n 0&#xff0c;称为空树&#xff1b;如果 n > 0&#xff0c;则&#xff1a; 有一个特定的节…...

STL_unordered_map_01_基本用法

&#x1f44b; Hi, I’m liubo&#x1f440; I’m interested in harmony&#x1f331; I’m currently learning harmony&#x1f49e;️ I’m looking to collaborate on …&#x1f4eb; How to reach me …&#x1f4c7; sssssdsdsdsdsdsdasd&#x1f383; dsdsdsdsdsddfsg…...

ARCGIS国土超级工具集1.5更新说明

ARCGIS国土超级工具集V1.5版本更新说明&#xff1a;因作者近段时间工作比较忙及正在编写ARCGISPro国土超级工具集&#xff08;截图附后&#xff09;的原因&#xff0c;故本次更新为小更新&#xff08;没有增加新功能&#xff0c;只更新了已有的工具&#xff09;。本次更新主要修…...

主流物理仿真引擎和机器人/强化学习仿真平台对比

以下是当前主流的物理仿真引擎和机器人/强化学习仿真平台的特点和适用场景&#xff0c;方便根据需求选择&#xff1a; &#x1f9e0; NVIDIA 系列 ✅ Isaac Lab v1.4 / v2 特点&#xff1a; 基于 Omniverse Isaac Sim&#xff0c;属于高端视觉机器人仿真框架v2 更加模块化&a…...

STM32 HAL库内部 Flash 读写实现

一、STM32F407 内部 Flash 概述 1.1 Flash 存储器的基本概念 Flash 存储器是一种非易失性存储器&#xff0c;它可以在掉电的情况下保持数据。STM32F407 系列微控制器内部集成了一定容量的 Flash 存储器&#xff0c;用于存储程序代码和数据。Flash 存储器具有擦除和编程次数的…...

C++学习:六个月从基础到就业——面向对象编程:构造函数与析构函数

C学习&#xff1a;六个月从基础到就业——面向对象编程&#xff1a;构造函数与析构函数 本文是我C学习之旅系列的第十篇技术文章&#xff0c;主要讨论C中构造函数与析构函数的概念、特点和使用技巧。这些是C对象生命周期管理的关键组成部分。查看完整系列目录了解更多内容。 引…...

dfs二叉树中的深搜(回溯、剪枝)--力扣129、814、230、257

目录 1.1题目链接&#xff1a;129.求根节点到叶结点数字之和 1.2题目描述&#xff1a;给你一个二叉树的根节点 root &#xff0c;树中每个节点都存放有一个 0 到 9 之间的数字。 1.3解法(dfs-前序遍历)&#xff1a; 2.1题目链接&#xff1a;814.二叉树剪枝 2.2题目描述&…...

Python Selenium 一小时速通教程

Python Selenium 一小时速通教程 实战案例 一、环境配置&#xff08;10分钟&#xff09; 安装Python 确保已安装Python 3.x&#xff08;官网下载&#xff09;。 安装Selenium 在终端运行&#xff1a; pip install selenium下载浏览器驱动 Chrome&#xff1a;访问 ChromeDriv…...

通过GO后端项目实践理解DDD架构

最近在工作过程中重构的项目要求使用DDD架构&#xff0c;在网上查询资料发现教程五花八门&#xff0c;并且大部分内容都是长篇的概念讲解&#xff0c;晦涩难懂&#xff0c;笔者看了一些github上入门的使用DDD的GO项目&#xff0c;并结合自己开发中的经验&#xff0c;谈谈自己对…...

MybatisPlus最新版分页无法使用

在使用分页的时候发现分页拦截器关键API会报错&#xff0c;其实根本原因是在之前只需要导入一个mybatisplus依赖&#xff0c;而现在分页似乎被单独分离出来了&#xff0c;需要额外导入新依赖使其支持 ​ <dependency><groupId>com.baomidou</groupId><art…...

【Android学习记录】工具使用

文章目录 一. 精准找视图资源ID1. 准备工作2. 使用 uiautomator 工具2.1. 获取设备的窗口内容2.2. Pull XML 文件2.3. 查看 XML 文件 3. 直接使用 ADB 命令4. 使用 Android Studio 的 Layout Inspector总结 二. adb shell dumpsys activity1. 如何使用 ADB 命令2. 输出内容解析…...

youtube视频和telegram视频加载原理差异分析

1. 客户侧缓存与流式播放机制​​ 流式视频应用&#xff08;如 Netflix、YouTube&#xff09;通过​​边下载边播放​​实现流畅体验&#xff0c;其核心依赖以下技术&#xff1a; ​​缓存预加载​​&#xff1a;客户端在后台持续下载视频片段&#xff08;如 DASH/HLS 协议的…...

在机器视觉检测中为何选择线阵工业相机?

线阵工业相机&#xff0c;顾名思义是成像传感器呈“线”状的。虽然也是二维图像&#xff0c;但极宽&#xff0c;几千个像素的宽度&#xff0c;而高度却只有几个像素的而已。一般在两种情况下使用这种相机&#xff1a; 1. 被测视野为细长的带状&#xff0c;多用于滚筒上检测的问…...

lwip记录

Index of /releases/lwip/ (gnu.org) 以太网(Ethernet)是互联网技术的一种&#xff0c;由于它是在组网技术中占的比例最高&#xff0c;很多人 直接把以太网理解为互联网。 以太网是指遵守 IEEE 802.3 标准组成的局域网&#xff0c;由 IEEE 802.3 标准规定的主要是位于 参考模…...

Redis清空缓存

尽管redis可以设置ttl过期时间进行指定key的定时删除&#xff0c;但是在某些场景下&#xff0c;比如&#xff1a; 测试时需要批量删除指定库下所有库下所有的数据&#xff0c;则会涉及到缓存清除的话题。 如下为具体的操作及说明&#xff1a; 场景类型操作指令清空当前库下所有…...

WPF 依赖注入启动的问题

原因是在App.xaml 设置了 StartupUri“MainWindow.xaml” 1.依赖注入后启动的主窗体存在无参构造 程序正常启动&#xff0c;但是主窗体界面会弹出2个窗体。 2.依赖注入后启动的主窗体存在有参构造 报错...

Arcgis经纬线标注设置(英文、刻度显示)

在arcgis软件中绘制地图边框&#xff0c;添加经纬度度时常常面临经纬度出现中文&#xff0c;如下图所示&#xff1a; 解决方法&#xff0c;设置一下Arcgis的语言 点击高级--确认 这样Arcgis就转为英文版了&#xff0c;此时在来看经纬线刻度的标注&#xff0c;自动变成英文...

【电子通识】案例:电缆的安装方式也会影响设备的可靠性?

背景 在日常生活中&#xff0c;我们常常会忽略一些看似微不足道的细节&#xff0c;但这些细节有时却能决定设备的寿命和安全性。比如&#xff0c;你知道吗&#xff1f;一根电缆的布置方式&#xff0c;可能会决定你的设备是否会因为冷凝水而损坏。 今天&#xff0c;我们就来聊聊…...

房屋装修费用预算表:45594 =未付14509 + 付清31085【时间:20250416】

文章目录 引言I 房屋装修费用预算表II 市场价参考防水搬运3000III 装修计划整体流程进度细节国补IV 付款凭证(销售单)伟星 PPR +PVC+太阳线+地漏=6500入户门设计通铺大板瓷砖 | 湿贴 3408(地)+3600(加)+5209(墙)=12217元门头铁空调引言 关注我,发送【装修记账】获取预…...

Python文件操作完全指南:从基础到高级应用

目录 一、文件基础概念 1.1 什么是文件&#xff1f; 1.2 文件的存储方式 文本文件 二进制文件 二、Python文件操作基础 2.1 文件操作三步曲 2.2 核心函数与方法 2.3 文件读取详解 基本读取示例 文件指针机制 2.4 文件打开模式 写入文件示例 2.5 高效读取大文件 三…...

03(总)-docker篇 Dockerfile镜像制作(jdk,jar)与jar包制作成docker容器方式

全文目录,一步到位 1.前言简介1.1 专栏传送门1.1.2 上文传送门 2. docker镜像制作一: jdk2.1 制作jdk镜像2.1.1 准备工作2.1.2 jdk镜像的Dockerfile2.1.3 基于Dockerfile构建镜像2.1.4 docker使用镜像运行容器2.1.5 进入jdk1.8容器内测试 3. docker镜像制作二: java镜像(jar包)…...

CUDA的安装

打开nvidia控制面板 找到组件 打开 CUDA Toolkit Archive | NVIDIA Developer 下载CUDA...

四六级听力调频广播有线传输无线覆盖系统:弥补单一发射系统安全缺陷,构建稳定可靠听力系统平台

四六级听力调频广播有线传输无线覆盖系统:弥补单一发射系统安全缺陷&#xff0c;构建稳定可靠听力系统平台 北京海特伟业科技有限公司任洪卓发布于2025年4月16日 随着英语四六级考试的规模不断扩大&#xff0c;听力考试部分的设备可靠性问题日益凸显。传统的无线发射系统存在…...

信创服务器-大国崛起,信创当道!

信创产业是数据安全、网络安全的基础&#xff0c;也是新基建的重要组成部分。在政策的推动下&#xff0c;2020-2022 年&#xff0c;中国信创服务器出货量整体呈现出快速增长的趋势&#xff0c;其中党政、电信、金融等领域采购频次高&#xff0c;单次采购量大&#xff0c;是中国…...

【仿Mudou库one thread per loop式并发服务器实现】SERVER服务器模块实现

SERVER服务器模块实现 1. Buffer模块2. Socket模块3. Channel模块4. Poller模块5. EventLoop模块5.1 TimerQueue模块5.2 TimeWheel整合到EventLoop5.1 EventLoop与线程结合5.2 EventLoop线程池 6. Connection模块7. Acceptor模块8. TcpServer模块 1. Buffer模块 Buffer模块&…...

冒泡与 qsort 排序策略集

今天我们要学习两种排序方法&#xff0c;分别是冒泡排序和qsort函数排序,冒泡排序相对qsort函数排序要简单一点&#xff0c;更易于理解。 1.冒泡排序 冒泡排序&#xff08;Bubble Sort&#xff09;是一种简单的排序算法&#xff0c;它通过重复遍历元素列并比较相邻元素来实现排…...

【Linux】第七章 控制对文件的访问

目录 1. 什么是文件系统权限&#xff1f;它是如何工作的&#xff1f;如何查看文件的权限&#xff1f; 2. 解释‘-rw-r--r--’这个字符串。 3. 使用什么命令可以更改文件和目录的权限&#xff1f;写出分别使用符号法和数值法将权限从 754 修改为 775 的命令。 4. 如何修改文…...

网站301搬家后谷歌一直不收录新页面怎么办?

当网站因更换域名或架构调整启用301重定向后&#xff0c;许多站长发现谷歌迟迟不收录新页面&#xff0c;甚至流量大幅下滑。 例如&#xff0c;301跳转设置错误可能导致权重传递失效&#xff0c;而新站内容与原站高度重复则可能被谷歌判定为“低价值页面”。 即使技术层面无误&a…...

socket 客户端和服务器通信

服务器 using BarrageGrab; using System; using System.Collections.Concurrent; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading;namespace Lyx {class Server{private TcpListener listener;private Concurre…...

C实现md5功能

md5在线验证&#xff1a; 在线MD5计算_ip33.com 代码如下&#xff1a; #include "md5.h" #include <string.h> #include "stdio.h"/** 32-bit integer manipulation macros (little endian)*/ #ifndef GET_ULONG_LE #define GET_ULONG_LE(n,b,i) …...

【项目】CherrySudio配置MCP服务器

CherrySudio配置MCP服务器 &#xff08;一&#xff09;Cherry Studio介绍&#xff08;二&#xff09;MCP服务环境搭建&#xff08;1&#xff09;环境准备&#xff08;2&#xff09;依赖组件安装<1> Bun和UV安装 &#xff08;3&#xff09;MCP服务器使用<1> 搜索MCP…...

第五节:React Hooks进阶篇-如何用useMemo/useCallback优化性能

反模式&#xff1a;滥用导致的内存开销React 19编译器自动Memoization原理 React Hooks 性能优化进阶&#xff1a;从手动到自动 Memoization &#xff08;基于 React 18 及以下版本&#xff0c;结合 React 19 新特性分析&#xff09; 一、useMemo/useCallback 的正确使用场景…...

【Qt】QWidget 核⼼属性详解

&#x1f351;个人主页&#xff1a;Jupiter. &#x1f680; 所属专栏&#xff1a;QT 欢迎大家点赞收藏评论&#x1f60a; 目录 &#x1f3dd; 一.相关概念&#x1f3a8;二. 核⼼属性概览&#x1f344;2.1 enabled&#x1f96d;2.2geometry&#x1f338; 2.3 windowTitle&#…...

如何知道raid 有问题了

在 Rocky Linux 8 上&#xff0c;你的服务器使用了 RAID5&#xff08;根据 lsblk 输出&#xff0c;/dev/sda3、/dev/sdb1 和 /dev/sdc1 组成 md127 RAID5 阵列&#xff09;。为了监控 RAID5 阵列中磁盘的健康状态&#xff0c;并及时发现某块磁盘损坏&#xff0c;可以通过以下方…...

操作系统之shell实现(上)

&#x1f31f; 各位看官好&#xff0c;我是maomi_9526&#xff01; &#x1f30d; 种一棵树最好是十年前&#xff0c;其次是现在&#xff01; &#x1f680; 今天来学习C语言的相关知识。 &#x1f44d; 如果觉得这篇文章有帮助&#xff0c;欢迎您一键三连&#xff0c;分享给更…...