Skip to content

[duplicate-code] Duplicate Code Pattern: Inline Map Copy in NormalizeInputSchema #4775

Description

@github-actions

Part of duplicate code analysis: #4773

Summary

internal/mcp/schema.go's NormalizeInputSchema function contains two identical 4-line blocks that shallow-copy a map[string]interface{} before adding a key. This inline copy-before-mutate pattern is repeated twice and could be extracted into a single helper.

Duplication Details

Pattern: Inline map shallow-copy before adding a key

  • Severity: Low
  • Occurrences: 2 (within the same function)
  • Location: internal/mcp/schema.go (lines 39–44 and 74–79)

Duplicated code:

// Lines 39–44
normalized := make(map[string]interface{})
for k, v := range schema {
    normalized[k] = v
}
normalized["type"] = "object"
return normalized

// Lines 74–79
normalized := make(map[string]interface{})
for k, v := range schema {
    normalized[k] = v
}
normalized["properties"] = map[string]interface{}{}
return normalized

Impact Analysis

  • Maintainability: Minor — if the copy logic needs to change (e.g., to use maps.Clone from Go 1.21), it must be changed in two places
  • Bug Risk: Low, but divergence between the two blocks is possible
  • Code Bloat: 8 lines of boilerplate that could be 2 lines

Refactoring Recommendations

  1. Extract a copySchemaWithKey helper (or use maps.Clone from golang.org/x/exp/maps / Go 1.21 stdlib):

    // copySchemaWithKey returns a shallow copy of schema with the given key set.
    func copySchemaWithKey(schema map[string]interface{}, key string, value interface{}) map[string]interface{} {
        normalized := make(map[string]interface{}, len(schema)+1)
        for k, v := range schema {
            normalized[k] = v
        }
        normalized[key] = value
        return normalized
    }

    Then replace both blocks:

    return copySchemaWithKey(schema, "type", "object")
    // ...
    return copySchemaWithKey(schema, "properties", map[string]interface{}{})

    Estimated effort: 30 minutes.

Implementation Checklist

  • Add copySchemaWithKey helper (or use maps.Clone)
  • Replace the two inline copy blocks
  • Run make test to verify no regressions

Parent Issue

See parent analysis report: #4773
Related to #4773

Generated by Duplicate Code Detector · ● 1.4M ·

  • expires on May 6, 2026, 6:19 AM UTC

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions