opencode
opencode copied to clipboard
Cache MCP tools with invalidation
Problem
MCP tools are listed on every prompt build, which calls listTools per MCP server sequentially. A slow MCP server can slow all LLM requests.
Solution
- Add a per-mcp-server cache of raw MCP
listToolsresults in MCP state. - Cache validity is tracked by a per-mcp-server sequence counter.
- On a cache hit, rebuild AI SDK Tool objects from cached MCP tool definitions so the behavior matches a fresh
listToolsfetch. - Empty tool lists are treated as valid cache entries.
Invalidation events (existing flows only)
- MCP
ToolsChangednotification (now consumed to invalidate that server). - MCP add/connect/disconnect (existing endpoints).
- MCP
listToolsfailure (existing behavior: mark failed + remove client) also invalidates cache. - Instance disposal clears caches.
Behavior details
- If cache is valid, MCP.tools returns immediately without calling MCP
listTools. - If cache is invalid or missing, MCP.tools blocks and refreshes (same as current behavior for a fresh fetch).
- Failure behavior is unchanged: failed status + client removal.
Tests
- Added MCP tools cache tests (cache hit, ToolsChanged invalidation, empty tools).
- Ran:
bun test test/mcp/tools-cache.test.ts(from packages/opencode).
Notes
- No new config watcher; config reload behavior remains unchanged.