Skip to content

[duplicate-code] Duplicate Code Detected: Parallel MCP server implementations #5785

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: Parallel MCP server implementations

Analysis of commit 8d26b38

Assignee: @copilot

Summary

Two separate MCP server implementations exist in the JavaScript runtime: mcp_server.cjs and mcp_server_core.cjs. Both register tools and handle JSON-RPC requests but differ in data structures, logging, and validation, increasing the risk of divergent behavior across transports (HTTP vs stdio) and duplicated maintenance.

Duplication Details

Pattern: Duplicate MCP server cores for tool registration/JSON-RPC

  • Severity: Medium
  • Occurrences: 2 implementations
  • Locations:
    • pkg/workflow/js/mcp_server.cjs (lines 23-186)
    • pkg/workflow/js/mcp_server_core.cjs (lines 458-575)
  • Code Sample:
    // mcp_server.cjs: tool registration and handler dispatch
    tool(name, description, inputSchema, handler) { this.tools.set(name, { name, description, inputSchema, handler }); }
    async handleRequest({ id, method, params }) { /* initialize, tools/list, tools/call, ping */ }
    
    // mcp_server_core.cjs: duplicated registration and dispatch
    function registerTool(server, tool) { server.tools[normalizeTool(tool.name)] = { ...tool }; }
    async function handleMessage(server, req) { /* initialize, tools/list, tools/call */ }

Impact Analysis

  • Maintainability: Two code paths to keep in sync for MCP behavior and validation; fixes may land in one server but not the other.
  • Bug Risk: Divergent capabilities (e.g., error handling, schema validation) can yield different outcomes between HTTP and stdio transports, complicating debugging.
  • Code Bloat: Extra server core inflates surface area and documentation burden.

Refactoring Recommendations

  1. Unify MCP server core

    • Extract a single shared server implementation (e.g., reuse mcp_server_core for both HTTP and stdio) and make MCPServer a thin adapter around it.
    • Estimated effort: 4-6 hours (API alignment, tests).
    • Benefits: Single source of truth for JSON-RPC behavior and validation.
  2. Share validation and tool registration utilities

    • Export common functions (normalize/register/handle) and have transports depend on them instead of bespoke logic.
    • Estimated effort: 2-3 hours.
    • Benefits: Eliminates drift between transports and simplifies future fixes.

Implementation Checklist

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

Analysis Metadata

  • Analyzed Files: 4
  • Detection Method: Serena semantic code analysis
  • Commit: 8d26b38
  • Analysis Date: 2025-12-07 21:05:05Z

AI generated by Duplicate Code Detector

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions