文本转换服务器

一个基于C# .NET 9的MCP服务器实现,提供文本大小写转换工具,兼容Claude Desktop等MCP客户端。

  • by msv2017
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 msv2017/test-mcp-server

使用说明

MCP文本转换服务器

A. 模型上下文协议(MCP)服务器 用C#实现。NET 9提供了文本转换工具。此服务器与Claude Desktop和其他MCP客户端兼容。

📖 了解有关MCP的更多信息:访问 官方模型上下文协议文档 获取全面的指南和示例。

✨ 特性

此MCP服务器提供两个强大的文本转换工具:

  1. CapitalCase工具 (capital_case)

    • 将文本转换为大写(标题大小写)格式
    • 通过智能添加空格来处理camelCase/PascalCase
    • 例子: "camelCaseExample" → "Camel Case Example"
  2. SnakeCase工具 (snake_case)

    • 将文本转换为snake_case格式
    • 处理camelCase、PasclCase和空格
    • 例子: "HelloWorld" → "hello_world"

🏗️ 建筑

该项目使用 官方模型上下文协议SDK 遵循现代C#实践:

  • MCP协议合规性:用途 ModelContextProtocol NuGet包
  • STDIO传输:通过标准输入/输出(JSON-RPC)进行通信
  • 基于属性的工具:简单 [McpServerTool] 装饰
  • 依赖注入:内置。NET DI容器
  • 结构化日志记录:已配置MCP兼容性(stderr)
  • 现代C#特性: .NET 9、可空引用类型、异步模式

📁 项目结构

McpServer/
├── Tools/                     # MCP tool implementations
│   └── TextTransformTools.cs  # Capital case & snake case tools
├── Program.cs                 # MCP server entry point
├── McpServer.csproj          # Project file with MCP SDK
└── README.md                 # This documentation

🚀 入门指南

先决条件

  • .NET 9.0 SDK或更高版本
  • Claude Desktop(用于测试MCP集成)

构建服务器

# Clone and build
git clone <repository-url>
cd test-mcp-server
dotnet build

# Test the server
dotnet run

测试工具

服务器提供以下文本转换功能:

Input: "camelCaseExample"
├── CapitalCase → "Camel Case Example"  
└── SnakeCase   → "camel_case_example"

Input: "Hello World"
├── CapitalCase → "Hello World"
└── SnakeCase   → "hello_world"

🖥️ Claude桌面集成

要将此MCP服务器与Claude Desktop一起使用,请添加以下配置:

步骤1:找到Claude桌面配置

视窗: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Linux: ~/.config/Claude/claude_desktop_config.json

步骤2:添加服务器配置

{
  "mcpServers": {
    "text-transform": {
      "command": "dotnet",
      "args": [
        "run",
        "--project",
        "/path/to/your/test-mcp-server/McpServer.csproj",
        "--no-build"
      ]
    }
  }
}

替换 /path/to/your/test-mcp-server 使用您的实际项目路径:

Windows示例:

{
  "mcpServers": {
    "text-transform": {
      "command": "dotnet",
      "args": [
        "run",
        "--project",
        "C:\\Users\\YourName\\projects\\test-mcp-server\\McpServer.csproj",
        "--no-build"
      ]
    }
  }
}

macOS/Linux示例:

{
  "mcpServers": {
    "text-transform": {
      "command": "dotnet",
      "args": [
        "run",
        "--project",
        "/home/username/projects/test-mcp-server/McpServer.csproj",
        "--no-build"
      ]
    }
  }
}

重要提示:

  • 使用 绝对路径 到 McpServer.csproj 文件
  • 使用 双睫毛 (\\)在Windows上实现JSON转义
  • 确保项目文件路径存在
  • 这 --no-build 标志加速服务器启动(推荐)

步骤3:重新启动克劳德桌面

保存配置后,重新启动Claude Desktop。文本转换工具将在您的对话中可用。

步骤4:在Claude Desktop中进行测试

尝试以下提示以测试集成:

"Please convert 'HelloWorldExample' to capital case"
"Transform 'My Example Text' to snake_case format"  
"Convert 'camelCaseVariable' to title case"

🔧 发展

添加新工具

要添加新的MCP工具,只需将方法添加到 TextTransformTools 类别:

[McpServerTool]
[Description("Your tool description")]
public static string YourTool([Description("Input parameter")] string input)
{
    // Your transformation logic
    return transformedText;
}

本地测试

# Build and run
dotnet run

# Test with JSON-RPC (the server expects MCP protocol messages)
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' | dotnet run

🛠️ 故障排除

Claude Desktop未检测到MCP服务器

如果Claude Desktop未显示文本转换工具,请按照以下步骤操作:

1.验证服务器是否在本地工作

# Test the server responds to MCP commands:
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' | dotnet run

2.检查Claude桌面配置

配置文件的位置:

  • 视窗: %APPDATA%\Claude\claude_desktop_config.json
  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

精确配置格式:

{
  "mcpServers": {
    "text-transform": {
      "command": "dotnet",
      "args": [
        "run",
        "--project",
        "/path/to/your/test-mcp-server/McpServer.csproj",
        "--no-build"
      ]
    }
  }
}

替换 /path/to/your/test-mcp-server 使用您的实际项目路径:

窗户:

"args": [
  "run",
  "--project",
  "C:\\Users\\YourName\\projects\\test-mcp-server\\McpServer.csproj",
  "--no-build"
]

macOS/Linux:

"args": [
  "run",
  "--project",
  "/home/username/projects/test-mcp-server/McpServer.csproj",
  "--no-build"
]

重要提示:

  • 使用 绝对路径 到 McpServer.csproj 文件
  • 使用 双睫毛 (\\)在Windows上实现JSON转义
  • 确保项目文件路径存在且正确
  • 这 command 应 dotnet (非完整路径)

3.常见问题

  1. 在Claude Desktop中“找不到服务器”

    • 验证中的文件路径 claude_desktop_config.json 存在
    • 确保项目成功构建(dotnet build)
    • 检查一下。NET 9 SDK已安装(dotnet --version)
    • 尝试使用正斜杠 / 而不是路径中的反斜杠
  2. 服务器已启动,但工具不可用

    • 配置更改后完全重新启动Claude Desktop
    • 检查Claude Desktop的控制台/日志以获取错误消息
    • 验证配置中的服务器名称是否完全匹配(text-transform)
  3. 构建错误

    • 跑 dotnet restore 确保所有NuGet包都已安装
    • 确认你有。NET 9.0 SDK或更高版本
    • 尝试 dotnet clean 随后 dotnet build

4.高级调试

使用手动JSON-RPC进行测试:

# Initialize the server
echo '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0.0"}}}' | dotnet run

# List available tools  
echo '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}' | dotnet run

# Test tool execution
echo '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "CapitalCase", "arguments": {"text": "hello world"}}}' | dotnet run

检查克劳德桌面日志:

  • Windows:在事件查看器或Claude的应用程序数据文件夹中查找
  • macOS:使用Console.app查看日志
  • Linux:检查系统日志或从终端运行Claude

MCP协议详细信息

  1. 服务器已启动,但工具不可用

    • 配置更改后重新启动Claude Desktop
    • 在Claude Desktop的控制台中检查服务器日志
  2. 构建错误

    • 跑 dotnet restore 确保所有NuGet包都已安装
    • 确认你有。NET 9.0 SDK或更高版本

MCP协议详细信息

此服务器实现了模型上下文协议规范:

  • 运输:STDIO(通过标准输入/标准输出的JSON-RPC)
  • 协议版本:与MCP客户端兼容
  • 工具发现:自动通过 [McpServerTool] 属性
  • 错误处理:标准JSON-RPC错误响应

有关MCP协议的更多详细信息,请参阅 官方文件.

📚 技术细节

使用的MCP SDK功能

  • ModelContextProtocol v0.2.0--评测。2
  • STDIO传输与Claude Desktop兼容
  • 基于属性的工具注册
  • 自动参数验证和记录

依赖项

<PackageReference Include="ModelContextProtocol" Version="0.2.0-preview.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />

📄 许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 添加新的MCP工具或增强功能
  4. 提交您的更改(git commit -m 'Add amazing feature')
  5. 推到分支(git push origin feature/amazing-feature)
  6. 打开拉取请求

🔮 路线图

  • []添加更多文本转换工具(烤肉串案例、UPPER_case等)
  • []实施文本验证和净化工具
  • []添加对批量文本处理的支持
  • []创建全面的测试套件
  • []添加性能基准 “ServerName”:“文本转换MCP服务器”, “版本”:“1.0.0”, “端口”:8080, “日志记录”:{ “LogLevel”:“信息”, “EnableStructuredLogging”:true } } }

## Health Check

A health check endpoint is available at `/health` to verify the server status.

## Development

### Code Style

The project uses `.editorconfig` for consistent code formatting and follows:

- PascalCase for classes, methods, properties
- camelCase for private fields and parameters
- Underscore prefix for private fields (`_fieldName`)
- File-scoped namespaces
- Nullable reference types enabled

### Logging

Structured logging is implemented using `ILogger<T>` with appropriate log levels:

- **Debug**: Detailed transformation steps
- **Information**: Tool execution and server lifecycle
- **Warning**: Invalid inputs or missing tools
- **Error**: Exceptions and failures

### Testing

The project structure supports unit testing with:

- Dependency injection for testability
- Interface-based programming
- Separation of concerns

## MCP Protocol

This server implements the Model Context Protocol for integration with MCP-compatible clients. The tools can be discovered and executed through the standard MCP tool calling interface.

## License

This project is provided as-is for educational and development purposes.