Class: RailsAiBridge::ToolResultCache
- Inherits:
-
Object
- Object
- RailsAiBridge::ToolResultCache
- Defined in:
- lib/rails_ai_bridge/tool_result_cache.rb
Overview
Caches MCP tool call results per application, tool, and argument fingerprint.
The cache is in-memory, per-process, and TTL-based. It ignores server_context
when fingerprinting so identical client requests share the same cached response.
Keeps at most 1,000 responses, removing expired or oldest entries as needed.
Enable by setting config.mcp.tool_result_cache_ttl to a positive number of seconds.
Defined Under Namespace
Classes: CachedTool
Constant Summary collapse
- NON_CACHEABLE =
Tool names that must never be cached because their results may change between calls (external provider data) or represent live network state.
Set[ 'rails_get_provider_context' ].freeze
Class Method Summary collapse
-
.enabled? ⇒ Boolean
Whether tool result caching is enabled.
-
.fetch_response(tool_name, arguments) ⇒ MCP::Tool::Response
Fetches a cached response or yields to compute it.
-
.maybe_wrap(tool_class) ⇒ Class, CachedTool
Wraps a tool class if caching is enabled; otherwise returns the class unchanged.
-
.reset! ⇒ void
Clears all cached tool results.
-
.wrap(tool_class) ⇒ CachedTool
Wraps a tool class so every call goes through the cache.
Class Method Details
.enabled? ⇒ Boolean
Returns whether tool result caching is enabled.
128 129 130 |
# File 'lib/rails_ai_bridge/tool_result_cache.rb', line 128 def enabled? RailsAiBridge.configuration.mcp.tool_result_cache_ttl.to_i.positive? end |
.fetch_response(tool_name, arguments) ⇒ MCP::Tool::Response
Fetches a cached response or yields to compute it.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rails_ai_bridge/tool_result_cache.rb', line 98 def fetch_response(tool_name, arguments) return yield unless enabled? key = cache_key(tool_name, arguments) fingerprint = key.last mutex.synchronize do prune_expired entry = cache[key] if entry && ttl_valid?(entry) Instrumentation.instrument('tool.result_cache_hit', tool_name: tool_name, fingerprint: fingerprint) return entry[:response] end end Instrumentation.instrument('tool.result_cache_miss', tool_name: tool_name, fingerprint: fingerprint) do response = yield mutex.synchronize { store_response(key, response) } response end end |
.maybe_wrap(tool_class) ⇒ Class, CachedTool
Wraps a tool class if caching is enabled; otherwise returns the class unchanged.
77 78 79 80 81 82 |
# File 'lib/rails_ai_bridge/tool_result_cache.rb', line 77 def maybe_wrap(tool_class) return tool_class unless enabled? return tool_class if NON_CACHEABLE.include?(tool_class.tool_name) wrap(tool_class) end |
.reset! ⇒ void
This method returns an undefined value.
Clears all cached tool results.
123 124 125 |
# File 'lib/rails_ai_bridge/tool_result_cache.rb', line 123 def reset! mutex.synchronize { @cache = {} } end |
.wrap(tool_class) ⇒ CachedTool
Wraps a tool class so every call goes through the cache.
88 89 90 |
# File 'lib/rails_ai_bridge/tool_result_cache.rb', line 88 def wrap(tool_class) CachedTool.new(tool_class) end |