[PR #6870] Cache MCP tools with invalidation #12147

Open
opened 2026-02-16 18:17:04 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/6870

State: open
Merged: No


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.
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/6870 **State:** open **Merged:** No --- ## 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.
yindo added the pull-request label 2026-02-16 18:17:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12147