pub struct ResponseCachingMiddleware { /* private fields */ }Expand description
Response caching middleware for MCP servers.
Caches eligible responses with configurable TTL and bounded LRU eviction.
Production contexts are isolated by opaque session-state identity, state
mutation revision, and complete verified authentication facts. An
incomplete partition fails closed. tools/call is additionally disabled by
default and requires an explicit per-tool allowlist entry via
Self::include_tools.
Implementations§
Source§impl ResponseCachingMiddleware
impl ResponseCachingMiddleware
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates response caching middleware with default bounds and TTLs.
tools/call caching remains off until Self::include_tools is used.
Sourcepub fn max_entries(self, max: usize) -> Self
pub fn max_entries(self, max: usize) -> Self
Sets the maximum number of cache entries (0 disables storage).
Sourcepub fn max_size_bytes(self, max: usize) -> Self
pub fn max_size_bytes(self, max: usize) -> Self
Sets the maximum cache size in bytes (0 disables storage).
Sourcepub fn max_item_size(self, max: usize) -> Self
pub fn max_item_size(self, max: usize) -> Self
Sets the maximum size per cache item in bytes (0 disables storage).
Sourcepub fn list_ttl_secs(self, secs: u64) -> Self
pub fn list_ttl_secs(self, secs: u64) -> Self
Sets the TTL for list operations (tools/list, resources/list, prompts/list).
Sourcepub fn call_ttl_secs(self, secs: u64) -> Self
pub fn call_ttl_secs(self, secs: u64) -> Self
Sets the TTL for read/get operations and explicitly allowlisted calls.
Sourcepub fn disable_tools_list(self) -> Self
pub fn disable_tools_list(self) -> Self
Disables caching for tools/list.
Sourcepub fn disable_resources_list(self) -> Self
pub fn disable_resources_list(self) -> Self
Disables caching for resources/list.
Sourcepub fn disable_prompts_list(self) -> Self
pub fn disable_prompts_list(self) -> Self
Disables caching for prompts/list.
Sourcepub fn disable_tools_call(self) -> Self
pub fn disable_tools_call(self) -> Self
Disables caching for tools/call.
Sourcepub fn disable_resources_read(self) -> Self
pub fn disable_resources_read(self) -> Self
Disables caching for resources/read.
Sourcepub fn disable_prompts_get(self) -> Self
pub fn disable_prompts_get(self) -> Self
Disables caching for prompts/get.
Sourcepub fn include_tools(self, tools: Vec<String>) -> Self
pub fn include_tools(self, tools: Vec<String>) -> Self
Explicitly allowlists tools for tools/call caching.
An empty list disables tools/call caching. Exclusions configured with
Self::exclude_tools take precedence over this allowlist.
Sourcepub fn exclude_tools(self, tools: Vec<String>) -> Self
pub fn exclude_tools(self, tools: Vec<String>) -> Self
Excludes tools from tools/call caching, overriding the allowlist.
Sourcepub fn stats(&self) -> CacheStats
pub fn stats(&self) -> CacheStats
Returns current cache statistics.
Sourcepub fn invalidate_discovery(&self)
pub fn invalidate_discovery(&self)
Invalidates every final discovery response and advances its cache generation. A response or lookup holding an older generation cannot reuse or repopulate the invalidated discovery state.
Sourcepub fn invalidate(&self, method: &str, params: Option<&Value>)
pub fn invalidate(&self, method: &str, params: Option<&Value>)
Invalidates every session/auth partition and every cursor page for a method and semantic-parameter set.