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

Offen
#1,344 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
48/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
java
Bereich
backend, performance

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Java
Sterne
3.8k
Forks
1.1k
Ø Merge
1 T. 3 Std.
Gemergte PRs (30 T.)
38

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus modelcontextprotocol/java-sdk

Alle Issues in modelcontextprotocol/java-sdk

Ähnliche Issues

Weitere Issues zu Java

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.