44 "context"
55 "encoding/json"
66 "fmt"
7+ "net/url"
78 "os"
89 "os/exec"
910 "reflect"
@@ -1101,7 +1102,7 @@ func (c *Client) tryOAuthAuth(ctx context.Context) error {
11011102 zap .String ("server" , c .config .Name ))
11021103
11031104 // Handle OAuth authorization manually using the example pattern
1104- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
1105+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
11051106 c .clearOAuthState () // Clear state on OAuth failure
11061107 return fmt .Errorf ("OAuth authorization failed: %w" , oauthErr )
11071108 }
@@ -1167,7 +1168,7 @@ func (c *Client) tryOAuthAuth(ctx context.Context) error {
11671168 c .clearOAuthState ()
11681169
11691170 // Handle OAuth authorization manually using the example pattern
1170- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
1171+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
11711172 c .clearOAuthState () // Clear state on OAuth failure
11721173 return fmt .Errorf ("OAuth authorization during MCP init failed: %w" , oauthErr )
11731174 }
@@ -1312,7 +1313,7 @@ func (c *Client) trySSEOAuthAuth(ctx context.Context) error {
13121313 zap .String ("strategy" , "SSE OAuth" ))
13131314
13141315 // Create OAuth config using the oauth package
1315- oauthConfig , _ := oauth .CreateOAuthConfig (c .config , c .storage )
1316+ oauthConfig , extraParams := oauth .CreateOAuthConfig (c .config , c .storage )
13161317 if oauthConfig == nil {
13171318 return fmt .Errorf ("failed to create OAuth config" )
13181319 }
@@ -1420,7 +1421,7 @@ func (c *Client) trySSEOAuthAuth(ctx context.Context) error {
14201421 zap .String ("server" , c .config .Name ))
14211422
14221423 // Handle OAuth authorization manually using the example pattern
1423- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
1424+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
14241425 c .clearOAuthState () // Clear state on OAuth failure
14251426 return fmt .Errorf ("SSE OAuth authorization failed: %w" , oauthErr )
14261427 }
@@ -1721,7 +1722,7 @@ func (c *Client) DisconnectWithContext(_ context.Context) error {
17211722}
17221723
17231724// handleOAuthAuthorization handles the manual OAuth flow following the mcp-go example pattern
1724- func (c * Client ) handleOAuthAuthorization (ctx context.Context , authErr error , oauthConfig * client.OAuthConfig ) error {
1725+ func (c * Client ) handleOAuthAuthorization (ctx context.Context , authErr error , oauthConfig * client.OAuthConfig , extraParams map [ string ] string ) error {
17251726 // Check if OAuth is already in progress to prevent duplicate flows (CRITICAL FIX for Phase 1)
17261727 if c .isOAuthInProgress () {
17271728 c .logger .Warn ("⚠️ OAuth authorization already in progress, skipping duplicate attempt" ,
@@ -1842,6 +1843,28 @@ func (c *Client) handleOAuthAuthorization(ctx context.Context, authErr error, oa
18421843 return fmt .Errorf ("failed to get authorization URL: %w" , authURLErr )
18431844 }
18441845
1846+ // Add extra OAuth parameters if configured (workaround until mcp-go supports this natively)
1847+ if len (extraParams ) > 0 {
1848+ u , err := url .Parse (authURL )
1849+ if err != nil {
1850+ c .logger .Error ("Failed to parse OAuth authorization URL for extra params injection" ,
1851+ zap .String ("server" , c .config .Name ),
1852+ zap .Error (err ))
1853+ return fmt .Errorf ("failed to parse authorization URL: %w" , err )
1854+ }
1855+
1856+ q := u .Query ()
1857+ for k , v := range extraParams {
1858+ q .Set (k , v )
1859+ }
1860+ u .RawQuery = q .Encode ()
1861+ authURL = u .String ()
1862+
1863+ c .logger .Info ("✨ Added extra OAuth parameters to authorization URL" ,
1864+ zap .String ("server" , c .config .Name ),
1865+ zap .Any ("extra_params" , extraParams ))
1866+ }
1867+
18451868 // Always log the computed authorization URL so users can copy/paste if auto-launch fails.
18461869 c .logger .Info ("OAuth authorization URL ready" ,
18471870 zap .String ("server" , c .config .Name ),
@@ -2074,7 +2097,7 @@ func (c *Client) ForceOAuthFlow(ctx context.Context) error {
20742097// forceHTTPOAuthFlow forces OAuth flow for HTTP transport
20752098func (c * Client ) forceHTTPOAuthFlow (ctx context.Context ) error {
20762099 // Create OAuth config
2077- oauthConfig , _ := oauth .CreateOAuthConfig (c .config , c .storage )
2100+ oauthConfig , extraParams := oauth .CreateOAuthConfig (c .config , c .storage )
20782101 if oauthConfig == nil {
20792102 return fmt .Errorf ("failed to create OAuth config - server may not support OAuth" )
20802103 }
@@ -2111,7 +2134,7 @@ func (c *Client) forceHTTPOAuthFlow(ctx context.Context) error {
21112134 c .logger .Info ("✅ OAuth authorization requirement triggered - starting manual OAuth flow" )
21122135
21132136 // Handle OAuth authorization manually using the example pattern
2114- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
2137+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
21152138 return fmt .Errorf ("OAuth authorization failed: %w" , oauthErr )
21162139 }
21172140
@@ -2133,7 +2156,7 @@ func (c *Client) forceHTTPOAuthFlow(ctx context.Context) error {
21332156// forceSSEOAuthFlow forces OAuth flow for SSE transport
21342157func (c * Client ) forceSSEOAuthFlow (ctx context.Context ) error {
21352158 // Create OAuth config
2136- oauthConfig , _ := oauth .CreateOAuthConfig (c .config , c .storage )
2159+ oauthConfig , extraParams := oauth .CreateOAuthConfig (c .config , c .storage )
21372160 if oauthConfig == nil {
21382161 return fmt .Errorf ("failed to create OAuth config - server may not support OAuth" )
21392162 }
@@ -2163,7 +2186,7 @@ func (c *Client) forceSSEOAuthFlow(ctx context.Context) error {
21632186 c .logger .Info ("✅ OAuth authorization required from SSE Start() - triggering manual OAuth flow" )
21642187
21652188 // Handle OAuth authorization manually
2166- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
2189+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
21672190 return fmt .Errorf ("OAuth authorization failed: %w" , oauthErr )
21682191 }
21692192
@@ -2187,7 +2210,7 @@ func (c *Client) forceSSEOAuthFlow(ctx context.Context) error {
21872210 c .logger .Info ("✅ OAuth authorization requirement from initialize - starting manual OAuth flow" )
21882211
21892212 // Handle OAuth authorization manually using the example pattern
2190- if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig ); oauthErr != nil {
2213+ if oauthErr := c .handleOAuthAuthorization (ctx , err , oauthConfig , extraParams ); oauthErr != nil {
21912214 return fmt .Errorf ("OAuth authorization failed: %w" , oauthErr )
21922215 }
21932216
0 commit comments