Skip to content

Commit 9ee0e18

Browse files
committed
Enhance OAuth configuration handling and add base URL parsing
- Updated CreateOAuthConfig to construct the OAuth server metadata URL from the server URL. - Introduced parseBaseURL function to extract the base URL from a full URL, handling cases without a scheme. - Improved logging for OAuth server metadata URL setting and error handling for URL parsing failures.
1 parent b36d42d commit 9ee0e18

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

internal/oauth/config.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package oauth
33
import (
44
"context"
55
"fmt"
6-
"mcpproxy-go/internal/config"
76
"net"
87
"net/http"
8+
"net/url"
99
"os"
10+
"strings"
1011
"sync"
1112
"time"
1213

14+
"mcpproxy-go/internal/config"
15+
1316
"github.com/mark3labs/mcp-go/client"
1417
"go.uber.org/zap"
1518
)
@@ -88,6 +91,20 @@ func CreateOAuthConfig(serverConfig *config.ServerConfig) *client.OAuthConfig {
8891

8992
// Determine the correct OAuth server metadata URL based on the server URL
9093
var authServerMetadataURL string
94+
if serverConfig.URL != "" {
95+
// Extract base URL from server URL and construct the well-known metadata endpoint
96+
if baseURL, err := parseBaseURL(serverConfig.URL); err == nil {
97+
authServerMetadataURL = baseURL + "/.well-known/oauth-authorization-server"
98+
logger.Debug("Setting OAuth server metadata URL",
99+
zap.String("server", serverConfig.Name),
100+
zap.String("auth_server_metadata_url", authServerMetadataURL))
101+
} else {
102+
logger.Warn("Failed to parse base URL for OAuth metadata",
103+
zap.String("server", serverConfig.Name),
104+
zap.String("url", serverConfig.URL),
105+
zap.Error(err))
106+
}
107+
}
91108

92109
oauthConfig := &client.OAuthConfig{
93110
ClientID: "", // Will be obtained via Dynamic Client Registration
@@ -308,3 +325,26 @@ func ShouldUseOAuth(serverConfig *config.ServerConfig) bool {
308325
func IsOAuthConfigured(serverConfig *config.ServerConfig) bool {
309326
return serverConfig.OAuth != nil
310327
}
328+
329+
// parseBaseURL extracts the base URL (scheme + host) from a full URL
330+
func parseBaseURL(fullURL string) (string, error) {
331+
if fullURL == "" {
332+
return "", fmt.Errorf("empty URL")
333+
}
334+
335+
// Handle URLs that might not have a scheme
336+
if !strings.HasPrefix(fullURL, "http://") && !strings.HasPrefix(fullURL, "https://") {
337+
fullURL = "https://" + fullURL
338+
}
339+
340+
u, err := url.Parse(fullURL)
341+
if err != nil {
342+
return "", fmt.Errorf("invalid URL: %w", err)
343+
}
344+
345+
if u.Scheme == "" || u.Host == "" {
346+
return "", fmt.Errorf("invalid URL: missing scheme or host")
347+
}
348+
349+
return fmt.Sprintf("%s://%s", u.Scheme, u.Host), nil
350+
}

0 commit comments

Comments
 (0)