Introduction

Recently, I started to learn about Model Context Protocol (MCP) and wrote a small project to use on Visual Studio Code with GitHub Copilot agent. After completing some tutorials, I wanted to create something more useful for my own tasks. I decided to write an MCP server that checks code quality and code coverage using ReSharper CLI, which is free and available with my Visual Studio subscription. This use case is perfect for me as it integrates seamlessly with my development workflow.


1. Code Quality Checker MCP Server

Description: An MCP server that integrates with code quality tools to analyze code for potential issues, style violations, and best practices.

Features:

  • Static code analysis
  • Code style enforcement
  • Automated reports

Example Contract:

[McpServerToolType]
public static class CodeQualityTool
{
    [McpServerTool, Description("Analyze code quality.")]
    public static async Task<string> AnalyzeCode(string pathToSolution)
    {
        // Integrate with ReSharper CLI for code analysis
        var analysisResult = await CodeQualityService.AnalyzeAsync(pathToSolution);
        return analysisResult;
    }

    [McpServerTool, Description("Check code style.")]
    public static string CheckStyle(string code)
    {
        // Check code against style guidelines using ReSharper CLI
        var styleIssues = CodeStyleService.CheckStyle(code);
        return string.Join("\n", styleIssues);
    }

    [McpServerTool, Description("Cleanup code style.")]
    public static string CleanupCodeStyle(string solutionPath)
    {
        // Cleanup code style using ReSharper CLI
        var cleanupResult = CodeStyleService.Cleanup(solutionPath);
        return string.join("\n", cleanupResult);
    }
}

Setting Up ReSharper CLI

To install ReSharper command line tools as a global .NET tool, run:

dotnet tool install -g JetBrains.ReSharper.GlobalTools
  • InspectCode: To find code issues in a solution, run:
    jb inspectcode YourSolution.sln -o=<PathToOutputFile>
    
  • CleanupCode: To reformat code and fix code style in a solution, run:
    jb cleanupcode YourSolution.sln
    

Integrating MCP Server with ReSharper CLI

Analyze Code Quality:

public static async Task<string> AnalyzeAsync(string pathToSolution)
{
    var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "jb",
            Arguments = $"inspectcode {pathToSolution} -o=analysisResult.xml",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };
    process.Start();
    await process.WaitForExitAsync();
    var result = await File.ReadAllTextAsync("analysisResult.xml");
    return result;
}

Check and Cleanup Code Style:

public static string CheckStyle(string code)
{
    // Implementation for checking code style
    // Example: using ReSharper CLI to check style
    return "Style issues found.";
}

public static string Cleanup(string solutionPath)
{
    var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "jb",
            Arguments = $"cleanupcode {solutionPath}",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };
    process.Start();
    process.WaitForExit();
    var result = process.StandardOutput.ReadToEnd();
    return result;
}

Conclusion

By integrating MCP with ReSharper CLI, I created a powerful tool to enhance code quality and enforce code style guidelines. This setup not only helps in maintaining a clean codebase but also provides automated reports for continuous improvement. I encourage you to try this approach and see how it can benefit your development workflow.

Related Posts