Skip to content

[duplicate-code] 🔍 Duplicate Code Detected: MCP Tool Execution Handler #2749

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: MCP Tool Execution Handler

Analysis of commit d1b68bd

Assignee: @copilot

Summary

The MCP server command implementations duplicate the same command execution and error-handling block for every tool (status, compile, logs, audit, mcp-inspect). Any change to how CLI invocations are executed or reported must be repeated in five places, increasing the risk of drift.

Duplication Details

Pattern: Repeated CLI execution + jq filtering block

  • Severity: Medium
  • Occurrences: 5
  • Locations:
    • pkg/cli/mcp_server.go:159
    • pkg/cli/mcp_server.go:205
    • pkg/cli/mcp_server.go:294
    • pkg/cli/mcp_server.go:358
    • pkg/cli/mcp_server.go:438
  • Code Sample:
    // Execute the CLI command
    cmd := execCmd(ctx, cmdArgs...)
    output, err := cmd.CombinedOutput()
    
    if err != nil {
        return &mcp.CallToolResult{
            Content: []mcp.Content{
                &mcp.TextContent{Text: fmt.Sprintf("Error: %v\nOutput: %s", err, string(output))},
            },
        }, nil, nil
    }

Impact Analysis

  • Maintainability: Fixes to error handling, exit code interpretation, or logging must be applied manually to every tool implementation.
  • Bug Risk: Future tweaks (e.g., adding structured error metadata) could land inconsistently, leading to divergent behavior per tool.
  • Code Bloat: Repeated ~13-line blocks inflate mcp_server.go, making it harder to scan each tool’s unique logic.

Refactoring Recommendations

  1. Extract shared runner helper
    • Introduce a helper (e.g., runMCPCommand(ctx context.Context, cmdArgs []string, jqFilter string) (*mcp.CallToolResult, error)) that encapsulates command execution, shared jq filtering, and error formatting.
    • Estimated effort: 2–3 hours to implement and update callers.
    • Benefits: Single point for tweaks (future retries, richer error output, guardrails).
  2. Layer reusable jq handling
    • Move jq filtering and guardrail logic into composable helpers so individual tools remain focused on argument construction.
    • Benefits: Clear separation between argument assembly and response shaping.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 168 Go sources (excluding tests)
  • Detection Method: Serena semantic code analysis
  • Commit: d1b68bd
  • Analysis Date: 2025-10-29T21:07:24Z

AI generated by Duplicate Code Detector

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions