|
| 1 | +using ModelContextProtocol.Server; |
| 2 | +using DotNetDevMCP.Analysis.Services; |
| 3 | +using DotNetDevMCP.Analysis.Models; |
| 4 | + |
| 5 | +namespace DotNetDevMCP.Analysis.Mcp.Tools; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// MCP tools for code analysis and metrics |
| 9 | +/// </summary> |
| 10 | +[McpServerToolType] |
| 11 | +public sealed class AnalysisTools( |
| 12 | + CodeAnalysisService analysisService, |
| 13 | + ILogger<AnalysisTools> logger) |
| 14 | +{ |
| 15 | + private readonly CodeAnalysisService _analysisService = analysisService; |
| 16 | + private readonly ILogger<AnalysisTools> _logger = logger; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Analyzes a .NET project or solution and returns comprehensive code metrics |
| 20 | + /// </summary> |
| 21 | + /// <param name="path">Path to the project file (.csproj), solution file (.sln), or directory</param> |
| 22 | + /// <param name="includeMetrics">Include detailed code metrics (default: true)</param> |
| 23 | + /// <param name="includeDependencies">Include dependency information (default: true)</param> |
| 24 | + [McpServerTool(Name = "dotnet_analyze_project")] |
| 25 | + public async Task<AnalysisResult> AnalyzeProjectAsync( |
| 26 | + string path, |
| 27 | + bool includeMetrics = true, |
| 28 | + bool includeDependencies = true, |
| 29 | + CancellationToken cancellationToken = default) |
| 30 | + { |
| 31 | + _logger.LogInformation("MCP: Analyzing project at {Path}", path); |
| 32 | + |
| 33 | + if (!Directory.Exists(path) && !File.Exists(path)) |
| 34 | + { |
| 35 | + throw new FileNotFoundException($"Path not found: {path}"); |
| 36 | + } |
| 37 | + |
| 38 | + return await _analysisService.AnalyzeAsync(path, cancellationToken); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Gets dependency information for a .NET project |
| 43 | + /// </summary> |
| 44 | + /// <param name="projectPath">Path to the project file (.csproj)</param> |
| 45 | + [McpServerTool(Name = "dotnet_get_dependencies")] |
| 46 | + public async Task<DependencyInfo> GetDependenciesAsync( |
| 47 | + string projectPath, |
| 48 | + CancellationToken cancellationToken = default) |
| 49 | + { |
| 50 | + _logger.LogInformation("MCP: Getting dependencies for {Path}", projectPath); |
| 51 | + |
| 52 | + if (!File.Exists(projectPath)) |
| 53 | + { |
| 54 | + throw new FileNotFoundException($"Project file not found: {projectPath}"); |
| 55 | + } |
| 56 | + |
| 57 | + return await _analysisService.GetDependenciesAsync(projectPath, cancellationToken); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Analyzes code quality and identifies potential issues |
| 62 | + /// </summary> |
| 63 | + /// <param name="path">Path to the project or directory to analyze</param> |
| 64 | + [McpServerTool(Name = "dotnet_analyze_quality")] |
| 65 | + public async Task<CodeQualityMetrics> AnalyzeQualityAsync( |
| 66 | + string path, |
| 67 | + CancellationToken cancellationToken = default) |
| 68 | + { |
| 69 | + _logger.LogInformation("MCP: Analyzing code quality at {Path}", path); |
| 70 | + |
| 71 | + if (!Directory.Exists(path) && !File.Exists(path)) |
| 72 | + { |
| 73 | + throw new FileNotFoundException($"Path not found: {path}"); |
| 74 | + } |
| 75 | + |
| 76 | + return await _analysisService.AnalyzeQualityAsync(path, cancellationToken); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Scans for outdated NuGet packages in a project |
| 81 | + /// </summary> |
| 82 | + /// <param name="projectPath">Path to the project file (.csproj)</param> |
| 83 | + [McpServerTool(Name = "dotnet_scan_outdated_packages")] |
| 84 | + public async Task<IReadOnlyList<PackageDependency>> ScanOutdatedPackagesAsync( |
| 85 | + string projectPath, |
| 86 | + CancellationToken cancellationToken = default) |
| 87 | + { |
| 88 | + _logger.LogInformation("MCP: Scanning for outdated packages in {Path}", projectPath); |
| 89 | + |
| 90 | + var deps = await _analysisService.GetDependenciesAsync(projectPath, cancellationToken); |
| 91 | + |
| 92 | + // In a real implementation, this would check NuGet.org for latest versions |
| 93 | + // For now, return all direct dependencies as potentially outdated |
| 94 | + return deps.PackageDependencies |
| 95 | + .Where(d => d.IsDirect) |
| 96 | + .ToList() |
| 97 | + .AsReadOnly(); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Detects circular dependencies in a solution |
| 102 | + /// </summary> |
| 103 | + /// <param name="solutionPath">Path to the solution file (.sln)</param> |
| 104 | + [McpServerTool(Name = "dotnet_detect_circular_dependencies")] |
| 105 | + public async Task<bool> DetectCircularDependenciesAsync( |
| 106 | + string solutionPath, |
| 107 | + CancellationToken cancellationToken = default) |
| 108 | + { |
| 109 | + _logger.LogInformation("MCP: Detecting circular dependencies in {Path}", solutionPath); |
| 110 | + |
| 111 | + // Simplified implementation - would need full graph analysis in production |
| 112 | + var deps = await _analysisService.GetDependenciesAsync(solutionPath, cancellationToken); |
| 113 | + return deps.HasCircularDependencies; |
| 114 | + } |
| 115 | +} |
0 commit comments