Skip to content

feat(client): HTTP Headers Support for SSE MCP Client at Start #122

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 3 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 37 additions & 1 deletion client/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -55,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 {
Expand Down Expand Up @@ -94,6 +106,16 @@ 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 {
// Skip standard headers that should not be overridden
switch k {
case "Accept", "Cache-Control", "Connection", "Content-Type":
continue
}
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)
Expand Down Expand Up @@ -301,8 +323,13 @@ 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 {
case "Accept", "Cache-Control", "Connection", "Content-Type":
continue
}
req.Header.Set(k, v)
}

Expand Down Expand Up @@ -391,6 +418,15 @@ func (c *SSEMCPClient) Initialize(
}

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)
}

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