Expand description
Result-cache layer for large MCP tool outputs.
Large tool results (long search hit lists, document corpora, scraped pages) don’t belong in the model window. This module provides a deterministic, transport-neutral way to:
- Cache an oversized JSON array under an opaque handle.
- Envelope what the model actually sees: the handle, the total item count, the page size, and a deterministic first page.
- Page through the remaining items on demand via follow-up tools that read from the cache.
- Release the handle when the caller is done so memory bounds stay deterministic.
rig-mcp keeps raw transports lossless. Callers can invoke
cache_if_large directly, or opt into
CachedResultsTransport and
the page/release tools from the cache_tools
module at the
model boundary.
§Example
use rig_mcp::result_cache::{
CachedResultEnvelope, MemoryResultCache, ResultCache, cache_if_large,
};
use serde_json::{Value, json};
use std::sync::Arc;
let cache: Arc<dyn ResultCache> = Arc::new(MemoryResultCache::new());
let big = json!([{"id": 1}, {"id": 2}, {"id": 3}]);
let envelope = cache_if_large(big, cache.as_ref(), 16, 1);
// The first page is a tiny preview; the rest are paged through the cache.
let env: CachedResultEnvelope = serde_json::from_value(envelope).unwrap();
assert_eq!(env.total_items, 3);Structs§
- Cached
Result Envelope - JSON envelope returned to the model in place of an oversized array.
- Cached
Result Handle - Opaque, unique handle for a cached result. Stable for the lifetime of
the
ResultCacheentry; invalidated byResultCache::release. - Memory
Result Cache - Process-local, in-memory
ResultCache.
Traits§
- Result
Cache - Transport-neutral cache for paged tool results.
Functions§
- cache_
if_ large - If
valueis a JSON array whose serialized form exceedsthreshold_bytes, store it incacheand return a JSONCachedResultEnvelope. Otherwise returnvalueunchanged.