由Roslyn驱动的MCP服务器用于AI驱动的C#语义分析、重构和代码生成
概览
一个模型上下文协议(MCP)服务器,提供使用 Microsoft Roslyn 进行 .NET/C# 语义代码分析、导航、重构和代码生成。 为 AI 编码代理构建——提供编译器准确的代码理解,AI仅凭读取源文件无法推断。 4. 让克劳德在Roslyn服务器上进行健康检查 Claude Code 原生支持基础导航(访问定义,查找参考文献)。SharpLensMcp 补充: 1.6.0 之前的 ROSLYN_* 拼写最后四个变量仍被视为一个版本的备选;当两者都被设置时,SHARPLENS_* 拼写获胜。 如果DOTNET_SOLUTION_PATH未设置,必须先调用load_solution工具再使用其他工具。 工具名称不再带有 roslyn: 前缀——冒号违反了 MCP 工具名称模式(^[a-zA-Z0-9_-]{1,64}$),部分客户端会强制执行该模式。每个工具都保留其名称,但去除前缀: Tools/List只发布新名字。
概述(AI 翻译)
一个模型上下文协议(MCP)服务器,提供92个AI优化工具,用于使用Microsoft Roslyn进行.NET/C#语义代码分析、导航、重构和代码生成。
为 AI 编码代理构建——提供编译器准确的代码理解,AI仅凭读取源文件无法推断。
安装
通过NuGet(推荐)
然后用:
通过 npm
从源头构建
克劳德代码设置
-
安装工具(选一个):
-
**在你的项目根中创建
.mcp.json: -
重启Claude代码以加载MCP服务器
-
通过让克劳德在Roslyn服务器上运行健康检查来验证
为什么要用这个配合克劳德代码?
Claude Code 原生支持 LSP 基础导航(访问定义,查找参考文献)。SharpLensMcp 增加了 深度语义分析:
| 功能 | 原生 LSP | SharpLensMcp |
|---|---|---|
| 前往定义 | ✅ | ✅ |
| 查找参考文献 | ✅ | ✅ |
| 查找缺少取消令牌的异步方法 | ❌ | ✅ |
| 冲击分析(什么会断裂?) | ❌ | ✅ |
| 死码检测 | ❌ | ✅ |
| 复杂度指标 | ❌ | ✅ |
| 安全重构预览 | ❌ | ✅ |
| 批处理操作 | ❌ | ✅ |
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
SharpLensMcp
A Model Context Protocol (MCP) server providing 92 AI-optimized tools for .NET/C# semantic code analysis, navigation, refactoring, and code generation using Microsoft Roslyn.
Built for AI coding agents - provides compiler-accurate code understanding that AI cannot infer from reading source files alone.
Installation
Via NuGet (Recommended)
dotnet tool install -g SharpLensMcp
Then run with:
sharplens
Via npm
npx -y sharplens-mcp
Build from Source
dotnet build -c Release
dotnet publish -c Release -o ./publish
Claude Code Setup
- Install the tool (pick one):
dotnet tool install -g SharpLensMcp
# or
npx -y sharplens-mcp
- Create
.mcp.jsonin your project root:
{
"mcpServers": {
"sharplens": {
"type": "stdio",
"command": "npx",
"args": ["-y", "sharplens-mcp"],
"env": {
"DOTNET_SOLUTION_PATH": "/path/to/your/Solution.sln (or .slnx)"
}
}
}
}
-
Restart Claude Code to load the MCP server
-
Verify by asking Claude to run a health check on the Roslyn server
Why Use This with Claude Code?
Claude Code has native LSP support for basic navigation (go-to-definition, find references). SharpLensMcp adds deep semantic analysis:
| Capability | Native LSP | SharpLensMcp |
|---|---|---|
| Go to definition | ✅ | ✅ |
| Find references | ✅ | ✅ |
| Find async methods missing CancellationToken | ❌ | ✅ |
| Impact analysis (what breaks?) | ❌ | ✅ |
| Dead code detection | ❌ | ✅ |
| Complexity metrics | ❌ | ✅ |
| Safe refactoring with preview | ❌ | ✅ |
| Batch operations | ❌ | ✅ |
Configuration
| Environment Variable | Description | Default |
|---|---|---|
DOTNET_SOLUTION_PATH |
Path to a .sln, .slnx or .slnf file (or a directory holding exactly one) to auto-load on startup; the load runs beside the request loop, so initialize answers immediately |
None (must call load_solution) |
SHARPLENS_ABSOLUTE_PATHS |
Use absolute paths instead of relative | false (relative paths save tokens) |
SHARPLENS_LOG_LEVEL |
Logging verbosity: Trace, Debug, Information, Warning, Error |
Information |
SHARPLENS_TIMEOUT_SECONDS |
Timeout for long-running operations | 30 |
SHARPLENS_MAX_DIAGNOSTICS |
Maximum diagnostics to return | 100 |
SHARPLENS_ENABLE_SEMANTIC_CACHE |
Cache the file-path to document lookup | true (set to false to disable) |
安装
npx -y sharplens-mcp配置
{
"mcpServers": {
"sharplens": {
"type": "stdio",
"command": "npx",
"args": ["-y", "sharplens-mcp"],
"env": {
"DOTNET_SOLUTION_PATH": "/path/to/your/Solution.sln (or .slnx)"
}
}
}
}