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
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
-
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},
},
}
}
-
Replace all 4 call sites with the new helper.
Implementation Checklist
Parent Issue
See parent analysis report: #4325
Related to #4325
Generated by Duplicate Code Detector · ● 2.6M · ◷
Part of duplicate code analysis: #4325
Summary
The raw MCP text response structure —
{"content": [{"type": "text", "text": "..."}]}— is hand-built withmap[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) —callCollaboratorPermissioninternal/proxy/proxy.go(lines ~334–339) —restBackendCallerinternal/server/system_tools.go(lines ~93–99) —sysInitinternal/server/system_tools.go(lines ~109–116) —listServersCode Sample (repeated verbatim or near-verbatim in all 4 locations):
Impact Analysis
"role"field, switching to a typed struct). One missed site causes silent inconsistency.Refactoring Recommendations
Add a
BuildMCPTextResponse(text string) interface{}helperinternal/mcp/tool_result.go(alongside existingConvertToCallToolResult,NewErrorCallToolResult)internal/server/if the raw-map form is intentionally kept internal to the server packageReplace all 4 call sites with the new helper.
Implementation Checklist
BuildMCPTextResponse(or equivalent) helperunified.go,proxy.go,system_tools.gomake test)Parent Issue
See parent analysis report: #4325
Related to #4325