Skip to content

perf(mcp): schema caching for tools/list to reduce context bloat #29986

Closed as duplicate

Description

@samba-solo

Problem

MCP injects full tool schemas (~37 tools from 5 MCPs) into every prompt. Context bloat up to 86% (Issue #17482).

From packages/opencode/src/mcp/index.ts:

  • listTools() in defs() calls client.listTools() on every connection
  • Cached only in-memory state (s.defs) — lost on restart
  • tools() in the Service regenerates all tools from cached defs on each call
  • No configurable TTL or persistent caching mechanism

Proposed solution

Cache tools/list response with configurable TTL:

interface MCPServerConfig {
  schemaCacheTTL?: number;  // seconds, default 3600
}

Impact

  • 37 tools × ~486 tokens avg = ~18k tokens saved per prompt
  • Affects: LEX_SUISSE (8 tools) + fedlex (7) + swiss-courts (7) + bag-epl (6) + mcp-biblioteca (9)

Workaround

Currently using subagent_type instead of raw MCP for INVESTIGADOR. But bridge skill (rag-mcp-bridge) still needs MCP tools.

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions