csharp-mcp-server-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Generate C# MCP Server

生成C# MCP服务器

Create a complete Model Context Protocol (MCP) server in C# with the following specifications:
创建一个符合以下规范的完整Model Context Protocol (MCP)服务器:

Requirements

需求

  1. Project Structure: Create a new C# console application with proper directory structure
  2. NuGet Packages: Include ModelContextProtocol (prerelease) and Microsoft.Extensions.Hosting
  3. Logging Configuration: Configure all logs to stderr to avoid interfering with stdio transport
  4. Server Setup: Use the Host builder pattern with proper DI configuration
  5. Tools: Create at least one useful tool with proper attributes and descriptions
  6. Error Handling: Include proper error handling and validation
  1. 项目结构:创建一个具有合理目录结构的全新C#控制台应用程序
  2. NuGet包:包含ModelContextProtocol(预发布版)和Microsoft.Extensions.Hosting
  3. 日志配置:将所有日志输出到stderr,避免干扰stdio传输
  4. 服务器设置:使用Host构建器模式并配置正确的依赖注入(DI)
  5. 工具:创建至少一个实用工具,并添加合适的特性与描述
  6. 错误处理:包含完善的错误处理与验证机制

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
    Host.CreateApplicationBuilder
    for DI and lifecycle management
  • Configure
    AddMcpServer()
    with stdio transport
  • Use
    WithToolsFromAssembly()
    for automatic tool discovery
  • Ensure the server runs with
    RunAsync()
  • 使用
    Host.CreateApplicationBuilder
    进行依赖注入和生命周期管理
  • 配置
    AddMcpServer()
    并使用stdio传输
  • 使用
    WithToolsFromAssembly()
    自动发现工具
  • 确保服务器通过
    RunAsync()
    启动运行

Tool Implementation

工具实现

  • Use
    [McpServerToolType]
    attribute on tool classes
  • Use
    [McpServerTool]
    attribute on tool methods
  • Add
    [Description]
    attributes to tools and parameters
  • 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服务器,并附带全面的文档和错误处理机制。