feat(mcp): Add MCP Gateway integration for tool invocation and enrichment#671
feat(mcp): Add MCP Gateway integration for tool invocation and enrichment#671
Conversation
…ment Implements issue #653 - MCP Gateway Integration with simplified architecture following expert panel recommendations (Fowler, Newman, Nygard, Hohpe). ## Implementation (~200 lines of core code) ### Core Components - `MCPGatewayClient`: Resilient HTTP client with circuit breaker pattern - 5 failure threshold, 60s recovery timeout - Health checks with 5-second timeout - 30-second default timeout on all calls - Graceful degradation on failures - `SearchResultEnricher`: Content Enricher pattern implementation - Parallel tool invocation with concurrency limits - Error isolation (failures don't cascade) - Timeout handling with graceful degradation - `mcp_router.py`: Two API endpoints - POST /api/mcp/tools/invoke - Tool invocation - POST /api/mcp/enrich - Search result enrichment - GET /api/mcp/tools - List available tools - GET /api/mcp/health - Gateway health status ### Configuration - Added MCP settings to Settings class (config.py) - Updated .env.example with MCP configuration - Added optional mcp-gateway service to docker-compose-infra.yml ### Testing - Unit tests for circuit breaker pattern - Unit tests for enricher service - Unit tests for Pydantic schemas Closes #653
🚀 Development Environment OptionsThis repository supports Dev Containers for a consistent development environment. Option 1: GitHub Codespaces (Recommended)Create a cloud-based development environment:
Option 2: VS Code Dev Containers (Local)Use Dev Containers on your local machine:
Option 3: Traditional Local SetupSet up the development environment manually: # Clone the repository
git clone https://github.com/manavgup/rag_modulo.git
cd rag_modulo
git checkout claude/implement-issue-653-01EuqiJs9sZZeUzRV85p8Nwj
# Initialize development environment
make dev-init
make dev-build
make dev-up
make dev-validateAvailable CommandsOnce in your development environment: make help # Show all available commands
make dev-validate # Validate environment setup
make test-atomic # Run atomic tests
make test-unit # Run unit tests
make lint # Run lintingServices AvailableWhen running
This automated message helps reviewers quickly set up the development environment. |
Code Review - PR #671: MCP Gateway IntegrationSummaryThis PR implements MCP Gateway integration following a simplified architecture pattern. The implementation is production-grade with excellent resilience patterns, comprehensive testing, and clean separation of concerns. Overall, this is very high quality work that demonstrates strong software engineering practices. 🎯 Strengths1. Excellent Architecture Decisions
2. Production-Grade Resilience Patterns
3. Comprehensive Testing
4. Code Quality
5. Security & Configuration
🔍 Issues & RecommendationsCRITICAL: Resource Leak 🔴Location: Issue: The HTTP client is never closed, creating a resource leak. The singleton pattern creates one client but never calls Impact:
Fix Required: # In main.py lifespan context manager (around line 222)
from rag_solution.router.mcp_router import _mcp_client
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
# Existing startup code...
yield
# CLEANUP: Close MCP client on shutdown
global _mcp_client
if _mcp_client:
await _mcp_client.close()
_mcp_client = NoneOR use FastAPI startup/shutdown events in mcp_router.py. HIGH: Circuit Breaker Race Condition 🟠Location: Issue: Current Code: if self.state == CircuitState.OPEN:
if elapsed >= self.recovery_timeout:
self.state = CircuitState.HALF_OPEN # ← Race here
return True
# HALF_OPEN: Allow one test request
return True # ← All requests pass!Fix: from threading import Lock
class CircuitBreaker:
def __init__(self, ...):
# ...
self._lock = Lock()
self._half_open_test_in_progress = False
def can_execute(self) -> bool:
with self._lock:
if self.state == CircuitState.HALF_OPEN:
if self._half_open_test_in_progress:
raise CircuitBreakerOpenError(0)
self._half_open_test_in_progress = True
return True
# ... rest of logic
def record_success(self) -> None:
with self._lock:
# ... existing logic
self._half_open_test_in_progress = FalseMEDIUM: Missing Configuration Validation 🟡Location: Issue: No validation that MCP settings are mutually consistent when Example Problem: User sets Fix: from pydantic import field_validator, model_validator
class Settings(BaseSettings):
# ... existing fields ...
@model_validator(mode='after')
def validate_mcp_config(self) -> 'Settings':
if self.mcp_enabled:
if not self.mcp_gateway_url or self.mcp_gateway_url == "http://localhost:8080":
raise ValueError(
"MCP_GATEWAY_URL must be configured when MCP_ENABLED=true"
)
return selfMEDIUM: Inconsistent Error Handling 🟡Location: Issue: Generic Current Code: except Exception as e:
# Catch-all to ensure enrichment never crashes the main flow
error_msg = f"Enrichment failed: {e!s}"
logger.error(error_msg, exc_info=True)
result.errors.append(error_msg)Recommendation: Be more specific or at least separate expected vs unexpected errors: except TimeoutError:
# ... existing timeout handling
except (httpx.RequestError, httpx.HTTPError) as e:
# Expected network errors
error_msg = f"Enrichment network error: {e!s}"
logger.warning(error_msg)
result.errors.append(error_msg)
except Exception as e:
# Unexpected errors (programming bugs)
error_msg = f"Enrichment unexpected error: {e!s}"
logger.exception(error_msg) # Full stack trace
result.errors.append(error_msg)LOW: Missing Integration Test 🟡Test Coverage: Unit tests are excellent, but missing end-to-end integration test. Recommendation: Add integration test that verifies: # tests/integration/test_mcp_integration.py
@pytest.mark.integration
async def test_mcp_enrichment_flow(test_client, auth_headers):
"""Test full MCP enrichment flow from API to gateway."""
# 1. Check health endpoint
response = await test_client.get("/api/mcp/health")
# 2. List tools
response = await test_client.get("/api/mcp/tools", headers=auth_headers)
# 3. Enrich results (requires mock MCP gateway)
# ...LOW: Documentation Gaps 📝Missing:
📊 Performance ConsiderationsCurrent Design Impact:
Recommendations:
🧪 Testing ChecklistUnit Tests: ✅ Excellent (395 lines, 40+ test cases) 🔐 Security ReviewStrengths:
Concerns:
📋 Action ItemsMust Fix Before Merge 🔴:
Should Fix 🟡:
Nice to Have 🔵:
✅ Compliance with Repository StandardsCLAUDE.md Compliance:
Missing (from CLAUDE.md):
🎓 Learning ValueThis PR demonstrates excellent software engineering practices:
Estimated Review Time: ~90 minutes 🚀 RecommendationConditional Approval - Fix the 3 MUST FIX items (resource leak, race condition, config validation), then this is ready to merge. The architecture and implementation quality are excellent. Great work on this integration! The simplified approach (200 lines vs 2000+) was the right choice. Generated with Claude Code - Automated PR Review |
Add comprehensive architecture documentation for the Agentic RAG Platform: - agentic-ui-architecture.md: React component hierarchy, state management, and API integration for agent features - backend-architecture-diagram.md: Overall backend architecture with Mermaid diagrams showing service layers and data flow - mcp-integration-architecture.md: MCP client/server integration strategy, PR comparison (#671 vs #684), and Context Forge integration - rag-modulo-mcp-server-architecture.md: Exposing RAG capabilities as MCP server with tools (rag_search, rag_ingest, etc.) and resources - search-agent-hooks-architecture.md: 3-stage agent pipeline (pre-search, post-search, response) with database schema and execution flow - system-architecture.md: Complete system architecture overview with technology stack and data flows These documents guide implementation of: - PR #695 (SPIFFE/SPIRE agent identity) - PR #671 (MCP Gateway client) - Issue #697 (Agent execution hooks) - Issue #698 (MCP Server) - Issue #699 (Agentic UI) Closes #696 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive architecture documentation for the Agentic RAG Platform: - agentic-ui-architecture.md: React component hierarchy, state management, and API integration for agent features - backend-architecture-diagram.md: Overall backend architecture with Mermaid diagrams showing service layers and data flow - mcp-integration-architecture.md: MCP client/server integration strategy, PR comparison (#671 vs #684), and Context Forge integration - rag-modulo-mcp-server-architecture.md: Exposing RAG capabilities as MCP server with tools (rag_search, rag_ingest, etc.) and resources - search-agent-hooks-architecture.md: 3-stage agent pipeline (pre-search, post-search, response) with database schema and execution flow - system-architecture.md: Complete system architecture overview with technology stack and data flows These documents guide implementation of: - PR #695 (SPIFFE/SPIRE agent identity) - PR #671 (MCP Gateway client) - Issue #697 (Agent execution hooks) - Issue #698 (MCP Server) - Issue #699 (Agentic UI) Closes #696 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Add comprehensive architecture documentation for the Agentic RAG Platform: - agentic-ui-architecture.md: React component hierarchy, state management, and API integration for agent features - backend-architecture-diagram.md: Overall backend architecture with Mermaid diagrams showing service layers and data flow - mcp-integration-architecture.md: MCP client/server integration strategy, PR comparison (#671 vs #684), and Context Forge integration - rag-modulo-mcp-server-architecture.md: Exposing RAG capabilities as MCP server with tools (rag_search, rag_ingest, etc.) and resources - search-agent-hooks-architecture.md: 3-stage agent pipeline (pre-search, post-search, response) with database schema and execution flow - system-architecture.md: Complete system architecture overview with technology stack and data flows These documents guide implementation of: - PR #695 (SPIFFE/SPIRE agent identity) - PR #671 (MCP Gateway client) - Issue #697 (Agent execution hooks) - Issue #698 (MCP Server) - Issue #699 (Agentic UI) Closes #696 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
|
Closing in favor of PR #684 which uses preferred file naming convention ( |
Implements issue #653 - MCP Gateway Integration with simplified architecture following expert panel recommendations (Fowler, Newman, Nygard, Hohpe).
Implementation (~200 lines of core code)
Core Components
MCPGatewayClient: Resilient HTTP client with circuit breaker patternSearchResultEnricher: Content Enricher pattern implementationmcp_router.py: Two API endpointsConfiguration
Testing
Closes #653