Skip to content

[SSEClient] Add ability to override the http.Client #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package client

import (
"fmt"
"github.com/mark3labs/mcp-go/client/transport"
"net/http"
"net/url"

"github.com/mark3labs/mcp-go/client/transport"
)

func WithHeaders(headers map[string]string) transport.ClientOption {
return transport.WithHeaders(headers)
}

func WithHTTPClient(httpClient *http.Client) transport.ClientOption {
return transport.WithHTTPClient(httpClient)
}

// NewSSEMCPClient creates a new SSE-based MCP client with the given base URL.
// Returns an error if the URL is invalid.
func NewSSEMCPClient(baseURL string, options ...transport.ClientOption) (*Client, error) {
Expand Down
6 changes: 6 additions & 0 deletions client/transport/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func WithHeaders(headers map[string]string) ClientOption {
}
}

func WithHTTPClient(httpClient *http.Client) ClientOption {
return func(sc *SSE) {
sc.httpClient = httpClient
}
}

// NewSSE creates a new SSE-based MCP client with the given base URL.
// Returns an error if the URL is invalid.
func NewSSE(baseURL string, options ...ClientOption) (*SSE, error) {
Expand Down
25 changes: 25 additions & 0 deletions client/transport/sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,31 @@ func TestSSEErrors(t *testing.T) {
}
})

t.Run("WithHTTPClient", func(t *testing.T) {
// Create a custom client with a very short timeout
customClient := &http.Client{Timeout: 1 * time.Nanosecond}

url, closeF := startMockSSEEchoServer()
defer closeF()
// Initialize SSE transport with the custom HTTP client
trans, err := NewSSE(url, WithHTTPClient(customClient))
if err != nil {
t.Fatalf("Failed to create SSE with custom client: %v", err)
}

// Starting should immediately error due to timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = trans.Start(ctx)
if err == nil {
t.Error("Expected Start to fail with custom timeout, got nil")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Errorf("Expected error 'context deadline exceeded', got '%s'", err.Error())
}
trans.Close()
})

t.Run("RequestBeforeStart", func(t *testing.T) {
url, closeF := startMockSSEEchoServer()
defer closeF()
Expand Down