Home / Class/ GraphCache Class — mcp Architecture

GraphCache Class — mcp Architecture

Architecture documentation for the GraphCache class in graph-cache.ts from the mcp codebase.

Entity Profile

Dependency Diagram

graph TD
  04799611_7360_96bd_a002_0c06de0b1d90["GraphCache"]
  108c9ff4_bdb8_518a_9256_9ff4cd9d39a7["graph-cache.ts"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|defined in| 108c9ff4_bdb8_518a_9256_9ff4cd9d39a7
  6b8403ed_3225_353c_2302_260d0a18d1db["constructor()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 6b8403ed_3225_353c_2302_260d0a18d1db
  2ef71e67_fa6d_b33f_d005_85a400698718["get()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 2ef71e67_fa6d_b33f_d005_85a400698718
  4e1e5a38_002c_8f16_1767_0260a7001d5e["set()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 4e1e5a38_002c_8f16_1767_0260a7001d5e
  5b58088c_d9af_5bc6_19ab_1fff095079b9["has()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 5b58088c_d9af_5bc6_19ab_1fff095079b9
  aa12a27e_97dc_0771_0ad0_d1af55e1e15d["evictOldest()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| aa12a27e_97dc_0771_0ad0_d1af55e1e15d
  901790e8_bb47_a221_74f6_961092a7bb3f["evictStale()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 901790e8_bb47_a221_74f6_961092a7bb3f
  2d443d0a_cb15_34ec_5e27_69055ed75a85["status()"]
  04799611_7360_96bd_a002_0c06de0b1d90 -->|method| 2d443d0a_cb15_34ec_5e27_69055ed75a85

Relationship Graph

Source Code

src/cache/graph-cache.ts lines 262–365

export class GraphCache {
  private cache = new Map<string, CacheEntry>();
  private maxGraphs: number;
  private maxNodes: number;
  private maxAgeMs: number;
  private currentNodes = 0;

  constructor(options?: { maxGraphs?: number; maxNodes?: number; maxAgeMs?: number }) {
    this.maxGraphs = options?.maxGraphs || DEFAULT_MAX_GRAPHS;
    this.maxNodes = options?.maxNodes || DEFAULT_MAX_NODES;
    this.maxAgeMs = options?.maxAgeMs || DEFAULT_CACHE_TTL_MS;
  }

  get(cacheKey: string): IndexedGraph | null {
    const entry = this.cache.get(cacheKey);
    if (entry) {
      // Update access time (LRU)
      entry.lastAccessed = Date.now();
      return entry.graph;
    }
    return null;
  }

  set(cacheKey: string, graph: IndexedGraph): void {
    const nodeCount = graph.summary.nodeCount;

    // Evict stale entries first
    this.evictStale();

    // Evict if needed
    while (
      (this.cache.size >= this.maxGraphs || this.currentNodes + nodeCount > this.maxNodes) &&
      this.cache.size > 0
    ) {
      this.evictOldest();
    }

    // Store
    const now = Date.now();
    this.cache.set(cacheKey, {
      graph,
      nodeCount,
      lastAccessed: now,
      createdAt: now,
    });
    this.currentNodes += nodeCount;
  }

  has(cacheKey: string): boolean {
    return this.cache.has(cacheKey);
  }

  private evictOldest(): void {
    let oldestKey: string | null = null;
    let oldestTime = Infinity;

    for (const [key, entry] of this.cache) {
      if (entry.lastAccessed < oldestTime) {
        oldestTime = entry.lastAccessed;
        oldestKey = key;
      }
    }

    if (oldestKey) {
      const entry = this.cache.get(oldestKey)!;
      this.currentNodes -= entry.nodeCount;
      this.cache.delete(oldestKey);
    }
  }

  /**
   * Evict all cache entries that have exceeded their TTL (maxAgeMs)
   * This method can be called manually or is automatically invoked before adding new entries
   * @returns Number of entries evicted
   */
  evictStale(): number {
    const now = Date.now();
    const keysToEvict: string[] = [];

    // Find all stale entries
    for (const [key, entry] of this.cache) {

Domain

Frequently Asked Questions

What is the GraphCache class?
GraphCache is a class in the mcp codebase, defined in src/cache/graph-cache.ts.
Where is GraphCache defined?
GraphCache is defined in src/cache/graph-cache.ts at line 262.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free