Purpose
SharpLensMcp is a Model Context Protocol (MCP) server that provides Microsoft Roslyn-based semantic code analysis, navigation, refactoring, and code generation for .NET/C# code. It is built for AI coding agents, providing compiler-level accurate code understanding that AI cannot infer by reading source files alone.
Main Capabilities
The project provides 92 AI-optimized tools, divided into the following categories:
- Navigation and Discovery (24 tools)
- Analysis (17 tools)
- Refactoring (16 tools)
- Code Generation (3 tools)
- Composite Tools (7 tools)
- Audit and Quality (10 tools)
- Discovery (3 tools)
- Infrastructure (12 tools)
Other features include:
- AI-optimized descriptions: clear USAGE/OUTPUT/WORKFLOW patterns
- Structured responses: consistent success/error/data format, with suggestedNextTools
- Zero-based coordinates: with explicit warnings to prevent off-by-one errors
- Preview mode: preview before applying, safe refactoring
- Batch operations: execute multiple queries in one call, reducing context usage
Reasons for Using with Claude Code
Claude Code's native LSP supports basic navigation (go to definition, find references). SharpLensMcp adds deep semantic analysis. The documentation recommends adding instructions to the project's CLAUDE.md to prioritize using SharpLensMcp tools over native tools, for example: use search_symbols instead of Grep to find symbols, use get_method_source instead of Read to view methods, use find_references for semantic (rather than textual) reference lookup.
Usage
There are two installation methods: via NuGet (recommended), or building from source.
Claude Code setup steps: install the tool; create .mcp.jsonin the project root directory; restart Claude Code to load the MCP server; verify by having Claude run a health check on the Roslyn server.
Other MCP clients: add the corresponding settings in the configuration. The usage flow is: call load_solution and provide the .sln or .slnx file path to load the solution (or set DOTNET_SOLUTION_PATH); use any one of the 92 tools to perform analysis; use preview: true to preview before applying changes to refactor safely.
If DOTNET_SOLUTION_PATHis not set, you must first call the load_solution tool before using other tools.
Agent Responsibility: Documentation Synchronization
SharpLensMcp maintains an in-memory representation of the solution for fast queries. When files are modified externally through the Edit/Write tools, the agent is responsible for synchronizing the changes, and needs to call sync_documents. Failure to synchronize may cause queries to return stale data (old method signatures, missing new files, etc.). This design mimics how LSP works, with the client notifying the server of changes, thereby eliminating race conditions, avoiding file watcher complexity and platform differences, being faster than a full reload, and giving the agent explicit control over workspace state.
Applicable Scenarios
Scenarios requiring semantic analysis beyond text search, especially overloaded methods, partial classes, and inheritance hierarchies. A single installation can analyze solutions targeting .NET 8, 9, 10, and higher.
Limitations and Requirements
- Requires .NET 8.0 SDK or higher. The tool itself runs on net8.0, but can analyze any .NET 8+ project/solution.
- Requires an MCP-compatible AI agent.
- AI models may have training bias toward using native tools (Grep, Read, LSP), and tool priority needs to be explicitly guided in the configuration.
- Tool names no longer carry the
roslyn:prefix (the colon violates the MCP tool name pattern); the old prefixed names are retained only as aliases for one version. - Legacy
ROSLYN_*A variable spelling retains only one version as a fallback.
Development and Testing
New tools need to add the method to the matching src/RoslynService.*.cs division, and register a ToolDefinition in src/ToolRegistry.cs . Building and publishing require running the corresponding commands. Each tool needs two layers of testing: unit tests for the RoslynService methods, and online tests through the MCP scheduler (located in tests/SharpLensMcp.Tests/Mcp/). All tests need to satisfy the Testing Charter (C1–C9) contract in tests/SharpLensMcp.Tests/TESTING.md .
License
MIT, see LICENSE for details.