Part of duplicate code analysis: #3827
Summary
The same 5-line conditional logging block (if shouldAttachAgentTags { ... } else { ... }) for RPC responses is copy-pasted 3 times within SendRequestWithServerID in internal/mcp/connection.go. Each copy dispatches to either LogRPCResponseWithAgentSnapshot or LogRPCResponse based on the same boolean flag.
Duplication Details
Pattern: if shouldAttachAgentTags response logging
There is also a similar block for the outbound request (lines 438–442), but it only appears once and is less problematic.
Impact Analysis
- Maintainability: If the logging signature changes (e.g., adding a new tag type or changing the direction argument), all three copies must be updated in sync. A single missed update creates silent inconsistency.
- Bug Risk: Medium — the pattern is correct today, but divergence becomes likely under future refactoring.
- Code Bloat: ~15 lines of identical code in one function.
Refactoring Recommendations
-
Extract a logRPCResponse helper function
Add a small unexported helper in internal/mcp/connection.go (or a nearby log_helpers.go):
// logRPCResponse logs an inbound RPC response, optionally attaching agent DIFC tag snapshots.
func logRPCResponse(serverID string, payload []byte, err error, shouldAttachTags bool, snapshot agentTagsSnapshot) {
if shouldAttachTags {
logger.LogRPCResponseWithAgentSnapshot(logger.RPCDirectionInbound, serverID, payload, err, snapshot.Secrecy, snapshot.Integrity)
} else {
logger.LogRPCResponse(logger.RPCDirectionInbound, serverID, payload, err)
}
}
Then each of the three call sites becomes a single line:
logRPCResponse(serverID, responsePayload, err, shouldAttachAgentTags, snapshot)
Estimated effort: ~30 minutes. No functional change — pure refactor.
-
Alternative: extend LogRPCResponse to accept optional tags
Add a variadic agentTags ...[]string parameter to LogRPCResponse so callers don't need to branch. This requires a change to the public logger API but eliminates the if/else entirely.
Implementation Checklist
Parent Issue
See parent analysis report: #3827
Related to #3827
Generated by Duplicate Code Detector · ● 1.7M · ◷
Part of duplicate code analysis: #3827
Summary
The same 5-line conditional logging block (
if shouldAttachAgentTags { ... } else { ... }) for RPC responses is copy-pasted 3 times withinSendRequestWithServerIDininternal/mcp/connection.go. Each copy dispatches to eitherLogRPCResponseWithAgentSnapshotorLogRPCResponsebased on the same boolean flag.Duplication Details
Pattern:
if shouldAttachAgentTagsresponse loggingSeverity: Medium
Occurrences: 3 identical blocks
Locations:
internal/mcp/connection.go(lines 457–461) — insideif c.httpTransportType == HTTPTransportPlainJSONinternal/mcp/connection.go(lines 472–476) — inside SDK streamable/SSE branchinternal/mcp/connection.go(lines 488–492) — stdio branchCode Sample (repeated verbatim three times):
There is also a similar block for the outbound request (lines 438–442), but it only appears once and is less problematic.
Impact Analysis
Refactoring Recommendations
Extract a
logRPCResponsehelper functionAdd a small unexported helper in
internal/mcp/connection.go(or a nearbylog_helpers.go):Then each of the three call sites becomes a single line:
Estimated effort: ~30 minutes. No functional change — pure refactor.
Alternative: extend
LogRPCResponseto accept optional tagsAdd a variadic
agentTags ...[]stringparameter toLogRPCResponseso callers don't need to branch. This requires a change to the public logger API but eliminates the if/else entirely.Implementation Checklist
SendRequestWithServerIDlogRPCResponsehelper (or equivalent)make test-unit)Parent Issue
See parent analysis report: #3827
Related to #3827