Refactor HTTP request initialization to eliminate duplicate code pattern#937
Merged
Refactor HTTP request initialization to eliminate duplicate code pattern#937
Conversation
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request successfully refactors duplicate HTTP request initialization code in internal/mcp/connection.go by extracting a common helper function. The refactoring eliminates ~90 lines of duplicated logic while maintaining existing functionality and test coverage.
Changes:
- Introduced
executeHTTPRequesthelper function with callback-based header customization to consolidate JSON-RPC request creation, marshaling, HTTP setup, execution, and response reading - Refactored
initializeHTTPSessionandsendHTTPRequestto use the new helper, reducing code duplication and providing a single location for future HTTP-related enhancements - Minor whitespace cleanup in
main.go
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/mcp/connection.go | Added httpRequestResult struct and executeHTTPRequest helper function; refactored initializeHTTPSession and sendHTTPRequest to use the helper with callback-based header modification |
| main.go | Removed trailing whitespace on line 16 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
The
internal/mcp/connection.gofile contained duplicate HTTP request initialization workflow acrossinitializeHTTPSessionandsendHTTPRequestmethods, differing only in header customization logic. This made the codebase harder to maintain and inconsistent for future HTTP-related changes.Changes
Extracted
executeHTTPRequesthelper function that consolidates the common pattern: create JSON-RPC request → marshal → setup HTTP → execute → read responsehttpRequestResultstruct containing status code, response body, and headersheaderModifiercallback for caller-specific header customizationRefactored
initializeHTTPSessionto use the helper with session ID header managementRefactored
sendHTTPRequestto use the helper with context-based session ID priority handlingExample
Before (duplicated in both methods):
After (single location):
Impact
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
example.com/tmp/go-build3466960917/b275/launcher.test /tmp/go-build3466960917/b275/launcher.test -test.testlogfile=/tmp/go-build3466960917/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 64/src/net /bin/sh(dns block)/tmp/go-build3643262344/b275/launcher.test /tmp/go-build3643262344/b275/launcher.test -test.testlogfile=/tmp/go-build3643262344/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /opt/hostedtoolcache/go/1.25.7/x64/src/net ache/go/1.25.7/x64/src/crypto/internal/fips140hash/hash.go /usr/bin/gcc --gdwarf-5 --64 -o /usr/bin/gcc -I /opt/hostedtoolcache/go/1.25.7/x64/src/net -fPIC 64/pkg/tool/linux_amd64/vet -pthread -Wl,--no-gc-sect-d -fmessage-length=0 64/pkg/tool/linux_amd64/vet(dns block)/tmp/go-build3690481232/b279/launcher.test /tmp/go-build3690481232/b279/launcher.test -test.testlogfile=/tmp/go-build3690481232/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil.go /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil_test.go ache/uv/0.10.2/x86_64/git elect.go nix.go 64/pkg/tool/linu/tmp/go-build4145022949/b166/vet.cfg bash /usr�� --version T.md d ache/go/1.25.7/x/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet -trimpath /x86_64-linux-gn/tmp/go-build4145022949/b183/vet.cfg /usr/bin/runc.original(dns block)invalid-host-that-does-not-exist-12345.com/tmp/go-build3466960917/b260/config.test /tmp/go-build3466960917/b260/config.test -test.testlogfile=/tmp/go-build3466960917/b260/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 64/src/net node cal/bin/as --output-format stream-json cal/bin/which 01.o -p 64/src/net client.go 64/pkg/tool/linux_amd64/compile -I /tmp/go-build271-unsafeptr=false -I 64/pkg/tool/linu/tmp/go-build3466960917/b144/vet.cfg(dns block)nonexistent.local/tmp/go-build3466960917/b275/launcher.test /tmp/go-build3466960917/b275/launcher.test -test.testlogfile=/tmp/go-build3466960917/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 64/src/net /bin/sh(dns block)/tmp/go-build3643262344/b275/launcher.test /tmp/go-build3643262344/b275/launcher.test -test.testlogfile=/tmp/go-build3643262344/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /opt/hostedtoolcache/go/1.25.7/x64/src/net ache/go/1.25.7/x64/src/crypto/internal/fips140hash/hash.go /usr/bin/gcc --gdwarf-5 --64 -o /usr/bin/gcc -I /opt/hostedtoolcache/go/1.25.7/x64/src/net -fPIC 64/pkg/tool/linux_amd64/vet -pthread -Wl,--no-gc-sect-d -fmessage-length=0 64/pkg/tool/linux_amd64/vet(dns block)/tmp/go-build3690481232/b279/launcher.test /tmp/go-build3690481232/b279/launcher.test -test.testlogfile=/tmp/go-build3690481232/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil.go /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil_test.go ache/uv/0.10.2/x86_64/git elect.go nix.go 64/pkg/tool/linu/tmp/go-build4145022949/b166/vet.cfg bash /usr�� --version T.md d ache/go/1.25.7/x/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet -trimpath /x86_64-linux-gn/tmp/go-build4145022949/b183/vet.cfg /usr/bin/runc.original(dns block)slow.example.com/tmp/go-build3466960917/b275/launcher.test /tmp/go-build3466960917/b275/launcher.test -test.testlogfile=/tmp/go-build3466960917/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 64/src/net /bin/sh(dns block)/tmp/go-build3643262344/b275/launcher.test /tmp/go-build3643262344/b275/launcher.test -test.testlogfile=/tmp/go-build3643262344/b275/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /opt/hostedtoolcache/go/1.25.7/x64/src/net ache/go/1.25.7/x64/src/crypto/internal/fips140hash/hash.go /usr/bin/gcc --gdwarf-5 --64 -o /usr/bin/gcc -I /opt/hostedtoolcache/go/1.25.7/x64/src/net -fPIC 64/pkg/tool/linux_amd64/vet -pthread -Wl,--no-gc-sect-d -fmessage-length=0 64/pkg/tool/linux_amd64/vet(dns block)/tmp/go-build3690481232/b279/launcher.test /tmp/go-build3690481232/b279/launcher.test -test.testlogfile=/tmp/go-build3690481232/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil.go /home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/internal/envutil/envutil_test.go ache/uv/0.10.2/x86_64/git elect.go nix.go 64/pkg/tool/linu/tmp/go-build4145022949/b166/vet.cfg bash /usr�� --version T.md d ache/go/1.25.7/x/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet -trimpath /x86_64-linux-gn/tmp/go-build4145022949/b183/vet.cfg /usr/bin/runc.original(dns block)this-host-does-not-exist-12345.com/tmp/go-build3466960917/b284/mcp.test /tmp/go-build3466960917/b284/mcp.test -test.testlogfile=/tmp/go-build3466960917/b284/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true g_.a -trimpath ache/go/1.25.7/x64/pkg/tool/linux_amd64/asm 2690816/b098/ strings ctor ache/go/1.25.7/x/tmp/go-build3466960917/b205/vet.cfg -I fips140/sha3 gkWDTCvzb 2690816/b130=> --gdwarf-5 2690816/b130/ -o ache/go/1.25.7/x64/pkg/include(dns block)/tmp/go-build330381427/b001/mcp.test /tmp/go-build330381427/b001/mcp.test -test.testlogfile=/tmp/go-build330381427/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -test.run=TestNewHTTPConnection|TestSendHTTPRequest|Test.*HTTP .cfg 64/pkg/tool/linux_amd64/vet -D GOAMD64_v1 -gensymabis 64/pkg/tool/linux_amd64/vet s#\.�� 64/src/runtime/trace/annotation.-s .cfg ache/go/1.25.7/x64/pkg/tool/linu-buildmode=exe rsions$# d; base64 HEAD 64/pkg/tool/linu-o ache/go/1.25.7/x64/pkg/tool/linuHEAD(dns block)/tmp/go-build2307564073/b001/mcp.test /tmp/go-build2307564073/b001/mcp.test -test.testlogfile=/tmp/go-build2307564073/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true -test.run=TestNewHTTPConnection.* 64/pkg/tool/linu-plugin-opt=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet go syscall/unix/at.-d x_amd64/compile ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>[duplicate-code] Duplicate Code Pattern: HTTP Request Initialization in MCP Connection</issue_title>
<issue_description># 🔍 Duplicate Code Pattern: HTTP Request Initialization
Part of duplicate code analysis: #892
Summary
The
internal/mcp/connection.gofile contains 2 nearly identical instances of HTTP request initialization workflow, differing only in the RPC method name. This pattern appears in bothinitializeHTTPSessionandsendHTTPRequestmethods.Duplication Details
Pattern: HTTP Request Creation Workflow
Severity: Medium
Occurrences: 2 instances in connection.go
Locations:
internal/mcp/connection.go(lines 576-625) - initializeHTTPSession methodinternal/mcp/connection.go(lines 678-726) - sendHTTPRequest methodCode Sample:
Impact Analysis
Refactoring Recommendations
1. Extract Helper Function: executeHTTPRequest
internal/mcp/connection.go:2. Future Enhancement: Add Request/Response Logging
Implementation Checklist