Skip to content

Commit 682b7ad

Browse files
Add custom headers to http client (#60)
1 parent 219a972 commit 682b7ad

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Diff for: examples/http_example/auth_example_client/main.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/davecgh/go-spew/spew"
8+
mcp_golang "github.com/metoro-io/mcp-golang"
9+
"github.com/metoro-io/mcp-golang/transport/http"
10+
)
11+
12+
func main() {
13+
// Create an HTTP transport that connects to the server
14+
transport := http.NewHTTPClientTransport("/mcp")
15+
transport.WithBaseURL("http://localhost:8080/api/v1")
16+
// Public metoro token - not a leak
17+
transport.WithHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdXN0b21lcklkIjoiOThlZDU1M2QtYzY4ZC00MDRhLWFhZjItNDM2ODllNWJiMGUzIiwiZW1haWwiOiJ0ZXN0QGNocmlzYmF0dGFyYmVlLmNvbSIsImV4cCI6MTgyMTI0NzIzN30.QeFzKsP1yO16pVol0mkAdt7qhJf6nTqBoqXqdWawBdE")
18+
19+
// Create a new client with the transport
20+
client := mcp_golang.NewClient(transport)
21+
22+
// Initialize the client
23+
if resp, err := client.Initialize(context.Background()); err != nil {
24+
log.Fatalf("Failed to initialize client: %v", err)
25+
} else {
26+
log.Printf("Initialized client: %v", spew.Sdump(resp))
27+
}
28+
29+
// List available tools
30+
tools, err := client.ListTools(context.Background(), nil)
31+
if err != nil {
32+
log.Fatalf("Failed to list tools: %v", err)
33+
}
34+
35+
log.Println("Available Tools:")
36+
for _, tool := range tools.Tools {
37+
desc := ""
38+
if tool.Description != nil {
39+
desc = *tool.Description
40+
}
41+
log.Printf("Tool: %s. Description: %s", tool.Name, desc)
42+
}
43+
44+
response, err := client.CallTool(context.Background(), "get_log_attributes", map[string]interface{}{})
45+
if err != nil {
46+
log.Fatalf("Failed to call get_log_attributes tool: %v", err)
47+
}
48+
49+
log.Printf("Response: %v", spew.Sdump(response))
50+
}

Diff for: transport/http/http_client.go

+11
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ type HTTPClientTransport struct {
2121
closeHandler func()
2222
mu sync.RWMutex
2323
client *http.Client
24+
headers map[string]string
2425
}
2526

2627
// NewHTTPClientTransport creates a new HTTP client transport that connects to the specified endpoint
2728
func NewHTTPClientTransport(endpoint string) *HTTPClientTransport {
2829
return &HTTPClientTransport{
2930
endpoint: endpoint,
3031
client: &http.Client{},
32+
headers: make(map[string]string),
3133
}
3234
}
3335

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

42+
// WithHeader adds a header to the request
43+
func (t *HTTPClientTransport) WithHeader(key, value string) *HTTPClientTransport {
44+
t.headers[key] = value
45+
return t
46+
}
47+
4048
// Start implements Transport.Start
4149
func (t *HTTPClientTransport) Start(ctx context.Context) error {
4250
// Does nothing in the stateless http client transport
@@ -56,6 +64,9 @@ func (t *HTTPClientTransport) Send(ctx context.Context, message *transport.BaseJ
5664
return fmt.Errorf("failed to create request: %w", err)
5765
}
5866
req.Header.Set("Content-Type", "application/json")
67+
for key, value := range t.headers {
68+
req.Header.Set(key, value)
69+
}
5970

6071
resp, err := t.client.Do(req)
6172
if err != nil {

0 commit comments

Comments
 (0)