DefaultJsonSchemaValidator: schema cache grows without bound (no TTL or size limit)

Aperta
#1,344 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java

Direzione di ricerca

Read DefaultJsonSchemaValidator.java in both the Jackson 2 and Jackson 3 modules, then inspect the existing cache tests and clearCache() behavior. Compare bounded cache policies such as a size limit or TTL and check how each behaves with repeated unique schemas. Done means cache growth is bounded while schema validation continues to work; the issue does not identify specific test files.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

DefaultJsonSchemaValidator keeps compiled NetworkNT Schema instances in a ConcurrentHashMap forever. There is an explicit TODO to add eviction; nothing purges entries on tool/schema updates or long-running servers.

Affected code

  • mcp-json-jackson2/src/main/java/io/modelcontextprotocol/json/schema/jackson2/DefaultJsonSchemaValidator.java
  • mcp-json-jackson3/src/main/java/io/modelcontextprotocol/json/schema/jackson3/DefaultJsonSchemaValidator.java
// TODO: Implement a strategy to purge the cache (TTL, size limit, etc.)
private final ConcurrentHashMap<String, Schema> schemaCache;

clearCache() exists for tests but is not wired into server lifecycle or dynamic tool registration.

Steps to reproduce

  1. Run a server that repeatedly addTool / updates tools with distinct inputSchema / outputSchema maps (or stress-test the validator in a loop with unique schemas).
  2. Monitor heap or call getCacheSize() on a test instance.
  3. Observe cache size monotonically increases; entries are never removed.

Expected behavior

Bounded memory for schema compilation cache, for example:

  • max entries + LRU, and/or
  • TTL, and/or
  • invalidation when tools/schemas are removed or replaced

Actual behavior

Every distinct cache key retains a compiled Schema for the JVM lifetime.

Impact

Long-lived MCP servers with dynamic tool catalogs or many unique schemas may accumulate unbounded native/heap pressure over days or weeks.

Related

  • #1328 — incorrect cache keys (correctness); this issue is resource / lifecycle
  • Dynamic tool APIs: McpAsyncServer#addTool, etc.

Suggested direction

Content-addressed keys (fixing $id/hash issues) plus a bounded cache policy documented if needed.

Lingua principale
Java
Stelle
3.8k
Fork
1.1k
Merge medio
1g 3h
PR unite (30g)
38

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/java-sdk

Tutte le issue di modelcontextprotocol/java-sdk

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.