From 6d0b954627adfb8568a68bbcb75b7f165aba22f0 Mon Sep 17 00:00:00 2001 From: "@k33g" Date: Tue, 8 Apr 2025 05:58:23 +0200 Subject: [PATCH 1/3] Add custom headers at the start of the SSE client --- client/sse.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/sse.go b/client/sse.go index cf4a102..79c08ce 100644 --- a/client/sse.go +++ b/client/sse.go @@ -94,6 +94,11 @@ func (c *SSEMCPClient) Start(ctx context.Context) error { req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Connection", "keep-alive") + // set custom http headers + for k, v := range c.headers { + req.Header.Set(k, v) + } + resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("failed to connect to SSE stream: %w", err) From a2d8ace1564ac77adc2b77103b7ac133f103c7c1 Mon Sep 17 00:00:00 2001 From: "@k33g" Date: Sun, 13 Apr 2025 04:08:06 +0200 Subject: [PATCH 2/3] Add custom headers in Initialize. Add comment to WithHeaders. Add header conflict protection --- client/sse.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/client/sse.go b/client/sse.go index 79c08ce..d701ef1 100644 --- a/client/sse.go +++ b/client/sse.go @@ -41,6 +41,9 @@ type SSEMCPClient struct { type ClientOption func(*SSEMCPClient) +// WithHeaders sets custom HTTP headers that will be included in all requests made by the client. +// This is particularly useful for authentication (e.g., bearer tokens, API keys) and other +// custom header requirements. func WithHeaders(headers map[string]string) ClientOption { return func(sc *SSEMCPClient) { sc.headers = headers @@ -96,6 +99,11 @@ func (c *SSEMCPClient) Start(ctx context.Context) error { // set custom http headers for k, v := range c.headers { + // Skip standard headers that should not be overridden + switch k { + case "Accept", "Cache-Control", "Connection", "Content-Type": + continue + } req.Header.Set(k, v) } @@ -308,6 +316,11 @@ func (c *SSEMCPClient) sendRequest( req.Header.Set("Content-Type", "application/json") // set custom http headers for k, v := range c.headers { + // Skip standard headers that should not be overridden + switch k { + case "Accept", "Cache-Control", "Connection", "Content-Type": + continue + } req.Header.Set(k, v) } @@ -396,6 +409,10 @@ func (c *SSEMCPClient) Initialize( } req.Header.Set("Content-Type", "application/json") + // set custom http headers + for k, v := range c.headers { + req.Header.Set(k, v) + } resp, err := c.httpClient.Do(req) if err != nil { From da33720868c9c2586cd513b9f84f687828856b96 Mon Sep 17 00:00:00 2001 From: "@k33g" Date: Sun, 13 Apr 2025 04:19:15 +0200 Subject: [PATCH 3/3] Standardize comments. Add Example comment. Add header conflict protection to Initialize --- client/sse.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/client/sse.go b/client/sse.go index d701ef1..00ba59f 100644 --- a/client/sse.go +++ b/client/sse.go @@ -58,6 +58,15 @@ func WithSSEReadTimeout(timeout time.Duration) ClientOption { // NewSSEMCPClient creates a new SSE-based MCP client with the given base URL. // Returns an error if the URL is invalid. +// Example: +// +// // Create a client with authentication headers +// client, err := NewSSEMCPClient( +// "https://mcp.example.com", +// WithHeaders(map[string]string{ +// "Authorization": "Bearer your-token-here", +// }), +// ) func NewSSEMCPClient(baseURL string, options ...ClientOption) (*SSEMCPClient, error) { parsedURL, err := url.Parse(baseURL) if err != nil { @@ -97,7 +106,7 @@ func (c *SSEMCPClient) Start(ctx context.Context) error { req.Header.Set("Cache-Control", "no-cache") req.Header.Set("Connection", "keep-alive") - // set custom http headers + // Set custom http headers for k, v := range c.headers { // Skip standard headers that should not be overridden switch k { @@ -314,7 +323,7 @@ func (c *SSEMCPClient) sendRequest( } req.Header.Set("Content-Type", "application/json") - // set custom http headers + // Set custom http headers for k, v := range c.headers { // Skip standard headers that should not be overridden switch k { @@ -409,8 +418,13 @@ func (c *SSEMCPClient) Initialize( } req.Header.Set("Content-Type", "application/json") - // set custom http headers + // Set custom http headers for k, v := range c.headers { + // Skip standard headers that should not be overridden + switch k { + case "Accept", "Cache-Control", "Connection", "Content-Type": + continue + } req.Header.Set(k, v) }