-
Notifications
You must be signed in to change notification settings - Fork 17
chore: add caching for MCP registry client #1460
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
qstearns
wants to merge
1
commit into
main
Choose a base branch
from
ex-mcp/registry-caching
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.
+147
−15
Open
Changes from all commits
Commits
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
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
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,59 @@ | ||
| package externalmcp | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "fmt" | ||
| "net/http" | ||
| "sort" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/speakeasy-api/gram/server/gen/types" | ||
| "github.com/speakeasy-api/gram/server/internal/cache" | ||
| ) | ||
|
|
||
| const registryCacheTTL = 24 * time.Hour | ||
|
|
||
| // CachedListServersResponse wraps a list of external MCP servers for caching. | ||
| type CachedListServersResponse struct { | ||
| Key string | ||
| Servers []*types.ExternalMCPServer | ||
| } | ||
|
|
||
| var _ cache.CacheableObject[CachedListServersResponse] = (*CachedListServersResponse)(nil) | ||
|
|
||
| func (c CachedListServersResponse) CacheKey() string { return c.Key } | ||
| func (c CachedListServersResponse) AdditionalCacheKeys() []string { return []string{} } | ||
| func (c CachedListServersResponse) TTL() time.Duration { return registryCacheTTL } | ||
|
|
||
| // CachedServerDetailsResponse wraps server details for caching. | ||
| type CachedServerDetailsResponse struct { | ||
| Key string | ||
| Details *ServerDetails | ||
| } | ||
|
|
||
| var _ cache.CacheableObject[CachedServerDetailsResponse] = (*CachedServerDetailsResponse)(nil) | ||
|
|
||
| func (c CachedServerDetailsResponse) CacheKey() string { return c.Key } | ||
| func (c CachedServerDetailsResponse) AdditionalCacheKeys() []string { return []string{} } | ||
| func (c CachedServerDetailsResponse) TTL() time.Duration { return registryCacheTTL } | ||
|
|
||
| // registryCacheKey builds a cache key from a prefix and the request's URL + headers. | ||
| // Headers are sorted and hashed with SHA-256 to capture tenant/auth identity. | ||
| func registryCacheKey(prefix string, req *http.Request) string { | ||
| // Sort header keys for deterministic hashing | ||
| keys := make([]string, 0, len(req.Header)) | ||
| for k := range req.Header { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
|
|
||
| h := sha256.New() | ||
| for _, k := range keys { | ||
| vals := req.Header[k] | ||
| sort.Strings(vals) | ||
| _, _ = fmt.Fprintf(h, "%s=%s\n", k, strings.Join(vals, ",")) | ||
| } | ||
|
|
||
| return fmt.Sprintf("registry:%s:%s:%x", prefix, req.URL.String(), h.Sum(nil)) | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Cache key generation mutates HTTP request headers in place
The
registryCacheKeyfunction mutates the original HTTP request's headers by sorting header values in place.Click to expand
Issue
At
server/internal/externalmcp/registry_cache.go:53-54, the code gets a reference to the header values slice and sorts it in place:In Go,
req.Header[k]returns the actual slice stored in the map, not a copy. Whensort.Strings(vals)is called, it modifies the original slice, thereby mutating the HTTP request's headers.Impact
The cache key is generated before the HTTP request is sent (see
registryclient.go:202andregistryclient.go:316). This means the request headers are mutated beforec.httpClient.Do(req)is called. While header order typically doesn't affect HTTP semantics, this:Expected Behavior
The cache key generation should not modify the input request. A copy of the values should be made before sorting.
Recommendation: Make a copy of the slice before sorting:
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1