-
Notifications
You must be signed in to change notification settings - Fork 296
feat: Add HeaderProvider to mcptoolset #338
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
Open
kaugesaar
wants to merge
11
commits into
google:main
Choose a base branch
from
kaugesaar:mcp-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+861
−34
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb08d7d
feat: mcp tool header provider
kaugesaar 977c316
add header provider example
kaugesaar 967a8b3
ignore headers for non http
kaugesaar 6297a30
dont export session manager
kaugesaar ecb1601
simplify roundtrip
kaugesaar fc633b0
dont hold read lock during ping
kaugesaar 966f2cd
dont copy headers
kaugesaar af4fd87
add HeaderProvider tests
kaugesaar f149725
add defaultSessionKey
kaugesaar 35e05ad
use default
kaugesaar 103b27f
cleanup
kaugesaar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package mcptoolset | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/md5" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "net/http" | ||
| "sort" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultSessionKey = "default" | ||
| defaultPingTimeout = 2 * time.Second | ||
| ) | ||
|
|
||
| // sessionManager manages MCP client sessions with header-based pooling | ||
| type sessionManager struct { | ||
| client *mcp.Client | ||
| transport mcp.Transport | ||
|
|
||
| mu sync.RWMutex | ||
| sessions map[string]*sessionEntry | ||
| } | ||
|
|
||
| type sessionEntry struct { | ||
| session *mcp.ClientSession | ||
| headers map[string]string | ||
| } | ||
|
|
||
| // newSessionManager creates a new session manager | ||
| func newSessionManager(client *mcp.Client, transport mcp.Transport) *sessionManager { | ||
| return &sessionManager{ | ||
| client: client, | ||
| transport: transport, | ||
| sessions: make(map[string]*sessionEntry), | ||
| } | ||
| } | ||
|
|
||
| // headersAffectSession returns true only for HTTP-based transports where | ||
| // headers are actually used by the connection. | ||
| func (sm *sessionManager) headersAffectSession() bool { | ||
| switch sm.transport.(type) { | ||
| case *mcp.SSEClientTransport, *mcp.StreamableClientTransport: | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // generateSessionKey creates a hash-based key from headers | ||
| func (sm *sessionManager) generateSessionKey(headers map[string]string) string { | ||
| // For non-HTTP transports (e.g., stdio, in-memory), headers don't apply, | ||
| // so we always pool into the same session. | ||
| if !sm.headersAffectSession() { | ||
| return defaultSessionKey | ||
| } | ||
| if len(headers) == 0 { | ||
| return defaultSessionKey | ||
| } | ||
|
|
||
| keys := make([]string, 0, len(headers)) | ||
| for k := range headers { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
|
|
||
| var pairs []string | ||
| for _, k := range keys { | ||
| pairs = append(pairs, fmt.Sprintf("%q:%q", k, headers[k])) | ||
| } | ||
| jsonStr := "{" + strings.Join(pairs, ",") + "}" | ||
|
|
||
| h := md5.Sum([]byte(jsonStr)) | ||
| return hex.EncodeToString(h[:]) | ||
| } | ||
|
|
||
| // GetSession returns a session for the given headers, creating if necessary | ||
| func (sm *sessionManager) GetSession(ctx context.Context, headers map[string]string) (*mcp.ClientSession, error) { | ||
| key := sm.generateSessionKey(headers) | ||
|
|
||
| sm.mu.RLock() | ||
| entry, ok := sm.sessions[key] | ||
| sm.mu.RUnlock() | ||
|
|
||
| if ok && sm.isSessionValid(ctx, entry.session) { | ||
| return entry.session, nil | ||
| } | ||
|
|
||
| sm.mu.Lock() | ||
| defer sm.mu.Unlock() | ||
|
|
||
| if entry, ok := sm.sessions[key]; ok && sm.isSessionValid(ctx, entry.session) { | ||
| return entry.session, nil | ||
| } | ||
|
|
||
| wrappedTransport := sm.wrapTransportWithHeaders(headers) | ||
|
|
||
| session, err := sm.client.Connect(ctx, wrappedTransport, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create session: %w", err) | ||
| } | ||
|
|
||
| sm.sessions[key] = &sessionEntry{ | ||
| session: session, | ||
| headers: headers, | ||
| } | ||
|
|
||
| return session, nil | ||
| } | ||
|
|
||
| // isSessionValid checks if a session is still usable | ||
| func (sm *sessionManager) isSessionValid(ctx context.Context, session *mcp.ClientSession) bool { | ||
| if session == nil { | ||
| return false | ||
| } | ||
|
|
||
| pingCtx := ctx | ||
| if _, hasDeadline := ctx.Deadline(); !hasDeadline { | ||
| var cancel context.CancelFunc | ||
| pingCtx, cancel = context.WithTimeout(ctx, defaultPingTimeout) | ||
| defer cancel() | ||
| } | ||
|
|
||
| if err := session.Ping(pingCtx, nil); err != nil { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // wrapTransportWithHeaders creates a transport that injects headers | ||
| func (sm *sessionManager) wrapTransportWithHeaders(headers map[string]string) mcp.Transport { | ||
| switch t := sm.transport.(type) { | ||
|
|
||
| case *mcp.SSEClientTransport: | ||
| return &mcp.SSEClientTransport{ | ||
| Endpoint: t.Endpoint, | ||
| HTTPClient: wrapHTTPClient(t.HTTPClient, headers), | ||
| } | ||
|
|
||
| case *mcp.StreamableClientTransport: | ||
| return &mcp.StreamableClientTransport{ | ||
| Endpoint: t.Endpoint, | ||
| HTTPClient: wrapHTTPClient(t.HTTPClient, headers), | ||
| } | ||
|
|
||
| default: | ||
| return sm.transport | ||
| } | ||
| } | ||
|
|
||
| func wrapHTTPClient(httpClient *http.Client, headers map[string]string) *http.Client { | ||
| if httpClient == nil { | ||
| httpClient = &http.Client{} | ||
| } | ||
|
|
||
| return &http.Client{ | ||
| Transport: &headerTransport{ | ||
| Base: httpClient.Transport, | ||
| Headers: headers, | ||
| }, | ||
| CheckRedirect: httpClient.CheckRedirect, | ||
| Jar: httpClient.Jar, | ||
| Timeout: httpClient.Timeout, | ||
| } | ||
| } | ||
|
|
||
| // Close closes all sessions | ||
| func (sm *sessionManager) Close() error { | ||
| sm.mu.Lock() | ||
| defer sm.mu.Unlock() | ||
|
|
||
| var errs []error | ||
| for _, entry := range sm.sessions { | ||
| if err := entry.session.Close(); err != nil { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
|
|
||
| sm.sessions = make(map[string]*sessionEntry) | ||
|
|
||
| if len(errs) > 0 { | ||
| return fmt.Errorf("errors closing sessions: %v", errs) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type headerTransport struct { | ||
| Base http.RoundTripper | ||
| Headers map[string]string | ||
| } | ||
|
|
||
| // RoundTrip adds the configured headers to the request. | ||
| func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| if len(t.Headers) == 0 { | ||
| return t.base().RoundTrip(req) | ||
| } | ||
|
|
||
| req2 := req.Clone(req.Context()) | ||
| for key, value := range t.Headers { | ||
| req2.Header.Set(key, value) | ||
| } | ||
| return t.base().RoundTrip(req2) | ||
| } | ||
|
|
||
| func (t *headerTransport) base() http.RoundTripper { | ||
| if t.Base != nil { | ||
| return t.Base | ||
| } | ||
| return http.DefaultTransport | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.