MCP Integration Guide - Infrastructure API

How to update MCP server tools to use the new MongoDB-based Infrastructure API

MCP Integration Guide - Infrastructure API

This guide explains how to update the MCP server (gograb-development-ai) to use the new MongoDB-based Infrastructure API instead of the legacy JSON file.


Overview

Old Flow (Legacy)

preflight() → Read entire .cursor/ai-memory/development-memory.json (185K decisions)
log_decision() → Append to JSON file
find_similar() → Linear scan of JSON

New Flow (MongoDB)

preflight() → POST /ai-api/infrastructure/context/:sessionNumber
log_decision() → POST /ai-api/infrastructure/decisions
find_similar() → POST /ai-api/infrastructure/decisions/search

API Endpoints

Base URL

http://localhost:3001/ai-api/infrastructure

Sessions

Endpoint Method Description
/sessions GET List sessions
/sessions POST Create/upsert session
/sessions/:sessionID GET Get session by ID
/sessions/:sessionID/config GET Get session config
/sessions/:sessionID/config PATCH Update session config

Decisions

Endpoint Method Description
/decisions GET Query decisions
/decisions POST Create decision
/decisions/search POST Full-text search

Context

Endpoint Method Description
/context/:sessionNumber POST Get intelligent context

Updated Tool Implementations

preflight()

Before (legacy):

async preflight({ task }) {
  const json = await readFile('.cursor/ai-memory/development-memory.json');
  const data = JSON.parse(json);
  // Returns 185K+ decisions to context window!
  return data.decisions;
}

After (MongoDB):

async preflight({ task, categories }) {
  // 1. Load MCP config
  const config = JSON.parse(await readFile('.cursor/mcp-config.json'));
  
  // 2. Call intelligent context API
  const response = await fetch(`http://localhost:3001/ai-api/infrastructure/context/${config.session.current}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      task,
      agentId: config.agents.current,
      sessionID: config.session.currentID
    })
  });
  
  const context = await response.json();
  
  // 3. Return focused context (10-15 items, not 185K!)
  return {
    decisions: context.decisions,       // ~10 relevant decisions
    patterns: context.patterns,         // ~5 relevant patterns
    keywords: context.keywords,
    category: context.category,
    tokenEstimate: context.estimatedTokens,
    config: context.usedConfig
  };
}

log_decision()

Before (legacy):

async log_decision(decision) {
  const json = await readFile('.cursor/ai-memory/development-memory.json');
  const data = JSON.parse(json);
  data.decisions.push(decision);
  await writeFile('.cursor/ai-memory/development-memory.json', JSON.stringify(data));
}

After (MongoDB):

async log_decision(decision) {
  const config = JSON.parse(await readFile('.cursor/mcp-config.json'));
  
  // 1. Save to MongoDB via API
  const response = await fetch('http://localhost:3001/ai-api/infrastructure/decisions', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      ...decision,
      sessionNumber: config.session.current,
      sessionID: config.session.currentID,
      agentId: config.agents.current,
      timestamp: new Date().toISOString()
    })
  });
  
  return response.json();
}

find_similar()

Before (legacy):

async find_similar({ query, type, limit = 3 }) {
  const json = await readFile('.cursor/ai-memory/development-memory.json');
  const data = JSON.parse(json);
  
  // Linear scan - O(n)
  return data.decisions
    .filter(d => d.type === type)
    .filter(d => d.question.includes(query) || d.chosen.includes(query))
    .slice(0, limit);
}

After (MongoDB):

async find_similar({ query, type, limit = 3 }) {
  const config = JSON.parse(await readFile('.cursor/mcp-config.json'));
  
  // Indexed search - O(log n)
  const response = await fetch('http://localhost:3001/ai-api/infrastructure/decisions/search', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      query,
      type,
      limit,
      agentId: config.agents.current,
      minConfidence: 0.8
    })
  });
  
  const result = await response.json();
  return result.decisions;
}

MCP Config File

The MCP server should read configuration from .cursor/mcp-config.json:

{
  "version": "1.0",
  "session": {
    "current": 9,
    "currentID": "9.1",
    "mongodbUrl": "mongodb+srv://...",
    "syncInterval": 300000
  },
  "behavior": {
    "getFeedback": true,
    "confidenceThreshold": 0.95,
    "askClarifyingQuestions": true,
    "maxContextItems": 10,
    "usePatternLibrary": true,
    "timeWindow": "7d"
  },
  "agents": {
    "current": "cursor-angular-agent",
    "available": ["cursor-orchestrator", "cursor-architecture-agent", ...]
  },
  "api": {
    "baseUrl": "http://localhost:3001/ai-api",
    "endpoints": {
      "sessions": "/infrastructure/sessions",
      "decisions": "/infrastructure/decisions",
      "context": "/infrastructure/context"
    }
  }
}

New Tool: configure_session()

async configure_session(config: Partial<SessionConfig>) {
  const mcpConfig = JSON.parse(await readFile('.cursor/mcp-config.json'));
  
  const response = await fetch(
    `http://localhost:3001/ai-api/infrastructure/sessions/${mcpConfig.session.currentID}/config`,
    {
      method: 'PATCH',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        agentId: mcpConfig.agents.current,
        config
      })
    }
  );
  
  return response.json();
}

Performance Comparison

Metric Legacy (JSON) New (MongoDB)
Context load 2-3 seconds < 100ms
Tokens loaded ~500K < 5K
Query complexity O(n) O(log n)
Offline support Yes Flash cache
Multi-dev sharing No Yes (MongoDB)

Migration Steps

  1. Start backend-ai-api: yarn serve:backend-ai
  2. Run migration script: node tools/scripts/database/migrate-decisions-to-mongodb.js
  3. Verify data: Check MongoDB has decisions
  4. Update MCP server: Use new API endpoints
  5. Test: Run preflight, log_decision, find_similar
  6. Archive JSON: Move .cursor/ai-memory/development-memory.json to backup

Error Handling

async preflight({ task }) {
  try {
    // Try MongoDB API first
    return await this.fetchFromAPI(task);
  } catch (error) {
    console.warn('MongoDB API unavailable, falling back to cache');
    
    // Fall back to flash-persistence cache
    const cached = this.flashCache.getRecentDecisions(10);
    if (cached.length > 0) {
      return { decisions: cached, source: 'cache' };
    }
    
    // Last resort: read JSON file
    console.warn('Cache empty, falling back to JSON file');
    return await this.readLegacyJSON();
  }
}

Testing Checklist


Related Files