Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

Consolidates ~60-70 lines of duplicated code across three patterns in internal/mcp/connection.go: SDK client initialization, HTTP connection struct setup, and JSON-RPC request creation.

Changes

SDK Client Creation

  • Extracted newMCPClient() to replace 3 identical sdk.NewClient(&sdk.Implementation{...}, nil) calls
  • Used in: NewConnection, tryStreamableHTTPTransport, trySSETransport

HTTP Connection Initialization

  • Extracted newHTTPConnection() to centralize 11-line Connection struct setup
  • Eliminates duplicate initialization in tryStreamableHTTPTransport and trySSETransport

JSON-RPC Request Setup

  • Extracted createJSONRPCRequest() for JSON-RPC 2.0 request creation
  • Extracted setupHTTPRequest() for HTTP request header configuration
  • Refactored initializeHTTPSession and sendHTTPRequest to use helpers

Example

Before:

client := sdk.NewClient(&sdk.Implementation{
    Name:    "awmg",
    Version: "1.0.0",
}, nil)

After:

client := newMCPClient()

Net change: -5 lines in connection.go, +139 lines of test coverage in connection_test.go

Original prompt

This section details on the original issue you should resolve

<issue_title>[duplicate-code] Duplicate Code Analysis Report - January 2026</issue_title>
<issue_description># 🔍 Duplicate Code Analysis Report

Analysis of commit 87ced49

Summary

This analysis identified 3 significant duplication patterns in the MCP Gateway codebase, with approximately 60-70 lines of duplicated code affecting maintainability and increasing the risk of inconsistent bug fixes.

Detected Patterns

This analysis found 3 significant duplication patterns:

  1. HTTP Request Setup Duplication - Severity: High - See sub-issue #aw_http1234abcd
  2. SDK Client Creation Duplication - Severity: Medium - See sub-issue #aw_sdk5678efgh
  3. Connection Struct Initialization - Severity: Medium - See sub-issue #aw_conn9012ijkl

Overall Impact

  • Total Duplicated Lines: ~60-70 lines across patterns
  • Affected Files: internal/mcp/connection.go (primary)
  • Maintainability Risk: High - duplicated code makes consistent updates difficult
  • Refactoring Priority: High - patterns are in critical connection/transport code

Key Findings

Pattern 1: HTTP Request Setup (High Severity)

Identical HTTP request creation and header setup logic appears in two separate functions (initializePlainJSON and sendHTTPRequest), with ~30-35 lines of duplicated code including JSON marshaling, request creation, and header configuration.

Pattern 2: SDK Client Creation (Medium Severity)

The same sdk.NewClient initialization with identical Implementation struct appears 3+ times throughout the connection code, creating unnecessary repetition.

Pattern 3: Connection Struct Initialization (Medium Severity)

Nearly identical Connection struct initialization patterns appear in multiple transport attempt functions (tryStreamableHTTPTransport, trySSETransport), with ~15-20 lines of duplicated struct setup.

Next Steps

  1. Review individual pattern sub-issues for detailed analysis
  2. Prioritize refactoring based on severity and impact
  3. Create implementation plan for highest priority patterns
  4. Consider extracting common HTTP setup logic into reusable helper functions

Analysis Metadata

  • Analyzed Files: 45 Go files (excluding tests, workflows, agent configs)
  • Detection Method: Manual semantic code analysis
  • Commit: 87ced49 (Propagate HTTP backend errors to clients with full context)
  • Analysis Date: 2026-01-16
  • Triggered by: @pelikhan
  • Primary Focus: internal/mcp/connection.go (907 lines)

Recommendations

  • Extract HTTP request setup into a helper function
  • Create a factory function for SDK client initialization
  • Standardize Connection struct initialization with a builder or factory pattern
  • Add linting rules to detect future duplication (consider using dupl or gocyclo)

AI generated by Duplicate Code Detector

<agent_instructions>Please take care to maintain clean separation between unified and routed modules even if they share some support modules.

Also please be sure to fix all linting issues before finishing.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 16, 2026 16:41
- Added helper function newMCPClient() for SDK client creation
- Added helper function newHTTPConnection() for HTTP connection initialization
- Added helper functions createJSONRPCRequest() and setupHTTPRequest() for request setup
- Refactored tryStreamableHTTPTransport, trySSETransport, NewConnection to use newMCPClient()
- Refactored tryStreamableHTTPTransport, trySSETransport to use newHTTPConnection()
- Refactored initializeHTTPSession and sendHTTPRequest to use request helpers
- Reduced ~60-70 lines of duplicated code
- All existing tests pass

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
- Added TestNewMCPClient to verify SDK client creation
- Added TestCreateJSONRPCRequest with multiple test cases
- Added TestSetupHTTPRequest with multiple scenarios
- Added TestNewHTTPConnection to verify connection initialization
- All tests pass

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate code patterns in MCP Gateway Eliminate duplicate code patterns in MCP connection handling Jan 16, 2026
Copilot AI requested a review from lpcox January 16, 2026 16:47
@lpcox lpcox marked this pull request as ready for review January 16, 2026 16:52
@lpcox lpcox merged commit 40ece90 into main Jan 16, 2026
3 checks passed
@lpcox lpcox deleted the copilot/fix-duplicate-code-issues branch January 16, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Analysis Report - January 2026

2 participants