A demonstration of Model Context Protocol (MCP) and Agent Communication Protocol (ACP) for smart home energy optimization in Pakistan.
This project showcases two intelligent agents working together to optimize energy consumption for Pakistani households:
- Agent A (Homeowner Interface): Receives user requests and coordinates with other services
- Agent B (Energy Optimizer): Applies rule-based optimization logic for appliance scheduling
- MCP Server: Provides Pakistani electricity tariff data via JSON-RPC 2.0
User Request β Agent A β MCP Server (tariff data) β Agent B β Optimization Result
β β β
REST API JSON-RPC 2.0 ACP Protocol
- MCP (Model Context Protocol): JSON-RPC 2.0 based protocol for fetching tariff context
- ACP (Agent Communication Protocol): Structured HTTP-based messaging between agents
- Python 3.8+
- pip
-
Clone and setup:
git clone <repository-url> cd pakistani-smart-home-demo
-
Create virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Run the demo:
python run_demo.py
The demo will automatically start all services and run interactive tests.
POST /optimize-appliance- Main optimization endpointGET /health- Health checkGET /tariff-info- Current tariff informationPOST /test-mcp- Test MCP connectivityPOST /test-acp- Test ACP connectivity
POST /acp/optimize- ACP optimization endpointPOST /optimize-direct- Direct optimization (bypass ACP)GET /health- Health checkGET /optimization-analysis/{appliance}- Detailed analysis
POST /mcp- JSON-RPC 2.0 endpointGET /health- Health checkGET /tariff-info- Available tariff types
curl -X POST http://localhost:8001/optimize-appliance \
-H "Content-Type: application/json" \
-d '{
"appliance_type": "washing_machine",
"power_consumption": 1.5,
"duration": 90,
"preferred_time": "20:00",
"max_delay": 480,
"location": "karachi"
}'curl -X POST http://localhost:8003/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "get_tariff",
"params": {"type": "residential", "location": "karachi"},
"id": 1
}'- Peak Hours: 6 PM - 10 PM (25.50 PKR/kWh)
- Off-Peak Hours: 10 PM - 6 AM (15.20 PKR/kWh)
- Providers: K-Electric (Karachi), WAPDA (other cities)
- Washing Machine (1.5 kWh, 90 min)
- Dishwasher (1.8 kWh, 120 min)
- Clothes Dryer (2.5 kWh, 60 min)
- Water Heater (3.0 kWh, 30 min)
- Air Conditioner (2.0 kWh, 240 min)
- Standard: JSON-RPC 2.0
- Transport: HTTP POST
- Methods:
get_tariff,get_peak_hours,calculate_cost - Error Handling: Standard JSON-RPC error codes
- Format: Structured JSON messages
- Transport: HTTP POST
- Features: Correlation IDs, timestamps, agent identification
- Message Types: Request, Response, Error
- Threshold: 20% minimum savings to recommend delay
- Peak Avoidance: 15% bonus for avoiding peak hours
- Maximum Delay: Configurable (default: 8 hours)
- Cost Calculation: Based on Pakistani electricity rates
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ run_demo.py # Demo runner script
βββ shared/
β βββ __init__.py
β βββ protocols.py # MCP & ACP protocol definitions
β βββ mcp_server.py # MCP JSON-RPC server
βββ agent_a/
β βββ __init__.py
β βββ main.py # FastAPI app for Agent A
β βββ mcp_client.py # MCP client implementation
β βββ acp_sender.py # ACP message sender
β βββ models.py # Pydantic models
βββ agent_b/
βββ __init__.py
βββ main.py # FastAPI app for Agent B
βββ optimizer.py # Energy optimization logic
βββ models.py # Pydantic models
The demo runner includes automatic tests for both protocols:
python run_demo.py-
Test MCP directly:
curl http://localhost:8003/tariff-info
-
Test Agent A health:
curl http://localhost:8001/health
-
Test Agent B health:
curl http://localhost:8002/health
The demo includes an interactive mode where you can test different appliances and scenarios.
All services provide detailed logging. Check console output for:
- MCP JSON-RPC calls and responses
- ACP message correlation IDs
- Optimization calculations and results
Each service provides health endpoints that check:
- Service availability
- Dependency connectivity
- Configuration status
- MCP Errors: Standard JSON-RPC error codes
- ACP Errors: Structured error responses with correlation IDs
- HTTP Errors: Standard HTTP status codes with detailed messages
Edit shared/mcp_server.py to modify:
- Peak/off-peak rates
- Peak hour windows
- Regional variations
Edit agent_b/optimizer.py to adjust:
- Minimum savings threshold
- Peak avoidance bonus
- Maximum delay limits
- Port conflicts: Ensure ports 8001, 8002, 8003 are available
- Service startup: Wait for health checks to pass before testing
- Network issues: Check firewall settings for local HTTP traffic
Run individual services with debug logging:
python -m uvicorn agent_a.main:app --host 0.0.0.0 --port 8001 --reload --log-level debug- Response Time: < 100ms for typical optimizations
- Throughput: Tested up to 100 concurrent requests
- Memory Usage: ~50MB per service
- Dependencies: All services are stateless and horizontally scalable
This is a demonstration project. For production use, consider:
- Authentication and authorization
- Database persistence
- Load balancing
- Monitoring and alerting
- Enhanced error handling
This project is for demonstration purposes. Adapt as needed for your use case.
Built for the Pakistani smart home market with local electricity tariff structures and optimization strategies.