Repository navigation
mcp: cap the client caches and stop retaining uncacheable results - #1527
Merged
guglielmo-san merged 3 commits intoNov 16, 2026
Merged
guglielmo-san merged 3 commits into
guglielmo-san merged 3 commits into
Conversation
On a 2026-07-28 session, ClientSession.ReadResource stored each result in the session's resource cache whatever its ttlMs. Results with a ttlMs of 0 can never be served from the cache, and an entry was only removed when its own URI was read again after expiring, or on notifications/resources/updated, so a client reading many distinct URIs kept all of their contents until the session ended. Store a result only if its ttlMs is positive, and drop any entry for the URI otherwise. Remove the expired entries each time the resource cache has doubled in size, so that the cost is amortized over the reads. The list caches keep using put, since lookupTool reads stale tools/list entries (modelcontextprotocol#1053). Fixes modelcontextprotocol#1526 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Address review feedback: - Cap each cache that putCacheable fills at 512 entries, as the TypeScript and Python SDKs do. To add a key to a full cache, remove the expired entries and, if none have expired, the oldest one. This replaces the sweep each time the cache doubles in size. - Use putCacheable for the prompts, resources and resource templates list caches too. The tools list cache keeps using put for lookupTool. - Move the tests to client_test.go. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
guglielmo-san
approved these changes
Nov 16, 2026
Contributor
|
@Assertive-xxh thank you for the contribution |
guglielmo-san
enabled auto-merge (squash)
November 16, 2026 11:32
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1526.
On a 2026-07-28 session,
ClientSession.ReadResourcestored each result in the session's resource cache whatever itsttlMs. Results withttlMs0, which a Go SDK server sends unless a TTL is set, can never be served from the cache, and an entry was only removed when its own URI was read again after expiring, or onnotifications/resources/updated. A client reading many distinct URIs therefore kept all of their contents until the session ended.The client now stores a result only if its
ttlMsis positive, and otherwise drops any entry for that key. This applies to theresources/readcache and to theprompts/list,resources/listandresources/templates/listcaches. Thetools/listcache still stores every result, sincelookupToolreads stale entries (#1053); a comment onputsays so.Each of these caches holds at most 512 entries, as in the TypeScript and Python SDKs. To add a key to a full cache,
putCacheableremoves the expired entries and, if none have expired, the oldest one. There are no exported API changes.Tests, in
mcp/client_test.go:TestClientCachesOnlyCacheableResultslists prompts, tools, resources and resource templates, which have no TTL by default, and reads a resource without a TTL and one with a TTL, twice each. It checks that only thetools/listresult and the resource with a TTL are cached, and that the latter reaches the server once. It fails if any of the four caches usesput.TestMethodCachePutCacheablechecks that a result withttlMs0 removes the cached entry for its key, and that a full cache removes its expired entries, or else its oldest one (usingtesting/synctest). Each subtest fails without the step it covers.With the reproduction from #1526, the heap after 4000 reads stays at about 2 MiB instead of growing to about 280 MiB.
go test ./...,go test -race ./mcp/,go vet ./...and staticcheck v0.6.1 pass (Go 1.25.0, Windows).AI disclosure: written with Claude Code; I reviewed the change and can explain it.
🤖 Generated with Claude Code