Skip to main content
Defined in: server/middleware.ts:146 Custom cache key function. Receives the full middleware context so callers can incorporate auth identity or any other dimension into the key. The default key already partitions by auth identity — you do not need a keyFn for auth safety. The default is "method:<auth-partition>:JSON(params)", where the auth partition is anon for a request with no bearer token and the SHA-256 hash of the bearer token for an authenticated request. So a result computed under one identity is never served under another (anonymous and authenticated included, both directions), and auth-filtered list results (tools, resources, prompts) stay per identity. A custom keyFn REPLACES the default partitioning entirely — you then own it. The key you return is used verbatim; the auth partition is not merged in. If your cached values depend on the caller identity, include an identity dimension yourself. Use the same dimension the default uses — the bearer token, HASHED. Never place the raw token in a cache key:
clientId is a coarser alternative (ctx.mcpContext.auth?.clientId), but two tokens may share a clientId yet differ in scope, so a clientId key can merge identities that must stay apart. Prefer the hashed token.

Parameters

ctx

MiddlewareContext

Returns

string