Skip to content

Commit a2d8ace

Browse files
committed
Add custom headers in Initialize. Add comment to WithHeaders. Add header conflict protection
1 parent 6d0b954 commit a2d8ace

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

client/sse.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ type SSEMCPClient struct {
4141

4242
type ClientOption func(*SSEMCPClient)
4343

44+
// WithHeaders sets custom HTTP headers that will be included in all requests made by the client.
45+
// This is particularly useful for authentication (e.g., bearer tokens, API keys) and other
46+
// custom header requirements.
4447
func WithHeaders(headers map[string]string) ClientOption {
4548
return func(sc *SSEMCPClient) {
4649
sc.headers = headers
@@ -96,6 +99,11 @@ func (c *SSEMCPClient) Start(ctx context.Context) error {
9699

97100
// set custom http headers
98101
for k, v := range c.headers {
102+
// Skip standard headers that should not be overridden
103+
switch k {
104+
case "Accept", "Cache-Control", "Connection", "Content-Type":
105+
continue
106+
}
99107
req.Header.Set(k, v)
100108
}
101109

@@ -308,6 +316,11 @@ func (c *SSEMCPClient) sendRequest(
308316
req.Header.Set("Content-Type", "application/json")
309317
// set custom http headers
310318
for k, v := range c.headers {
319+
// Skip standard headers that should not be overridden
320+
switch k {
321+
case "Accept", "Cache-Control", "Connection", "Content-Type":
322+
continue
323+
}
311324
req.Header.Set(k, v)
312325
}
313326

@@ -396,6 +409,10 @@ func (c *SSEMCPClient) Initialize(
396409
}
397410

398411
req.Header.Set("Content-Type", "application/json")
412+
// set custom http headers
413+
for k, v := range c.headers {
414+
req.Header.Set(k, v)
415+
}
399416

400417
resp, err := c.httpClient.Do(req)
401418
if err != nil {

0 commit comments

Comments
 (0)