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
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
}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.)
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
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.)
Done
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
// 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 {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.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
// 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 {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.
Sorry, afk for the rest of the today. Feel free to submit your version.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
// 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 {Andrey StarodubtsevThe 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.
Sorry, afk for the rest of the today. Feel free to submit your version.
Ack, thanks, and sorry for the delay getting to this.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |