# Bluetooth MCP Server Architecture
<div align="center">

**Detailed architectural overview of the Bluetooth MCP Server project**
</div>
## π Overview
This document outlines the architectural structure of the Bluetooth MCP Server project, which enables AI assistants to discover and interact with Bluetooth devices. The architecture follows a modular design with clear separation of concerns, adhering to best practices for Python applications.
## ποΈ Project Structure
```
bluetooth-mcp-server/ # Root project directory
β
βββ app/ # Main application package
β βββ __init__.py # Package initializer
β βββ main.py # FastAPI application entry point
β β
β βββ api/ # API endpoints package
β β βββ __init__.py # Package initializer
β β βββ bluetooth.py # Bluetooth operation endpoints
β β βββ session.py # MCP session management endpoints
β β
β βββ core/ # Core configuration package
β β βββ __init__.py # Package initializer
β β βββ config.py # Application configuration
β β
β βββ data/ # Static data package
β β βββ __init__.py # Package initializer
β β βββ company_identifiers.py # Bluetooth manufacturer IDs database
β β βββ mac_prefixes.py # MAC address prefix database
β β
β βββ models/ # Data models package
β β βββ __init__.py # Package initializer
β β βββ bluetooth.py # Bluetooth data models
β β βββ session.py # Session data models
β β
β βββ services/ # Business logic package
β β βββ __init__.py # Package initializer
β β βββ bluetooth_service.py # Main Bluetooth operations service
β β βββ ble_scanner.py # BLE scanning service
β β βββ classic_scanner.py # Classic Bluetooth scanning service
β β βββ windows_scanner.py # Windows-specific scanner
β β βββ windows_advanced_scanner.py # Enhanced Windows scanner
β β
β βββ utils/ # Utilities package
β βββ __init__.py # Package initializer
β βββ bluetooth_utils.py # Bluetooth utility functions
β
βββ mcp_sdk/ # MCP SDK package
β βββ __init__.py # Package initializer
β βββ bluetooth_tool.py # MCP Bluetooth tool implementation
β βββ setup.py # Package installation configuration
β βββ tests/ # SDK tests
β βββ __init__.py # Package initializer
β βββ test_bluetooth_tool.py # Bluetooth tool tests
β
βββ tests/ # Test suites
β βββ __init__.py # Package initializer
β βββ test_main.py # Main application tests
β βββ TEST.md # Test status documentation
β β
β βββ api/ # API tests package
β β βββ __init__.py # Package initializer
β β βββ test_bluetooth.py # Bluetooth API tests
β β βββ test_session.py # Session API tests
β β
β βββ models/ # Model tests package
β β βββ __init__.py # Package initializer
β β βββ test_bluetooth_model.py # Bluetooth model tests
β β βββ test_session_model.py # Session model tests
β β
β βββ services/ # Service tests package
β β βββ __init__.py # Package initializer
β β βββ test_bluetooth_service.py # Bluetooth service tests
β β βββ test_classic_bluetooth.py # Classic Bluetooth tests
β β
β βββ utils/ # Utility tests package
β βββ __init__.py # Package initializer
β βββ test_bluetooth_utils.py # Bluetooth utilities tests
β
βββ .env # Local environment variables
βββ .env.example # Environment variables example
βββ run.py # Application startup script
βββ bluetooth_mcp_server.py # MCP server startup script
βββ requirements.txt # Project dependencies
βββ architecture.md # This architecture document
βββ README.md # Project documentation
```
## π§© Component Descriptions
### π Core Components
#### `run.py`
Entry point script for starting the FastAPI server, using environment variables for configuration.
#### `bluetooth_mcp_server.py`
Entry point script for the MCP server that integrates with the Bluetooth API.
#### `app/main.py`
Configures and initializes the FastAPI application, sets up CORS, and includes routers.
### π API Layer (`app/api/`)
#### `bluetooth.py`
Provides REST endpoints for Bluetooth scanning operations:
- POST `/mcp/v1/tools/bluetooth-scan`: Standard Bluetooth scan
- POST `/mcp/v1/tools/bluetooth-scan-fast`: Optimized for speed
- POST `/mcp/v1/tools/bluetooth-scan-thorough`: Optimized for device discovery
#### `session.py`
Handles MCP session management:
- POST `/mcp/v1/session`: Creates a new MCP session and returns available tools
### π Data Models (`app/models/`)
#### `bluetooth.py`
Pydantic models for Bluetooth data:
- `BluetoothDevice`: Represents a discovered Bluetooth device
- `BluetoothScanParams`: Parameters for scan operations
- `ScanResponse`: Container for scan results
#### `session.py`
Models for MCP session handling:
- `SessionResponse`: Represents the MCP session response
- `bluetooth_scan_tool`: Defines the Bluetooth tool for MCP
### π§ Services (`app/services/`)
#### `bluetooth_service.py`
Orchestrates the different Bluetooth scanners and manages device data:
- Handles device deduplication
- Merges information from different scan sources
- Provides platform-specific optimizations
#### `ble_scanner.py`
BLE-specific scanning service using Bleak library.
#### `classic_scanner.py`
Classic Bluetooth scanning service, with fallback modes for different platforms.
#### `windows_scanner.py`
Windows-specific scanner using native Windows APIs.
#### `windows_advanced_scanner.py`
Enhanced Windows scanner for detecting special devices (TVs, Freebox, etc.).
### π οΈ Utilities (`app/utils/`)
#### `bluetooth_utils.py`
Helper functions for Bluetooth operations:
- MAC address normalization
- Manufacturer data formatting
- Device name deduction
- Device information merging
### π Static Data (`app/data/`)
#### `company_identifiers.py`
Database of Bluetooth manufacturer identifiers.
#### `mac_prefixes.py`
Database of MAC address prefixes for device identification.
### π MCP SDK (`mcp_sdk/`)
#### `bluetooth_tool.py`
Implementation of the Bluetooth tool for the MCP protocol.
### π§ͺ Tests (`tests/`)
Test suites following the same structure as the application code, adhering to Test-Driven Development principles.
## π Data Flow
1. **Client Request** β The MCP client (Claude) sends a tool execution request
2. **MCP Server** β Processes the request and calls the appropriate Bluetooth API endpoint
3. **API Endpoint** β Validates parameters and calls the Bluetooth service
4. **Bluetooth Service** β Orchestrates the appropriate scanners based on parameters
5. **Scanners** β Execute device discovery using platform-specific methods
6. **Device Processing** β Discovered devices are processed, deduplicated, and enhanced
7. **Response** β Results are returned through the service β API β MCP chain
## π Configuration Management
The application uses a hierarchical configuration approach:
1. **Default values** hardcoded in the application
2. **.env file** for environment-specific configuration
3. **Environment variables** that can override file settings
4. **Runtime parameters** passed to API endpoints
## π Dependency Injection
The project uses a simple form of dependency injection:
- Services are instantiated as singletons
- API endpoints access services through imports
- Test mocks replace real implementations for testing
## π§© Extension Points
The architecture is designed to be extendable:
1. **Adding new scan methods**: Implement a new scanner in `app/services/`
2. **Supporting new device types**: Extend the databases in `app/data/`
3. **Adding new MCP tools**: Implement new tools in `mcp_sdk/`
4. **Enhanced device information**: Extend the `BluetoothDevice` model
## π Performance Considerations
- **Parallel scanning**: Multiple scan methods run concurrently
- **Scan duration control**: Adjustable to balance speed vs. thoroughness
- **Device deduplication**: Optimizes result size and clarity
- **Platform-specific optimizations**: Maximizes performance on each OS
## π Security Considerations
- **Limited scope**: The server only performs Bluetooth scanning operations
- **Input validation**: All parameters are validated using Pydantic models
- **Error handling**: Proper exception handling prevents information leakage
- **No persistent storage**: No sensitive data is stored between requests
## π Testing Strategy
The project follows a Test-Driven Development (TDD) approach:
1. **Unit Tests**: For individual components (models, utilities)
2. **Integration Tests**: For service interactions
3. **API Tests**: For endpoint behavior
4. **Mocking**: External dependencies are mocked for reliable testing