Skip to content

Commit e3a1934

Browse files
committed
change tokensources into resolved strings
1 parent 9d3c16a commit e3a1934

2 files changed

Lines changed: 6 additions & 65 deletions

File tree

‎core/transport/mcp/v20250326/mcp.go‎

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/google/uuid"
2828
"github.com/googleapis/mcp-toolbox-sdk-go/core/transport"
2929
"github.com/googleapis/mcp-toolbox-sdk-go/core/transport/mcp"
30-
"golang.org/x/oauth2"
3130
)
3231

3332
const (
@@ -63,16 +62,11 @@ func New(baseURL string, client *http.Client) (*McpTransport, error) {
6362
}
6463

6564
// ListTools fetches available tools
66-
func (t *McpTransport) ListTools(ctx context.Context, toolsetName string, headers map[string]oauth2.TokenSource) (*transport.ManifestSchema, error) {
65+
func (t *McpTransport) ListTools(ctx context.Context, toolsetName string, headers map[string]string) (*transport.ManifestSchema, error) {
6766
if err := t.EnsureInitialized(ctx); err != nil {
6867
return nil, err
6968
}
7069

71-
finalHeaders, err := t.resolveHeaders(headers)
72-
if err != nil {
73-
return nil, err
74-
}
75-
7670
// Append toolset name to base URL if provided
7771
requestURL := t.BaseURL()
7872
if toolsetName != "" {
@@ -84,7 +78,7 @@ func (t *McpTransport) ListTools(ctx context.Context, toolsetName string, header
8478
}
8579

8680
var result listToolsResult
87-
if _, err := t.sendRequest(ctx, requestURL, "tools/list", map[string]any{}, finalHeaders, &result); err != nil {
81+
if _, err := t.sendRequest(ctx, requestURL, "tools/list", map[string]any{}, headers, &result); err != nil {
8882
return nil, fmt.Errorf("failed to list tools: %w", err)
8983
}
9084

@@ -117,7 +111,7 @@ func (t *McpTransport) ListTools(ctx context.Context, toolsetName string, header
117111
}
118112

119113
// GetTool fetches a single tool
120-
func (t *McpTransport) GetTool(ctx context.Context, toolName string, headers map[string]oauth2.TokenSource) (*transport.ManifestSchema, error) {
114+
func (t *McpTransport) GetTool(ctx context.Context, toolName string, headers map[string]string) (*transport.ManifestSchema, error) {
121115
manifest, err := t.ListTools(ctx, "", headers)
122116
if err != nil {
123117
return nil, err
@@ -135,22 +129,17 @@ func (t *McpTransport) GetTool(ctx context.Context, toolName string, headers map
135129
}
136130

137131
// InvokeTool executes a tool
138-
func (t *McpTransport) InvokeTool(ctx context.Context, toolName string, payload map[string]any, headers map[string]oauth2.TokenSource) (any, error) {
132+
func (t *McpTransport) InvokeTool(ctx context.Context, toolName string, payload map[string]any, headers map[string]string) (any, error) {
139133
if err := t.EnsureInitialized(ctx); err != nil {
140134
return "", err
141135
}
142136

143-
finalHeaders, err := t.resolveHeaders(headers)
144-
if err != nil {
145-
return "", err
146-
}
147-
148137
params := callToolRequestParams{
149138
Name: toolName,
150139
Arguments: payload,
151140
}
152141
var result callToolResult
153-
if _, err := t.sendRequest(ctx, t.BaseURL(), "tools/call", params, finalHeaders, &result); err != nil {
142+
if _, err := t.sendRequest(ctx, t.BaseURL(), "tools/call", params, headers, &result); err != nil {
154143
return "", fmt.Errorf("failed to invoke tool '%s': %w", toolName, err)
155144
}
156145

@@ -223,27 +212,6 @@ func (t *McpTransport) initializeSession(ctx context.Context) error {
223212
return err
224213
}
225214

226-
// resolveHeaders converts a map of TokenSources into standard HTTP headers.
227-
func (t *McpTransport) resolveHeaders(sources map[string]oauth2.TokenSource) (map[string]string, error) {
228-
if sources == nil {
229-
return nil, nil
230-
}
231-
232-
headers := make(map[string]string, len(sources))
233-
for headerKey, source := range sources {
234-
if source == nil {
235-
continue
236-
}
237-
238-
token, err := source.Token()
239-
if err != nil {
240-
return nil, fmt.Errorf("failed to get token for header %s: %w", headerKey, err)
241-
}
242-
headers[headerKey] = token.AccessToken
243-
}
244-
return headers, nil
245-
}
246-
247215
// sendRequest sends a JSON-RPC request and injects the Session ID if active.
248216
func (t *McpTransport) sendRequest(ctx context.Context, url string, method string, params any, headers map[string]string, dest any) (http.Header, error) {
249217

‎core/transport/mcp/v20250326/mcp_test.go‎

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"github.com/stretchr/testify/assert"
2929
"github.com/stretchr/testify/require"
30-
"golang.org/x/oauth2"
3130
)
3231

3332
// mockMCPServer is a helper to mock MCP JSON-RPC responses
@@ -316,8 +315,7 @@ func TestListTools_WithAuthHeaders(t *testing.T) {
316315
}
317316

318317
client, _ := New(server.URL, server.Client())
319-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "secret"})
320-
headers := map[string]oauth2.TokenSource{"Authorization": ts}
318+
headers := map[string]string{"Authorization": "secret"}
321319

322320
_, err := client.ListTools(context.Background(), "", headers)
323321
require.NoError(t, err)
@@ -442,31 +440,6 @@ func TestListTools_InitFailure(t *testing.T) {
442440
assert.Contains(t, err.Error(), "http request failed")
443441
}
444442

445-
type failingTokenSource struct{}
446-
447-
func (f *failingTokenSource) Token() (*oauth2.Token, error) {
448-
return nil, errors.New("token failure")
449-
}
450-
451-
func TestHeaders_ResolutionError(t *testing.T) {
452-
// Fix: Use mock server to pass initialization so we hit the header resolution logic
453-
server := newMockMCPServer()
454-
defer server.Close()
455-
456-
client, _ := New(server.URL, server.Client())
457-
headers := map[string]oauth2.TokenSource{"auth": &failingTokenSource{}}
458-
459-
// ListTools: EnsureInitialized succeeds, then header resolution fails
460-
_, err := client.ListTools(context.Background(), "", headers)
461-
assert.Error(t, err)
462-
assert.Contains(t, err.Error(), "token failure")
463-
464-
// InvokeTool: EnsureInitialized succeeds, then header resolution fails
465-
_, err = client.InvokeTool(context.Background(), "tool", nil, headers)
466-
assert.Error(t, err)
467-
assert.Contains(t, err.Error(), "token failure")
468-
}
469-
470443
func TestInit_NotificationFailure(t *testing.T) {
471444
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
472445
var req jsonRPCRequest

0 commit comments

Comments
 (0)