Skip to content

[duplicate-code] Duplicate Code Pattern: Raw MCP Text-Content Envelope Construction #4326

@github-actions

Description

@github-actions

Part of duplicate code analysis: #4325

Summary

The raw MCP text response structure — {"content": [{"type": "text", "text": "..."}]} — is hand-built with map[string]interface{} in at least 4 locations across 3 files. There is no shared helper, so every call site must be updated independently if the envelope format ever changes.

Duplication Details

Pattern: Raw MCP text-content envelope construction

  • Severity: Medium

  • Occurrences: 4

  • Locations:

    • internal/server/unified.go (lines ~349–354) — callCollaboratorPermission
    • internal/proxy/proxy.go (lines ~334–339) — restBackendCaller
    • internal/server/system_tools.go (lines ~93–99) — sysInit
    • internal/server/system_tools.go (lines ~109–116) — listServers
  • Code Sample (repeated verbatim or near-verbatim in all 4 locations):

    mcpResp := map[string]interface{}{
        "content": []map[string]interface{}{
            {"type": "text", "text": string(body)},
        },
    }
    return mcpResp, nil

Impact Analysis

  • Maintainability: Every location must be updated by hand if the MCP content envelope changes (e.g., adding a "role" field, switching to a typed struct). One missed site causes silent inconsistency.
  • Bug Risk: Low today, but grows as new call sites are added following the same copy-paste pattern.
  • Code Bloat: Minor — 5 lines × 4 sites = ~20 duplicated lines.

Refactoring Recommendations

  1. Add a BuildMCPTextResponse(text string) interface{} helper

    • Suggested location: internal/mcp/tool_result.go (alongside existing ConvertToCallToolResult, NewErrorCallToolResult)
    • Alternatively internal/server/ if the raw-map form is intentionally kept internal to the server package
    • Estimated effort: < 1 hour
    • Benefits: single definition of the envelope, easier to evolve format, type-checked call sites
    // BuildMCPTextResponse returns a raw MCP response map with a single text content item.
    // Use this when a raw map[string]interface{} is required (e.g., for WASM guard parsing).
    func BuildMCPTextResponse(text string) map[string]interface{} {
        return map[string]interface{}{
            "content": []map[string]interface{}{
                {"type": "text", "text": text},
            },
        }
    }
  2. Replace all 4 call sites with the new helper.

Implementation Checklist

  • Add BuildMCPTextResponse (or equivalent) helper
  • Replace usages in unified.go, proxy.go, system_tools.go
  • Verify existing tests still pass (make test)
  • Add a unit test for the helper

Parent Issue

See parent analysis report: #4325
Related to #4325

Generated by Duplicate Code Detector · ● 2.6M ·

  • expires on Apr 29, 2026, 6:13 AM UTC

Metadata

Metadata

Assignees

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