opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Cache MCP tools with invalidation

Open ktsaou opened this issue 1 week ago • 1 comments

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 listTools results 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 listTools fetch.
  • Empty tool lists are treated as valid cache entries.

Invalidation events (existing flows only)

  • MCP ToolsChanged notification (now consumed to invalidate that server).
  • MCP add/connect/disconnect (existing endpoints).
  • MCP listTools failure (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.

ktsaou avatar Jan 04 '26 21:01 ktsaou