refactor(mcp): extract withReconnectLock to eliminate reconnect boilerplate#7103
Merged
Conversation
6 tasks
…t boilerplate Both reconnectPlainJSON and reconnectSDKTransport shared identical preamble (acquire write lock, log reconnect attempt, call logReconnectStart) and epilogue (call logReconnectResult, wrap error with "session reconnect failed: %w"). Extract withReconnectLock(transportName string, reconnect func() error) error that handles the lifecycle uniformly. Each reconnect function now passes only its unique transport logic as a closure, making the reconnect contract compiler-enforced and reducing the risk of drift between the two paths. Resolves the duplicate-code issue raised in gh-aw-mcpg#7078.
Copilot
AI
changed the title
[WIP] Refactor duplicate reconnect functions for MCP connection
refactor(mcp): extract withReconnectLock to eliminate reconnect boilerplate
Jun 6, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors MCP HTTP session reconnection logic by extracting the shared session-locking + reconnect-logging + error-wrapping scaffolding into a single helper, reducing duplicated reconnect boilerplate and ensuring consistent reconnect telemetry/error handling across transports.
Changes:
- Added
(*Connection).withReconnectLock(...)to centralize session write-locking, reconnect start/result logging, and uniform error wrapping. - Simplified
reconnectPlainJSONto only perform HTTP session initialization + session ID assignment inside the helper closure. - Simplified
reconnectSDKTransportto only perform SDK transport construction + connect logic inside the helper closure, and ensured the default/unsupported transport branch now gets consistent logging/wrapping.
Show a summary per file
| File | Description |
|---|---|
internal/mcp/connection.go |
Extracts shared reconnect lock/log/error epilogue into withReconnectLock and rewrites both reconnect paths to use it. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
reconnectPlainJSONandreconnectSDKTransportshared identical lock/log/error-wrap scaffolding, requiring any change to the reconnect lifecycle to be applied in two places.Changes
withReconnectLockhelper (connection.go) — acquires the session write lock, emits the reconnect log, callslogReconnectStart/logReconnectResult, and wraps errors uniformly; accepts the transport-specific logic as afunc() errorclosurereconnectPlainJSON— reduced to awithReconnectLock("plain JSON-RPC", ...)call containing only the session-init and ID assignment logicreconnectSDKTransport— reduced to awithReconnectLock(fmt.Sprintf("SDK transport (type=%s)", ...), ...)call containing only transport construction and session connect logic; thedefaultbranch now also flows through the shared epilogue (logging + wrapping), which is a minor improvement