Conversation
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| h := sha256.New() | ||
| for _, k := range keys { | ||
| vals := req.Header[k] | ||
| sort.Strings(vals) |
There was a problem hiding this comment.
🟡 Cache key generation mutates HTTP request headers in place
The registryCacheKey function 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:
vals := req.Header[k]
sort.Strings(vals)In Go, req.Header[k] returns the actual slice stored in the map, not a copy. When sort.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:202 and registryclient.go:316). This means the request headers are mutated before c.httpClient.Do(req) is called. While header order typically doesn't affect HTTP semantics, this:
- Violates the principle of least surprise - a cache key function shouldn't have side effects
- Could cause issues if the HTTP client, transport, or any middleware depends on header order
- Could cause issues if the request object is inspected or reused after this call
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:
vals := req.Header[k]
valsCopy := make([]string, len(vals))
copy(valsCopy, vals)
sort.Strings(valsCopy)
_, _ = fmt.Fprintf(h, "%s=%s\n", k, strings.Join(valsCopy, ","))Was this helpful? React with 👍 or 👎 to provide feedback.
🚀 Preview Environment (PR #1460)Preview URL: https://pr-1460.dev.getgram.ai
Gram Preview Bot |
a0afead to
96c4e7b
Compare
Summary
Adds Redis-backed caching to the MCP registry client to reduce redundant HTTP requests to the registry backend.
Implementation Details
CachedListServersResponseandCachedServerDetailsResponsetypes that implementcache.CacheableObjectTest plan
🤖 Generated with Claude Code