ARCHITECTURE.mdâ¢18.5 kB
# ðïž Architecture Documentation
Comprehensive architecture guide for Home Assistant MCP server. Understand the system design, components, and data flow.
## ð Table of Contents
- [System Overview](#system-overview)
- [Architecture Layers](#architecture-layers)
- [Core Components](#core-components)
- [Data Flow](#data-flow)
- [Module Structure](#module-structure)
- [Design Patterns](#design-patterns)
- [Technology Stack](#technology-stack)
- [Scalability & Performance](#scalability--performance)
---
## System Overview
### High-Level Architecture
```
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â AI Assistants Layer â
â (Claude Desktop, Cursor, VS Code, etc.) â
ââââââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââââââ
â MCP Protocol
ââââ stdio (Standard I/O)
ââââ HTTP/REST
ââââ WebSocket
ââââââââââââââââââââââââŒâââââââââââââââââââââââââââââââââââââââ
â Home Assistant MCP Server â
â ââââââââââââââ ââââââââââââââ âââââââââââââââââââââââ â
â â Transport â â Middleware â â Resource Manager â â
â â Layer ââââ Layer ââââ (State/Cache) â â
â ââââââââââââââ ââââââââââââââ âââââââââââââââââââââââ â
â ââââââââââââââ ââââââââââââââ âââââââââââââââââââââââ â
â â Tools â â Prompts â â Security â â
â â Layer â â Layer â â (Auth/Validate) â â
â ââââââââââââââ ââââââââââââââ âââââââââââââââââââââââ â
ââââââââââââââââââââââââ¬âââââââââââââââââââââââââââââââââââââââ
â Home Assistant API
ââââ REST API
ââââ WebSocket API
ââââââââââââââââââââââââŒâââââââââââââââââââââââââââââââââââââââ
â Home Assistant Instance â
â (Devices, Automations, Entities, Services) â
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
```
### Component Interactions
```
User Request â AI Assistant â MCP Protocol
â
Transport Layer (stdio/HTTP/WS)
â
Middleware (Auth, Validation, Rate Limiting)
â
Tool Dispatcher (Route to appropriate tool)
â
Tool Execution (Business Logic)
â
Home Assistant Client (API calls)
â
Home Assistant (Execute command)
â
Response Chain (Reverse flow)
â
AI Assistant â User
```
---
## Architecture Layers
### 1. Transport Layer
**Purpose**: Handle communication between AI assistants and MCP server
**Components**:
- **stdio Transport** (`src/mcp/transports/stdio.transport.ts`)
- Standard input/output communication
- Primary mode for MCP clients (Claude, Cursor)
- JSON-RPC 2.0 protocol
- **HTTP Transport** (`src/mcp/transports/http.transport.ts`)
- RESTful API endpoints
- Swagger/OpenAPI documentation
- CORS and security headers
- **WebSocket Transport** (`src/websocket/`)
- Real-time bidirectional communication
- Event streaming
- Connection pooling
**Key Files**:
```
src/mcp/transport.ts # Transport abstraction
src/mcp/transports/
âââ stdio.transport.ts # Standard I/O
âââ http.transport.ts # HTTP/REST
âââ websocket.transport.ts # WebSocket
src/stdio-server.ts # stdio entry point
src/http-server.ts # HTTP entry point
```
### 2. Middleware Layer
**Purpose**: Cross-cutting concerns and request processing
**Components**:
- **Authentication** (`src/security/auth.ts`)
- JWT token validation
- Home Assistant token verification
- Session management
- **Rate Limiting** (`src/middleware/rate-limit.ts`)
- Request throttling
- Per-endpoint limits
- DDoS protection
- **Validation** (`src/middleware/validation.ts`)
- Input sanitization
- Schema validation (Valibot)
- Type checking
- **Logging** (`src/middleware/logger.ts`)
- Request/response logging
- Error tracking
- Performance metrics
- **Error Handling** (`src/middleware/error-handler.ts`)
- Centralized error processing
- User-friendly error messages
- Error recovery
**Key Files**:
```
src/middleware/
âââ index.ts # Middleware orchestration
âââ auth.ts # Authentication
âââ rate-limit.ts # Rate limiting
âââ validation.ts # Input validation
âââ logger.ts # Logging
âââ error-handler.ts # Error handling
src/security/
âââ auth.ts # Auth logic
âââ jwt.ts # JWT handling
âââ sanitize.ts # Input sanitization
```
### 3. Tools Layer
**Purpose**: Business logic and Home Assistant interactions
**Structure**:
```
src/tools/
âââ homeassistant/ # HA-specific tools
â âââ lights.tool.ts # Light control
â âââ climate.tool.ts # Climate control
â âââ media-player.tool.ts # Media control
â âââ automation.tool.ts # Automations
â âââ maintenance.tool.ts # Maintenance
â âââ smart-scenarios.tool.ts # Smart scenarios
âââ base-tool.ts # Tool base class
âââ control.tool.ts # Generic control
âââ index.ts # Tool registry
```
**Tool Architecture**:
```typescript
// Base Tool Structure
abstract class BaseTool {
name: string;
description: string;
abstract execute(params: any): Promise<ToolResult>;
validate(params: any): ValidationResult;
getSchema(): JSONSchema;
}
// Example: Lights Tool
class LightsTool extends BaseTool {
async execute(params: LightsParams) {
// 1. Validate input
this.validate(params);
// 2. Call Home Assistant
const result = await this.hassClient.callService(
'light',
params.action,
params.data
);
// 3. Return formatted response
return this.formatResponse(result);
}
}
```
### 4. Resource Manager
**Purpose**: State management and caching
**Components**:
- **State Manager** (`src/mcp/resources.ts`)
- Entity state caching
- Resource lifecycle
- State synchronization
- **Cache Layer** (`src/utils/cache.ts`)
- In-memory caching
- Redis integration (optional)
- TTL management
- **Resource Types**:
- Device lists
- Entity states
- Area/room configurations
- Automation listings
**Key Files**:
```
src/mcp/resources.ts # Resource manager
src/utils/cache.ts # Cache implementation
src/context/ # Context management
```
### 5. Home Assistant Client
**Purpose**: Communication with Home Assistant
**Components**:
- **REST Client** (`src/hass/client.ts`)
- API endpoint calls
- State queries
- Service execution
- **WebSocket Client** (`src/hass/websocket.ts`)
- Real-time events
- State subscriptions
- Bidirectional communication
- **Connection Pool** (`src/hass/pool.ts`)
- Connection reuse
- Health checking
- Load balancing
**Key Files**:
```
src/hass/
âââ client.ts # HTTP client
âââ websocket.ts # WebSocket client
âââ pool.ts # Connection pool
âââ types.ts # HA types
```
---
## Core Components
### MCP Server
**Location**: `src/mcp/MCPServer.ts`
**Responsibilities**:
- Protocol implementation
- Tool registration
- Resource management
- Transport coordination
**Key Methods**:
```typescript
class MCPServer {
// Initialize server
async initialize(): Promise<void>
// Register tools
registerTool(tool: BaseTool): void
// Handle requests
async handleRequest(request: MCPRequest): Promise<MCPResponse>
// List resources
async listResources(): Promise<Resource[]>
// Get resource
async getResource(uri: string): Promise<ResourceContent>
}
```
### Tool Registry
**Location**: `src/tools/index.ts`
**Purpose**: Central tool management
```typescript
class ToolRegistry {
private tools: Map<string, BaseTool>;
register(tool: BaseTool): void {
this.tools.set(tool.name, tool);
}
get(name: string): BaseTool | undefined {
return this.tools.get(name);
}
list(): BaseTool[] {
return Array.from(this.tools.values());
}
}
```
### Configuration Manager
**Location**: `src/config/`
**Responsibilities**:
- Environment variable loading
- Configuration validation
- Default values
- Type-safe config access
```typescript
interface Config {
homeAssistant: {
url: string;
token: string;
timeout: number;
};
server: {
port: number;
host: string;
env: 'development' | 'production';
};
security: {
jwtSecret: string;
rateLimit: RateLimitConfig;
};
logging: LogConfig;
}
```
---
## Data Flow
### Request Flow
1. **Request Reception**
```
AI Assistant â Transport Layer
- Parse JSON-RPC request
- Extract method and params
```
2. **Middleware Processing**
```
Transport â Middleware Chain
- Authentication
- Rate limiting
- Input validation
- Logging
```
3. **Tool Dispatch**
```
Middleware â Tool Registry
- Lookup tool by name
- Validate tool exists
- Check permissions
```
4. **Tool Execution**
```
Tool Registry â Tool Instance
- Validate parameters
- Execute business logic
- Call Home Assistant API
```
5. **Home Assistant Interaction**
```
Tool â HA Client â Home Assistant
- REST API call
- WebSocket message
- Wait for response
```
6. **Response Formation**
```
HA Response â Tool â Middleware
- Format response
- Add metadata
- Error handling
```
7. **Response Return**
```
Middleware â Transport â AI Assistant
- Serialize JSON-RPC response
- Send to client
```
### Event Flow (Real-time)
```
Home Assistant Event
â
WebSocket Client (receives)
â
Event Handler (processes)
â
Event Dispatcher (routes)
â
Subscribed Clients (notify via SSE)
â
AI Assistant (receives update)
```
---
## Module Structure
### Directory Organization
```
src/
âââ mcp/ # MCP Protocol implementation
â âââ MCPServer.ts # Main MCP server
â âââ transport.ts # Transport abstraction
â âââ transports/ # Transport implementations
â âââ resources.ts # Resource manager
â âââ prompts.ts # Prompt templates
â âââ utils/ # MCP utilities
âââ tools/ # Tool implementations
â âââ homeassistant/ # HA-specific tools
â âââ base-tool.ts # Base tool class
â âââ control.tool.ts # Generic control
â âââ index.ts # Tool registry
âââ hass/ # Home Assistant client
â âââ client.ts # HTTP client
â âââ websocket.ts # WebSocket client
â âââ types.ts # Type definitions
âââ middleware/ # Middleware layer
â âââ auth.ts # Authentication
â âââ rate-limit.ts # Rate limiting
â âââ validation.ts # Validation
â âââ logger.ts # Logging
âââ security/ # Security utilities
â âââ auth.ts # Auth logic
â âââ jwt.ts # JWT handling
â âââ sanitize.ts # Input sanitization
âââ config/ # Configuration
â âââ index.ts # Config loader
â âââ schema.ts # Config schema
âââ utils/ # Utility functions
â âââ cache.ts # Caching
â âââ logger.ts # Logger setup
â âââ helpers.ts # Helper functions
âââ types/ # Type definitions
â âââ mcp.ts # MCP types
â âââ hass.ts # HA types
â âââ common.ts # Common types
âââ schemas/ # Validation schemas
â âââ tools.ts # Tool schemas
â âââ common.ts # Common schemas
âââ index.ts # Main entry (Bun)
âââ stdio-server.ts # stdio entry (Node)
âââ http-server.ts # HTTP entry (Node)
```
---
## Design Patterns
### 1. Strategy Pattern (Transport)
Different transport strategies (stdio, HTTP, WebSocket):
```typescript
interface Transport {
send(message: Message): Promise<void>;
receive(): Promise<Message>;
close(): Promise<void>;
}
class StdioTransport implements Transport { }
class HttpTransport implements Transport { }
class WebSocketTransport implements Transport { }
```
### 2. Factory Pattern (Tool Creation)
```typescript
class ToolFactory {
static create(type: string): BaseTool {
switch (type) {
case 'lights': return new LightsTool();
case 'climate': return new ClimateTool();
// ...
}
}
}
```
### 3. Decorator Pattern (Middleware)
```typescript
function withAuth(handler: RequestHandler): RequestHandler {
return async (req, res) => {
await authenticate(req);
return handler(req, res);
};
}
function withRateLimit(handler: RequestHandler): RequestHandler {
return async (req, res) => {
await checkRateLimit(req);
return handler(req, res);
};
}
// Usage
const handler = withAuth(withRateLimit(baseHandler));
```
### 4. Observer Pattern (Events)
```typescript
class EventEmitter {
private listeners: Map<string, Function[]>;
on(event: string, callback: Function): void {
this.listeners.get(event)?.push(callback);
}
emit(event: string, data: any): void {
this.listeners.get(event)?.forEach(cb => cb(data));
}
}
```
### 5. Singleton Pattern (Config, Logger)
```typescript
class Logger {
private static instance: Logger;
static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}
}
```
---
## Technology Stack
### Core Technologies
| Technology | Purpose | Version |
|-----------|---------|---------|
| **Bun** | Runtime | >=1.0.26 |
| **TypeScript** | Language | ^5.0.0 |
| **FastMCP** | MCP Framework | ^3.22.0 |
| **Express** | HTTP Server | ^4.21.2 |
| **Winston** | Logging | ^3.11.0 |
### Key Libraries
**Validation & Schema**:
- **Valibot** - Lightweight validation
- **Zod** - Type-safe schemas
- **@valibot/to-json-schema** - JSON Schema generation
**Security**:
- **helmet** - Security headers
- **jsonwebtoken** - JWT handling
- **sanitize-html** - Input sanitization
- **express-rate-limit** - Rate limiting
**HTTP & Communication**:
- **express** - HTTP framework
- **cors** - CORS handling
- **ws** - WebSocket library
- **node-fetch** - HTTP client
**Documentation**:
- **swagger-ui-express** - API docs
- **openapi-types** - OpenAPI types
---
## Scalability & Performance
### Performance Optimizations
1. **Connection Pooling**
- Reuse HTTP connections to Home Assistant
- Configurable pool size
- Health checking
2. **Caching Strategy**
- In-memory cache for entity states
- Configurable TTL
- Optional Redis backend
3. **Async Processing**
- Non-blocking I/O
- Promise-based API
- Event-driven architecture
4. **Resource Management**
- Lazy loading
- Memory-efficient data structures
- Garbage collection optimization
### Scalability Considerations
1. **Horizontal Scaling**
- Stateless design (mostly)
- Shared cache (Redis)
- Load balancer ready
2. **Vertical Scaling**
- Efficient memory usage
- Connection pooling
- Resource limits
3. **Rate Limiting**
- Per-client limits
- Per-endpoint limits
- Graceful degradation
### Monitoring Points
```typescript
// Performance metrics
- Request latency
- Tool execution time
- HA API response time
- Cache hit rate
- Memory usage
- Connection pool stats
```
---
## Extension Points
### Adding New Tools
1. Create tool class extending `BaseTool`
2. Implement required methods
3. Register in tool registry
4. Add schema validation
5. Write tests
### Adding Middleware
1. Create middleware function
2. Register in middleware chain
3. Order appropriately
4. Handle errors
### Adding Transport
1. Implement `Transport` interface
2. Add configuration
3. Register in server
4. Update documentation
---
## Next Steps
**For Developers**:
- [Development Guide](DEVELOPMENT.md) - Setup and workflow
- [API Reference](API_REFERENCE.md) - API documentation
- [Testing Guide](TESTING.md) - Testing practices
- [Contributing Guide](CONTRIBUTING.md) - Contribution workflow
**For Operations**:
- [Docker Guide](DOCKER_GUIDE.md) - Docker deployment
- [Security Guide](SECURITY.md) - Security practices
- [Performance Guide](PERFORMANCE.md) - Performance tuning
---
**Questions?** See [GitHub Discussions](https://github.com/jango-blockchained/advanced-homeassistant-mcp/discussions)