rajkumarrawal's picture
Initial commit
2ec0d39

A newer version of the Gradio SDK is available: 6.1.0

Upgrade

MCP Orchestration Platform

A production-grade Gradio application that functions as an orchestration platform for multiple Model Context Protocol (MCP) servers, featuring advanced architecture with connection pooling, dynamic tool cataloging, resilient concurrency, and enterprise-grade monitoring.

🌟 Key Features

Core Architecture

  • Advanced Connection Pooling: Multi-layer connection management with circuit breaker patterns
  • Dynamic Tool Cataloging: Real-time capability introspection with automatic discovery
  • Resilient Concurrency: Async/await architecture with fault tolerance and rate limiting
  • Secure Session Management: Per-user isolation with TTLs and safe cancellation
  • Intelligent Caching: Multi-layer cache with LRU eviction and ETag support
  • Comprehensive Monitoring: Structured logging, Prometheus metrics, and health checks

User Experience

  • Responsive Gradio UI: Dynamic form generation and real-time streaming results
  • Server Discovery: Automatic MCP server detection and management
  • Configuration Management: Hot-reload configuration with secrets integration
  • Analytics Dashboard: Live metrics visualization and performance monitoring

Enterprise Patterns

  • Dependency Injection: Modular plugin architecture with service composition
  • Security: Enterprise-grade encryption, access control, and audit logging
  • Production Ready: Health checks, graceful degradation, and error recovery
  • High Performance: Optimized for 1000+ concurrent connections

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • Node.js 16+ (for sample servers)
  • 2GB RAM minimum, 4GB recommended

Installation

  1. Clone the repository
git clone <repository-url>
cd orchestration_platform
  1. Install dependencies
pip install -r requirements.txt
  1. Run the demo
python demo.py

First Run Demo

The demo script provides three modes:

  1. Quick Demo: Basic features demonstration
  2. Full Demo: Complete integration examples
  3. Interactive Mode: Manual testing interface
python demo.py
# Select option 1 for quick demo

πŸ“ Project Structure

orchestration_platform/
β”œβ”€β”€ mcp_orchestrator.py       # Core orchestration engine
β”œβ”€β”€ secrets_manager.py        # Enterprise secrets management
β”œβ”€β”€ gradio_interface.py       # Responsive web UI
β”œβ”€β”€ test_orchestrator.py      # Comprehensive test suite
β”œβ”€β”€ demo.py                   # Demo application
β”œβ”€β”€ requirements.txt          # Production dependencies
β”œβ”€β”€ sample_servers/           # Example MCP servers
β”‚   β”œβ”€β”€ weather_server.py     # Weather API integration
β”‚   └── crm_server.py         # CRM/CRM operations
β”œβ”€β”€ examples/                 # Integration examples
β”‚   └── integration_examples.py # Real-world workflows
└── docs/                     # Documentation
    β”œβ”€β”€ api_reference.md
    β”œβ”€β”€ deployment.md
    └── troubleshooting.md

πŸ—οΈ Architecture Overview

Core Components

1. MCPOrchestrator (mcp_orchestrator.py)

Main orchestration engine handling:

  • Connection Pooling: Manages MCP server connections with health monitoring
  • Session Management: Secure per-user session handling with TTL
  • Tool Cataloging: Dynamic discovery and introspection of available tools
  • Circuit Breakers: Fault tolerance with automatic recovery
  • Caching: Multi-layer cache for performance optimization

2. SecretsManager (secrets_manager.py)

Enterprise secrets management supporting:

  • Multiple Backends: Local encrypted, Vault, AWS Secrets Manager
  • Encryption: PBKDF2 and Fernet for data protection
  • Rotation: Automated secret lifecycle management
  • Access Control: RBAC and audit logging

3. Gradio Interface (gradio_interface.py)

Responsive web application with:

  • Dynamic Forms: Automatic UI generation from JSON schemas
  • Real-time Updates: Streaming results and progress tracking
  • Server Management: Discovery, configuration, and monitoring
  • Analytics: Performance metrics and usage analytics

4. Sample MCP Servers

Production-ready example implementations:

  • Weather Server: External API integration with 3-step tool catalog
  • CRM Server: Database operations with full CRUD capabilities

Data Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Gradio UI     │────│ MCP Orchestrator │────│ MCP Servers     β”‚
β”‚                 β”‚    β”‚                  β”‚    β”‚                 β”‚
β”‚ - Dynamic Forms β”‚    β”‚ - Connection Poolβ”‚    β”‚ - Weather API   β”‚
β”‚ - Real-time UI  β”‚    β”‚ - Tool Discovery β”‚    β”‚ - CRM Database  β”‚
β”‚ - Analytics     β”‚    β”‚ - Circuit Breakerβ”‚    β”‚ - Custom Logic  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚ Secrets Manager  β”‚
                    β”‚                  β”‚
                    β”‚ - Encryption     β”‚
                    β”‚ - Access Control β”‚
                    β”‚ - Rotation       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”§ Configuration

Environment Variables

# Core Configuration
ORCHESTRATOR_PORT=7860
ORCHESTRATOR_HOST=localhost
LOG_LEVEL=INFO

# Database Configuration
DATABASE_URL=postgresql://user:pass@localhost/orchestrator
CACHE_URL=redis://localhost:6379

# Secrets Management
SECRETS_BACKEND=local  # local, vault, aws
VAULT_ADDR=http://localhost:8200
AWS_REGION=us-east-1

# Security
JWT_SECRET=your-jwt-secret-key
ENCRYPTION_KEY=your-encryption-key

# Monitoring
PROMETHEUS_ENABLED=true
METRICS_PORT=9090

Configuration Files

config/orchestrator.yaml

orchestrator:
  host: "localhost"
  port: 7860
  max_connections: 100
  connection_timeout: 30
  
cache:
  layers:
    - type: "memory"
      max_size: 1000
    - type: "redis"
      ttl: 3600
      
secrets:
  backend: "local"
  encryption:
    algorithm: "fernet"
    key_rotation_days: 90
    
monitoring:
  prometheus:
    enabled: true
    port: 9090
  logging:
    level: "INFO"
    format: "json"

config/servers/weather.yaml

server:
  name: "weather-server"
  url: "http://localhost:8001/mcp"
  timeout: 10
  retry_attempts: 3
  
authentication:
  type: "api_key"
  api_key: "${WEATHER_API_KEY}"
  
health_check:
  interval: 30
  timeout: 5

πŸ“š API Reference

Core Orchestrator API

MCPOrchestrator

initialize()

Initialize the orchestrator with configuration.

orchestrator = MCPOrchestrator()
await orchestrator.initialize()
add_server(name: str, url: str) -> bool

Register a new MCP server.

success = await orchestrator.add_server("weather-server", "http://localhost:8001/mcp")
call_tool(server: str, tool: str, args: dict) -> dict

Execute a tool on a registered server.

result = await orchestrator.call_tool("weather-server", "get_current_weather", {
    "location": "New York"
})
list_all_tools() -> dict

Get catalog of all available tools across servers.

tools = await orchestrator.list_all_tools()
# Returns: {"weather-server": [...], "crm-server": [...]}

Secrets Manager API

SecretsManager

initialize()

Initialize secrets manager with backend.

secrets = SecretsManager()
await secrets.initialize()
get_secret(key: str) -> str

Retrieve a secret value.

api_key = await secrets.get_secret("WEATHER_API_KEY")
set_secret(key: str, value: str)

Store a secret value.

await secrets.set_secret("DATABASE_PASSWORD", "secure_password")

πŸ” Sample Integration Examples

1. Customer Intake Workflow

Complete customer onboarding using weather and CRM integration:

from orchestration_platform.examples.integration_examples import IntegrationOrchestrator

# Initialize with your orchestrator
integration = IntegrationOrchestrator(orchestrator)

# Run customer intake workflow
result = await integration.run_example("customer_intake_workflow")
print(f"Customer ID: {result['customer_id']}")

Workflow Steps:

  1. Create lead from website inquiry
  2. Get weather data for territory assignment
  3. Assign to sales rep based on conditions
  4. Convert qualified lead to customer
  5. Create initial sales opportunity

2. Sales Territory Optimization

Analyze sales performance by weather patterns:

result = await integration.run_example("sales_territory_optimization")
print(f"Territory recommendations: {result['recommendations']}")

3. Marketing Campaign Analysis

Correlate campaign performance with weather forecasts:

result = await integration.run_example("marketing_campaign_analysis")
print(f"Campaign insights: {result['campaign_insights']}")

πŸ§ͺ Testing

Running Tests

# Run all tests
python -m pytest test_orchestrator.py

# Run with coverage
python -m pytest test_orchestrator.py --cov=orchestration_platform

# Run specific test categories
python -m pytest test_orchestrator.py -m "unit"
python -m pytest test_orchestrator.py -m "integration"
python -m pytest test_orchestrator.py -m "performance"

Test Categories

  • Unit Tests: Individual component testing
  • Integration Tests: Cross-server workflow testing
  • Performance Tests: Load testing and benchmarking
  • Security Tests: Authentication and authorization validation

Test Coverage

The test suite targets:

  • 95%+ code coverage
  • All critical paths and edge cases
  • Performance benchmarks
  • Security validations

πŸš€ Deployment

Docker Deployment

1. Build Image

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 7860

CMD ["python", "demo.py"]

2. Run with Docker Compose

version: '3.8'
services:
  orchestrator:
    build: .
    ports:
      - "7860:7860"
    environment:
      - DATABASE_URL=postgresql://postgres:password@db:5432/orchestrator
      - REDIS_URL=redis://redis:6379
    depends_on:
      - db
      - redis

  db:
    image: postgres:15
    environment:
      - POSTGRES_DB=orchestrator
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=password

  redis:
    image: redis:7-alpine

Kubernetes Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcp-orchestrator
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mcp-orchestrator
  template:
    metadata:
      labels:
        app: mcp-orchestrator
    spec:
      containers:
      - name: orchestrator
        image: mcp-orchestrator:latest
        ports:
        - containerPort: 7860
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: orchestrator-secrets
              key: database-url
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
  name: mcp-orchestrator-service
spec:
  selector:
    app: mcp-orchestrator
  ports:
    - port: 80
      targetPort: 7860
  type: LoadBalancer

Cloud Deployment

AWS Deployment

# Using AWS ECS
aws ecs create-cluster --cluster-name mcp-orchestrator
aws ecs register-task-definition --cli-input-json file://task-definition.json
aws ecs create-service --cluster mcp-orchestrator --service-name orchestrator --task-definition orchestrator --desired-count 2

Azure Container Instances

az container create \
  --resource-group mcp-orchestrator-rg \
  --name orchestrator \
  --image mcp-orchestrator:latest \
  --cpu 2 \
  --memory 4 \
  --ports 7860

πŸ“Š Monitoring

Metrics Collection

The platform exposes comprehensive metrics via Prometheus:

  • Connection Metrics: Active connections, pool utilization
  • Performance Metrics: Response times, throughput
  • Error Metrics: Error rates, circuit breaker trips
  • Cache Metrics: Hit rates, eviction counts
  • Security Metrics: Authentication failures, access patterns

Grafana Dashboard

Pre-built dashboards available for:

  • Server performance overview
  • Connection pool statistics
  • Tool usage analytics
  • Error rate monitoring
  • Cache performance metrics

Health Checks

Liveness Probe

GET /health/live

Readiness Probe

GET /health/ready

Detailed Health Status

GET /health/detailed

πŸ”’ Security

Authentication & Authorization

  • JWT Tokens: Stateless authentication with configurable expiry
  • Role-Based Access: Granular permissions system
  • API Rate Limiting: Protection against abuse
  • Input Validation: Comprehensive sanitization

Secrets Management

  • Encryption at Rest: AES-256 encryption for stored secrets
  • Key Rotation: Automated key rotation policies
  • Audit Logging: All secret access is logged
  • Access Control: Principle of least privilege

Network Security

  • TLS Encryption: All communications encrypted in transit
  • Certificate Validation: Strict certificate verification
  • CORS Configuration: Controlled cross-origin access
  • Security Headers: Comprehensive security header set

πŸ› οΈ Troubleshooting

Common Issues

1. Connection Failures

# Check server connectivity
curl http://localhost:8001/health

# Verify orchestrator configuration
python -c "from orchestration_platform.mcp_orchestrator import MCPOrchestrator; print(MCPOrchestrator().config)"

2. Performance Issues

# Monitor connection pool
curl http://localhost:9090/metrics | grep connection_pool

# Check cache hit rates
curl http://localhost:9090/metrics | grep cache_hit_rate

3. Memory Usage

# Profile memory usage
python -m memory_profiler demo.py

# Monitor garbage collection
python -c "import gc; gc.set_debug(gc.DEBUG_STATS)"

Log Analysis

Structured Logging

All logs use structured JSON format for easy analysis:

{
  "timestamp": "2024-11-29T18:30:00Z",
  "level": "INFO",
  "component": "MCPOrchestrator",
  "event": "tool_call",
  "server": "weather-server",
  "tool": "get_current_weather",
  "duration_ms": 150,
  "status": "success"
}

Log Levels

  • DEBUG: Detailed execution traces
  • INFO: General operational messages
  • WARN: Warning conditions
  • ERROR: Error conditions
  • CRITICAL: Critical failures

Debug Mode

Enable detailed debugging:

import logging
logging.basicConfig(level=logging.DEBUG)

# Enable debug mode in orchestrator
orchestrator = MCPOrchestrator(debug=True)

🀝 Contributing

Development Setup

  1. Fork the repository
  2. Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate  # Windows
  1. Install development dependencies
pip install -r requirements.txt
pip install -r requirements-dev.txt
  1. Run tests
python -m pytest test_orchestrator.py --cov=orchestration_platform

Code Standards

  • Type Hints: All functions must include type annotations
  • Documentation: Comprehensive docstrings for all public APIs
  • Testing: Minimum 90% test coverage required
  • Linting: Black + isort + flake8 formatting

Pull Request Process

  1. Create feature branch from main
  2. Implement changes with tests
  3. Ensure all tests pass
  4. Update documentation
  5. Submit pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Model Context Protocol (MCP) specification
  • Gradio team for the excellent web UI framework
  • Structlog for structured logging
  • All contributors and the open source community

πŸ“ž Support

Getting Help

  • Documentation: Check the /docs directory
  • Issues: Report bugs via GitHub Issues
  • Discussions: Community discussions for questions
  • Email: support@orchestrator.com

Professional Support

Enterprise support available including:

  • 24/7 incident response
  • Dedicated support engineer
  • Custom feature development
  • Performance optimization
  • Security audits

Built with ❀️ for the MCP ecosystem