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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# 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**
```bash
git clone <repository-url>
cd orchestration_platform
```

2. **Install dependencies**
```bash
pip install -r requirements.txt
```

3. **Run the demo**
```bash
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

```bash
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

```bash
# 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`
```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`
```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.
```python
orchestrator = MCPOrchestrator()
await orchestrator.initialize()
```

##### `add_server(name: str, url: str) -> bool`
Register a new MCP server.
```python
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.
```python
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.
```python
tools = await orchestrator.list_all_tools()
# Returns: {"weather-server": [...], "crm-server": [...]}
```

### Secrets Manager API

#### `SecretsManager`

##### `initialize()`
Initialize secrets manager with backend.
```python
secrets = SecretsManager()
await secrets.initialize()
```

##### `get_secret(key: str) -> str`
Retrieve a secret value.
```python
api_key = await secrets.get_secret("WEATHER_API_KEY")
```

##### `set_secret(key: str, value: str)`
Store a secret value.
```python
await secrets.set_secret("DATABASE_PASSWORD", "secure_password")
```

## πŸ” Sample Integration Examples

### 1. Customer Intake Workflow

Complete customer onboarding using weather and CRM integration:

```python
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:

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

### 3. Marketing Campaign Analysis

Correlate campaign performance with weather forecasts:

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

## πŸ§ͺ Testing

### Running Tests

```bash
# 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
```dockerfile
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
```yaml
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

```yaml
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
```bash
# 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
```bash
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
```http
GET /health/live
```

#### Readiness Probe
```http
GET /health/ready
```

#### Detailed Health Status
```http
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
```bash
# 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
```bash
# 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
```bash
# 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:

```json
{
  "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:

```python
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**
```bash
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
venv\Scripts\activate  # Windows
```

3. **Install development dependencies**
```bash
pip install -r requirements.txt
pip install -r requirements-dev.txt
```

4. **Run tests**
```bash
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](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**