Skip to content

feat(mcp): Implement lazy initialization for MCP server #152

@prosdev

Description

@prosdev

Problem

MCP server performs eager initialization causing 2-5s delays in AI tools. Every AI interaction is blocked by full indexer startup, creating poor UX.

Current behavior:

  • Server initializes RepositoryIndexer on startup
  • Location: packages/mcp-server/bin/dev-agent-mcp.ts:117
  • TODO comment exists: "Make this truly lazy (only initialize on first tool call)"

Impact

  • User Experience: 2-5s delay before every AI tool interaction
  • Adoption: Makes dev-agent feel sluggish compared to competitors
  • Success Criteria: <500ms MCP server startup (currently 2-5s)

Solution

Initialize indexer only on first tool call, not server start:

  1. Move initialization from server startup to first adapter call
  2. Add mutex to prevent concurrent initialization
  3. Cache initialized indexer for subsequent calls
  4. Keep centralized storage pattern

Implementation approach:

// In dev-agent-mcp.ts
let indexer: RepositoryIndexer | null = null;
let initPromise: Promise<RepositoryIndexer> | null = null;

async function ensureIndexer() {
  if (indexer) return indexer;
  if (initPromise) return initPromise;
  
  initPromise = (async () => {
    // Initialize indexer
    indexer = new RepositoryIndexer({...});
    await indexer.initialize();
    return indexer;
  })();
  
  return initPromise;
}

Acceptance Criteria

  • MCP server starts in <500ms
  • First tool call initializes indexer (may take 1-2s)
  • Subsequent calls are instant (cached indexer)
  • No race conditions with concurrent first calls
  • All existing tests pass

Files to Modify

  • packages/mcp-server/bin/dev-agent-mcp.ts - Main initialization logic
  • Tests for lazy loading behavior

Priority: P0 - Blocking user experience
Part of: #104 - Performance & Reliability Critical Path

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