CLAUDE GATE

2 Squirrels AI

Technology Stack

AI EngineClaude CLI
ProtocolMCP (Model Context)
QueueBull + Redis
BackendFastify (Node.js)
MetricsPrometheus

Architecture Note: Production-ready API gateway for Claude CLI with MCP routing, Redis caching, request deduplication, webhook delivery, and comprehensive observability via Prometheus metrics.

Gateway Components

🔌
Query Router
Request Submission
🎯
Intent Detector
MCP Server Selection
📋
Job Processor
Bull Queue Worker
💾
Cache Layer
Redis Response Store
🔄
Deduplicator
In-Flight Tracking
📊
Metrics
Prometheus Export

Query Submission & Routing

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117', 'background': '#0D1117', 'mainBkg': '#161B22', 'nodeBorder': '#4A5E32', 'clusterBkg': '#161B22', 'clusterBorder': '#4A5E32', 'titleColor': '#E6C98F', 'edgeLabelBackground': '#161B22'}}}%%
flowchart TB
    subgraph Client["📥 Client Request"]
        REQ["🔌 POST /api/query"]
        QRY["📝 Query String"]
    end
    subgraph Hash["🔢 Hash Generation"]
        SHA["🔐 SHA256 Hash"]
        NRM["📐 Normalize Query"]
    end
    subgraph Check["🔍 Checks"]
        CC{"💾 Cache Hit?"}
        DC{"🔄 Dedup Match?"}
    end
    subgraph Queue["📋 Bull Queue"]
        ADD["➕ Add Job"]
        PRI["⭐ Priority Level"]
    end
    subgraph Response["📤 Immediate Response"]
        RID["🆔 Request ID"]
        JID["📋 Job ID"]
        STS["📊 Status"]
    end
    REQ --> QRY
    QRY --> SHA
    SHA --> NRM
    NRM --> CC
    CC -->|"Yes"| STS
    CC -->|"No"| DC
    DC -->|"Yes"| JID
    DC -->|"No"| ADD
    ADD --> PRI
    PRI --> RID
    RID --> STS
    JID --> STS
                

MCP Intent Detection & Routing

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117', 'background': '#0D1117', 'mainBkg': '#161B22', 'nodeBorder': '#4A5E32', 'clusterBkg': '#161B22', 'clusterBorder': '#4A5E32', 'titleColor': '#E6C98F', 'edgeLabelBackground': '#161B22'}}}%%
flowchart LR
    subgraph Input["📥 Query"]
        QRY["📝 Query Text"]
    end
    subgraph Detect["🔍 Intent Detection"]
        RGX["📐 Regex Patterns"]
        MAT["🎯 Pattern Matching"]
    end
    subgraph Intents["🏷️ Intent Types"]
        WEB["🌐 Web
HTTP, API, URL"] DB["🗄️ Database
SQL, Schema"] FS["📁 Filesystem
File, Path"] CODE["💻 Code
Function, Debug"] GEN["🔮 General
Default"] end subgraph Servers["🖥️ MCP Servers"] FETCH["🌐 fetch"] SQLITE["🗄️ sqlite"] FSRV["📁 filesystem"] GH["💻 github"] end subgraph Output["📤 Claude Exec"] CLI["🤖 Claude CLI"] MCP["--mcp flags"] end QRY --> RGX RGX --> MAT MAT --> WEB MAT --> DB MAT --> FS MAT --> CODE MAT --> GEN WEB --> FETCH DB --> SQLITE FS --> FSRV CODE --> GH FETCH --> MCP SQLITE --> MCP FSRV --> MCP GH --> MCP MCP --> CLI

Job Processing & Execution

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117', 'background': '#0D1117', 'mainBkg': '#161B22', 'nodeBorder': '#4A5E32', 'clusterBkg': '#161B22', 'clusterBorder': '#4A5E32', 'titleColor': '#E6C98F', 'edgeLabelBackground': '#161B22'}}}%%
flowchart TB
    subgraph Queue["📋 Bull Queue"]
        JOB["📋 Job Received"]
        CON["⚙️ Concurrency: 5"]
    end
    subgraph PreCheck["🔍 Pre-Check"]
        CC{"💾 Re-check Cache"}
        MCP["🎯 Resolve MCP"]
    end
    subgraph Execute["🤖 Claude Execution"]
        SPW["🔧 spawn() Process"]
        FLG["📋 --print --json"]
        TMO["⏱️ Timeout: 120s"]
    end
    subgraph Progress["📊 Progress"]
        P10["10% Started"]
        P80["80% Response"]
        P90["90% Cached"]
        P100["100% Done"]
    end
    subgraph Post["📤 Post-Process"]
        PRS["🔧 Parse JSON"]
        STR["💾 Store Cache"]
        CLR["🔄 Clear Dedup"]
        WHK["📡 Webhook"]
    end
    JOB --> CON
    CON --> CC
    CC -->|"Miss"| MCP
    MCP --> SPW
    SPW --> FLG
    FLG --> TMO
    TMO --> P10
    P10 --> P80
    P80 --> PRS
    PRS --> STR
    STR --> P90
    P90 --> CLR
    CLR --> WHK
    WHK --> P100
                

Caching & Deduplication

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117', 'background': '#0D1117', 'mainBkg': '#161B22', 'nodeBorder': '#4A5E32', 'clusterBkg': '#161B22', 'clusterBorder': '#4A5E32', 'titleColor': '#E6C98F', 'edgeLabelBackground': '#161B22'}}}%%
flowchart TB
    subgraph Request["📥 Incoming"]
        QRY["📝 Query"]
        HSH["🔐 SHA256 Hash"]
    end
    subgraph Cache["💾 Cache Layer"]
        CK["🔑 claude:{hash}"]
        TTL["⏱️ TTL: 3600s"]
        OPS["📋 get/set/delete"]
    end
    subgraph Dedup["🔄 Deduplication"]
        DK["🔑 dedup:{hash}"]
        DT["⏱️ TTL: 300s"]
        INF["📡 In-Flight Mark"]
    end
    subgraph Flow["🔀 Decision Flow"]
        C1{"💾 Cache?"}
        C2{"🔄 In-Flight?"}
        NEW["🆕 New Job"]
    end
    subgraph Results["📤 Results"]
        R1["✅ Cached Response"]
        R2["📋 Existing Job ID"]
        R3["🆔 New Job ID"]
    end
    QRY --> HSH
    HSH --> C1
    C1 -->|"Hit"| R1
    C1 -->|"Miss"| C2
    C2 -->|"Yes"| R2
    C2 -->|"No"| NEW
    NEW --> INF
    INF --> R3
    R3 --> TTL
                

Complete Gateway Architecture

%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#C17852', 'primaryTextColor': '#F0F6FC', 'primaryBorderColor': '#4A5E32', 'lineColor': '#E6C98F', 'secondaryColor': '#161B22', 'tertiaryColor': '#0D1117', 'background': '#0D1117', 'mainBkg': '#161B22', 'nodeBorder': '#4A5E32', 'clusterBkg': '#161B22', 'clusterBorder': '#4A5E32', 'titleColor': '#E6C98F', 'edgeLabelBackground': '#161B22'}}}%%
flowchart TB
    subgraph Client["🖥️ Clients"]
        API["🔌 API Requests"]
        ADM["👤 Admin Dashboard"]
    end
    subgraph Gateway["🚪 Fastify Gateway"]
        RT["🛤️ Routes"]
        QR["/api/query"]
        ST["/api/query/:id"]
        MET["/metrics"]
    end
    subgraph Services["⚙️ Services"]
        INT["🎯 Intent Detector"]
        MCPs["🖥️ MCP Resolver"]
        CLU["🤖 Claude Executor"]
    end
    subgraph Queue["📋 Bull + Redis"]
        BQ["📋 Job Queue"]
        PR["⚙️ Processor"]
        EV["📡 Events"]
    end
    subgraph Storage["💾 Redis"]
        CACHE[("💾 Cache")]
        DEDUP[("🔄 Dedup")]
    end
    subgraph External["🌐 External"]
        CLI["🤖 Claude CLI"]
        WHK["📡 Webhooks"]
    end
    subgraph Observe["📊 Observability"]
        PROM["📈 Prometheus"]
        LOGS["📝 Logs"]
    end
    API --> RT
    ADM --> RT
    RT --> QR
    RT --> ST
    RT --> MET
    QR --> INT
    INT --> MCPs
    QR --> BQ
    BQ --> PR
    PR --> CLU
    CLU --> CLI
    PR --> CACHE
    PR --> DEDUP
    PR --> WHK
    EV --> PROM
    MET --> PROM