-
Notifications
You must be signed in to change notification settings - Fork 327
[duplicate-code] 🔍 Duplicate Code Detected: MCP Tool Execution Handler #2749
Copy link
Copy link
Closed as not planned
Closed as not planned
Copy link
Description
🔍 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:159pkg/cli/mcp_server.go:205pkg/cli/mcp_server.go:294pkg/cli/mcp_server.go:358pkg/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
- 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).
- Introduce a helper (e.g.,
- 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Type
Fields
Give feedbackNo fields configured for issues without a type.