csharp-mcp-server-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerate C# MCP Server
生成C# MCP服务器
Create a complete Model Context Protocol (MCP) server in C# with the following specifications:
创建一个符合以下规范的完整Model Context Protocol (MCP)服务器:
Requirements
需求
- Project Structure: Create a new C# console application with proper directory structure
- NuGet Packages: Include ModelContextProtocol (prerelease) and Microsoft.Extensions.Hosting
- Logging Configuration: Configure all logs to stderr to avoid interfering with stdio transport
- Server Setup: Use the Host builder pattern with proper DI configuration
- Tools: Create at least one useful tool with proper attributes and descriptions
- Error Handling: Include proper error handling and validation
- 项目结构:创建一个具有合理目录结构的全新C#控制台应用程序
- NuGet包:包含ModelContextProtocol(预发布版)和Microsoft.Extensions.Hosting
- 日志配置:将所有日志输出到stderr,避免干扰stdio传输
- 服务器设置:使用Host构建器模式并配置正确的依赖注入(DI)
- 工具:创建至少一个实用工具,并添加合适的特性与描述
- 错误处理:包含完善的错误处理与验证机制
Implementation Details
实现细节
Basic Project Setup
基础项目设置
- Use .NET 8.0 or later
- Create a console application
- Add necessary NuGet packages with --prerelease flag
- Configure logging to stderr
- 使用.NET 8.0或更高版本
- 创建控制台应用程序
- 添加必要的NuGet包,加上--prerelease参数
- 配置日志输出到stderr
Server Configuration
服务器配置
- Use for DI and lifecycle management
Host.CreateApplicationBuilder - Configure with stdio transport
AddMcpServer() - Use for automatic tool discovery
WithToolsFromAssembly() - Ensure the server runs with
RunAsync()
- 使用进行依赖注入和生命周期管理
Host.CreateApplicationBuilder - 配置并使用stdio传输
AddMcpServer() - 使用自动发现工具
WithToolsFromAssembly() - 确保服务器通过启动运行
RunAsync()
Tool Implementation
工具实现
- Use attribute on tool classes
[McpServerToolType] - Use attribute on tool methods
[McpServerTool] - Add attributes to tools and parameters
[Description] - Support async operations where appropriate
- Include proper parameter validation
- 在工具类上使用特性
[McpServerToolType] - 在工具方法上使用特性
[McpServerTool] - 为工具和参数添加特性
[Description] - 酌情支持异步操作
- 包含完善的参数验证
Code Quality
代码质量
- Follow C# naming conventions
- Include XML documentation comments
- Use nullable reference types
- Implement proper error handling with McpProtocolException
- Use structured logging for debugging
- 遵循C#命名规范
- 添加XML文档注释
- 使用可空引用类型
- 使用McpProtocolException实现完善的错误处理
- 使用结构化日志进行调试
Example Tool Types to Consider
可参考的工具类型示例
- File operations (read, write, search)
- Data processing (transform, validate, analyze)
- External API integrations (HTTP requests)
- System operations (execute commands, check status)
- Database operations (query, update)
- 文件操作(读取、写入、搜索)
- 数据处理(转换、验证、分析)
- 外部API集成(HTTP请求)
- 系统操作(执行命令、检查状态)
- 数据库操作(查询、更新)
Testing Guidance
测试指导
- Explain how to run the server
- Provide example commands to test with MCP clients
- Include troubleshooting tips
Generate a complete, production-ready MCP server with comprehensive documentation and error handling.
- 说明如何启动服务器
- 提供使用MCP客户端进行测试的示例命令
- 包含故障排除技巧
生成一个完整、可用于生产环境的MCP服务器,并附带全面的文档和错误处理机制。