Skip to content

MCP Gateway Integration: Expert Architecture Review & Implementation Plan #653

@manavgup

Description

@manavgup

MCP Gateway Integration: Expert Architecture Review

Executive Summary

Expert panel review of MCP (Model Context Protocol) integration approaches for RAG Modulo. Unanimous recommendation: Proceed with simplified MCP Context Forge Gateway integration instead of building a complex custom agent framework.

Decision: ✅ Approved - Simple Gateway Integration
Confidence: 95%
Timeline: 5 days
Code Reduction: 80% (200 lines vs 2,000+ lines)


Expert Panel

  • Martin Fowler - Software Architecture & Design (Lead)
  • Sam Newman - Microservices & Distributed Systems
  • Michael Nygard - Production Systems & Reliability
  • Gregor Hohpe - Enterprise Integration Patterns

Reviewed Approaches

❌ Complex Approach: Full Agent Framework

  • Effort: 2+ weeks, 2,000+ lines of code
  • Components: AgentRegistry, BaseAgent, AgentManifest, AgentService, AgentContext, Database models
  • Issues Identified:
    • Tight coupling between agent abstraction and MCP protocol
    • God Object anti-pattern in AgentContext
    • Missing observability, circuit breakers, resilience patterns
    • No API versioning strategy
    • Sequential execution bottleneck

✅ Simple Approach: MCP Gateway Integration

  • Effort: 3-5 days, ~200 lines of code
  • Components: Thin MCPGatewayClient wrapper, 2 API endpoints
  • Benefits:
    • 80% less code to maintain
    • Production features included (rate limiting, protocol translation, auth)
    • Battle-tested gateway (400+ tests)
    • Evolutionary architecture

Architecture

RAG Modulo Backend (FastAPI)
        ↓ HTTP/SSE
MCP Context Forge Gateway
        ↓ stdio/SSE/HTTP
MCP Servers (PowerPoint, Translation, etc.)

Expert Findings & Recommendations

🎯 Martin Fowler - Architecture

Key Point: "Build the simplest thing that could work. Evolve based on real needs."

Critical Addition: Anti-Corruption Layer

class ContextForgeAdapter:
    """Protect from external API changes"""
    def list_tools(self) -> List[AgentTool]:
        cf_tools = await self.client.list_tools()
        return [self._translate_tool(t) for t in cf_tools]

🔧 Sam Newman - Service Integration

Non-Negotiable Requirements:

  1. Health Checks
async def health_check(self) -> bool:
    try:
        response = await self.client.get(f"{self.base_url}/health", timeout=5)
        return response.status_code == 200
    except Exception:
        return False
  1. Don't Fail Core RAG Flow
try:
    ppt_data = await mcp_gateway.invoke_tool(...)
    search_results.artifacts.append(ppt_data)
except Exception as e:
    logger.warning(f"Optional tool failed: {e}")
    # Continue - search results are still valid
  1. API Versioning
API_VERSION = "v1"
response = await self.client.post(
    f"{self.base_url}/api/{self.API_VERSION}/tools/{tool_name}/invoke"
)

🛡️ Michael Nygard - Production Reliability

Critical Patterns:

  1. Circuit Breaker
from circuitbreaker import circuit

@circuit(failure_threshold=5, recovery_timeout=60)
async def invoke_tool(self, tool_name: str, args: Dict) -> Dict:
    # Implementation with timeout
    pass
  1. Observability
from prometheus_client import Counter, Histogram

mcp_tool_invocations = Counter(
    'mcp_tool_invocations_total',
    'Total MCP tool invocations',
    ['tool_name', 'status']
)

mcp_tool_duration = Histogram(
    'mcp_tool_duration_seconds',
    'MCP tool invocation duration',
    ['tool_name']
)
  1. Structured Logging
logger.error(
    "MCP tool invocation failed",
    extra={
        "tool_name": tool_name,
        "error": str(e),
        "arguments": args
    }
)

🔗 Gregor Hohpe - Integration Patterns

Recommended Pattern: Content Enricher

class SearchResultEnricher:
    async def enrich(
        self,
        search_results: SearchOutput,
        enrichments: List[EnrichmentRequest]
    ) -> SearchOutput:
        # Parallel execution
        tasks = [self._apply_enrichment(r, req) for req in enrichments]
        results = await asyncio.gather(*tasks, return_exceptions=True)
        
        # Attach artifacts
        search_results.artifacts = [r for r in results if r]
        return search_results

Benefits:

  • Enrichment is optional (core flow works without it)
  • Enrichment is asynchronous (doesn't block main flow)
  • Clean separation of concerns

Implementation Plan (5 Days)

Day 1: Infrastructure Setup

  • Deploy MCP Context Forge (Docker Compose)
  • Deploy Redis for gateway
  • Set up health monitoring
  • Configure JWT authentication

Day 2: Client Implementation

  • Implement ResilientMCPGatewayClient
    • Health checks
    • Circuit breaker
    • Timeout handling
    • Structured logging
    • Prometheus metrics

Day 3: Search Integration

  • Implement SearchResultEnricher
    • Content Enricher pattern
    • Parallel execution
    • Retry logic
    • Error isolation

Day 4: API & Testing

  • Add 2 API endpoints
    • GET /api/v1/mcp/tools
    • POST /api/v1/mcp/tools/{name}/invoke
  • Write integration tests
  • Test failure scenarios
  • Validate circuit breaker

Day 5: Example Integration

  • Register PowerPoint MCP server
  • Integrate with SearchService
  • Update frontend for artifact download
  • End-to-end testing

Production Checklist

Before deploying to production:

  • Health Checks: Gateway health monitoring active
  • Circuit Breaker: Implemented and tested
  • Timeouts: All MCP calls have timeouts (default: 30s)
  • Graceful Degradation: Core RAG works if tools fail
  • Observability: Metrics, logs, and traces implemented
  • Rate Limiting: Request limits configured in gateway
  • Error Handling: All exceptions caught and logged
  • Documentation: API docs updated, runbook created

Success Metrics

Availability

  • MCP Gateway uptime: >99.5%
  • Tool availability: >95%
  • Circuit breaker activations: <1% of requests

Performance

  • Tool invocation latency: p95 <2s
  • Search latency impact: <10% increase with tools enabled
  • Timeout rate: <1%

Usage

  • Tool invocation success rate: >90%
  • Enrichment adoption: >20% of searches use tools
  • User satisfaction: Artifact downloads per search

Risk Mitigation

Risk Probability Impact Mitigation
Gateway becomes SPOF Medium High Circuit breaker, graceful degradation
Tool latency impacts UX Medium Medium Async execution, timeouts
Gateway API changes Low Medium API versioning, anti-corruption layer
Tool failures cascade Low High Error isolation, retry logic
Resource exhaustion Low High Rate limiting, quotas in gateway

Files to Create

backend/rag_solution/mcp/
├── __init__.py
├── gateway_client.py          # MCPGatewayClient (100 lines)
└── enricher.py                # SearchResultEnricher (150 lines)

backend/rag_solution/router/
└── mcp_router.py              # API endpoints (50 lines)

docker-compose.yml             # Add MCP Gateway service
.env.example                   # Add MCP_GATEWAY_* vars
core/config.py                 # Add MCP settings

Evolutionary Path

Phase 1 (Now): Simple Gateway Integration

  • Direct tool invocation
  • Manual tool selection in UI
  • Basic enrichment

Phase 2 (Month 2): Smart Orchestration [Only if proven user demand]

  • Auto-select tools based on query type
  • Cache expensive tool results
  • User tool preferences

Phase 3 (Month 3+): Advanced Features [Only if needed]

  • Tool chaining
  • Conditional execution
  • Custom tool bundles per collection

References


Expert Consensus

Martin Fowler: "Build the simplest thing that could work. Evolve based on real needs, not imagined ones."

Sam Newman: "Service dependencies are fine when they're healthy. Make yours healthy with versioning, health checks, and fallbacks."

Michael Nygard: "Production systems fail. Build for failure from day one - circuit breakers aren't optional."

Gregor Hohpe: "Integration patterns exist for a reason. Use Content Enricher to keep your core flow clean."


Labels

enhancement, architecture, mcp, integration, approved

Priority

High - Foundational capability for extensibility

Estimate

5 days

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions