MCPcopy Index your code
hub / github.com/simstudioai/sim / MemoryMcpCache

Class MemoryMcpCache

apps/sim/lib/mcp/storage/memory-cache.ts:8–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6const logger = createLogger('McpMemoryCache')
7
8export class MemoryMcpCache implements McpCacheStorageAdapter {
9 private cache = new Map<string, McpCacheEntry>()
10 private readonly maxCacheSize = MCP_CONSTANTS.MAX_CACHE_SIZE
11 private cleanupInterval: NodeJS.Timeout | null = null
12
13 constructor() {
14 this.startPeriodicCleanup()
15 }
16
17 private startPeriodicCleanup(): void {
18 this.cleanupInterval = setInterval(
19 () => {
20 this.cleanupExpiredEntries()
21 },
22 5 * 60 * 1000 // 5 minutes
23 )
24 // Don't keep Node process alive just for cache cleanup
25 this.cleanupInterval.unref()
26 }
27
28 private cleanupExpiredEntries(): void {
29 const now = Date.now()
30 const expiredKeys: string[] = []
31
32 this.cache.forEach((entry, key) => {
33 if (entry.expiry <= now) {
34 expiredKeys.push(key)
35 }
36 })
37
38 expiredKeys.forEach((key) => this.cache.delete(key))
39
40 if (expiredKeys.length > 0) {
41 logger.debug(`Cleaned up ${expiredKeys.length} expired cache entries`)
42 }
43 }
44
45 private evictIfNeeded(): void {
46 if (this.cache.size <= this.maxCacheSize) {
47 return
48 }
49
50 // Evict oldest entries (by insertion order - Map maintains order)
51 const entriesToRemove = this.cache.size - this.maxCacheSize
52 const keys = Array.from(this.cache.keys()).slice(0, entriesToRemove)
53 keys.forEach((key) => this.cache.delete(key))
54
55 logger.debug(`Evicted ${entriesToRemove} cache entries`)
56 }
57
58 async get(key: string): Promise<McpCacheEntry | null> {
59 const entry = this.cache.get(key)
60 const now = Date.now()
61
62 if (!entry || entry.expiry <= now) {
63 if (entry) {
64 this.cache.delete(key)
65 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected