Skip to content

feat(mcp): Add memory monitoring for long-running processes #154

Description

@prosdev

Problem

No memory monitoring for long-running MCP processes. While circular buffers exist, there's no usage tracking or automatic restart thresholds. AI tools become unresponsive after heavy use due to unbounded memory growth.

Current state:

  • Circular buffers exist but no memory monitoring
  • No automatic restart thresholds
  • No heap usage tracking
  • Production MCP servers can grow unbounded

Impact

  • Reliability: MCP server becomes unresponsive after heavy use
  • User Experience: AI tools slow down or crash during long sessions
  • Production: No visibility into memory issues
  • Success Criteria: <500MB steady state memory usage

Solution

Add memory monitoring with automatic restart thresholds:

  1. Track heap usage periodically (every 30s)
  2. Log warnings when approaching thresholds
  3. Emit health check failures when over threshold
  4. Optional: Graceful restart when critical threshold hit

Implementation approach:

// Memory monitoring service
interface MemoryStats {
  heapUsed: number;
  heapTotal: number;
  rss: number;
  external: number;
}

class MemoryMonitor {
  private checkInterval = 30000; // 30s
  private warnThresholdMB = 400;
  private criticalThresholdMB = 500;
  
  start() {
    setInterval(() => {
      const mem = process.memoryUsage();
      const heapMB = mem.heapUsed / 1024 / 1024;
      
      if (heapMB > this.criticalThresholdMB) {
        logger.error(`Memory critical: ${heapMB}MB`);
        // Emit health check failure
      } else if (heapMB > this.warnThresholdMB) {
        logger.warn(`Memory warning: ${heapMB}MB`);
      }
    }, this.checkInterval);
  }
}

Acceptance Criteria

  • Memory usage logged every 30s in debug mode
  • Warning emitted at 400MB heap usage
  • Critical error at 500MB heap usage
  • Health check reports memory status
  • Circular buffer limits enforced (already exists)
  • No memory leaks in 24h stress test

Files to Create/Modify

  • packages/mcp-server/src/monitoring/memory-monitor.ts - New monitoring service
  • packages/mcp-server/bin/dev-agent-mcp.ts - Wire up monitor
  • packages/mcp-server/src/adapters/built-in/health-adapter.ts - Report memory
  • Tests for memory monitoring

Priority: P1 - Production stability
Part of: #104 - Performance & Reliability Critical Path

Future Enhancement

Consider automatic graceful restart when hitting critical threshold (v2):

  • Notify client of restart
  • Save current state
  • Spawn new process
  • Transfer connections

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpkg:mcppackages/mcp-server - adapters, tools

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions