Skip to content

mcp: cap the client caches and stop retaining uncacheable results - #1527

Merged
guglielmo-san merged 3 commits into
modelcontextprotocol:mainfrom
Assertive-xxh:fix/read-resource-cache-growth
Nov 16, 2026
Merged

guglielmo-san merged 3 commits into
modelcontextprotocol:mainfrom
Assertive-xxh:fix/read-resource-cache-growth

Conversation

@Assertive-xxh

@Assertive-xxh Assertive-xxh commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1526.

On a 2026-07-28 session, ClientSession.ReadResource stored each result in the session's resource cache whatever its ttlMs. Results with ttlMs 0, 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 on notifications/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 ttlMs is positive, and otherwise drops any entry for that key. This applies to the resources/read cache and to the prompts/list, resources/list and resources/templates/list caches. The tools/list cache still stores every result, since lookupTool reads stale entries (#1053); a comment on put says 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, putCacheable removes the expired entries and, if none have expired, the oldest one. There are no exported API changes.

Tests, in mcp/client_test.go:

  • TestClientCachesOnlyCacheableResults lists 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 the tools/list result and the resource with a TTL are cached, and that the latter reaches the server once. It fails if any of the four caches uses put.
  • TestMethodCachePutCacheable checks that a result with ttlMs 0 removes the cached entry for its key, and that a full cache removes its expired entries, or else its oldest one (using testing/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

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>
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 14, 2026
@github-actions github-actions Bot closed this Nov 14, 2026
@github-actions github-actions Bot reopened this Nov 16, 2026
@github-actions github-actions Bot removed the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 16, 2026
Comment thread mcp/client.go Resolved
Comment thread mcp/cache.go Resolved
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>
@Assertive-xxh Assertive-xxh changed the title mcp: stop retaining uncacheable and expired resources/read results mcp: cap the client caches and stop retaining uncacheable results Nov 16, 2026
@guglielmo-san

Copy link
Copy Markdown
Contributor

@Assertive-xxh thank you for the contribution

@guglielmo-san
guglielmo-san enabled auto-merge (squash) November 16, 2026 11:32
@guglielmo-san
guglielmo-san merged commit f424fff into modelcontextprotocol:main Nov 16, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: ClientSession.ReadResource keeps uncacheable and expired results until the session ends

2 participants