Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pakistani Smart-Home Energy Agent Demo

A demonstration of Model Context Protocol (MCP) and Agent Communication Protocol (ACP) for smart home energy optimization in Pakistan.

🎯 Overview

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

πŸ—οΈ Architecture

User Request β†’ Agent A β†’ MCP Server (tariff data) β†’ Agent B β†’ Optimization Result
                ↓              ↑                      ↓
            REST API      JSON-RPC 2.0         ACP Protocol

Protocols Demonstrated

  1. MCP (Model Context Protocol): JSON-RPC 2.0 based protocol for fetching tariff context
  2. ACP (Agent Communication Protocol): Structured HTTP-based messaging between agents

πŸš€ Quick Start

Prerequisites

  • Python 3.8+
  • pip

Installation

  1. Clone and setup:

    git clone <repository-url>
    cd pakistani-smart-home-demo
  2. Create virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Run the demo:

    python run_demo.py

The demo will automatically start all services and run interactive tests.

🌐 Service Endpoints

Agent A - Homeowner Interface (Port 8001)

  • POST /optimize-appliance - Main optimization endpoint
  • GET /health - Health check
  • GET /tariff-info - Current tariff information
  • POST /test-mcp - Test MCP connectivity
  • POST /test-acp - Test ACP connectivity

Agent B - Energy Optimizer (Port 8002)

  • POST /acp/optimize - ACP optimization endpoint
  • POST /optimize-direct - Direct optimization (bypass ACP)
  • GET /health - Health check
  • GET /optimization-analysis/{appliance} - Detailed analysis

MCP Server - Tariff Provider (Port 8003)

  • POST /mcp - JSON-RPC 2.0 endpoint
  • GET /health - Health check
  • GET /tariff-info - Available tariff types

πŸ“Š Usage Examples

Basic Optimization Request

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"
  }'

MCP Tariff Request

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
  }'

πŸ‡΅πŸ‡° Pakistani Context

Electricity Tariff Structure

  • 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)

Supported Appliances

  • 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)

πŸ”§ Technical Details

MCP Protocol Implementation

  • Standard: JSON-RPC 2.0
  • Transport: HTTP POST
  • Methods: get_tariff, get_peak_hours, calculate_cost
  • Error Handling: Standard JSON-RPC error codes

ACP Protocol Implementation

  • Format: Structured JSON messages
  • Transport: HTTP POST
  • Features: Correlation IDs, timestamps, agent identification
  • Message Types: Request, Response, Error

Optimization Logic

  • 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

πŸ“ Project Structure

β”œβ”€β”€ 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

πŸ§ͺ Testing

Automated Tests

The demo runner includes automatic tests for both protocols:

python run_demo.py

Manual Testing

  1. Test MCP directly:

    curl http://localhost:8003/tariff-info
  2. Test Agent A health:

    curl http://localhost:8001/health
  3. Test Agent B health:

    curl http://localhost:8002/health

Interactive Demo

The demo includes an interactive mode where you can test different appliances and scenarios.

πŸ” Monitoring & Debugging

Logs

All services provide detailed logging. Check console output for:

  • MCP JSON-RPC calls and responses
  • ACP message correlation IDs
  • Optimization calculations and results

Health Checks

Each service provides health endpoints that check:

  • Service availability
  • Dependency connectivity
  • Configuration status

Error Handling

  • MCP Errors: Standard JSON-RPC error codes
  • ACP Errors: Structured error responses with correlation IDs
  • HTTP Errors: Standard HTTP status codes with detailed messages

πŸ› οΈ Configuration

Tariff Configuration

Edit shared/mcp_server.py to modify:

  • Peak/off-peak rates
  • Peak hour windows
  • Regional variations

Optimization Parameters

Edit agent_b/optimizer.py to adjust:

  • Minimum savings threshold
  • Peak avoidance bonus
  • Maximum delay limits

🚨 Troubleshooting

Common Issues

  1. Port conflicts: Ensure ports 8001, 8002, 8003 are available
  2. Service startup: Wait for health checks to pass before testing
  3. Network issues: Check firewall settings for local HTTP traffic

Debug Mode

Run individual services with debug logging:

python -m uvicorn agent_a.main:app --host 0.0.0.0 --port 8001 --reload --log-level debug

πŸ“ˆ Performance Notes

  • 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

🀝 Contributing

This is a demonstration project. For production use, consider:

  • Authentication and authorization
  • Database persistence
  • Load balancing
  • Monitoring and alerting
  • Enhanced error handling

πŸ“œ License

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.

About

A lightweight agent demonstrating ACP and MCP integration for smart-home energy management built to explore multi-protocol agent communication patterns.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages