-
Notifications
You must be signed in to change notification settings - Fork 368
persist cookies in remote MCP client for sticky sessions #2902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "net/http/cookiejar" | ||
|
|
||
| gomcp "github.com/modelcontextprotocol/go-sdk/mcp" | ||
|
|
||
|
|
@@ -57,7 +58,10 @@ func (c *remoteMCPClient) Initialize(ctx context.Context, _ *gomcp.InitializeReq | |
| // oauthTransport so we can enrich Connect errors with the server's own | ||
| // explanation — without this, a plain `Bad Request` bubbles up and the | ||
| // user has no idea that, say, the Slack app hasn't been enabled for MCP. | ||
| httpClient, oauthT := c.createHTTPClient() | ||
| httpClient, oauthT, err := c.createHTTPClient() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("creating HTTP client: %w", err) | ||
| } | ||
|
|
||
| var transport gomcp.Transport | ||
|
|
||
|
|
@@ -146,7 +150,7 @@ func (c *remoteMCPClient) SetManagedOAuth(managed bool) { | |
| // The oauthTransport is returned alongside the client so callers can inspect | ||
| // the most recent server-side failure (via lastServerError) when Connect() | ||
| // returns a bare HTTP-status error and we need to surface the actual cause. | ||
| func (c *remoteMCPClient) createHTTPClient() (*http.Client, *oauthTransport) { | ||
| func (c *remoteMCPClient) createHTTPClient() (*http.Client, *oauthTransport, error) { | ||
| base := c.headerTransport() | ||
|
|
||
| // Then wrap with OAuth support | ||
|
|
@@ -160,7 +164,13 @@ func (c *remoteMCPClient) createHTTPClient() (*http.Client, *oauthTransport) { | |
| oauthHTTPClient: oauthHTTPClientForAllowPrivateIPs(c.allowPrivateIPs), | ||
| } | ||
|
|
||
| return &http.Client{Transport: oauthT}, oauthT | ||
| // Persist cookies across requests | ||
| // So sticky sessions work if implemented by the server (e.g. in a multiple replica setup) | ||
| jar, err := cookiejar.New(nil) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [MEDIUM] Sticky-session cookie jar is ephemeral: cookies are lost on reconnection
Within a single session (first request → subsequent requests on the same To persist cookies across reconnections, the jar should be allocated once (e.g. lazily on the |
||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("creating cookie jar: %w", err) | ||
| } | ||
| return &http.Client{Transport: oauthT, Jar: jar}, oauthT, nil | ||
| } | ||
|
|
||
| func (c *remoteMCPClient) headerTransport() http.RoundTripper { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.