pub struct McpClientCache { /* private fields */ }Expand description
Bounded cache of McpClient instances keyed by the
command-line string. Keeps a Vec of (key, client) pairs
in usage order — most-recently-used at the back. On cache
miss past cap, the front (oldest) entry is dropped.
Why a Vec rather than a HashMap + linked list: cap is
small (16 by default) so linear scan is cheaper than the
pointer chase, and a Vec lets us own the Clients directly
instead of through RefCell/Arc.
Subprocess death is detected lazily: when call_tool fails
the offending entry is dropped. Next call to the same
server respawns. A handler that sits idle long enough for
upstream MCP servers to be killed by ops will see one
Err per server before recovering.
Implementations§
Source§impl McpClientCache
impl McpClientCache
pub fn with_capacity(cap: usize) -> Self
Sourcepub fn call(
&mut self,
server: &str,
tool: &str,
args: Value,
) -> Result<Value, String>
pub fn call( &mut self, server: &str, tool: &str, args: Value, ) -> Result<Value, String>
Send a tools/call to the named server, spawning the
subprocess on cache miss and reusing it on hit. Returns
the server’s result JSON or an error message; on error,
the offending client is dropped so the next call respawns.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of cached subprocesses. Useful for tests and observability; not on the hot path.