list_ble_devices
Retrieve all registered Bluetooth sensor devices and their latest temperature and humidity readings.
Instructions
列出所有已注册的蓝牙传感器设备,返回设备信息和最新温湿度读数。
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- mcp_server/server.py:158-161 (registration)MCP tool registration for 'list_ble_devices' using @mcp.tool() decorator. The tool is registered as an async function that calls GET /ble/devices via the internal _request helper.
@mcp.tool() async def list_ble_devices() -> dict: """列出所有已注册的蓝牙传感器设备,返回设备信息和最新温湿度读数。""" return await _request("GET", "/ble/devices") - app/services/ble_service.py:36-45 (handler)Actual business logic: BLEService.list_devices() queries BLEDevice by user_id, converts each to dict via _device_to_dict(), and attaches latest_reading via _get_latest_reading().
@staticmethod def list_devices(user_id: int) -> list[dict]: devices = BLEDevice.query.filter_by(user_id=user_id).all() result = [] for d in devices: info = BLEService._device_to_dict(d) latest = BLEService._get_latest_reading(d.id) info["latest_reading"] = latest result.append(info) return result - app/api/ble.py:10-15 (handler)Flask HTTP handler that receives GET /ble/devices, extracts user_id from auth, calls BLEService.list_devices(user_id), and returns JSON success response.
@ble_bp.route("/ble/devices", methods=["GET"]) @auth_required def list_ble_devices(): user_id = get_current_user_id() devices = BLEService.list_devices(user_id) return success(data=devices) - mcp_server/server.py:23-26 (helper)Internal _request() helper used by the MCP tool to make HTTP calls to the backend API with authorization headers.
async def _request(method: str, path: str, *, json_data: dict | None = None, params: dict | None = None): async with httpx.AsyncClient(timeout=30) as client: resp = await client.request(method, f"{_BASE_URL}{path}", json=json_data, params=params, headers=_headers()) return resp.json() - app/services/ble_service.py:206-215 (helper)Helper method _device_to_dict() that converts a BLEDevice model instance into a dictionary for API serialization.
def _device_to_dict(device: BLEDevice) -> dict: return { "id": device.id, "did": device.did, "mac_address": device.mac_address, "model": device.model, "capabilities": device.capabilities or [], "is_enabled": device.is_enabled, "last_seen_at": device.last_seen_at.isoformat() if device.last_seen_at else None, }