Skip to content

Add custom headers to http client #60

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

Merged
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
50 changes: 50 additions & 0 deletions examples/http_example/auth_example_client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"context"
"log"

"github.com/davecgh/go-spew/spew"
mcp_golang "github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/http"
)

func main() {
// Create an HTTP transport that connects to the server
transport := http.NewHTTPClientTransport("/mcp")
transport.WithBaseURL("http://localhost:8080/api/v1")
// Public metoro token - not a leak
transport.WithHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdXN0b21lcklkIjoiOThlZDU1M2QtYzY4ZC00MDRhLWFhZjItNDM2ODllNWJiMGUzIiwiZW1haWwiOiJ0ZXN0QGNocmlzYmF0dGFyYmVlLmNvbSIsImV4cCI6MTgyMTI0NzIzN30.QeFzKsP1yO16pVol0mkAdt7qhJf6nTqBoqXqdWawBdE")

// Create a new client with the transport
client := mcp_golang.NewClient(transport)

// Initialize the client
if resp, err := client.Initialize(context.Background()); err != nil {
log.Fatalf("Failed to initialize client: %v", err)
} else {
log.Printf("Initialized client: %v", spew.Sdump(resp))
}

// List available tools
tools, err := client.ListTools(context.Background(), nil)
if err != nil {
log.Fatalf("Failed to list tools: %v", err)
}

log.Println("Available Tools:")
for _, tool := range tools.Tools {
desc := ""
if tool.Description != nil {
desc = *tool.Description
}
log.Printf("Tool: %s. Description: %s", tool.Name, desc)
}

response, err := client.CallTool(context.Background(), "get_log_attributes", map[string]interface{}{})
if err != nil {
log.Fatalf("Failed to call get_log_attributes tool: %v", err)
}

log.Printf("Response: %v", spew.Sdump(response))
}
11 changes: 11 additions & 0 deletions transport/http/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type HTTPClientTransport struct {
closeHandler func()
mu sync.RWMutex
client *http.Client
headers map[string]string
}

// NewHTTPClientTransport creates a new HTTP client transport that connects to the specified endpoint
func NewHTTPClientTransport(endpoint string) *HTTPClientTransport {
return &HTTPClientTransport{
endpoint: endpoint,
client: &http.Client{},
headers: make(map[string]string),
}
}

Expand All @@ -37,6 +39,12 @@ func (t *HTTPClientTransport) WithBaseURL(baseURL string) *HTTPClientTransport {
return t
}

// WithHeader adds a header to the request
func (t *HTTPClientTransport) WithHeader(key, value string) *HTTPClientTransport {
t.headers[key] = value
return t
}

// Start implements Transport.Start
func (t *HTTPClientTransport) Start(ctx context.Context) error {
// Does nothing in the stateless http client transport
Expand All @@ -56,6 +64,9 @@ func (t *HTTPClientTransport) Send(ctx context.Context, message *transport.BaseJ
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
for key, value := range t.headers {
req.Header.Set(key, value)
}

resp, err := t.client.Do(req)
if err != nil {
Expand Down
Loading