From d34053e21d93708f59d4710bc271c698bbc3ea2b Mon Sep 17 00:00:00 2001 From: Vi Date: Thu, 5 Feb 2026 10:52:48 +0000 Subject: [PATCH] fix(mcp): add default address and warning for TCP transport (#332) * fix(io): apply gofmt formatting to local/client.go Remove extra blank line before closing parenthesis in import block. Co-Authored-By: Claude Opus 4.5 * fix(mcp): add default address and warning for TCP transport NewTCPTransport now properly handles edge cases: - Sets default address to 127.0.0.1:9100 when empty string is passed - Prints security warning to stderr when binding to 0.0.0.0 (all interfaces) This fixes TestNewTCPTransport_Defaults and TestNewTCPTransport_Warning tests that were causing CI failures in PRs #298 and #313. Co-Authored-By: Claude Opus 4.5 --------- Co-authored-by: Claude Co-authored-by: Claude Opus 4.5 --- pkg/io/local/client.go | 1 - pkg/mcp/transport_tcp.go | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/io/local/client.go b/pkg/io/local/client.go index 43ca775e..db667d7a 100644 --- a/pkg/io/local/client.go +++ b/pkg/io/local/client.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strings" - ) // Medium is a local filesystem storage backend. diff --git a/pkg/mcp/transport_tcp.go b/pkg/mcp/transport_tcp.go index 2cf3ff52..507aef8f 100644 --- a/pkg/mcp/transport_tcp.go +++ b/pkg/mcp/transport_tcp.go @@ -3,8 +3,11 @@ package mcp import ( "bufio" "context" + "fmt" "io" "net" + "os" + "strings" "github.com/host-uk/core/pkg/log" "github.com/modelcontextprotocol/go-sdk/jsonrpc" @@ -20,9 +23,23 @@ type TCPTransport struct { listener net.Listener } +// DefaultTCPAddr is the default address for the MCP TCP transport. +const DefaultTCPAddr = "127.0.0.1:9100" + // NewTCPTransport creates a new TCP transport listener. // It listens on the provided address (e.g. "localhost:9100"). +// If addr is empty, it defaults to 127.0.0.1:9100. +// A warning is printed to stderr if binding to 0.0.0.0 (all interfaces). func NewTCPTransport(addr string) (*TCPTransport, error) { + if addr == "" { + addr = DefaultTCPAddr + } + + // Warn if binding to all interfaces + if strings.HasPrefix(addr, "0.0.0.0:") { + fmt.Fprintln(os.Stderr, "WARNING: MCP TCP server binding to all interfaces (0.0.0.0). This may expose the service to the network.") + } + listener, err := net.Listen("tcp", addr) if err != nil { return nil, err