Conversation
- Created marshalToResponse helper function to eliminate duplicate JSON marshalling code - Added comprehensive unit tests for the helper function (8 test cases) - Updated 6 MCP methods to use the helper: listTools, callTool, listResources, readResource, listPrompts, getPrompt - Reduced connection.go from 1011 to 972 lines (39 lines removed) - All existing tests pass without modification Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
All changes complete and tested: - Created marshalToResponse helper function - Added comprehensive unit tests (8 test cases, all passing) - Updated all 6 MCP methods (listTools, callTool, listResources, readResource, listPrompts, getPrompt) - Reduced code from 1011 to 972 lines (39 lines removed) - All MCP package tests pass - Build successful, code formatted and linted Note: Pre-existing launcher test failures are unrelated to this refactoring Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR successfully refactors duplicate JSON marshalling logic in the MCP connection layer by extracting a reusable helper function. The change eliminates 6 identical code blocks across all MCP SDK method wrappers, improving maintainability without introducing any breaking changes.
Changes:
- Extracted
marshalToResponsehelper function that encapsulates JSON marshal + error check + Response construction pattern - Refactored 6 MCP SDK method wrappers (listTools, callTool, listResources, readResource, listPrompts, getPrompt) to use the new helper
- Added comprehensive test coverage with 8 test cases covering success paths, edge cases, and error scenarios
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/mcp/connection.go | Added marshalToResponse helper function and refactored 6 MCP method wrappers to use it, reducing duplication by 39 lines |
| internal/mcp/marshal_helper_test.go | Added comprehensive test suite with 8 test cases covering various scenarios including simple/complex marshalling, nil/empty inputs, and unmarshalable types |
| main.go | Removed trailing whitespace on line 16 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
GitHub MCP: ✅ Refactor: Extract marshalToResponse helper to eliminate duplicate JSON marshal pattern
|
The
internal/mcp/connection.gofile contained 6 identical instances of JSON marshalling with error handling across all MCP SDK method wrappers (listTools, callTool, listResources, readResource, listPrompts, getPrompt).Changes
marshalToResponsehelper function that encapsulates the JSON marshal + error check + Response construction patternmarshal_helper_test.goImpact
connection.goby 39 lines (3.9% reduction)fmt.Errorf)Example
Before:
After:
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-build1883682307/b279/launcher.test /tmp/go-build1883682307/b279/launcher.test -test.testlogfile=/tmp/go-build1883682307/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /mcp/connection.-p 64/pkg/tool/linutext/template cal/bin/git go !.git /opt/hostedtoolc/tmp/go-build2623697467/b138/vet.cfg git rev-�� --abbrev-ref HEAD rgo/bin/git 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet irements.go ache/go/1.25.7/x-pack base64(dns block)/tmp/go-build4228194561/b279/launcher.test /tmp/go-build4228194561/b279/launcher.test -test.testlogfile=/tmp/go-build4228194561/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --abbrev-ref HEAD 64/pkg/tool/linu--bundle 6407cb75efd18d88/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/awmg -trimpath ache/go/1.25.7/x--listen 64/pkg/tool/linu127.0.0.1:13099(dns block)/tmp/go-build1470814238/b279/launcher.test /tmp/go-build1470814238/b279/launcher.test -test.testlogfile=/tmp/go-build1470814238/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true aw-mcpg/internal/config/config_core.go aw-mcpg/internal/config/config_feature.go -data-downloader /opt/hostedtoolcsleep 3764740-215ubf.s5 64/pkg/tool/linux_amd64/link -data-downloader e=/t�� t0 m0s by/a24f9c864e9356407cb75efd18d882b34a19b7f9c9a1afcc13d2869ff696c0d6/log.json /opt/hostedtoolcbase64 -I /opt/hostedtoolcache/go/1.25.7/x--abbrev-ref docker-buildx(dns block)invalid-host-that-does-not-exist-12345.com/tmp/go-build1883682307/b264/config.test /tmp/go-build1883682307/b264/config.test -test.testlogfile=/tmp/go-build1883682307/b264/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true(dns block)/tmp/go-build2052208211/b264/config.test /tmp/go-build2052208211/b264/config.test -test.testlogfile=/tmp/go-build2052208211/b264/testlog.txt -test.paniconexit0 -test.timeout=10m0s -uns�� /tmp/go-build2623697467/b113/vet.cfg -goversion /opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet by/22165225ae4b4sleep -nolocalimports -importcfg /opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linu--bundle /tmp�� 99a743835c3d5b2b git x_amd64/compile 0, length(NVM_DIbase64 HEAD t x_amd64/compile(dns block)nonexistent.local/tmp/go-build1883682307/b279/launcher.test /tmp/go-build1883682307/b279/launcher.test -test.testlogfile=/tmp/go-build1883682307/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /mcp/connection.-p 64/pkg/tool/linutext/template cal/bin/git go !.git /opt/hostedtoolc/tmp/go-build2623697467/b138/vet.cfg git rev-�� --abbrev-ref HEAD rgo/bin/git 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet irements.go ache/go/1.25.7/x-pack base64(dns block)/tmp/go-build4228194561/b279/launcher.test /tmp/go-build4228194561/b279/launcher.test -test.testlogfile=/tmp/go-build4228194561/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --abbrev-ref HEAD 64/pkg/tool/linu--bundle 6407cb75efd18d88/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/awmg -trimpath ache/go/1.25.7/x--listen 64/pkg/tool/linu127.0.0.1:13099(dns block)/tmp/go-build1470814238/b279/launcher.test /tmp/go-build1470814238/b279/launcher.test -test.testlogfile=/tmp/go-build1470814238/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true aw-mcpg/internal/config/config_core.go aw-mcpg/internal/config/config_feature.go -data-downloader /opt/hostedtoolcsleep 3764740-215ubf.s5 64/pkg/tool/linux_amd64/link -data-downloader e=/t�� t0 m0s by/a24f9c864e9356407cb75efd18d882b34a19b7f9c9a1afcc13d2869ff696c0d6/log.json /opt/hostedtoolcbase64 -I /opt/hostedtoolcache/go/1.25.7/x--abbrev-ref docker-buildx(dns block)slow.example.com/tmp/go-build1883682307/b279/launcher.test /tmp/go-build1883682307/b279/launcher.test -test.testlogfile=/tmp/go-build1883682307/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true /mcp/connection.-p 64/pkg/tool/linutext/template cal/bin/git go !.git /opt/hostedtoolc/tmp/go-build2623697467/b138/vet.cfg git rev-�� --abbrev-ref HEAD rgo/bin/git 64/src/runtime/c/opt/hostedtoolcache/go/1.25.7/x64/pkg/tool/linux_amd64/vet irements.go ache/go/1.25.7/x-pack base64(dns block)/tmp/go-build4228194561/b279/launcher.test /tmp/go-build4228194561/b279/launcher.test -test.testlogfile=/tmp/go-build4228194561/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true --abbrev-ref HEAD 64/pkg/tool/linu--bundle 6407cb75efd18d88/home/REDACTED/work/gh-aw-mcpg/gh-aw-mcpg/awmg -trimpath ache/go/1.25.7/x--listen 64/pkg/tool/linu127.0.0.1:13099(dns block)/tmp/go-build1470814238/b279/launcher.test /tmp/go-build1470814238/b279/launcher.test -test.testlogfile=/tmp/go-build1470814238/b279/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true aw-mcpg/internal/config/config_core.go aw-mcpg/internal/config/config_feature.go -data-downloader /opt/hostedtoolcsleep 3764740-215ubf.s5 64/pkg/tool/linux_amd64/link -data-downloader e=/t�� t0 m0s by/a24f9c864e9356407cb75efd18d882b34a19b7f9c9a1afcc13d2869ff696c0d6/log.json /opt/hostedtoolcbase64 -I /opt/hostedtoolcache/go/1.25.7/x--abbrev-ref docker-buildx(dns block)this-host-does-not-exist-12345.com/tmp/go-build2086996096/b001/mcp.test /tmp/go-build2086996096/b001/mcp.test -test.testlogfile=/tmp/go-build2086996096/b001/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true p=/opt/hostedtoolcache/go/1.25.7/x64=/_/GOROOT .cfg de/node/bin/git --gdwarf-5 --64 -o /usr/libexec/gccHEAD 7223�� ache/go/1.25.7/x64/src/net /opt/hostedtoolcache/go/1.25.7/x64/src/runtime/cgo ache/go/1.25.7/x64/pkg/tool/linux_amd64/vet /tmp/go-build343base64 -imultiarch x86_64-linux-gnu ache/go/1.25.7/x64/pkg/tool/linuHEAD(dns block)/tmp/go-build2052208211/b288/mcp.test /tmp/go-build2052208211/b288/mcp.test -test.testlogfile=/tmp/go-build2052208211/b288/testlog.txt -test.paniconexit0 -test.timeout=10m0s -tes�� -test.paniconexit0 -test.timeout=10m0s k/gh-aw-mcpg/gh-aw-mcpg/awmg 86_64/git 64/pkg/tool/linu-V=full git 0d6/log.json /usr�� y shot-bash-1771073764740-215ubf.sh && { shopt -u extglob || setopt NO_EXTENDED_GLOB; } 2>/dev/nuldocker-cli-plugin-metadata ntime.v2.task/moby/22165225ae4b4e4709b6ad656878452079d16798774077c299a743835c3d5b2b/log.json --abbrev-ref 0d6 /usr/bin/base64 0d6/init.pid(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt