Skip to content

Commit 9e6ccca

Browse files
committed
Formatting
1 parent 2c8bf2b commit 9e6ccca

File tree

14 files changed

+107
-107
lines changed

14 files changed

+107
-107
lines changed

client/oauth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var NewMemoryTokenStore = transport.NewMemoryTokenStore
2727
func NewOAuthStreamableHttpClient(baseURL string, oauthConfig OAuthConfig, options ...transport.StreamableHTTPCOption) (*Client, error) {
2828
// Add OAuth option to the list of options
2929
options = append(options, transport.WithOAuth(oauthConfig))
30-
30+
3131
trans, err := transport.NewStreamableHTTP(baseURL, options...)
3232
if err != nil {
3333
return nil, fmt.Errorf("failed to create HTTP transport: %w", err)
@@ -49,8 +49,8 @@ type OAuthAuthorizationRequiredError = transport.OAuthAuthorizationRequiredError
4949

5050
// IsOAuthAuthorizationRequiredError checks if an error is an OAuthAuthorizationRequiredError
5151
func IsOAuthAuthorizationRequiredError(err error) bool {
52-
var target *OAuthAuthorizationRequiredError
53-
return errors.As(err, &target)
52+
var target *OAuthAuthorizationRequiredError
53+
return errors.As(err, &target)
5454
}
5555

5656
// GetOAuthHandler extracts the OAuthHandler from an OAuthAuthorizationRequiredError
@@ -60,4 +60,4 @@ func GetOAuthHandler(err error) *transport.OAuthHandler {
6060
return oauthErr.Handler
6161
}
6262
return nil
63-
}
63+
}

client/oauth_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func TestNewOAuthStreamableHttpClient(t *testing.T) {
5757

5858
// Create OAuth config
5959
oauthConfig := OAuthConfig{
60-
ClientID: "test-client",
61-
RedirectURI: "http://localhost:8085/callback",
62-
Scopes: []string{"mcp.read", "mcp.write"},
63-
TokenStore: tokenStore,
64-
PKCEEnabled: true,
60+
ClientID: "test-client",
61+
RedirectURI: "http://localhost:8085/callback",
62+
Scopes: []string{"mcp.read", "mcp.write"},
63+
TokenStore: tokenStore,
64+
PKCEEnabled: true,
6565
}
6666

6767
// Create client with OAuth
@@ -124,4 +124,4 @@ func TestIsOAuthAuthorizationRequiredError(t *testing.T) {
124124
if handler != nil {
125125
t.Errorf("Expected GetOAuthHandler to return nil")
126126
}
127-
}
127+
}

client/transport/oauth.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func NewOAuthHandler(config OAuthConfig) *OAuthHandler {
123123
if config.TokenStore == nil {
124124
config.TokenStore = NewMemoryTokenStore()
125125
}
126-
126+
127127
return &OAuthHandler{
128128
config: config,
129129
httpClient: &http.Client{Timeout: 30 * time.Second},
@@ -239,7 +239,7 @@ func extractOAuthError(body []byte, statusCode int, context string) error {
239239
if err := json.Unmarshal(body, &oauthErr); err == nil && oauthErr.ErrorCode != "" {
240240
return fmt.Errorf("%s: %w", context, oauthErr)
241241
}
242-
242+
243243
// If not a valid OAuth error, return the raw response
244244
return fmt.Errorf("%s with status %d: %s", context, statusCode, body)
245245
}
@@ -447,7 +447,7 @@ func (h *OAuthHandler) getDefaultEndpoints(baseURL string) (*AuthServerMetadata,
447447
// Discard any path component to get the authorization base URL
448448
parsedURL.Path = ""
449449
authBaseURL := parsedURL.String()
450-
450+
451451
// Validate that the URL has a scheme and host
452452
if parsedURL.Scheme == "" || parsedURL.Host == "" {
453453
return nil, fmt.Errorf("invalid base URL: missing scheme or host in %q", baseURL)
@@ -543,16 +543,16 @@ func (h *OAuthHandler) ProcessAuthorizationResponse(ctx context.Context, code, s
543543
if h.expectedState == "" {
544544
return errors.New("no expected state found, authorization flow may not have been initiated properly")
545545
}
546-
546+
547547
if state != h.expectedState {
548548
return ErrInvalidState
549549
}
550-
550+
551551
// Clear the expected state after validation
552552
defer func() {
553553
h.expectedState = ""
554554
}()
555-
555+
556556
metadata, err := h.getServerMetadata(ctx)
557557
if err != nil {
558558
return fmt.Errorf("failed to get server metadata: %w", err)
@@ -629,7 +629,7 @@ func (h *OAuthHandler) GetAuthorizationURL(ctx context.Context, state, codeChall
629629
params.Set("client_id", h.config.ClientID)
630630
params.Set("redirect_uri", h.config.RedirectURI)
631631
params.Set("state", state)
632-
632+
633633
if len(h.config.Scopes) > 0 {
634634
params.Set("scope", strings.Join(h.config.Scopes, " "))
635635
}
@@ -640,4 +640,4 @@ func (h *OAuthHandler) GetAuthorizationURL(ctx context.Context, state, codeChall
640640
}
641641

642642
return metadata.AuthorizationEndpoint + "?" + params.Encode(), nil
643-
}
643+
}

client/transport/oauth_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ func TestOAuthHandler_GetAuthorizationHeader_EmptyAccessToken(t *testing.T) {
172172

173173
// Create an OAuth handler
174174
config := OAuthConfig{
175-
ClientID: "test-client",
176-
RedirectURI: "http://localhost:8085/callback",
177-
Scopes: []string{"mcp.read", "mcp.write"},
178-
TokenStore: tokenStore,
179-
PKCEEnabled: true,
175+
ClientID: "test-client",
176+
RedirectURI: "http://localhost:8085/callback",
177+
Scopes: []string{"mcp.read", "mcp.write"},
178+
TokenStore: tokenStore,
179+
PKCEEnabled: true,
180180
}
181181

182182
handler := NewOAuthHandler(config)
@@ -214,8 +214,8 @@ func TestOAuthHandler_GetServerMetadata_EmptyURL(t *testing.T) {
214214

215215
// Verify the error message contains something about a connection error
216216
// since we're now trying to connect to the well-known endpoint
217-
if !strings.Contains(err.Error(), "connection refused") &&
218-
!strings.Contains(err.Error(), "failed to send protected resource request") {
217+
if !strings.Contains(err.Error(), "connection refused") &&
218+
!strings.Contains(err.Error(), "failed to send protected resource request") {
219219
t.Errorf("Expected error message to contain connection error, got %s", err.Error())
220220
}
221221
}
@@ -251,7 +251,7 @@ func TestOAuthError(t *testing.T) {
251251
ErrorDescription: tc.description,
252252
ErrorURI: tc.uri,
253253
}
254-
254+
255255
if oauthErr.Error() != tc.expected {
256256
t.Errorf("Expected error message %q, got %q", tc.expected, oauthErr.Error())
257257
}
@@ -271,25 +271,25 @@ func TestOAuthHandler_ProcessAuthorizationResponse_StateValidation(t *testing.T)
271271
}
272272

273273
handler := NewOAuthHandler(config)
274-
274+
275275
// Mock the server metadata to avoid nil pointer dereference
276276
handler.serverMetadata = &AuthServerMetadata{
277277
Issuer: "http://example.com",
278278
AuthorizationEndpoint: "http://example.com/authorize",
279279
TokenEndpoint: "http://example.com/token",
280280
}
281-
281+
282282
// Set the expected state
283283
expectedState := "test-state-123"
284284
handler.expectedState = expectedState
285-
285+
286286
// Test with non-matching state - this should fail immediately with ErrInvalidState
287287
// before trying to connect to any server
288288
err := handler.ProcessAuthorizationResponse(context.Background(), "test-code", "wrong-state", "test-code-verifier")
289289
if !errors.Is(err, ErrInvalidState) {
290290
t.Errorf("Expected ErrInvalidState, got %v", err)
291291
}
292-
292+
293293
// Test with empty expected state
294294
handler.expectedState = ""
295295
err = handler.ProcessAuthorizationResponse(context.Background(), "test-code", expectedState, "test-code-verifier")
@@ -299,4 +299,4 @@ func TestOAuthHandler_ProcessAuthorizationResponse_StateValidation(t *testing.T)
299299
if errors.Is(err, ErrInvalidState) {
300300
t.Errorf("Got ErrInvalidState when expected a different error for empty expected state")
301301
}
302-
}
302+
}

client/transport/oauth_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ func ValidateRedirectURI(redirectURI string) error {
6565
}
6666

6767
return fmt.Errorf("redirect URI must use either HTTP with localhost or HTTPS")
68-
}
68+
}

client/transport/oauth_utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ func TestGenerateState(t *testing.T) {
8585
if state == state2 {
8686
t.Errorf("Generated identical states: %s", state)
8787
}
88-
}
88+
}

client/transport/streamable_http.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type StreamableHTTP struct {
7474
notifyMu sync.RWMutex
7575

7676
closed chan struct{}
77-
77+
7878
// OAuth support
7979
oauthHandler *OAuthHandler
8080
}
@@ -213,7 +213,7 @@ func (c *StreamableHTTP) SendRequest(
213213
for k, v := range c.headers {
214214
req.Header.Set(k, v)
215215
}
216-
216+
217217
// Add OAuth authorization if configured
218218
if c.oauthHandler != nil {
219219
authHeader, err := c.oauthHandler.GetAuthorizationHeader(ctx)
@@ -227,8 +227,8 @@ func (c *StreamableHTTP) SendRequest(
227227
return nil, fmt.Errorf("failed to get authorization header: %w", err)
228228
}
229229
req.Header.Set("Authorization", authHeader)
230-
}
231-
230+
}
231+
232232
if c.headerFunc != nil {
233233
for k, v := range c.headerFunc(ctx) {
234234
req.Header.Set(k, v)
@@ -249,7 +249,7 @@ func (c *StreamableHTTP) SendRequest(
249249
c.sessionID.CompareAndSwap(sessionID, "")
250250
return nil, fmt.Errorf("session terminated (404). need to re-initialize")
251251
}
252-
252+
253253
// Handle OAuth unauthorized error
254254
if resp.StatusCode == http.StatusUnauthorized && c.oauthHandler != nil {
255255
return nil, &OAuthAuthorizationRequiredError{
@@ -431,7 +431,7 @@ func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.
431431
for k, v := range c.headers {
432432
req.Header.Set(k, v)
433433
}
434-
434+
435435
// Add OAuth authorization if configured
436436
if c.oauthHandler != nil {
437437
authHeader, err := c.oauthHandler.GetAuthorizationHeader(ctx)
@@ -445,8 +445,8 @@ func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.
445445
return fmt.Errorf("failed to get authorization header: %w", err)
446446
}
447447
req.Header.Set("Authorization", authHeader)
448-
}
449-
448+
}
449+
450450
if c.headerFunc != nil {
451451
for k, v := range c.headerFunc(ctx) {
452452
req.Header.Set(k, v)
@@ -467,7 +467,7 @@ func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.
467467
Handler: c.oauthHandler,
468468
}
469469
}
470-
470+
471471
body, _ := io.ReadAll(resp.Body)
472472
return fmt.Errorf(
473473
"notification failed with status %d: %s",

client/transport/streamable_http_oauth_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http/httptest"
99
"testing"
1010
"time"
11-
11+
1212
"github.com/mark3labs/mcp-go/mcp"
1313
)
1414

@@ -65,11 +65,11 @@ func TestStreamableHTTP_WithOAuth(t *testing.T) {
6565

6666
// Create OAuth config
6767
oauthConfig := OAuthConfig{
68-
ClientID: "test-client",
69-
RedirectURI: "http://localhost:8085/callback",
70-
Scopes: []string{"mcp.read", "mcp.write"},
71-
TokenStore: tokenStore,
72-
PKCEEnabled: true,
68+
ClientID: "test-client",
69+
RedirectURI: "http://localhost:8085/callback",
70+
Scopes: []string{"mcp.read", "mcp.write"},
71+
TokenStore: tokenStore,
72+
PKCEEnabled: true,
7373
}
7474

7575
// Create StreamableHTTP with OAuth
@@ -82,7 +82,7 @@ func TestStreamableHTTP_WithOAuth(t *testing.T) {
8282
if !transport.IsOAuthEnabled() {
8383
t.Errorf("Expected IsOAuthEnabled() to return true")
8484
}
85-
85+
8686
// Verify the OAuth handler is set
8787
if transport.GetOAuthHandler() == nil {
8888
t.Errorf("Expected GetOAuthHandler() to return a handler")
@@ -94,48 +94,48 @@ func TestStreamableHTTP_WithOAuth(t *testing.T) {
9494
ID: mcp.NewRequestId(1),
9595
Method: "test",
9696
})
97-
97+
9898
// Verify the error is an OAuthAuthorizationRequiredError
9999
if err == nil {
100100
t.Fatalf("Expected error on first request, got nil")
101101
}
102-
102+
103103
var oauthErr *OAuthAuthorizationRequiredError
104104
if !errors.As(err, &oauthErr) {
105105
t.Fatalf("Expected OAuthAuthorizationRequiredError, got %T: %v", err, err)
106106
}
107-
107+
108108
// Verify the error has the handler
109109
if oauthErr.Handler == nil {
110110
t.Errorf("Expected OAuthAuthorizationRequiredError to have a handler")
111111
}
112-
112+
113113
// Verify the server received the first request
114114
if requestCount != 1 {
115115
t.Errorf("Expected server to receive 1 request, got %d", requestCount)
116116
}
117-
117+
118118
// Second request should succeed
119119
response, err := transport.SendRequest(context.Background(), JSONRPCRequest{
120120
JSONRPC: "2.0",
121121
ID: mcp.NewRequestId(2),
122122
Method: "test",
123123
})
124-
124+
125125
if err != nil {
126126
t.Fatalf("Failed to send second request: %v", err)
127127
}
128-
128+
129129
// Verify the response
130130
var resultStr string
131131
if err := json.Unmarshal(response.Result, &resultStr); err != nil {
132132
t.Fatalf("Failed to unmarshal result: %v", err)
133133
}
134-
134+
135135
if resultStr != "success" {
136136
t.Errorf("Expected result to be 'success', got %v", resultStr)
137137
}
138-
138+
139139
// Verify the server received the Authorization header
140140
if authHeaderReceived != "Bearer test-token" {
141141
t.Errorf("Expected server to receive Authorization header 'Bearer test-token', got '%s'", authHeaderReceived)
@@ -155,11 +155,11 @@ func TestStreamableHTTP_WithOAuth_Unauthorized(t *testing.T) {
155155

156156
// Create OAuth config
157157
oauthConfig := OAuthConfig{
158-
ClientID: "test-client",
159-
RedirectURI: "http://localhost:8085/callback",
160-
Scopes: []string{"mcp.read", "mcp.write"},
161-
TokenStore: tokenStore,
162-
PKCEEnabled: true,
158+
ClientID: "test-client",
159+
RedirectURI: "http://localhost:8085/callback",
160+
Scopes: []string{"mcp.read", "mcp.write"},
161+
TokenStore: tokenStore,
162+
PKCEEnabled: true,
163163
}
164164

165165
// Create StreamableHTTP with OAuth
@@ -215,4 +215,4 @@ func TestStreamableHTTP_IsOAuthEnabled(t *testing.T) {
215215
if !transport2.IsOAuthEnabled() {
216216
t.Errorf("Expected IsOAuthEnabled() to return true")
217217
}
218-
}
218+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy