[tools] Add the ability to provide a custom HTTP client for SSE client connections

8 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Jun 11, 2025, 5:08:17 PMJun 11
to goph...@pubsubhelper.golang.org, Andrey Starodubtsev, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

Add the ability to provide a custom HTTP client for SSE client connections

Previously, the MCP client relied on `http.DefaultClient` which didn't allow for customization. Here I'm adding an overload that accepts an `http.Client` as parameter.

Fixes https://github.com/golang/go/issues/74108
Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
GitHub-Last-Rev: 83ed6769be874254d8e6d8787804c1c978b3e184
GitHub-Pull-Request: golang/tools#580

Change diff

diff --git a/internal/mcp/sse.go b/internal/mcp/sse.go
index f49424a..b33da62 100644
--- a/internal/mcp/sse.go
+++ b/internal/mcp/sse.go
@@ -310,19 +310,30 @@
// https://modelcontextprotocol.io/specification/2024-11-05/basic/transports
type SSEClientTransport struct {
sseEndpoint *url.URL
+ httpClient *http.Client
}

// NewSSEClientTransport returns a new client transport that connects to the
-// SSE server at the provided URL.
+// SSE server at the provided URL using the default HTTP client.
//
// NewSSEClientTransport panics if the given URL is invalid.
func NewSSEClientTransport(baseURL string) *SSEClientTransport {
+ // Use the default HTTP client.
+ return NewSSEClientTransportWithHTTPClient(baseURL, http.DefaultClient)
+}
+
+// NewSSEClientTransportWithHTTPClient returns a new client transport that connects to the
+// SSE server at the provided URL using the provided HTTP client.
+//
+// NewSSEClientTransportWithHTTPClient panics if the given URL is invalid.
+func NewSSEClientTransportWithHTTPClient(baseURL string, httpClient *http.Client) *SSEClientTransport {
url, err := url.Parse(baseURL)
if err != nil {
panic(fmt.Sprintf("invalid base url: %v", err))
}
return &SSEClientTransport{
sseEndpoint: url,
+ httpClient: httpClient,
}
}

@@ -333,7 +344,7 @@
return nil, err
}
req.Header.Set("Accept", "text/event-stream")
- resp, err := http.DefaultClient.Do(req)
+ resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
@@ -404,6 +415,7 @@
// From here on, the stream takes ownership of resp.Body.
s := &sseClientStream{
sseEndpoint: c.sseEndpoint,
+ httpClient: c.httpClient,
msgEndpoint: msgEndpoint,
incoming: make(chan []byte, 100),
body: resp.Body,
@@ -435,9 +447,10 @@
// - Reads are SSE 'message' events, and pushes them onto a buffered channel.
// - Close terminates the GET request.
type sseClientStream struct {
- sseEndpoint *url.URL // SSE endpoint for the GET
- msgEndpoint *url.URL // session endpoint for POSTs
- incoming chan []byte // queue of incoming messages
+ sseEndpoint *url.URL // SSE endpoint for the GET
+ msgEndpoint *url.URL // session endpoint for POSTs
+ httpClient *http.Client // HTTP client to use for requests
+ incoming chan []byte // queue of incoming messages

mu sync.Mutex
body io.ReadCloser // body of the hanging GET
@@ -484,7 +497,7 @@
return err
}
req.Header.Set("Content-Type", "application/json")
- resp, err := http.DefaultClient.Do(req)
+ resp, err := c.httpClient.Do(req)
if err != nil {
return err
}

Change information

Files:
  • M internal/mcp/sse.go
Change size: S
Delta: 1 file changed, 19 insertions(+), 6 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
Gerrit-Change-Number: 681115
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Jun 11, 2025, 5:08:17 PMJun 11
to Gerrit Bot, Andrey Starodubtsev, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems.

These findings are based on simple heuristics. If a finding appears wrong, briefly reply here saying so. Otherwise, please address any problems and update the GitHub PR. When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above.

Possible problems detected:
1. The commit title should start with the primary affected package name followed by a colon, like "net/http: improve [...]".
2. The first word in the commit title after the package should be a lowercase English word (usually a verb).
3. Lines in the commit message should be wrapped at ~76 characters unless needed for things like URLs or tables. You have a 167 character line.
4. Are you using markdown? Markdown should not be used to augment text in the commit message.
5. You usually need to reference a bug number for all but trivial or cosmetic fixes. For the tools repo, the format is usually 'Fixes golang/go#12345' or 'Updates golang/go#12345' at the end of the commit message. Should you have a bug reference?

The commit title and commit message body come from the GitHub PR title and description, and must be edited in the GitHub web interface (not via git). For instructions, see [here](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message). For guidelines on commit messages for the Go project, see [here](https://go.dev/doc/contribute#commit_messages).


(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
    Gerrit-Change-Number: 681115
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 11 Jun 2025 21:08:14 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Jun 11, 2025, 5:10:32 PMJun 11
    to Gerrit Bot, Andrey Starodubtsev, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
    Gerrit-Change-Number: 681115
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Wed, 11 Jun 2025 21:10:26 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gerrit Bot (Gerrit)

    unread,
    Jun 12, 2025, 7:34:34 AMJun 12
    to Andrey Starodubtsev, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded new patchset

    Gerrit Bot uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
    Gerrit-Change-Number: 681115
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Andrey Starodubtsev (Gerrit)

    unread,
    Jun 12, 2025, 7:35:54 AMJun 12
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

    Andrey Starodubtsev added 1 comment

    Patchset-level comments
    File-level comment, Patchset 1:
    Gopher Robot . resolved

    I spotted some possible problems.

    These findings are based on simple heuristics. If a finding appears wrong, briefly reply here saying so. Otherwise, please address any problems and update the GitHub PR. When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above.

    Possible problems detected:
    1. The commit title should start with the primary affected package name followed by a colon, like "net/http: improve [...]".
    2. The first word in the commit title after the package should be a lowercase English word (usually a verb).
    3. Lines in the commit message should be wrapped at ~76 characters unless needed for things like URLs or tables. You have a 167 character line.
    4. Are you using markdown? Markdown should not be used to augment text in the commit message.
    5. You usually need to reference a bug number for all but trivial or cosmetic fixes. For the tools repo, the format is usually 'Fixes golang/go#12345' or 'Updates golang/go#12345' at the end of the commit message. Should you have a bug reference?

    The commit title and commit message body come from the GitHub PR title and description, and must be edited in the GitHub web interface (not via git). For instructions, see [here](https://go.dev/wiki/GerritBot/#how-does-gerritbot-determine-the-final-commit-message). For guidelines on commit messages for the Go project, see [here](https://go.dev/doc/contribute#commit_messages).


    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Andrey Starodubtsev

    Done

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
      Gerrit-Change-Number: 681115
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Comment-Date: Thu, 12 Jun 2025 11:35:46 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Jun 17, 2025, 6:38:08 AMJun 17
      to Andrey Starodubtsev, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #3 to this change.
      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
      Gerrit-Change-Number: 681115
      Gerrit-PatchSet: 3
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Robert Findley (Gerrit)

      unread,
      Jun 18, 2025, 2:28:12 PMJun 18
      to Gerrit Bot, Andrey Starodubtsev, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

      Robert Findley added 1 comment

      File internal/mcp/sse.go
      Line 321, Patchset 3 (Latest): // Use the default HTTP client.
      return NewSSEClientTransportWithHTTPClient(baseURL, http.DefaultClient)

      }

      // NewSSEClientTransportWithHTTPClient returns a new client transport that connects to the
      // SSE server at the provided URL using the provided HTTP client.
      //

      // NewSSEClientTransportWithHTTPClient panics if the given URL is invalid.
      func NewSSEClientTransportWithHTTPClient(baseURL string, httpClient *http.Client) *SSEClientTransport {
      Robert Findley . unresolved

      The pattern we've settled on for configuration is to pass

      ```go
      type SSEClientTransportOptions struct {
      // If set, HTTPClient configures http client used by the transport to
      // make outgoing HTTP requests. Otherwise, the http.DefaultClient is
      // used.
      HTTPClient *http.Client
      }
      ```

      Could you also make the same change for the new StreamableClientTransport, and update the design doc (briefly) in internal/mcp/design/design.md?

      If not, I am happy to do this (we need this today, I'm afraid, so if you don't happen to be around I'll do it myself, or have a coding agent help :)).

      Thanks for the change.

      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
        Gerrit-Change-Number: 681115
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Robert Findley <rfin...@google.com>
        Gerrit-Comment-Date: Wed, 18 Jun 2025 18:28:09 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        unsatisfied_requirement
        open
        diffy

        Andrey Starodubtsev (Gerrit)

        unread,
        Jun 18, 2025, 3:46:56 PMJun 18
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Robert Findley, Gopher Robot, golang-co...@googlegroups.com

        Andrey Starodubtsev added 1 comment

        File internal/mcp/sse.go
        Line 321, Patchset 3 (Latest): // Use the default HTTP client.
        return NewSSEClientTransportWithHTTPClient(baseURL, http.DefaultClient)
        }

        // NewSSEClientTransportWithHTTPClient returns a new client transport that connects to the
        // SSE server at the provided URL using the provided HTTP client.
        //
        // NewSSEClientTransportWithHTTPClient panics if the given URL is invalid.
        func NewSSEClientTransportWithHTTPClient(baseURL string, httpClient *http.Client) *SSEClientTransport {
        Robert Findley . unresolved

        The pattern we've settled on for configuration is to pass

        ```go
        type SSEClientTransportOptions struct {
        // If set, HTTPClient configures http client used by the transport to
        // make outgoing HTTP requests. Otherwise, the http.DefaultClient is
        // used.
        HTTPClient *http.Client
        }
        ```

        Could you also make the same change for the new StreamableClientTransport, and update the design doc (briefly) in internal/mcp/design/design.md?

        If not, I am happy to do this (we need this today, I'm afraid, so if you don't happen to be around I'll do it myself, or have a coding agent help :)).

        Thanks for the change.

        Andrey Starodubtsev

        Sorry, afk for the rest of the today. Feel free to submit your version.

        Open in Gerrit

        Related details

        Attention set is empty
        Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
        Gerrit-Change-Number: 681115
        Gerrit-PatchSet: 3
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-CC: Robert Findley <rfin...@google.com>
        Gerrit-Comment-Date: Wed, 18 Jun 2025 19:46:48 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: No
        Comment-In-Reply-To: Robert Findley <rfin...@google.com>
        unsatisfied_requirement
        open
        diffy

        Robert Findley (Gerrit)

        unread,
        Jun 18, 2025, 3:53:38 PMJun 18
        to Gerrit Bot, Andrey Starodubtsev, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from Andrey Starodubtsev

        Robert Findley added 1 comment

        File internal/mcp/sse.go
        Line 321, Patchset 3 (Latest): // Use the default HTTP client.
        return NewSSEClientTransportWithHTTPClient(baseURL, http.DefaultClient)
        }

        // NewSSEClientTransportWithHTTPClient returns a new client transport that connects to the
        // SSE server at the provided URL using the provided HTTP client.
        //
        // NewSSEClientTransportWithHTTPClient panics if the given URL is invalid.
        func NewSSEClientTransportWithHTTPClient(baseURL string, httpClient *http.Client) *SSEClientTransport {
        Robert Findley . resolved

        The pattern we've settled on for configuration is to pass

        ```go
        type SSEClientTransportOptions struct {
        // If set, HTTPClient configures http client used by the transport to
        // make outgoing HTTP requests. Otherwise, the http.DefaultClient is
        // used.
        HTTPClient *http.Client
        }
        ```

        Could you also make the same change for the new StreamableClientTransport, and update the design doc (briefly) in internal/mcp/design/design.md?

        If not, I am happy to do this (we need this today, I'm afraid, so if you don't happen to be around I'll do it myself, or have a coding agent help :)).

        Thanks for the change.

        Andrey Starodubtsev

        Sorry, afk for the rest of the today. Feel free to submit your version.

        Robert Findley

        Ack, thanks, and sorry for the delay getting to this.

        Open in Gerrit

        Related details

        Attention is currently required from:
        • Andrey Starodubtsev
        Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: comment
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: I1c106c5e98a6e4624a46c7d1d5e19e4a559c22bb
          Gerrit-Change-Number: 681115
          Gerrit-PatchSet: 3
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-CC: Andrey Starodubtsev <andre...@google.com>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-CC: Robert Findley <rfin...@google.com>
          Gerrit-Attention: Andrey Starodubtsev <andre...@google.com>
          Gerrit-Comment-Date: Wed, 18 Jun 2025 19:53:33 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: No
          Comment-In-Reply-To: Robert Findley <rfin...@google.com>
          Comment-In-Reply-To: Andrey Starodubtsev <andre...@google.com>
          unsatisfied_requirement
          satisfied_requirement
          open
          diffy

          Gopher Robot (Gerrit)

          unread,
          Jun 20, 2025, 5:20:43 AMJun 20
          to Gerrit Bot, Andrey Starodubtsev, goph...@pubsubhelper.golang.org, Robert Findley, golang-co...@googlegroups.com

          Gopher Robot abandoned this change

          Related details

          Attention set is empty
          Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: abandon
          unsatisfied_requirement
          satisfied_requirement
          open
          diffy
          Reply all
          Reply to author
          Forward
          0 new messages