UNPKG

leshan-mcp-server

Version:

A standards-compliant MCP server for Leshan LwM2M, exposing Leshan as Model Context Protocol tools.

74 lines (63 loc) 1.88 kB
import { get } from "../utils/leshanClient.js"; import logger from "../utils/loggerConfig.js"; /** * List all registered LwM2M devices * @returns {Promise<Object>} MCP tool response */ export async function listDevices() { const operationId = `list-devices-${Date.now()}`; try { logger.info("Listing devices operation started", { operationId }); const devices = await get("/clients"); if (!Array.isArray(devices)) { throw new Error("Invalid response format: expected array of devices"); } const deviceSummary = devices.map(device => ({ endpoint: device.endpoint, registrationId: device.registrationId, address: device.address, lastUpdate: new Date(device.lastUpdate).toISOString(), lifetime: device.lifetime, bindingMode: device.bindingMode, objectCount: Object.keys(device.availableInstances || {}).length })); logger.info("Devices listed successfully", { operationId, deviceCount: devices.length, endpoints: devices.map(d => d.endpoint) }); return { content: [{ type: "text", text: JSON.stringify({ success: true, operation: "listDevices", timestamp: new Date().toISOString(), deviceCount: devices.length, devices: deviceSummary, rawData: devices }, null, 2) }] }; } catch (error) { logger.error("List devices operation failed", { operationId, error: error.message, stack: error.stack }); return { content: [{ type: "text", text: JSON.stringify({ success: false, operation: "listDevices", error: error.message, operationId, timestamp: new Date().toISOString() }, null, 2) }], isError: true }; } } export default listDevices;