Skip to content

Every federated list was announcing a cache scope that does not exist - #88

Merged
blakebauman merged 1 commit into
mainfrom
feat/read-only-call-cache
Aug 26, 2026
Merged

blakebauman merged 1 commit into
mainfrom
feat/read-only-call-cache

Conversation

@blakebauman

Copy link
Copy Markdown
Member

Phase 3 of the audit follow-up — though not the phase that was planned. Independent of #84 and #87.

The planned feature was not buildable, and that is the first finding

The plan called for a read-through cache on readOnlyHint tool calls. Checking the specification rather than the plan:

Servers MUST include caching hints on results with resultType: "complete" returned by the following operations: server/discover, tools/list, prompts/list, resources/list, resources/templates/list, resources/read

tools/call is absent, and CallToolResult is the one result type in the Go SDK that does not embed Cacheable. MCP excludes tool calls from caching deliberately. The plan also said to honour the upstream's cache hint as a ceiling — impossible, since tool-call results carry no hints — and named the fields cache_ttl/cache_scope, which are a Python framework's API rather than the spec's ttlMs/cacheScope.

Building it would have meant fold inventing cache semantics for an operation the spec declines, keyed on readOnlyHint — a side-effect hint never defined as a cacheability signal.

What the same investigation found instead

Probing fold's actual wire output rather than reasoning about it: every federated list carried "cacheScope": "". Neither permitted value. fold constructs its list results itself, so it never reaches the SDK handlers that fill the field in for an ordinary server.

ttlMs: 0 meant this never caused an incident — conforming clients re-fetch regardless. But the two hints disagreed, and the one that would have contained the damage was the one fold was not setting. A client applying the SDK's documented "absent means public" default to an empty string would treat a list fold had filtered per principal as shareable with anyone. That is the spec's own example of what must be private:

"private" is appropriate for … filtered list results that vary per user

Now computed from configuration, once per routing snapshot: "private" whenever two callers can legitimately receive different lists — any policy rule, any tenant, or any caller-derived upstream — and "public" only for a federation that serves one list to everyone.

defaultDecision is deliberately not in that condition

Two wrong turns to get there, both worth reviewing:

  1. I first made the scope depend on defaultDecision == "deny". Wrong: policy.New sets defaultAllow only for the literal "allow", so an unset value denies — policy: {} would have been marked public while the engine refused every caller.
  2. I then broadened it, and the test table failed. Which exposed that the table was inconsistent too: it wanted {} public but {"defaultDecision":"deny"} private, when those are the same deny-all engine.

The resolution is neither: a default with no rules serves every caller the same list — the whole federation under allow, nothing under deny. Neither varies by who is asking, so the field simply does not belong in the condition. It is the only version where two identical configurations get the same answer.

A limitation documented rather than papered over

A list can also vary by the capability profile a client declared — an upstream may register a different tool set for a client supporting MCP Apps, which is why fold keys root sessions and list-cache entries by profile. Two callers in the same authorization context can therefore get different tools/list bytes.

That is a content-negotiation axis; HTTP would say Vary. The MCP caching section defines no equivalent, so cacheScope cannot express it, and "private" would be the wrong instrument — it bounds sharing across authorization contexts, not across client capabilities. What contains it in practice is ttlMs: 0. Written up in docs/configuration.md rather than left to be discovered.

ttlMs stays 0

A reload, a discovery sync, or a policy change can alter what a caller may see at any moment. fold emits list_changed on all three — the invalidation signal the spec pairs with a TTL — but choosing a lifetime here chooses it for every deployment at once, and the conservative value is the one that cannot serve a stale entitlement.

Verification

  • go test -race ./... green; lint 0 issues; gofmt / vet / tidy clean
  • conformance 40/40; bench 0.176 ms added p50
  • Tests assert the raw bytes, not the decoded struct — "" and absent are indistinguishable after unmarshalling into mcp.Cacheable, so a struct-level test could not have caught the original defect
  • A membership assertion (value ∈ {public,private}, and the field is present) runs on every case separately from the expected-value check, so a future third value fails even if someone updates the want
  • TestPlainFederationMatchesTheUpstreamsOwnCacheHints asserts a plain federation is byte-identical to hitting the upstream directly — not a tautology, since the SDK stamps "public" on its own handlers and fold intercepting before them is exactly why the field was empty
  • Reload in both directions, and FuzzListCacheScope (1.6M execs) asserting no config document yields a value outside the permitted set

Deferred, with reasons

Honouring upstream ttlMs/cacheScope when filling fold's own list cache — so cacheTtlMs cannot silently outlive an upstream's declared freshness. Blocked on a real structural issue: state.ListCache.GetOrFill(ctx, key, ttl, fill) takes the TTL before running the fill, and the hint only exists after it. That means changing a state.Provider interface with two implementations plus the Redis paths — its own change with its own reload/churn matrix, not a rider on a wire fix.

resources/read caching — cacheable per spec, and fold does not do it. The genuine opportunity the original phase 3 was reaching for, with normative backing.

🤖 Generated with Claude Code

MCP's caching rules define `cacheScope` as exactly two values, "public"
and "private", and require the hint on every complete list result. fold
builds its list results itself — federated, merged, and filtered per
principal — so they never reach the SDK handlers that fill those fields
in for an ordinary server. Every tools/list, prompts/list,
resources/list and resources/templates/list went out with
`"cacheScope": ""`, which is neither.

It never became an incident because the accompanying ttlMs is 0, the
spec's "immediately stale", so a conforming client re-fetches regardless.
But the two hints disagreed, and the one that would have contained the
damage is the one fold was not deliberately setting: a client applying
the documented "absent means public" default to an empty string would
have treated a list fold had filtered per principal as shareable with
anyone — the specification's own example of what must be private.

The scope is now computed from configuration, once per routing snapshot,
and is private whenever two callers can legitimately receive different
lists: any policy rule, any tenant, or any upstream whose credential is
caller-derived.

defaultDecision deliberately does not appear in that condition, and
getting there took two wrong turns worth recording. Reading it as
evidence of filtering is wrong because a default with no rules serves
every caller the same list — the whole federation under allow, nothing
at all under deny — so neither varies by who is asking. It also would
have split two documents that are the same engine: policy.New sets
defaultAllow only for the literal "allow", so `{}` and
`{"defaultDecision":"deny"}` both deny everything, and no rule that gave
them different scopes could be coherent.

ttlMs stays 0. A reload, a discovery sync, or a policy change can alter
what a caller is entitled to see at any moment; fold does emit
list_changed on all three, which is the invalidation signal the spec
pairs with a TTL, but choosing a lifetime here would choose it for every
deployment at once, and the conservative value is the one that cannot
serve a stale entitlement.

One limitation is documented rather than papered over: a list can also
vary by the capability profile a client declared, because an upstream may
register a different tool set for a client that supports MCP Apps. That
is a content-negotiation axis — HTTP would say Vary — and the caching
section defines no equivalent, so cacheScope cannot express it. private
would be the wrong instrument, bounding sharing across authorization
contexts rather than across client capabilities. ttlMs 0 is what contains
it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman merged commit 04e4376 into main Aug 26, 2026
8 checks passed
@blakebauman
blakebauman deleted the feat/read-only-call-cache branch August 26, 2026 01:20
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.

1 participant