diff --git a/coderd/x/chatd/mcpclient/mcpclient.go b/coderd/x/chatd/mcpclient/mcpclient.go index cce959f2a7..cbb698e038 100644 --- a/coderd/x/chatd/mcpclient/mcpclient.go +++ b/coderd/x/chatd/mcpclient/mcpclient.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "net/http" "net/url" "strings" "sync" @@ -214,17 +215,30 @@ func createTransport( cfg database.MCPServerConfig, headers map[string]string, ) (transport.Interface, error) { + // Each connection gets its own HTTP client with a dedicated + // transport so that httptest.Server.Close() (which calls + // CloseIdleConnections on http.DefaultTransport) does not + // disrupt unrelated connections during parallel tests. + var httpClient *http.Client + if dt, ok := http.DefaultTransport.(*http.Transport); ok { + httpClient = &http.Client{Transport: dt.Clone()} + } else { + httpClient = &http.Client{} + } + switch cfg.Transport { case "sse": return transport.NewSSE( cfg.Url, transport.WithHeaders(headers), + transport.WithHTTPClient(httpClient), ) case "", "streamable_http": // Default to streamable HTTP, the newer transport. return transport.NewStreamableHTTP( cfg.Url, transport.WithHTTPHeaders(headers), + transport.WithHTTPBasicClient(httpClient), ) default: return nil, xerrors.Errorf(