Skip to content

Refactor MCP connection response logging and timeout default handling to remove duplication#3966

Merged
lpcox merged 3 commits intomainfrom
copilot/duplicate-code-analysis-fix
Apr 16, 2026
Merged

Refactor MCP connection response logging and timeout default handling to remove duplication#3966
lpcox merged 3 commits intomainfrom
copilot/duplicate-code-analysis-fix

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 16, 2026

The HTTP backend transport refactor introduced two maintainability duplications in internal/mcp/connection.go: repeated inbound RPC response logging across request paths, and repeated inline connect-timeout fallback logic. This change consolidates both patterns without changing request/transport behavior.

  • Consolidate inbound response logging in SendRequestWithServerID

    • Reworked control flow so HTTP/plain-JSON, HTTP/SDK, and stdio branches only set result/err.
    • Moved response payload marshaling + logInboundRPCResponse(...) into a single shared post-branch block.
  • Centralize connect-timeout fallback

    • Added normalizeConnectTimeout(timeout time.Duration) time.Duration.
    • Replaced duplicated <= 0 ? 30s logic in:
      • NewHTTPConnection(...)
      • reconnectSDKTransport()
    • Keeps fallback behavior aligned to defaultConnectTimeout via one helper.
  • Add focused timeout normalization coverage

    • Extended internal/mcp/connect_timeout_test.go with a table-driven test for:
      • zero timeout → default
      • negative timeout → default
      • positive timeout → unchanged
func normalizeConnectTimeout(timeout time.Duration) time.Duration {
	if timeout <= 0 {
		return defaultConnectTimeout
	}
	return timeout
}

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
    • Triggering command: /tmp/go-build1492124397/b510/launcher.test /tmp/go-build1492124397/b510/launcher.test -test.testlogfile=/tmp/go-build1492124397/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 1.80.0/resolver/map.go 1.80.0/resolver/resolver.go x_amd64/vet -c -I /tmp/go-build1492124397/b165/ x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2158887558/b514/launcher.test /tmp/go-build2158887558/b514/launcher.test -test.testlogfile=/tmp/go-build2158887558/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s .cfg�� pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-errorsas pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-ifaceassert 64/bin/gofmt -I /tmp/go-build273-V=full -I ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet estl�� /tmp/go-build2730629838/b437/_pkg_.a -trimpath ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -p google.golang.or/usr/bin/runc -lang=go1.24 ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet (dns block)
  • invalid-host-that-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1492124397/b492/config.test /tmp/go-build1492124397/b492/config.test -test.testlogfile=/tmp/go-build1492124397/b492/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 0629838/b276/_pk-errorsas -I x_amd64/vet --gdwarf-5 re/xxhash/v2 -o x_amd64/vet go_.�� 1.80.0/credentials/insecure/inse-errorsas 2124397/b062/vet.cfg x_amd64/vet 0629838/b276/sym/opt/hostedtoolcache/go/1.25.8/x64/pkg/tool/linux_amd64/vet --gdwarf2 --64 x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2158887558/b496/config.test /tmp/go-build2158887558/b496/config.test -test.testlogfile=/tmp/go-build2158887558/b496/testlog.txt -test.paniconexit0 -test.timeout=10m0s -o .cfg -trimpath 64/pkg/tool/linux_amd64/vet -p JllqmO5GmEzd -lang=go1.23 64/pkg/tool/linu-trimpath .cfg�� pkg/mod/github.c-p pkg/mod/github.cmain 64/pkg/tool/linu-lang=go1.25 -p google.golang.or--norc -lang=go1.24 64/pkg/tool/linu-dwarf=false (dns block)
  • nonexistent.local
    • Triggering command: /tmp/go-build1492124397/b510/launcher.test /tmp/go-build1492124397/b510/launcher.test -test.testlogfile=/tmp/go-build1492124397/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 1.80.0/resolver/map.go 1.80.0/resolver/resolver.go x_amd64/vet -c -I /tmp/go-build1492124397/b165/ x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2158887558/b514/launcher.test /tmp/go-build2158887558/b514/launcher.test -test.testlogfile=/tmp/go-build2158887558/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s .cfg�� pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-errorsas pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-ifaceassert 64/bin/gofmt -I /tmp/go-build273-V=full -I ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet estl�� /tmp/go-build2730629838/b437/_pkg_.a -trimpath ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -p google.golang.or/usr/bin/runc -lang=go1.24 ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet (dns block)
  • slow.example.com
    • Triggering command: /tmp/go-build1492124397/b510/launcher.test /tmp/go-build1492124397/b510/launcher.test -test.testlogfile=/tmp/go-build1492124397/b510/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true 1.80.0/resolver/map.go 1.80.0/resolver/resolver.go x_amd64/vet -c -I /tmp/go-build1492124397/b165/ x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2158887558/b514/launcher.test /tmp/go-build2158887558/b514/launcher.test -test.testlogfile=/tmp/go-build2158887558/b514/testlog.txt -test.paniconexit0 -test.timeout=10m0s .cfg�� pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-errorsas pkg/mod/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptr-ifaceassert 64/bin/gofmt -I /tmp/go-build273-V=full -I ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet estl�� /tmp/go-build2730629838/b437/_pkg_.a -trimpath ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -p google.golang.or/usr/bin/runc -lang=go1.24 ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet (dns block)
  • this-host-does-not-exist-12345.com
    • Triggering command: /tmp/go-build1492124397/b519/mcp.test /tmp/go-build1492124397/b519/mcp.test -test.testlogfile=/tmp/go-build1492124397/b519/testlog.txt -test.paniconexit0 -test.timeout=10m0s -test.v=true .cfg olang.org/protobuf@v1.36.11/encoding/prototext/dgoogle.golang.org/grpc/health/grpc_health_v1 x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet -o om/grpc-ecosystem/grpc-gateway/v2@v2.28.0/utilit-errorsas om/grpc-ecosystem/grpc-gateway/v2@v2.28.0/utilit-ifaceassert x_amd64/vet -p g/protobuf/encod-o -lang=go1.25 x_amd64/vet (dns block)
    • Triggering command: /tmp/go-build2158887558/b523/mcp.test /tmp/go-build2158887558/b523/mcp.test -test.testlogfile=/tmp/go-build2158887558/b523/testlog.txt -test.paniconexit0 -test.timeout=10m0s estl�� -bool -buildtags ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -errorsas -ifaceassert -nilfunc ache/go/1.25.8/x64/pkg/tool/linu--others 0629�� /tmp/go-build2730629838/b442/_pkg_.a -trimpath ache/go/1.25.8/x64/pkg/tool/linux_amd64/vet -p github.com/githu/usr/bin/runc -lang=go1.25 ache/go/1.25.8/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:

Copilot AI linked an issue Apr 16, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Refactor duplicate code in HTTP backend transport Refactor MCP connection response logging and timeout default handling to remove duplication Apr 16, 2026
Copilot AI requested a review from lpcox April 16, 2026 18:32
@lpcox lpcox marked this pull request as ready for review April 16, 2026 18:45
Copilot AI review requested due to automatic review settings April 16, 2026 18:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the MCP connection layer to remove duplicated logic around inbound RPC response logging and connect-timeout defaulting, keeping behavior consistent across HTTP (plain JSON / SDK transports) and stdio connections.

Changes:

  • Introduces normalizeConnectTimeout to centralize the “non-positive => default” connect-timeout fallback.
  • Consolidates inbound RPC response logging in SendRequestWithServerID into a single post-branch block.
  • Adds focused table-driven unit coverage for timeout normalization behavior.
Show a summary per file
File Description
internal/mcp/connection.go Centralizes connect-timeout fallback and de-duplicates inbound response logging across transport branches.
internal/mcp/connect_timeout_test.go Adds table-driven tests covering normalizeConnectTimeout behavior.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@lpcox lpcox merged commit 594423d into main Apr 16, 2026
32 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-analysis-fix branch April 16, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[duplicate-code] Duplicate Code Analysis Report

3 participants